@@ -222,20 +222,19 @@ export const getAllSellerItems = async (
222
222
}
223
223
} ;
224
224
225
- export const addOrUpdateSellerItem1 = async (
225
+ export const addOrUpdateSellerItem = async (
226
226
seller : ISeller ,
227
227
item : ISellerItem
228
228
) : Promise < ISellerItem | null > => {
229
-
230
229
try {
231
230
const today = new Date ( ) ;
232
231
233
- // Calculate expiration date based on duration (defaults to 1 week)
232
+ // Ensure duration is valid (default to 1 week)
234
233
const duration = Number ( item . duration ) || 1 ;
235
234
const durationInMs = duration * 7 * 24 * 60 * 60 * 1000 ;
236
235
const expiredBy = new Date ( item . created_at . getTime ( ) + durationInMs ) ;
237
236
238
- // Ensure unique identifier is used for finding existing items
237
+ // Define a unique query for finding existing items
239
238
const query = {
240
239
_id : item . _id || undefined ,
241
240
seller_id : seller . seller_id ,
@@ -245,9 +244,11 @@ export const addOrUpdateSellerItem1 = async (
245
244
const existingItem = await SellerItem . findOne ( query ) ;
246
245
247
246
if ( existingItem ) {
248
- if ( item . expired_by < today ) {
249
- item . created_at = today
247
+ // If the item is expired, reset `created_at`
248
+ if ( item . expired_by < today ) {
249
+ item . created_at = today ;
250
250
}
251
+
251
252
// Update the existing item
252
253
existingItem . set ( {
253
254
...item ,
@@ -260,7 +261,7 @@ export const addOrUpdateSellerItem1 = async (
260
261
logger . info ( 'Item updated successfully:' , { updatedItem } ) ;
261
262
return updatedItem ;
262
263
} else {
263
- // Ensure item has a unique identifier for creation
264
+ // Create a new item with a unique ID
264
265
const newItemId = item . _id || new mongoose . Types . ObjectId ( ) . toString ( ) ;
265
266
266
267
// Create a new item
@@ -284,89 +285,11 @@ export const addOrUpdateSellerItem1 = async (
284
285
return newItem ;
285
286
}
286
287
} catch ( error ) {
287
- logger . error ( `Failed to add or update seller item for sellerID ${ seller . seller_id } :` , error ) ;
288
- throw new Error ( 'Failed to add or update seller item; please try again later' ) ;
289
- }
290
- } ;
291
-
292
- export const addOrUpdateSellerItem = async (
293
- seller : ISeller ,
294
- item : ISellerItem
295
- ) : Promise < ISellerItem | null > => {
296
- try {
297
- const today = new Date ( ) ;
298
-
299
- // Ensure duration is valid (default to 1 week)
300
- const duration = Number ( item . duration ) || 1 ;
301
- const durationInMs = duration * 7 * 24 * 60 * 60 * 1000 ;
302
-
303
- // Define a unique query for finding existing items
304
- const query = {
305
- _id : item . _id || undefined ,
306
- seller_id : seller . seller_id ,
307
- } ;
308
-
309
- // Attempt to find the existing item
310
- let existingItem = await SellerItem . findOne ( query ) ;
311
-
312
- if ( existingItem ) {
313
- // If the item is expired, reset `created_at`
314
- if ( existingItem . expired_by < today ) {
315
- existingItem . created_at = today ;
316
- }
317
-
318
- // Compute new expiration date based on `created_at` + duration
319
- let newExpiredBy = new Date ( existingItem . created_at . getTime ( ) + durationInMs ) ;
320
-
321
- // Ensure the expiration date is not reduced below the present date
322
- if ( newExpiredBy < today ) {
323
- newExpiredBy = today ;
324
- }
325
-
326
- // Update fields
327
- existingItem . set ( {
328
- ...item ,
329
- updated_at : today ,
330
- expired_by : newExpiredBy ,
331
- image : item . image || existingItem . image , // Keep existing image if none provided
332
- } ) ;
333
-
334
- const updatedItem = await existingItem . save ( ) ;
335
- logger . info ( 'Item updated successfully:' , { updatedItem } ) ;
336
- return updatedItem ;
337
- } else {
338
- // Create a new item with a unique ID
339
- const newItemId = item . _id || new mongoose . Types . ObjectId ( ) . toString ( ) ;
340
-
341
- // Set creation and expiration dates
342
- const createdAt = today ;
343
- const expiredBy = new Date ( createdAt . getTime ( ) + durationInMs ) ;
344
-
345
- const newItem = new SellerItem ( {
346
- _id : newItemId ,
347
- seller_id : seller . seller_id ,
348
- name : item . name ?. trim ( ) || '' ,
349
- description : item . description ?. trim ( ) || '' ,
350
- price : parseFloat ( item . price ?. toString ( ) || '0.01' ) ,
351
- stock_level : item . stock_level || '1 available' ,
352
- duration : duration ,
353
- image : item . image ,
354
- created_at : createdAt ,
355
- updated_at : createdAt ,
356
- expired_by : expiredBy ,
357
- } ) ;
358
-
359
- await newItem . save ( ) ;
360
- logger . info ( 'Item created successfully:' , { newItem } ) ;
361
- return newItem ;
362
- }
363
- } catch ( error ) {
364
288
logger . error ( `Failed to add or update seller item for sellerID ${ seller . seller_id } :` , error ) ;
365
289
throw new Error ( 'Failed to add or update seller item; please try again later' ) ;
366
290
}
367
291
} ;
368
292
369
-
370
293
// Delete existing seller item
371
294
export const deleteSellerItem = async ( id : string ) : Promise < ISellerItem | null > => {
372
295
try {
0 commit comments