LogicGoInfotechSpaces commited on
Commit
b69a1ee
·
1 Parent(s): 7fa5d1a

Add ai_edit_complete and ai_edit_last_date fields to media_clicks collection

Browse files
Files changed (1) hide show
  1. api/main.py +35 -14
api/main.py CHANGED
@@ -173,17 +173,23 @@ def log_media_click(user_id: Optional[str], category_id: Optional[str]) -> None:
173
  if doc:
174
  categories = doc.get("categories") or []
175
  if any(cat.get("categoryId") == category_obj for cat in categories):
 
176
  admin_media_clicks.update_one(
177
  {"_id": doc["_id"], "categories.categoryId": category_obj},
178
  {
179
- "$inc": {"categories.$.click_count": 1},
 
 
 
180
  "$set": {
181
  "categories.$.lastClickedAt": now,
182
  "updatedAt": now,
 
183
  },
184
  },
185
  )
186
  else:
 
187
  admin_media_clicks.update_one(
188
  {"_id": doc["_id"]},
189
  {
@@ -194,23 +200,38 @@ def log_media_click(user_id: Optional[str], category_id: Optional[str]) -> None:
194
  "lastClickedAt": now,
195
  }
196
  },
197
- "$set": {"updatedAt": now},
 
 
 
 
198
  },
199
  )
200
  else:
201
- admin_media_clicks.insert_one(
 
 
202
  {
203
- "userId": user_obj,
204
- "categories": [
205
- {
206
- "categoryId": category_obj,
207
- "click_count": 1,
208
- "lastClickedAt": now,
209
- }
210
- ],
211
- "createdAt": now,
212
- "updatedAt": now,
213
- }
 
 
 
 
 
 
 
 
 
214
  )
215
  except Exception as err:
216
  err_str = str(err)
 
173
  if doc:
174
  categories = doc.get("categories") or []
175
  if any(cat.get("categoryId") == category_obj for cat in categories):
176
+ # Category exists: increment click_count and ai_edit_complete, update dates
177
  admin_media_clicks.update_one(
178
  {"_id": doc["_id"], "categories.categoryId": category_obj},
179
  {
180
+ "$inc": {
181
+ "categories.$.click_count": 1,
182
+ "ai_edit_complete": 1, # $inc handles missing fields (backward compatible)
183
+ },
184
  "$set": {
185
  "categories.$.lastClickedAt": now,
186
  "updatedAt": now,
187
+ "ai_edit_last_date": now,
188
  },
189
  },
190
  )
191
  else:
192
+ # New category to existing document: push category, increment ai_edit_complete
193
  admin_media_clicks.update_one(
194
  {"_id": doc["_id"]},
195
  {
 
200
  "lastClickedAt": now,
201
  }
202
  },
203
+ "$inc": {"ai_edit_complete": 1}, # $inc handles missing fields
204
+ "$set": {
205
+ "updatedAt": now,
206
+ "ai_edit_last_date": now,
207
+ },
208
  },
209
  )
210
  else:
211
+ # New user: create document with default ai_edit_complete=0, then increment to 1
212
+ admin_media_clicks.update_one(
213
+ {"userId": user_obj},
214
  {
215
+ "$setOnInsert": {
216
+ "userId": user_obj,
217
+ "categories": [
218
+ {
219
+ "categoryId": category_obj,
220
+ "click_count": 1,
221
+ "lastClickedAt": now,
222
+ }
223
+ ],
224
+ "createdAt": now,
225
+ "updatedAt": now,
226
+ "ai_edit_complete": 0, # Default for new users
227
+ },
228
+ "$inc": {"ai_edit_complete": 1}, # Increment to 1 on first use
229
+ "$set": {
230
+ "updatedAt": now,
231
+ "ai_edit_last_date": now,
232
+ },
233
+ },
234
+ upsert=True,
235
  )
236
  except Exception as err:
237
  err_str = str(err)