@@ -3020,13 +3020,56 @@ async def create_custom_data_field(
30203020 id = uuid4 (),
30213021 product_id = product_id ,
30223022 name = custom_data_field .name ,
3023+ can_user_answer = custom_data_field .can_user_answer ,
30233024 )
30243025
30253026 cruds_cdr .create_customdata_field (db , db_data )
30263027 await db .flush ()
30273028 return db_data
30283029
30293030
3031+ @module .router .patch (
3032+ "/cdr/sellers/{seller_id}/products/{product_id}/data/{field_id}/" ,
3033+ status_code = 204 ,
3034+ )
3035+ async def update_custom_data_field (
3036+ seller_id : UUID ,
3037+ product_id : UUID ,
3038+ field_id : UUID ,
3039+ custom_data_field : schemas_cdr .CustomDataFieldBase ,
3040+ db : AsyncSession = Depends (get_db ),
3041+ user : models_users .CoreUser = Depends (is_user_a_member ),
3042+ ):
3043+ await is_user_in_a_seller_group (
3044+ seller_id ,
3045+ user ,
3046+ db = db ,
3047+ )
3048+ await check_request_consistency (db = db , seller_id = seller_id , product_id = product_id )
3049+ db_datafield = await cruds_cdr .get_customdata_field (db = db , field_id = field_id )
3050+ if db_datafield is None :
3051+ raise HTTPException (
3052+ status_code = 404 ,
3053+ detail = "Field not found." ,
3054+ )
3055+ if db_datafield .product_id != product_id :
3056+ raise HTTPException (
3057+ status_code = 403 ,
3058+ detail = "Field does not belong to this product." ,
3059+ )
3060+
3061+ datafield = schemas_cdr .CustomDataFieldBase (
3062+ name = custom_data_field .name ,
3063+ can_user_answer = custom_data_field .can_user_answer ,
3064+ )
3065+
3066+ await cruds_cdr .update_customdata_field (
3067+ db ,
3068+ field_id = field_id ,
3069+ datafield = datafield ,
3070+ )
3071+
3072+
30303073@module .router .delete (
30313074 "/cdr/sellers/{seller_id}/products/{product_id}/data/{field_id}/" ,
30323075 status_code = 204 ,
@@ -3105,18 +3148,29 @@ async def create_custom_data(
31053148 db : AsyncSession = Depends (get_db ),
31063149 user : models_users .CoreUser = Depends (is_user_a_member ),
31073150):
3108- await is_user_in_a_seller_group (
3109- seller_id ,
3110- user ,
3111- db = db ,
3112- )
31133151 await check_request_consistency (db = db , seller_id = seller_id , product_id = product_id )
31143152 db_field = await cruds_cdr .get_customdata_field (db = db , field_id = field_id )
31153153 if db_field is None :
31163154 raise HTTPException (
31173155 status_code = 404 ,
31183156 detail = "Field not found." ,
31193157 )
3158+ if not (
3159+ is_user_member_of_any_group (user , [GroupType .admin_cdr ])
3160+ or seller_id
3161+ in [
3162+ s .id
3163+ for s in await cruds_cdr .get_sellers_by_group_ids (
3164+ db = db ,
3165+ group_ids = [g .id for g in user .groups ],
3166+ )
3167+ ]
3168+ ) and not (db_field .can_user_answer and user_id == user .id ):
3169+ raise HTTPException (
3170+ status_code = 403 ,
3171+ detail = "You are not authorized to add data for this field." ,
3172+ )
3173+
31203174 if db_field .product_id != product_id :
31213175 raise HTTPException (
31223176 status_code = 403 ,
@@ -3147,18 +3201,28 @@ async def update_custom_data(
31473201 db : AsyncSession = Depends (get_db ),
31483202 user : models_users .CoreUser = Depends (is_user_a_member ),
31493203):
3150- await is_user_in_a_seller_group (
3151- seller_id ,
3152- user ,
3153- db = db ,
3154- )
31553204 await check_request_consistency (db = db , seller_id = seller_id , product_id = product_id )
31563205 db_data = await cruds_cdr .get_customdata (db = db , field_id = field_id , user_id = user_id )
31573206 if db_data is None :
31583207 raise HTTPException (
31593208 status_code = 404 ,
31603209 detail = "Field Data not found." ,
31613210 )
3211+ if not (
3212+ is_user_member_of_any_group (user , [GroupType .admin_cdr ])
3213+ or seller_id
3214+ in [
3215+ s .id
3216+ for s in await cruds_cdr .get_sellers_by_group_ids (
3217+ db = db ,
3218+ group_ids = [g .id for g in user .groups ],
3219+ )
3220+ ]
3221+ ) and not (db_data .field .can_user_answer and user_id == user .id ):
3222+ raise HTTPException (
3223+ status_code = 403 ,
3224+ detail = "You are not authorized to edit data for this field." ,
3225+ )
31623226 if db_data .field .product_id != product_id :
31633227 raise HTTPException (
31643228 status_code = 403 ,
@@ -3185,18 +3249,28 @@ async def delete_customdata(
31853249 db : AsyncSession = Depends (get_db ),
31863250 user : models_users .CoreUser = Depends (is_user_a_member ),
31873251):
3188- await is_user_in_a_seller_group (
3189- seller_id ,
3190- user ,
3191- db = db ,
3192- )
31933252 await check_request_consistency (db = db , seller_id = seller_id , product_id = product_id )
31943253 db_data = await cruds_cdr .get_customdata (db = db , field_id = field_id , user_id = user_id )
31953254 if db_data is None :
31963255 raise HTTPException (
31973256 status_code = 404 ,
31983257 detail = "Field Data not found." ,
31993258 )
3259+ if not (
3260+ is_user_member_of_any_group (user , [GroupType .admin_cdr ])
3261+ or seller_id
3262+ in [
3263+ s .id
3264+ for s in await cruds_cdr .get_sellers_by_group_ids (
3265+ db = db ,
3266+ group_ids = [g .id for g in user .groups ],
3267+ )
3268+ ]
3269+ ) and not (db_data .field .can_user_answer and user_id == user .id ):
3270+ raise HTTPException (
3271+ status_code = 403 ,
3272+ detail = "You are not authorized to delete data for this field." ,
3273+ )
32003274 if db_data .field .product_id != product_id :
32013275 raise HTTPException (
32023276 status_code = 403 ,
0 commit comments