Skip to content

Commit 70dd3f9

Browse files
authored
Merge pull request #226 from map-of-pi/ft/online-shopping
Approved (1).
2 parents 6b59c7b + edee3e9 commit 70dd3f9

File tree

8 files changed

+351
-376
lines changed

8 files changed

+351
-376
lines changed

package-lock.json

Lines changed: 295 additions & 313 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
},
1515
"dependencies": {
1616
"@sentry/integrations": "^7.114.0",
17-
"@sentry/node": "^9.3.0",
18-
"@sentry/profiling-node": "^9.3.0",
17+
"@sentry/node": "^8.26.0",
18+
"@sentry/profiling-node": "^8.26.0",
1919
"axios": "^1.7.4",
2020
"body-parser": "^1.20.2",
2121
"bottleneck": "^2.19.5",
2222
"cloudinary": "^2.4.0",
2323
"cookie-parser": "^1.4.7",
2424
"cors": "^2.8.5",
2525
"dotenv": "^16.4.5",
26-
"express": "^4.21.1",
26+
"express": "^4.21.2",
2727
"jsonwebtoken": "^9.0.2",
2828
"mongoose": "^8.9.5",
2929
"multer": "^1.4.5-lts.1",

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: 7 additions & 10 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[]> => {
@@ -226,16 +227,15 @@ export const addOrUpdateSellerItem = async (
226227
seller: ISeller,
227228
item: ISellerItem
228229
): Promise<ISellerItem | null> => {
229-
230230
try {
231231
const today = new Date();
232232

233-
// Calculate expiration date based on duration (defaults to 1 week)
233+
// Ensure duration is valid (default to 1 week)
234234
const duration = Number(item.duration) || 1;
235235
const durationInMs = duration * 7 * 24 * 60 * 60 * 1000;
236236
const expiredBy = new Date(today.getTime() + durationInMs);
237237

238-
// Ensure unique identifier is used for finding existing items
238+
// Define a unique query for finding existing items
239239
const query = {
240240
_id: item._id || undefined,
241241
seller_id: seller.seller_id,
@@ -248,7 +248,6 @@ export const addOrUpdateSellerItem = async (
248248
// Update the existing item
249249
existingItem.set({
250250
...item,
251-
updated_at: today,
252251
expired_by: expiredBy,
253252
image: item.image || existingItem.image, // Use existing image if a new one isn't provided
254253
});
@@ -257,7 +256,7 @@ export const addOrUpdateSellerItem = async (
257256
logger.info('Item updated successfully:', { updatedItem });
258257
return updatedItem;
259258
} else {
260-
// Ensure item has a unique identifier for creation
259+
// Create a new item with a unique ID
261260
const newItemId = item._id || new mongoose.Types.ObjectId().toString();
262261

263262
// Create a new item
@@ -267,11 +266,9 @@ export const addOrUpdateSellerItem = async (
267266
name: item.name ? item.name.trim() : '',
268267
description: item.description ? item.description.trim() : '',
269268
price: parseFloat(item.price?.toString() || '0.01'), // Ensure valid price
270-
stock_level: item.stock_level || '1 available',
269+
stock_level: item.stock_level || StockLevelType.AVAILABLE_1,
271270
duration: parseInt(item.duration?.toString() || '1'), // Ensure valid duration
272271
image: item.image,
273-
created_at: today,
274-
updated_at: today,
275272
expired_by: expiredBy,
276273
});
277274

@@ -281,7 +278,7 @@ export const addOrUpdateSellerItem = async (
281278
return newItem;
282279
}
283280
} catch (error) {
284-
logger.error(`Failed to add or update seller item for sellerID ${ seller.seller_id }:`, error);
281+
logger.error(`Failed to add or update seller item for sellerID ${seller.seller_id}:`, error);
285282
throw new Error('Failed to add or update seller item; please try again later');
286283
}
287284
};

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 & 21 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

@@ -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+
createdAt: '2025-02-20T00:00:00.000Z'
278279
} as unknown as ISellerItem;
279280

280281
const sellerItemData = (await addOrUpdateSellerItem(
@@ -300,9 +301,9 @@ describe('addOrUpdateSellerItem function', () => {
300301
stock_level: sellerItem.stock_level,
301302
duration: sellerItem.duration,
302303
image: sellerItem.image,
303-
created_at: current_date,
304-
updated_at: current_date,
305-
expired_by: expired_date
304+
expired_by: expired_date,
305+
createdAt: current_date,
306+
updatedAt: current_date
306307
});
307308
});
308309

@@ -343,8 +344,8 @@ describe('addOrUpdateSellerItem function', () => {
343344
stock_level: sellerItem.stock_level,
344345
duration: sellerItem.duration,
345346
image: sellerItem.image,
346-
updated_at: current_date,
347-
expired_by: expired_date
347+
expired_by: expired_date,
348+
updatedAt: current_date
348349
});
349350
});
350351

@@ -381,11 +382,11 @@ describe('deleteSellerItem function', () => {
381382
}
382383

383384
// Normalize timestamps
384-
if (plainObject.created_at instanceof Date) {
385-
plainObject.created_at = plainObject.created_at.toISOString();
385+
if (plainObject.createdAt instanceof Date) {
386+
plainObject.createdAt = plainObject.createdAt.toISOString();
386387
}
387-
if (plainObject.updated_at instanceof Date) {
388-
plainObject.updated_at = plainObject.updated_at.toISOString();
388+
if (plainObject.updatedAt instanceof Date) {
389+
plainObject.updatedAt = plainObject.updatedAt.toISOString();
389390
}
390391
if (plainObject.expired_by instanceof Date) {
391392
plainObject.expired_by = plainObject.expired_by.toISOString();
@@ -409,8 +410,8 @@ describe('deleteSellerItem function', () => {
409410
stock_level: "Ongoing service",
410411
duration: 1,
411412
image: 'http://example.com/testSellerTwoItemTwo.jpg',
412-
created_at: '2025-01-10T00:00:00.000Z',
413-
updated_at: '2025-01-10T00:00:00.000Z',
413+
createdAt: '2025-01-10T00:00:00.000Z',
414+
updatedAt: '2025-01-10T00:00:00.000Z',
414415
expired_by: '2025-01-17T00:00:00.000Z'
415416
} as unknown as ISellerItem;
416417

@@ -429,8 +430,8 @@ describe('deleteSellerItem function', () => {
429430
stock_level: sellerItem.stock_level,
430431
duration: sellerItem.duration,
431432
image: sellerItem.image,
432-
created_at: sellerItem.created_at,
433-
updated_at: sellerItem.updated_at,
433+
createdAt: sellerItem.createdAt,
434+
updatedAt: sellerItem.updatedAt,
434435
expired_by: sellerItem.expired_by
435436
});
436437
});

0 commit comments

Comments
 (0)