Skip to content

Commit 26eecc7

Browse files
committed
Add corresponding unit test to service layer; misc adjustments.
1 parent ed3c22e commit 26eecc7

File tree

7 files changed

+161
-6
lines changed

7 files changed

+161
-6
lines changed

src/config/docs/SellersSchema.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ components:
6767
required:
6868
- type
6969
- coordinates
70+
fulfillment_type:
71+
$ref: '/api/docs/enum/FulfillmentType.yml#/components/schemas/FulfillmentType'
72+
fulfillment_description:
73+
type: string
74+
example: This is a sample fulfillment description.
7075
order_online_enabled_pref:
7176
type: boolean
7277
example: true
@@ -122,6 +127,11 @@ components:
122127
required:
123128
- type
124129
- coordinates
130+
fulfillment_type:
131+
$ref: '/api/docs/enum/FulfillmentType.yml#/components/schemas/FulfillmentType'
132+
fulfillment_description:
133+
type: string
134+
example: This is a sample fulfillment description.
125135
order_online_enabled_pref:
126136
type: boolean
127137
example: true
@@ -192,6 +202,11 @@ components:
192202
required:
193203
- type
194204
- coordinates
205+
fulfillment_type:
206+
$ref: '/api/docs/enum/FulfillmentType.yml#/components/schemas/FulfillmentType'
207+
fulfillment_description:
208+
type: string
209+
example: This is a sample fulfillment description.
195210
order_online_enabled_pref:
196211
type: boolean
197212
example: true
@@ -242,6 +257,11 @@ components:
242257
required:
243258
- type
244259
- coordinates
260+
fulfillment_type:
261+
$ref: '/api/docs/enum/FulfillmentType.yml#/components/schemas/FulfillmentType'
262+
fulfillment_description:
263+
type: string
264+
example: This is a sample fulfillment description.
245265
order_online_enabled_pref:
246266
type: boolean
247267
example: true
@@ -294,6 +314,11 @@ components:
294314
required:
295315
- type
296316
- coordinates
317+
fulfillment_type:
318+
$ref: '/api/docs/enum/FulfillmentType.yml#/components/schemas/FulfillmentType'
319+
fulfillment_description:
320+
type: string
321+
example: This is a sample fulfillment description.
297322
order_online_enabled_pref:
298323
type: boolean
299324
example: true
@@ -347,6 +372,11 @@ components:
347372
required:
348373
- type
349374
- coordinates
375+
fulfillment_type:
376+
$ref: '/api/docs/enum/FulfillmentType.yml#/components/schemas/FulfillmentType'
377+
fulfillment_description:
378+
type: string
379+
example: This is a sample fulfillment description.
350380
order_online_enabled_pref:
351381
type: boolean
352382
example: true
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
components:
2+
schemas:
3+
FulfillmentType:
4+
type: string
5+
enum:
6+
- Collection by buyer
7+
- Delivered to buyer
8+
description: The type of fulfillment method.

src/models/enums/sellerType.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ export const SellerType = Object.assign({}, VisibleSellerType, InvisibleSellerTy
1111
export type SellerType = VisibleSellerType | InvisibleSellerType;
1212

1313
export enum FulfillmentType {
14-
CollectionByBuyer = 'Collection by Buyer',
15-
DeliveredToBuyer = 'Delivered to Buyer'
14+
CollectionByBuyer = 'Collection by buyer',
15+
DeliveredToBuyer = 'Delivered to buyer'
1616
}

src/services/seller.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export const registerOrUpdateSeller = async (authUser: IUser, formData: any): Pr
159159
image: formData.image || existingSeller?.image || '',
160160
address: formData.address || existingSeller?.address || '',
161161
sell_map_center: sellMapCenter,
162-
order_online_enabled_pref: formData.order_online_enabled_pref || existingSeller?.order_online_enabled_pref || '',
162+
order_online_enabled_pref: formData.order_online_enabled_pref || existingSeller?.order_online_enabled_pref || false,
163163
fulfillment_method: formData.fulfillment_method || existingSeller?.fulfillment_method || FulfillmentType.CollectionByBuyer,
164164
fulfillment_description: formData.fulfillment_description || existingSeller?.fulfillment_description || ''
165165
};

test/controllers/sellerController.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe('sellerController', () => {
112112
coordinates: [-87.6298, 41.8781]
113113
},
114114
order_online_enabled_pref: false,
115-
fulfillment_method: "Collection by Buyer"
115+
fulfillment_method: "Collection by buyer"
116116
}
117117
};
118118
res = {

test/mockData.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
"pi_uid": "0e0e0e-0e0e-0e0e",
2525
"pi_username": "TestUser5",
2626
"user_name": "test-user-5"
27+
},
28+
{
29+
"pi_uid": "0m0m0m-0m0m-0m0m",
30+
"pi_username": "TestUser13",
31+
"user_name": "test-user-13"
2732
}
2833
],
2934
"userSettings": [
@@ -127,7 +132,9 @@
127132
"sell_map_center": {
128133
"type": "Point",
129134
"coordinates": [-95.3698, 29.7604]
130-
}
135+
},
136+
"fulfillment_method": "Collection by buyer",
137+
"fulfillment_description": "Test Vendor 5 Fulfillment Description"
131138
},
132139
{
133140
"seller_id": "0f0f0f-0f0f-0f0f",

test/services/seller.service.spec.ts

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import Seller from '../../src/models/Seller';
22
import SellerItem from '../../src/models/SellerItem';
33
import {
44
getAllSellers,
5+
registerOrUpdateSeller,
56
getAllSellerItems,
67
addOrUpdateSellerItem,
78
deleteSellerItem,
89
getSellersWithinSanctionedRegion
910
} from '../../src/services/seller.service';
11+
import User from '../../src/models/User';
1012
import { SellerType } from '../../src/models/enums/sellerType';
1113
import { RestrictedArea, RestrictedAreaBoundaries } from '../../src/models/enums/restrictedArea';
12-
import { ISanctionedRegion, ISeller, ISellerItem } from '../../src/types';
14+
import { IUser, ISeller, ISellerItem, ISanctionedRegion } from '../../src/types';
1315

1416
describe('getAllSellers function', () => {
1517
const mockBoundingBox = {
@@ -93,6 +95,114 @@ describe('getAllSellers function', () => {
9395
});
9496
});
9597

98+
describe('registerOrUpdateSeller function', () => {
99+
// Helper function to convert Mongoose document to a plain object and normalize values accordingly
100+
const convertToPlainObject = (seller: ISeller): any => {
101+
const plainObject = seller.toObject();
102+
103+
if (plainObject.sell_map_center) {
104+
plainObject.sell_map_center = JSON.stringify(plainObject.sell_map_center);
105+
}
106+
107+
if (plainObject.average_rating) {
108+
plainObject.average_rating = plainObject.average_rating.toString();
109+
}
110+
111+
return plainObject;
112+
};
113+
114+
const assertSeller = (actual: any, expected: any) => {
115+
const { _id, __v, createdAt, updatedAt, order_online_enabled_pref, ...filteredActual } = actual; // ignore DB values.
116+
expect(filteredActual).toEqual(expect.objectContaining(expected));
117+
};
118+
119+
it('should add new seller if the seller does not exist', async () => {
120+
const userData = await User.findOne({ pi_username: 'TestUser13' }) as IUser;
121+
122+
const formData = {
123+
seller_id: "0m0m0m-0m0m-0m0m",
124+
name: 'Test Seller 13',
125+
description: "Test Seller 13 Description",
126+
address: "Test Seller 13 Address",
127+
image: "http://example.com/testThirteen.jpg",
128+
seller_type: "activeSeller",
129+
sell_map_center: JSON.stringify({
130+
type: "Point",
131+
coordinates: [24.1234, 24.1234]
132+
}),
133+
average_rating: "5",
134+
fulfillment_method: "Delivered to buyer",
135+
fulfillment_description: "Test Seller 13 Fulfillment Description"
136+
} as unknown as ISeller;
137+
138+
const sellerData = (await registerOrUpdateSeller(userData, formData)) as ISeller;
139+
140+
// Convert `sellerData` to a plain object if it's a Mongoose document
141+
const plainObject = await convertToPlainObject(sellerData);
142+
143+
assertSeller(plainObject, {
144+
seller_id: formData.seller_id,
145+
name: formData.name,
146+
description: formData.description,
147+
address: formData.address,
148+
image: formData.image,
149+
seller_type: formData.seller_type,
150+
sell_map_center: formData.sell_map_center,
151+
average_rating: formData.average_rating,
152+
fulfillment_method: formData.fulfillment_method,
153+
fulfillment_description: formData.fulfillment_description
154+
});
155+
});
156+
157+
it('should update existing seller if the seller does exist', async () => {
158+
const userData = await User.findOne({ pi_username: 'TestUser3' }) as IUser;
159+
160+
const formData = {
161+
seller_id: "0c0c0c-0c0c-0c0c",
162+
name: 'Test Vendor 3 Updated',
163+
description: "Test Vendor 3 Description Updated",
164+
address: "Test Vendor 3 Address Updated",
165+
fulfillment_method: "Delivered to buyer",
166+
fulfillment_description: "Test Vendor 3 Fulfillment Description"
167+
} as unknown as ISeller;
168+
169+
const sellerData = (await registerOrUpdateSeller(userData, formData)) as ISeller;
170+
171+
// Convert `sellerData` to a plain object if it's a Mongoose document
172+
const plainObject = await convertToPlainObject(sellerData);
173+
174+
assertSeller(plainObject, {
175+
seller_id: formData.seller_id,
176+
name: formData.name,
177+
description: formData.description,
178+
fulfillment_method: formData.fulfillment_method,
179+
fulfillment_description: formData.fulfillment_description
180+
});
181+
});
182+
183+
it('should throw an error when an exception occurs', async () => {
184+
const userData = await User.findOne({ pi_username: 'TestUser3' }) as IUser;
185+
186+
const formData = {
187+
seller_id: "0c0c0c-0c0c-0c0c",
188+
name: 'Test Vendor 3 Updated',
189+
description: "Test Vendor 3 Description Updated",
190+
address: "Test Vendor 3 Address Updated",
191+
fulfillment_method: "Delivered to buyer",
192+
fulfillment_description: "Test Vendor 3 Fulfillment Description"
193+
} as unknown as ISeller;
194+
195+
// Mock the Seller model to throw an error
196+
jest.spyOn(Seller, 'findOne').mockImplementationOnce(() => {
197+
throw new Error('Mock database error');
198+
});
199+
200+
await expect(registerOrUpdateSeller(userData, formData)).rejects.toThrow(
201+
'Failed to register or update seller; please try again later'
202+
);
203+
});
204+
});
205+
96206
describe('getAllSellerItems function', () => {
97207
it('should return all existing seller items associated with the seller', async () => {
98208
const sellerItemsData = await getAllSellerItems('0a0a0a-0a0a-0a0a');

0 commit comments

Comments
 (0)