Skip to content

Commit

Permalink
Update existing draft permit if it exists
Browse files Browse the repository at this point in the history
Always update existing draft permit if it exists for the given vehicle.
  • Loading branch information
mhieta committed Dec 5, 2024
1 parent 37bd5d9 commit c0ca12a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions parking_permits/admin_resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,10 @@ def resolve_create_resident_permit(obj, info, permit, audit_msg: AuditMsg = None
_("Vehicle registration number is mandatory for the permit")
)

has_valid_permit = active_permits.filter(
valid_permit_exists = active_permits.filter(
vehicle__registration_number=registration_number
).exists()
if has_valid_permit:
if valid_permit_exists:
raise CreatePermitError(
_("User already has a valid permit for the given vehicle.")
)
Expand Down Expand Up @@ -578,10 +578,19 @@ def resolve_create_resident_permit(obj, info, permit, audit_msg: AuditMsg = None
]:
permit_status = ParkingPermitStatus.VALID

permit_id = permit.get("id")
# always update existing draft permit if it exists for the given vehicle
draft_permits = customer.permits.fixed_period().filter(
vehicle__registration_number=registration_number,
status__in=[ParkingPermitStatus.DRAFT, ParkingPermitStatus.PRELIMINARY],
)
if draft_permits.exists():
permit_id = draft_permits.first().id

primary_vehicle = active_permits_count == 0
# only create a new permit when it doesn't exist
parking_permit, permit_created = ParkingPermit.objects.update_or_create(
pk=permit.get("id"),
pk=permit_id,
defaults={
"contract_type": ContractType.FIXED_PERIOD,
"customer": customer,
Expand Down

0 comments on commit c0ca12a

Please sign in to comment.