Skip to content

Commit 1d60f75

Browse files
authored
CDR : allow edition and deletion during cdr (#808)
### Description Please explain the changes you made here. ### Checklist - [ ] Created tests which fail without the change (if possible) - [ ] All tests passing - [ ] Extended the documentation, if necessary
1 parent bce0e1d commit 1d60f75

File tree

3 files changed

+50
-59
lines changed

3 files changed

+50
-59
lines changed

app/modules/cdr/cruds_cdr.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,18 @@ async def get_purchases_by_user_id_by_seller_id(
515515
return result.scalars().all()
516516

517517

518+
async def get_purchases_by_variant_id(
519+
db: AsyncSession,
520+
product_variant_id: UUID,
521+
) -> Sequence[models_cdr.Purchase]:
522+
result = await db.execute(
523+
select(models_cdr.Purchase).where(
524+
models_cdr.Purchase.product_variant_id == product_variant_id,
525+
),
526+
)
527+
return result.scalars().all()
528+
529+
518530
def create_purchase(
519531
db: AsyncSession,
520532
purchase: models_cdr.Purchase,

app/modules/cdr/endpoints_cdr.py

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -837,21 +837,19 @@ async def update_product(
837837
user,
838838
db=db,
839839
)
840-
status = await get_core_data(schemas_cdr.Status, db)
841-
db_product = await check_request_consistency(
840+
await check_request_consistency(
842841
db=db,
843842
seller_id=seller_id,
844843
product_id=product_id,
845844
)
846-
if status.status in [
847-
CdrStatus.onsite,
848-
CdrStatus.closed,
849-
] or (
850-
db_product and status.status == CdrStatus.online and db_product.available_online
851-
):
845+
variants = await cruds_cdr.get_product_variants(
846+
db=db,
847+
product_id=product_id,
848+
)
849+
if variants and product.related_membership:
852850
raise HTTPException(
853851
status_code=403,
854-
detail="This product can't be edited now. Please try creating a new product.",
852+
detail="You can't link or unlink this product to a membership if it has variant in it.",
855853
)
856854

857855
await cruds_cdr.update_product(
@@ -901,22 +899,13 @@ async def delete_product(
901899
user,
902900
db=db,
903901
)
904-
status = await get_core_data(schemas_cdr.Status, db)
905-
db_product = await check_request_consistency(
902+
903+
await check_request_consistency(
906904
db=db,
907905
seller_id=seller_id,
908906
product_id=product_id,
909907
)
910-
if status.status in [
911-
CdrStatus.onsite,
912-
CdrStatus.closed,
913-
] or (
914-
db_product and status.status == CdrStatus.online and db_product.available_online
915-
):
916-
raise HTTPException(
917-
status_code=403,
918-
detail="You can't delete a product once CDR has started.",
919-
)
908+
920909
variants = await cruds_cdr.get_product_variants(
921910
db=db,
922911
product_id=product_id,
@@ -1050,29 +1039,31 @@ async def update_product_variant(
10501039
user,
10511040
db=db,
10521041
)
1053-
status = await get_core_data(schemas_cdr.Status, db)
1042+
10541043
db_product = await check_request_consistency(
10551044
db=db,
10561045
seller_id=seller_id,
10571046
product_id=product_id,
10581047
variant_id=variant_id,
10591048
)
1060-
if product_variant.model_fields_set != {
1061-
"enabled",
1062-
}:
1063-
if status.status in [
1064-
CdrStatus.onsite,
1065-
CdrStatus.closed,
1066-
] or (
1067-
db_product
1068-
and status.status == CdrStatus.online
1069-
and db_product.available_online
1070-
):
1071-
# We allow to update the enabled field even if CDR is onsite or closed
1072-
raise HTTPException(
1073-
status_code=403,
1074-
detail="This product can't be edited now. Please try creating a new product.",
1075-
)
1049+
purchases = await cruds_cdr.get_purchases_by_variant_id(
1050+
db=db,
1051+
product_variant_id=variant_id,
1052+
)
1053+
if purchases and "price" in product_variant.model_fields_set:
1054+
raise HTTPException(
1055+
status_code=403,
1056+
detail="You can't edit the price of this variant because it has already been purchased.",
1057+
)
1058+
if (
1059+
purchases
1060+
and "related_membership_added_duration" in product_variant.model_fields_set
1061+
):
1062+
raise HTTPException(
1063+
status_code=403,
1064+
detail="You can't edit the membership added duration of this variant because it has already been purchased.",
1065+
)
1066+
10761067
if (
10771068
db_product
10781069
and not db_product.related_membership
@@ -1130,22 +1121,21 @@ async def delete_product_variant(
11301121
user,
11311122
db=db,
11321123
)
1133-
status = await get_core_data(schemas_cdr.Status, db)
1134-
db_product = await check_request_consistency(
1124+
await check_request_consistency(
11351125
db=db,
11361126
seller_id=seller_id,
11371127
product_id=product_id,
11381128
variant_id=variant_id,
11391129
)
1140-
if status.status in [
1141-
CdrStatus.onsite,
1142-
CdrStatus.closed,
1143-
] or (
1144-
db_product and status.status == CdrStatus.online and db_product.available_online
1145-
):
1130+
1131+
purchases = await cruds_cdr.get_purchases_by_variant_id(
1132+
db=db,
1133+
product_variant_id=variant_id,
1134+
)
1135+
if purchases:
11461136
raise HTTPException(
11471137
status_code=403,
1148-
detail="You can't delete a product once CDR has started.",
1138+
detail="You can't delete this variant because it has been purchased.",
11491139
)
11501140
await cruds_cdr.delete_allowed_curriculums(db=db, variant_id=variant_id)
11511141
await cruds_cdr.delete_product_variant(

tests/test_cdr.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ def test_patch_product_variant_cdr_started(client: TestClient):
14561456
response = client.patch(
14571457
f"/cdr/sellers/{seller.id!s}/products/{product.id!s}/variants/{variant.id}/",
14581458
json={
1459-
"name_fr": "Variante modifiée",
1459+
"price": "700",
14601460
},
14611461
headers={"Authorization": f"Bearer {token_bde}"},
14621462
)
@@ -2124,17 +2124,6 @@ def test_create_product_closed(client: TestClient):
21242124
assert response.status_code == 403
21252125

21262126

2127-
def test_patch_product_closed(client: TestClient):
2128-
response = client.patch(
2129-
f"/cdr/sellers/{seller.id!s}/products/{product.id}/",
2130-
json={
2131-
"name_fr": "Produit modifié",
2132-
},
2133-
headers={"Authorization": f"Bearer {token_bde}"},
2134-
)
2135-
assert response.status_code == 403
2136-
2137-
21382127
def test_create_product_variant_closed(client: TestClient):
21392128
response = client.post(
21402129
f"/cdr/sellers/{seller.id!s}/products/{product.id!s}/variants/",

0 commit comments

Comments
 (0)