MukeshKapoor25 commited on
Commit
cd02845
·
1 Parent(s): 405aa89

fix(guest_model): sanitize guest data before MongoDB writes

Browse files

Add data sanitization step using prepare_for_db() function before all MongoDB update operations to ensure proper data format and prevent potential issues with date/time fields

Files changed (1) hide show
  1. app/models/guest_model.py +12 -4
app/models/guest_model.py CHANGED
@@ -52,9 +52,11 @@ class GuestModel:
52
  guest_doc["is_default"] = True
53
  guests.append(guest_doc)
54
 
 
 
55
  update_res = await BookMyServiceUserModel.collection.update_one(
56
  {"customer_id": customer_id},
57
- {"$set": {"guests": guests}}
58
  )
59
 
60
  if update_res.modified_count > 0:
@@ -148,9 +150,11 @@ class GuestModel:
148
  logger.warning(f"Embedded guest not found for update: {guest_id} (user {customer_id})")
149
  return False
150
 
 
 
151
  result = await BookMyServiceUserModel.collection.update_one(
152
  {"customer_id": customer_id},
153
- {"$set": {"guests": guests}}
154
  )
155
 
156
  if result.modified_count > 0:
@@ -179,9 +183,11 @@ class GuestModel:
179
  logger.warning(f"Embedded guest not found for deletion: {guest_id} (user {customer_id})")
180
  return False
181
 
 
 
182
  result = await BookMyServiceUserModel.collection.update_one(
183
  {"customer_id": customer_id},
184
- {"$set": {"guests": new_guests}}
185
  )
186
 
187
  if result.modified_count > 0:
@@ -272,9 +278,11 @@ class GuestModel:
272
  g["updated_at"] = now
273
  if not found:
274
  return False
 
 
275
  res = await BookMyServiceUserModel.collection.update_one(
276
  {"customer_id": customer_id},
277
- {"$set": {"guests": guests}}
278
  )
279
  return True
280
  except Exception as e:
 
52
  guest_doc["is_default"] = True
53
  guests.append(guest_doc)
54
 
55
+ # Sanitize for MongoDB (convert date to datetime, strip tzinfo, etc.)
56
+ sanitized_guests = prepare_for_db(guests)
57
  update_res = await BookMyServiceUserModel.collection.update_one(
58
  {"customer_id": customer_id},
59
+ {"$set": {"guests": sanitized_guests}}
60
  )
61
 
62
  if update_res.modified_count > 0:
 
150
  logger.warning(f"Embedded guest not found for update: {guest_id} (user {customer_id})")
151
  return False
152
 
153
+ # Sanitize for MongoDB before write
154
+ sanitized_guests = prepare_for_db(guests)
155
  result = await BookMyServiceUserModel.collection.update_one(
156
  {"customer_id": customer_id},
157
+ {"$set": {"guests": sanitized_guests}}
158
  )
159
 
160
  if result.modified_count > 0:
 
183
  logger.warning(f"Embedded guest not found for deletion: {guest_id} (user {customer_id})")
184
  return False
185
 
186
+ # Sanitize for MongoDB before write
187
+ sanitized_new_guests = prepare_for_db(new_guests)
188
  result = await BookMyServiceUserModel.collection.update_one(
189
  {"customer_id": customer_id},
190
+ {"$set": {"guests": sanitized_new_guests}}
191
  )
192
 
193
  if result.modified_count > 0:
 
278
  g["updated_at"] = now
279
  if not found:
280
  return False
281
+ # Sanitize for MongoDB before write
282
+ sanitized_guests = prepare_for_db(guests)
283
  res = await BookMyServiceUserModel.collection.update_one(
284
  {"customer_id": customer_id},
285
+ {"$set": {"guests": sanitized_guests}}
286
  )
287
  return True
288
  except Exception as e: