Skip to content

Commit 50a2301

Browse files
committed
Sync pending status when updating grant
1 parent 16deb18 commit 50a2301

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

backend/grants/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ class ApprovedType(models.TextChoices):
230230

231231
def __init__(self, *args, **kwargs):
232232
super().__init__(*args, **kwargs)
233+
self._original_status = self.status
233234
self._original_pending_status = self.pending_status
234235
self._original_approved_type = self.approved_type
235236
self._original_country_type = self.country_type
@@ -241,19 +242,24 @@ def save(self, *args, **kwargs):
241242
self._update_country_type()
242243
self._calculate_grant_amounts()
243244

245+
if self.pending_status == self._original_status:
246+
self.pending_status = self.status
247+
244248
update_fields = kwargs.get("update_fields", None)
245249
if update_fields:
246250
update_fields.append("total_amount")
247251
update_fields.append("ticket_amount")
248252
update_fields.append("accommodation_amount")
249253
update_fields.append("travel_amount")
250254
update_fields.append("country_type")
255+
update_fields.append("pending_status")
251256

252257
super().save(*args, **kwargs)
253258

254259
self._original_approved_type = self.approved_type
255260
self._original_country_type = self.country_type
256261
self._original_pending_status = self.pending_status
262+
self._original_status = self.status
257263

258264
def _calculate_grant_amounts(self):
259265
if self.pending_status != Grant.Status.approved:

backend/grants/tests/test_models.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,35 @@ def test_sets_country_type_does_nothing_if_unset():
160160
grant = GrantFactory(departure_country=None)
161161

162162
assert grant.country_type is None
163+
164+
165+
def test_syncs_pending_status_on_change():
166+
grant = GrantFactory(
167+
pending_status=Grant.Status.pending,
168+
status=Grant.Status.pending,
169+
)
170+
171+
grant.status = Grant.Status.approved
172+
grant.save(update_fields=["status"])
173+
174+
# Pending status should be updated to match the status
175+
grant.refresh_from_db()
176+
177+
assert grant.pending_status == Grant.Status.approved
178+
assert grant.status == Grant.Status.approved
179+
180+
181+
def test_doesnt_sync_pending_status_if_different_values():
182+
grant = GrantFactory(
183+
pending_status=Grant.Status.refused,
184+
status=Grant.Status.pending,
185+
)
186+
187+
grant.status = Grant.Status.waiting_for_confirmation
188+
grant.save()
189+
190+
# Status should not be updated
191+
grant.refresh_from_db()
192+
193+
assert grant.pending_status == Grant.Status.refused
194+
assert grant.status == Grant.Status.waiting_for_confirmation

0 commit comments

Comments
 (0)