Skip to content

Commit a60d68c

Browse files
♻️(backend) refactor batch order serializers for contract field
Since we have changed we build the relation between batch order and a contract, we needed to update the serializers for the client and admin API that it returns the same information as before.
1 parent 3d84f12 commit a60d68c

File tree

7 files changed

+19
-13
lines changed

7 files changed

+19
-13
lines changed

src/backend/joanie/core/serializers/admin.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,7 @@ class AdminBatchOrderSerializer(serializers.ModelSerializer):
17741774
slug_field="id",
17751775
write_only=False,
17761776
)
1777-
contract = AdminContractSerializer(read_only=True)
1777+
contract = serializers.SerializerMethodField(read_only=True)
17781778
organization = AdminOrganizationLightSerializer(read_only=True)
17791779
main_invoice_reference = serializers.SlugRelatedField(
17801780
read_only=True, slug_field="reference", source="main_invoice"
@@ -1855,7 +1855,7 @@ class Meta:
18551855
"total",
18561856
"currency",
18571857
"main_invoice_reference",
1858-
"contract_id",
1858+
"contract",
18591859
"offering_rules",
18601860
"vouchers",
18611861
"quote",
@@ -1872,6 +1872,12 @@ def get_vouchers(self, instance) -> list:
18721872
"""Return the voucher codes generated"""
18731873
return instance.vouchers
18741874

1875+
def get_contract(self, instance):
1876+
"""Return serialized information of the contract related to the batch order"""
1877+
if contract := getattr(instance, "contract", None):
1878+
return AdminContractSerializer(contract).data
1879+
return None
1880+
18751881
def to_internal_value(self, data):
18761882
"""
18771883
Override to ensure that the 'offering' field is renamed to 'relation'

src/backend/joanie/core/serializers/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,7 @@ class BatchOrderSerializer(serializers.ModelSerializer):
15501550
required=False,
15511551
read_only=True,
15521552
)
1553+
contract_id = serializers.SerializerMethodField(read_only=True)
15531554
quote = QuoteLightSerializer(read_only=True)
15541555
payment_method = serializers.ChoiceField(
15551556
choices=enums.BATCH_ORDER_PAYMENT_METHOD_CHOICES,
@@ -1633,6 +1634,12 @@ def get_currency(self, *args, **kwargs) -> str:
16331634
"""
16341635
return settings.DEFAULT_CURRENCY
16351636

1637+
def get_contract_id(self, instance):
1638+
"""Return serialized contract id related to batch order"""
1639+
if contract := getattr(instance, "contract", None):
1640+
return str(contract.id)
1641+
return None
1642+
16361643
def to_internal_value(self, data):
16371644
"""
16381645
Override to ensure that the 'offering' field is renamed to 'relation'

src/backend/joanie/tests/core/api/admin/batch_order/test_read_detail.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def test_api_admin_read_detail_batch_order_with_quote(self):
126126
payment_method=enums.BATCH_ORDER_WITH_BANK_TRANSFER,
127127
)
128128
batch_order.freeze_total(Decimal("100.00"))
129-
129+
batch_order.refresh_from_db()
130130
response = self.client.get(f"/api/v1.0/admin/batch-orders/{batch_order.id}/")
131131

132132
self.assertStatusCodeEqual(response, HTTPStatus.OK)

src/backend/joanie/tests/core/api/batch_order/test_read_detail.perf.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
BatchOrderReadDetailAPITest.test_api_batch_order_read_detail_authenticated:
2-
- db: 'SELECT ... FROM "joanie_batch_order" INNER JOIN "joanie_user" ON ("joanie_batch_order"."owner_id" = "joanie_user"."id") INNER JOIN "joanie_course_product_relation" ON ("joanie_batch_order"."relation_id" = "joanie_course_product_relation"."id") LEFT OUTER JOIN "joanie_organization" ON ("joanie_batch_order"."organization_id" = "joanie_organization"."id") LEFT OUTER JOIN "joanie_contract" ON ("joanie_batch_order"."contract_id" = "joanie_contract"."id") WHERE ("joanie_user"."username" = # AND "joanie_batch_order"."id" = #::uuid) LIMIT #'
2+
- db: 'SELECT ... FROM "joanie_batch_order" INNER JOIN "joanie_user" ON ("joanie_batch_order"."owner_id" = "joanie_user"."id") INNER JOIN "joanie_course_product_relation" ON ("joanie_batch_order"."relation_id" = "joanie_course_product_relation"."id") LEFT OUTER JOIN "joanie_organization" ON ("joanie_batch_order"."organization_id" = "joanie_organization"."id") LEFT OUTER JOIN "joanie_contract" ON ("joanie_batch_order"."id" = "joanie_contract"."batch_order_id") WHERE ("joanie_user"."username" = # AND "joanie_batch_order"."id" = #::uuid) LIMIT #'
33
- db: 'SELECT ... FROM "joanie_user" WHERE "joanie_user"."id" = #::uuid LIMIT #'
44
- db: 'SELECT ... FROM "joanie_course" WHERE "joanie_course"."id" = #::uuid LIMIT #'
55
- cache|get: parler.core.CourseTranslation.#.en-us

src/backend/joanie/tests/core/api/batch_order/test_read_list.perf.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BatchOrderReadListAPITest.test_api_batch_order_read_authenticated_get_list:
22
- db: 'SELECT COUNT(*) AS "__count" FROM "joanie_batch_order" INNER JOIN "joanie_user" ON ("joanie_batch_order"."owner_id" = "joanie_user"."id") WHERE "joanie_user"."username" = #'
3-
- db: 'SELECT ... FROM "joanie_batch_order" INNER JOIN "joanie_user" ON ("joanie_batch_order"."owner_id" = "joanie_user"."id") INNER JOIN "joanie_course_product_relation" ON ("joanie_batch_order"."relation_id" = "joanie_course_product_relation"."id") LEFT OUTER JOIN "joanie_organization" ON ("joanie_batch_order"."organization_id" = "joanie_organization"."id") LEFT OUTER JOIN "joanie_contract" ON ("joanie_batch_order"."contract_id" = "joanie_contract"."id") WHERE "joanie_user"."username" = # ORDER BY "joanie_batch_order"."created_on" ASC LIMIT #'
3+
- db: 'SELECT ... FROM "joanie_batch_order" INNER JOIN "joanie_user" ON ("joanie_batch_order"."owner_id" = "joanie_user"."id") INNER JOIN "joanie_course_product_relation" ON ("joanie_batch_order"."relation_id" = "joanie_course_product_relation"."id") LEFT OUTER JOIN "joanie_organization" ON ("joanie_batch_order"."organization_id" = "joanie_organization"."id") LEFT OUTER JOIN "joanie_contract" ON ("joanie_batch_order"."id" = "joanie_contract"."batch_order_id") WHERE "joanie_user"."username" = # ORDER BY "joanie_batch_order"."created_on" ASC LIMIT #'
44
- db: 'SELECT ... FROM "joanie_user" WHERE "joanie_user"."id" = #::uuid LIMIT #'
55
- db: 'SELECT ... FROM "joanie_course" WHERE "joanie_course"."id" = #::uuid LIMIT #'
66
- cache|get: parler.core.CourseTranslation.#.en-us

src/backend/joanie/tests/swagger/admin-swagger.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6405,11 +6405,7 @@
64056405
"readOnly": true
64066406
},
64076407
"contract": {
6408-
"allOf": [
6409-
{
6410-
"$ref": "#/components/schemas/AdminContract"
6411-
}
6412-
],
6408+
"type": "string",
64136409
"readOnly": true
64146410
},
64156411
"company_name": {

src/backend/joanie/tests/swagger/swagger.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5670,9 +5670,6 @@
56705670
},
56715671
"contract_id": {
56725672
"type": "string",
5673-
"format": "uuid",
5674-
"description": "contract of type convention",
5675-
"nullable": true,
56765673
"readOnly": true
56775674
},
56785675
"company_name": {

0 commit comments

Comments
 (0)