@@ -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 (
0 commit comments