Skip to content

Commit 9db46b8

Browse files
committed
Retrofit temp function into existing function.
1 parent 33cf954 commit 9db46b8

File tree

2 files changed

+12
-87
lines changed

2 files changed

+12
-87
lines changed

src/services/seller.service.ts

Lines changed: 8 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,19 @@ export const getAllSellerItems = async (
222222
}
223223
};
224224

225-
export const addOrUpdateSellerItem1 = async (
225+
export const addOrUpdateSellerItem = async (
226226
seller: ISeller,
227227
item: ISellerItem
228228
): Promise<ISellerItem | null> => {
229-
230229
try {
231230
const today = new Date();
232231

233-
// Calculate expiration date based on duration (defaults to 1 week)
232+
// Ensure duration is valid (default to 1 week)
234233
const duration = Number(item.duration) || 1;
235234
const durationInMs = duration * 7 * 24 * 60 * 60 * 1000;
236235
const expiredBy = new Date(item.created_at.getTime() + durationInMs);
237236

238-
// Ensure unique identifier is used for finding existing items
237+
// Define a unique query for finding existing items
239238
const query = {
240239
_id: item._id || undefined,
241240
seller_id: seller.seller_id,
@@ -245,9 +244,11 @@ export const addOrUpdateSellerItem1 = async (
245244
const existingItem = await SellerItem.findOne(query);
246245

247246
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;
250250
}
251+
251252
// Update the existing item
252253
existingItem.set({
253254
...item,
@@ -260,7 +261,7 @@ export const addOrUpdateSellerItem1 = async (
260261
logger.info('Item updated successfully:', { updatedItem });
261262
return updatedItem;
262263
} else {
263-
// Ensure item has a unique identifier for creation
264+
// Create a new item with a unique ID
264265
const newItemId = item._id || new mongoose.Types.ObjectId().toString();
265266

266267
// Create a new item
@@ -284,89 +285,11 @@ export const addOrUpdateSellerItem1 = async (
284285
return newItem;
285286
}
286287
} 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) {
364288
logger.error(`Failed to add or update seller item for sellerID ${seller.seller_id}:`, error);
365289
throw new Error('Failed to add or update seller item; please try again later');
366290
}
367291
};
368292

369-
370293
// Delete existing seller item
371294
export const deleteSellerItem = async (id: string): Promise<ISellerItem | null> => {
372295
try {

test/services/seller.service.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ describe('addOrUpdateSellerItem function', () => {
274274
price: 0.50,
275275
stock_level: "Many available",
276276
duration: 2,
277-
image: 'http://example.com/testSellerThreeItemOne.jpg'
277+
image: 'http://example.com/testSellerThreeItemOne.jpg',
278+
created_at: new Date()
278279
} as unknown as ISellerItem;
279280

280281
const sellerItemData = (await addOrUpdateSellerItem(
@@ -315,7 +316,8 @@ describe('addOrUpdateSellerItem function', () => {
315316
price: 0.50,
316317
stock_level: "Sold",
317318
duration: 2,
318-
image: 'http://example.com/testSellerThreeItemOneUpdated.jpg'
319+
image: 'http://example.com/testSellerThreeItemOneUpdated.jpg',
320+
created_at: new Date()
319321
} as unknown as ISellerItem;
320322

321323
const sellerItemData = (

0 commit comments

Comments
 (0)