LogicGoInfotechSpaces commited on
Commit
c90ea81
·
1 Parent(s): 9ed3b4a

Add ai_edit_complete and ai_edit_last_date fields to media_clicks collection

Browse files
Files changed (1) hide show
  1. app.py +48 -22
app.py CHANGED
@@ -226,21 +226,8 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
226
  lastClickedAt: Date
227
  }
228
  ],
229
- createdAt: Date,
230
- updatedAt: Date
231
- }
232
- """
233
- """Save or update media click in admin database media_clicks collection
234
- Structure:
235
- {
236
- userId: ObjectId,
237
- categories: [
238
- {
239
- categoryId: ObjectId,
240
- click_count: int,
241
- lastClickedAt: Date
242
- }
243
- ],
244
  createdAt: Date,
245
  updatedAt: Date
246
  }
@@ -303,6 +290,8 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
303
  return False
304
 
305
  now = datetime.utcnow()
 
 
306
 
307
  # Check if document with userId exists
308
  existing_doc = collection.find_one({"userId": user_object_id})
@@ -327,15 +316,20 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
327
 
328
  if category_exists:
329
  # Category exists, increment click_count and update lastClickedAt
 
330
  result = collection.update_one(
331
  {
332
  "userId": user_object_id,
333
  "categories.categoryId": category_object_id
334
  },
335
  {
336
- "$inc": {"categories.$.click_count": 1},
 
 
 
337
  "$set": {
338
  "categories.$.lastClickedAt": now,
 
339
  "updatedAt": now
340
  }
341
  }
@@ -346,6 +340,7 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
346
  print(f"[MongoDB] WARNING: Update matched but did not modify - userId: {user_object_id}, categoryId: {category_object_id}, matched: {result.matched_count}, modified: {result.modified_count}")
347
  else:
348
  # Category doesn't exist, add new category to array
 
349
  print(f"[MongoDB] Category not found in existing document, adding new category - userId: {user_object_id}, categoryId: {category_object_id}")
350
  result = collection.update_one(
351
  {"userId": user_object_id},
@@ -357,7 +352,13 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
357
  "lastClickedAt": now
358
  }
359
  },
360
- "$set": {"updatedAt": now}
 
 
 
 
 
 
361
  }
362
  )
363
  if result.modified_count > 0:
@@ -367,13 +368,15 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
367
  else:
368
  # Document doesn't exist, try to create new document
369
  # Use find_one_and_update with upsert which handles conflicts better
 
370
  try:
371
  result = collection.find_one_and_update(
372
  {"userId": user_object_id},
373
  {
374
  "$setOnInsert": {
375
  "userId": user_object_id,
376
- "createdAt": now
 
377
  },
378
  "$push": {
379
  "categories": {
@@ -382,7 +385,11 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
382
  "lastClickedAt": now
383
  }
384
  },
 
 
 
385
  "$set": {
 
386
  "updatedAt": now
387
  }
388
  },
@@ -409,7 +416,13 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
409
  "lastClickedAt": now
410
  }
411
  },
412
- "$set": {"updatedAt": now}
 
 
 
 
 
 
413
  }
414
  )
415
  print(f"[MongoDB] Added category to existing document - userId: {user_object_id}, categoryId: {category_object_id}")
@@ -426,7 +439,8 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
426
  {
427
  "$setOnInsert": {
428
  "userId": user_object_id,
429
- "createdAt": now
 
430
  },
431
  "$push": {
432
  "categories": {
@@ -435,7 +449,11 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
435
  "lastClickedAt": now
436
  }
437
  },
 
 
 
438
  "$set": {
 
439
  "updatedAt": now
440
  }
441
  },
@@ -463,6 +481,8 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
463
  existing = collection.find_one({"userId": user_object_id})
464
  if existing:
465
  # Document exists, try to add category
 
 
466
  result = collection.update_one(
467
  {"userId": user_object_id},
468
  {
@@ -470,10 +490,16 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
470
  "categories": {
471
  "categoryId": category_object_id,
472
  "click_count": 1,
473
- "lastClickedAt": datetime.utcnow()
474
  }
475
  },
476
- "$set": {"updatedAt": datetime.utcnow()}
 
 
 
 
 
 
477
  }
478
  )
479
  print(f"[MongoDB] Recovered from duplicate key error - updated document")
 
226
  lastClickedAt: Date
227
  }
228
  ],
229
+ ai_edit_complete: int, # Count of times user used any model (default: 0 for new users)
230
+ ai_edit_last_date: str, # Last date user used any model (format: DD/MM/YYYY, e.g., "12/12/2025")
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  createdAt: Date,
232
  updatedAt: Date
233
  }
 
290
  return False
291
 
292
  now = datetime.utcnow()
293
+ # Format date as DD/MM/YYYY (e.g., 12/12/2025)
294
+ date_formatted = now.strftime("%d/%m/%Y")
295
 
296
  # Check if document with userId exists
297
  existing_doc = collection.find_one({"userId": user_object_id})
 
316
 
317
  if category_exists:
318
  # Category exists, increment click_count and update lastClickedAt
319
+ # For existing users: increment ai_edit_complete by 1 and update ai_edit_last_date to current date
320
  result = collection.update_one(
321
  {
322
  "userId": user_object_id,
323
  "categories.categoryId": category_object_id
324
  },
325
  {
326
+ "$inc": {
327
+ "categories.$.click_count": 1,
328
+ "ai_edit_complete": 1 # Increment, default to 0 if field doesn't exist
329
+ },
330
  "$set": {
331
  "categories.$.lastClickedAt": now,
332
+ "ai_edit_last_date": date_formatted,
333
  "updatedAt": now
334
  }
335
  }
 
340
  print(f"[MongoDB] WARNING: Update matched but did not modify - userId: {user_object_id}, categoryId: {category_object_id}, matched: {result.matched_count}, modified: {result.modified_count}")
341
  else:
342
  # Category doesn't exist, add new category to array
343
+ # For existing users: increment ai_edit_complete by 1 and update ai_edit_last_date to current date
344
  print(f"[MongoDB] Category not found in existing document, adding new category - userId: {user_object_id}, categoryId: {category_object_id}")
345
  result = collection.update_one(
346
  {"userId": user_object_id},
 
352
  "lastClickedAt": now
353
  }
354
  },
355
+ "$inc": {
356
+ "ai_edit_complete": 1 # Increment, default to 0 if field doesn't exist
357
+ },
358
+ "$set": {
359
+ "ai_edit_last_date": date_formatted,
360
+ "updatedAt": now
361
+ }
362
  }
363
  )
364
  if result.modified_count > 0:
 
368
  else:
369
  # Document doesn't exist, try to create new document
370
  # Use find_one_and_update with upsert which handles conflicts better
371
+ # For new users: ai_edit_complete = 0 (default), then increment to 1 when adding category
372
  try:
373
  result = collection.find_one_and_update(
374
  {"userId": user_object_id},
375
  {
376
  "$setOnInsert": {
377
  "userId": user_object_id,
378
+ "createdAt": now,
379
+ "ai_edit_complete": 0 # Default value for new users
380
  },
381
  "$push": {
382
  "categories": {
 
385
  "lastClickedAt": now
386
  }
387
  },
388
+ "$inc": {
389
+ "ai_edit_complete": 1 # Increment from 0 to 1 when adding first category
390
+ },
391
  "$set": {
392
+ "ai_edit_last_date": date_formatted,
393
  "updatedAt": now
394
  }
395
  },
 
416
  "lastClickedAt": now
417
  }
418
  },
419
+ "$inc": {
420
+ "ai_edit_complete": 1 # Increment, default to 0 if field doesn't exist
421
+ },
422
+ "$set": {
423
+ "ai_edit_last_date": date_formatted,
424
+ "updatedAt": now
425
+ }
426
  }
427
  )
428
  print(f"[MongoDB] Added category to existing document - userId: {user_object_id}, categoryId: {category_object_id}")
 
439
  {
440
  "$setOnInsert": {
441
  "userId": user_object_id,
442
+ "createdAt": now,
443
+ "ai_edit_complete": 0 # Default value for new users
444
  },
445
  "$push": {
446
  "categories": {
 
449
  "lastClickedAt": now
450
  }
451
  },
452
+ "$inc": {
453
+ "ai_edit_complete": 1 # Increment from 0 to 1 when adding first category
454
+ },
455
  "$set": {
456
+ "ai_edit_last_date": date_formatted,
457
  "updatedAt": now
458
  }
459
  },
 
481
  existing = collection.find_one({"userId": user_object_id})
482
  if existing:
483
  # Document exists, try to add category
484
+ now_retry = datetime.utcnow()
485
+ date_formatted_retry = now_retry.strftime("%d/%m/%Y")
486
  result = collection.update_one(
487
  {"userId": user_object_id},
488
  {
 
490
  "categories": {
491
  "categoryId": category_object_id,
492
  "click_count": 1,
493
+ "lastClickedAt": now_retry
494
  }
495
  },
496
+ "$inc": {
497
+ "ai_edit_complete": 1 # Increment, default to 0 if field doesn't exist
498
+ },
499
+ "$set": {
500
+ "ai_edit_last_date": date_formatted_retry,
501
+ "updatedAt": now_retry
502
+ }
503
  }
504
  )
505
  print(f"[MongoDB] Recovered from duplicate key error - updated document")