Skip to content

Commit e0fbcf0

Browse files
committed
Misc PR adjustments; use auto-generated DB timestamps; WIP.
1 parent 9db46b8 commit e0fbcf0

File tree

6 files changed

+50
-63
lines changed

6 files changed

+50
-63
lines changed

src/models/SellerItem.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,13 @@ const sellerItemSchema = new Schema<ISellerItem>(
3636
default: 1,
3737
min: 1
3838
},
39-
created_at: {
40-
type: Date,
41-
required: true,
42-
},
43-
updated_at: {
44-
type: Date,
45-
required: true,
46-
},
4739
expired_by: {
4840
type: Date,
4941
required: true,
5042
}
43+
},
44+
{
45+
timestamps: true, // Enables createdAt and updatedAt
5146
}
5247
);
5348

src/models/enums/stockLevelType.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export enum StockLevelType {
2-
available_1 = '1 available',
3-
available_2 = '2 available',
4-
available_3 = '3 available',
5-
many = 'Many available',
6-
made_to_order = 'Made to order',
7-
ongoing_service = 'Ongoing service',
8-
sold = 'Sold'
2+
AVAILABLE_1 = '1 available',
3+
AVAILABLE_2 = '2 available',
4+
AVAILABLE_3 = '3 available',
5+
MANY_AVAILABLE = 'Many available',
6+
MADE_TO_ORDER = 'Made to order',
7+
ONGOING_SERVICE = 'Ongoing service',
8+
SOLD = 'Sold'
99
}

src/services/seller.service.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import mongoose from 'mongoose';
22
import Seller from "../models/Seller";
33
import User from "../models/User";
44
import UserSettings from "../models/UserSettings";
5+
import SellerItem from "../models/SellerItem";
56
import { FulfillmentType, VisibleSellerType } from '../models/enums/sellerType';
7+
import { StockLevelType } from '../models/enums/stockLevelType';
68
import { TrustMeterScale } from "../models/enums/trustMeterScale";
79
import { getUserSettingsById } from "./userSettings.service";
810
import { IUser, IUserSettings, ISeller, ISellerWithSettings, ISellerItem, ISanctionedRegion } from "../types";
911

1012
import logger from "../config/loggingConfig";
11-
import SellerItem from "../models/SellerItem";
1213

1314
// Helper function to get settings for all sellers and merge them into seller objects
1415
const resolveSellerSettings = async (sellers: ISeller[]): Promise<ISellerWithSettings[]> => {
@@ -232,7 +233,7 @@ export const addOrUpdateSellerItem = async (
232233
// Ensure duration is valid (default to 1 week)
233234
const duration = Number(item.duration) || 1;
234235
const durationInMs = duration * 7 * 24 * 60 * 60 * 1000;
235-
const expiredBy = new Date(item.created_at.getTime() + durationInMs);
236+
const expiredBy = new Date(today.getTime() + durationInMs);
236237

237238
// Define a unique query for finding existing items
238239
const query = {
@@ -244,15 +245,9 @@ export const addOrUpdateSellerItem = async (
244245
const existingItem = await SellerItem.findOne(query);
245246

246247
if (existingItem) {
247-
// If the item is expired, reset `created_at`
248-
if (item.expired_by < today) {
249-
item.created_at = today;
250-
}
251-
252248
// Update the existing item
253249
existingItem.set({
254250
...item,
255-
updated_at: today,
256251
expired_by: expiredBy,
257252
image: item.image || existingItem.image, // Use existing image if a new one isn't provided
258253
});
@@ -271,11 +266,9 @@ export const addOrUpdateSellerItem = async (
271266
name: item.name ? item.name.trim() : '',
272267
description: item.description ? item.description.trim() : '',
273268
price: parseFloat(item.price?.toString() || '0.01'), // Ensure valid price
274-
stock_level: item.stock_level || '1 available',
269+
stock_level: item.stock_level || StockLevelType.AVAILABLE_1,
275270
duration: parseInt(item.duration?.toString() || '1'), // Ensure valid duration
276271
image: item.image,
277-
created_at: today,
278-
updated_at: today,
279272
expired_by: expiredBy,
280273
});
281274

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
stock_level: StockLevelType;
5353
image?: string;
5454
duration: number;
55-
created_at: Date;
56-
updated_at: Date;
5755
expired_by: Date;
56+
createdAt: Date;
57+
updatedAt: Date;
5858
}
5959
export interface IReviewFeedback extends Document {
6060
_id: string;

test/mockData.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@
224224
"stock_level": "1 available",
225225
"image": "http://example.com/testSellerOneItemOne.jpg",
226226
"duration": 1,
227-
"created_at": "2025-01-08T00:00:00.000Z",
228-
"updated_at": "2025-01-08T00:00:00.000Z",
229-
"expired_by": "2025-01-15T00:00:00.000Z"
227+
"expired_by": "2025-01-15T00:00:00.000Z",
228+
"createdAt": "2025-01-08T00:00:00.000Z",
229+
"updatedAt": "2025-01-08T00:00:00.000Z"
230230
},
231231
{
232232
"_id": "24f5a0f2a86d1f9f3b7e4e82",
@@ -237,9 +237,9 @@
237237
"stock_level": "2 available",
238238
"image": "http://example.com/testSellerOneItemTwo.jpg",
239239
"duration": 2,
240-
"created_at": "2025-01-08T00:00:00.000Z",
241-
"updated_at": "2025-01-08T00:00:00.000Z",
242-
"expired_by": "2025-01-22T00:00:00.000Z"
240+
"expired_by": "2025-01-22T00:00:00.000Z",
241+
"createdAt": "2025-01-08T00:00:00.000Z",
242+
"updatedAt": "2025-01-08T00:00:00.000Z"
243243
},
244244
{
245245
"_id": "25f5a0f2a86d1f9f3b7e4e81",
@@ -250,9 +250,9 @@
250250
"stock_level": "Sold",
251251
"image": "http://example.com/testSellerTwoItemOne.jpg",
252252
"duration": 1,
253-
"created_at": "2025-01-09T00:00:00.000Z",
254-
"updated_at": "2025-01-09T00:00:00.000Z",
255-
"expired_by": "2025-01-16T00:00:00.000Z"
253+
"expired_by": "2025-01-16T00:00:00.000Z",
254+
"createdAt": "2025-01-09T00:00:00.000Z",
255+
"updatedAt": "2025-01-09T00:00:00.000Z"
256256
},
257257
{
258258
"_id": "25f5a0f2a86d1f9f3b7e4e82",
@@ -263,9 +263,9 @@
263263
"stock_level": "Ongoing service",
264264
"image": "http://example.com/testSellerTwoItemTwo.jpg",
265265
"duration": 1,
266-
"created_at": "2025-01-10T00:00:00.000Z",
267-
"updated_at": "2025-01-10T00:00:00.000Z",
268-
"expired_by": "2025-01-17T00:00:00.000Z"
266+
"expired_by": "2025-01-17T00:00:00.000Z",
267+
"createdAt": "2025-01-10T00:00:00.000Z",
268+
"updatedAt": "2025-01-10T00:00:00.000Z"
269269
}
270270
],
271271
"reviews": [

test/services/seller.service.spec.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,14 @@ describe('addOrUpdateSellerItem function', () => {
239239
plainObject.price = Number(plainObject.price);
240240
}
241241

242-
if (plainObject.created_at) {
243-
plainObject.created_at = new Date(plainObject.created_at);
244-
plainObject.created_at.setHours(0, 0, 0, 0);
242+
if (plainObject.createdAt) {
243+
plainObject.createdAt = new Date(plainObject.createdAt);
244+
plainObject.createdAt.setHours(0, 0, 0, 0);
245245
}
246246

247-
if (plainObject.updated_at) {
248-
plainObject.updated_at = new Date(plainObject.updated_at);
249-
plainObject.updated_at.setHours(0, 0, 0, 0);
247+
if (plainObject.updatedAt) {
248+
plainObject.updatedAt = new Date(plainObject.updatedAt);
249+
plainObject.updatedAt.setHours(0, 0, 0, 0);
250250
}
251251

252252
if (plainObject.expired_by) {
@@ -262,7 +262,7 @@ describe('addOrUpdateSellerItem function', () => {
262262
};
263263

264264
const assertUpdatedSellerItem = (actual: any, expected: any) => {
265-
const { __v, created_at, ...filteredActual } = actual; // ignore DB values.
265+
const { __v, createdAt, ...filteredActual } = actual; // ignore DB values.
266266
expect(filteredActual).toEqual(expect.objectContaining({ ...expected, _id: actual._id }));
267267
};
268268

@@ -275,7 +275,7 @@ describe('addOrUpdateSellerItem function', () => {
275275
stock_level: "Many available",
276276
duration: 2,
277277
image: 'http://example.com/testSellerThreeItemOne.jpg',
278-
created_at: new Date()
278+
createdAt: '2025-02-20T00:00:00.000Z'
279279
} as unknown as ISellerItem;
280280

281281
const sellerItemData = (await addOrUpdateSellerItem(
@@ -301,9 +301,9 @@ describe('addOrUpdateSellerItem function', () => {
301301
stock_level: sellerItem.stock_level,
302302
duration: sellerItem.duration,
303303
image: sellerItem.image,
304-
created_at: current_date,
305-
updated_at: current_date,
306-
expired_by: expired_date
304+
expired_by: expired_date,
305+
createdAt: current_date,
306+
updatedAt: current_date
307307
});
308308
});
309309

@@ -316,8 +316,7 @@ describe('addOrUpdateSellerItem function', () => {
316316
price: 0.50,
317317
stock_level: "Sold",
318318
duration: 2,
319-
image: 'http://example.com/testSellerThreeItemOneUpdated.jpg',
320-
created_at: new Date()
319+
image: 'http://example.com/testSellerThreeItemOneUpdated.jpg'
321320
} as unknown as ISellerItem;
322321

323322
const sellerItemData = (
@@ -345,8 +344,8 @@ describe('addOrUpdateSellerItem function', () => {
345344
stock_level: sellerItem.stock_level,
346345
duration: sellerItem.duration,
347346
image: sellerItem.image,
348-
updated_at: current_date,
349-
expired_by: expired_date
347+
expired_by: expired_date,
348+
updatedAt: current_date
350349
});
351350
});
352351

@@ -383,11 +382,11 @@ describe('deleteSellerItem function', () => {
383382
}
384383

385384
// Normalize timestamps
386-
if (plainObject.created_at instanceof Date) {
387-
plainObject.created_at = plainObject.created_at.toISOString();
385+
if (plainObject.createdAt instanceof Date) {
386+
plainObject.createdAt = plainObject.createdAt.toISOString();
388387
}
389-
if (plainObject.updated_at instanceof Date) {
390-
plainObject.updated_at = plainObject.updated_at.toISOString();
388+
if (plainObject.updatedAt instanceof Date) {
389+
plainObject.updatedAt = plainObject.updatedAt.toISOString();
391390
}
392391
if (plainObject.expired_by instanceof Date) {
393392
plainObject.expired_by = plainObject.expired_by.toISOString();
@@ -411,8 +410,8 @@ describe('deleteSellerItem function', () => {
411410
stock_level: "Ongoing service",
412411
duration: 1,
413412
image: 'http://example.com/testSellerTwoItemTwo.jpg',
414-
created_at: '2025-01-10T00:00:00.000Z',
415-
updated_at: '2025-01-10T00:00:00.000Z',
413+
createdAt: '2025-01-10T00:00:00.000Z',
414+
updatedAt: '2025-01-10T00:00:00.000Z',
416415
expired_by: '2025-01-17T00:00:00.000Z'
417416
} as unknown as ISellerItem;
418417

@@ -431,8 +430,8 @@ describe('deleteSellerItem function', () => {
431430
stock_level: sellerItem.stock_level,
432431
duration: sellerItem.duration,
433432
image: sellerItem.image,
434-
created_at: sellerItem.created_at,
435-
updated_at: sellerItem.updated_at,
433+
createdAt: sellerItem.createdAt,
434+
updatedAt: sellerItem.updatedAt,
436435
expired_by: sellerItem.expired_by
437436
});
438437
});

0 commit comments

Comments
 (0)