Skip to content

Commit

Permalink
🚑️(backend) fix order update failure with archive course run
Browse files Browse the repository at this point in the history
Currently, if an order is not canceled and it relies on an archived course run,
it can be update at all! We fix the implied condition to fix this behaviour.
  • Loading branch information
jbpenrath committed Dec 11, 2024
1 parent 53bd3f3 commit 48e4432
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ and this project adheres to
- Update product endpoint to CRUD `teachers`, `skills`
and `certification level` fields

### Fixed

- Fix order update on archived course run

## [2.10.0] - 2024-11-20

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/backend/joanie/core/models/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ def clean(self):
)
)
if (
not self.created_on or self.state not in [enums.ORDER_STATE_CANCELED]
not self.created_on and self.state not in [enums.ORDER_STATE_CANCELED]
) and (
self.enrollment.course_run.course.state["priority"]
>= CourseState.ARCHIVED_CLOSED
Expand Down
40 changes: 39 additions & 1 deletion src/backend/joanie/tests/core/test_models_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.utils import timezone as django_timezone

from joanie.core import enums, factories
from joanie.core.enums import PAYMENT_STATE_PENDING
from joanie.core.factories import CourseRunFactory
from joanie.core.models import Contract, CourseState
from joanie.core.utils import contract_definition
Expand Down Expand Up @@ -1223,7 +1224,7 @@ def test_models_order_avoid_to_create_with_an_archived_course_run(self):
),
)

def test_api_order_allow_to_cancel_with_archived_course_run(self):
def test_models_order_allow_to_cancel_with_archived_course_run(self):
"""
An order should be cancelable even if the related course run is archived.
"""
Expand All @@ -1249,3 +1250,40 @@ def test_api_order_allow_to_cancel_with_archived_course_run(self):
order.flow.cancel()

self.assertEqual(order.state, enums.ORDER_STATE_CANCELED)

def test_models_order_update_order_with_archived_course_run(self):
"""
It should be possible to update a non canceled order with an archived course run.
"""
course_run = factories.CourseRunFactory(
is_listed=True, state=CourseState.ONGOING_OPEN
)
enrollment = factories.EnrollmentFactory(course_run=course_run, is_active=True)
order = factories.OrderFactory(
owner=enrollment.user,
enrollment=enrollment,
course=None,
product__type=enums.PRODUCT_TYPE_CERTIFICATE,
product__courses=[enrollment.course_run.course],
state=enums.ORDER_STATE_PENDING,
payment_schedule=[
{
"id": "1932fbc5-d971-48aa-8fee-6d637c3154a5",
"amount": "200.00",
"due_date": "2024-01-17",
"state": PAYMENT_STATE_PENDING,
},
],
)

# Update the course run to archived it
closing_date = django_timezone.now() - timedelta(days=1)
course_run.enrollment_end = closing_date
course_run.end = closing_date
course_run.save()

order.set_installment_paid("1932fbc5-d971-48aa-8fee-6d637c3154a5")
order.flow.update()

self.assertEqual(order.state, enums.ORDER_STATE_COMPLETED)
self.assertEqual(order.payment_schedule[0]["state"], enums.PAYMENT_STATE_PAID)

0 comments on commit 48e4432

Please sign in to comment.