Skip to content

Commit

Permalink
Exclude recurring reservation reservations from Pindora updates
Browse files Browse the repository at this point in the history
  • Loading branch information
matti-lamppu committed Feb 4, 2025
1 parent a2d321d commit e333b61
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 10 deletions.
6 changes: 5 additions & 1 deletion tilavarauspalvelu/admin/reservation/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ class Meta:
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)

if self.instance and self.instance.access_type == AccessType.ACCESS_CODE:
if (
self.instance
and self.instance.recurring_reservation is None
and self.instance.access_type == AccessType.ACCESS_CODE
):
pindora_field = self.fields["pindora_response"]
pindora_field.initial = self.get_pindora_response(self.instance)
pindora_field.widget.attrs.update({"cols": "100", "rows": "20"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def update(self, instance: Reservation, validated_data: ReservationAdjustTimeDat
with transaction.atomic():
instance = super().update(instance=instance, validated_data=validated_data)

instance.actions.create_or_update_access_code_if_required(from_access_type=access_type_before)
instance.actions.create_or_update_reservation_access_code_if_required(from_access_type=access_type_before)

EmailService.send_reservation_modified_email(reservation=instance)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def validate(self, data: ReservationApproveData) -> ReservationApproveData:
return data

def update(self, instance: Reservation, validated_data: ReservationApproveData) -> Reservation:
if self.instance.access_type == AccessType.ACCESS_CODE:
if self.instance.access_type == AccessType.ACCESS_CODE and instance.recurring_reservation is None:
# Allow activation in Pindora to fail, will be handled by a background task.
with suppress(Exception):
# If access code has not been generated (e.g. returned to handling after a deny and then approved),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def validate(self, data: ReservationCancellationData) -> ReservationCancellation
return data

def update(self, instance: Reservation, validated_data: ReservationCancellationData) -> Reservation:
if instance.access_type == AccessType.ACCESS_CODE:
if instance.access_type == AccessType.ACCESS_CODE and instance.recurring_reservation is None:
with suppress(PindoraNotFoundError):
PindoraClient.delete_reservation(reservation=instance)
validated_data["access_code_generated_at"] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def update(self, instance: Reservation, validated_data: ReservationConfirmData)
instance = super().update(instance=instance, validated_data=validated_data)

if instance.state == ReservationStateChoice.CONFIRMED:
if self.instance.access_type == AccessType.ACCESS_CODE:
if self.instance.access_type == AccessType.ACCESS_CODE and instance.recurring_reservation is None:
# Allow activation in Pindora to fail, will be handled by a background task.
with suppress(Exception):
PindoraClient.activate_reservation_access_code(reservation=instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def validate(self, data: ReservationDenyData) -> ReservationDenyData:
return data

def update(self, instance: Reservation, validated_data: ReservationDenyData) -> Reservation:
if instance.access_type == AccessType.ACCESS_CODE:
if instance.access_type == AccessType.ACCESS_CODE and instance.recurring_reservation is None:
with suppress(PindoraNotFoundError):
PindoraClient.delete_reservation(reservation=instance)
validated_data["access_code_generated_at"] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ def validate(self, data: ReservationHandlingData) -> ReservationHandlingData:

def update(self, instance: Reservation, validated_data: dict[str, Any]) -> Reservation:
# Denied reservations shouldn't have an access code. It will be regenerated if the reservation is approved.
if self.instance.access_type == AccessType.ACCESS_CODE and instance.access_code_generated_at is not None:
if (
self.instance.access_type == AccessType.ACCESS_CODE
and instance.recurring_reservation is None
and instance.access_code_generated_at is not None
):
# Allow reservation modification to succeed if reservation doesn't exist in Pindora.
with suppress(PindoraNotFoundError):
PindoraClient.deactivate_reservation_access_code(reservation=instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def update(self, instance: Reservation, validated_data: dict[str, Any]) -> Reser
with transaction.atomic():
instance = super().update(instance=instance, validated_data=validated_data)

instance.actions.create_or_update_access_code_if_required(from_access_type=access_type_before)
instance.actions.create_or_update_reservation_access_code_if_required(from_access_type=access_type_before)

EmailService.send_reservation_modified_email(reservation=instance)
EmailService.send_staff_notification_reservation_requires_handling_email(reservation=instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ def update(self, instance: Reservation, validated_data: StaffReservationData) ->
# If reservation was changed to or from blocked, change access code active state in Pindora.
changed_with_blocked = type_before != type_after and ReservationTypeChoice.BLOCKED in {type_before, type_after}

if self.instance.access_type == AccessType.ACCESS_CODE and changed_with_blocked:
if (
instance.access_type == AccessType.ACCESS_CODE
and instance.recurring_reservation is None
and changed_with_blocked
):
# Allow reservation modification to succeed if reservation doesn't exist in Pindora.
with suppress(PindoraNotFoundError):
if type_after == ReservationTypeChoice.BLOCKED:
Expand Down
6 changes: 5 additions & 1 deletion tilavarauspalvelu/models/reservation/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,12 @@ def refund_paid_reservation(self) -> None:
payment_order.status = OrderStatus.REFUNDED
payment_order.save(update_fields=["refund_id", "status"])

def create_or_update_access_code_if_required(self, *, from_access_type: AccessType) -> None:
def create_or_update_reservation_access_code_if_required(self, *, from_access_type: AccessType) -> None:
"""Notify Pindora about the time of the reservation if required."""
# Recurring reservations should be handled with separate endpoints
if self.reservation.recurring_reservation is not None:
return

current_access_type = self.reservation.access_type

is_active = self.reservation.access_code_should_be_active
Expand Down
2 changes: 2 additions & 0 deletions tilavarauspalvelu/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ def create_missing_pindora_reservations() -> None:

reservations: list[Reservation] = list(
Reservation.objects.filter(
recurring_reservation=None,
state=ReservationStateChoice.CONFIRMED,
access_type=AccessType.ACCESS_CODE,
access_code_generated_at=None,
Expand Down Expand Up @@ -771,6 +772,7 @@ def update_pindora_access_code_is_active() -> None:
(Q(access_code_is_active=True) & L(access_code_should_be_active=False))
| (Q(access_code_is_active=False) & L(access_code_should_be_active=True))
),
recurring_reservation=None,
access_code_generated_at__isnull=False,
end__gt=local_datetime(),
)
Expand Down

0 comments on commit e333b61

Please sign in to comment.