Skip to content

Commit 50dd944

Browse files
authored
Merge pull request #219 from map-of-pi/ft/online-shopping
Approved (2).
2 parents c7ac790 + 65fa9fb commit 50dd944

17 files changed

+1323
-40
lines changed

.env.development

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ NODE_ENV=development
77
JWT_SECRET=MAP_OF_PI
88
PLATFORM_API_URL=https://api.minepi.com
99
PI_API_KEY="ADD YOUR PI API KEY"
10+
ADMIN_API_USERNAME="ADD YOUR ADMIN API USERNAME"
11+
ADMIN_API_PASSWORD="ADD YOUR ADMIN API PASSWORD"
1012

1113
MONGODB_URL=mongodb://localhost:27017/demoDB
1214
SENTRY_DSN="ADD YOUR SENTRY DSN"

src/config/docs/SellersItemSchema.yml

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
components:
2+
schemas:
3+
GetSellerItemRs:
4+
type: object
5+
properties:
6+
seller_id:
7+
type: string
8+
example: 0d367ba3-a2e8-4380-86c3-ab7c0b7890c0
9+
name:
10+
type: string
11+
example: Test Item
12+
description:
13+
type: string
14+
example: This is a sample seller item description.
15+
price:
16+
type: number
17+
format: double
18+
example: 0.01
19+
stock_level:
20+
$ref: '/api/docs/enum/StockLevelType.yml#/components/schemas/StockLevelType'
21+
image:
22+
type: string
23+
format: binary
24+
example: https://example.com/image.jpg
25+
duration:
26+
type: number
27+
example: 1
28+
created_at:
29+
type: string
30+
format: date-time
31+
example: 2024-12-21T00:00:00.000Z
32+
updated_at:
33+
type: string
34+
format: date-time
35+
example: 2024-12-21T00:00:00.000Z
36+
expired_by:
37+
type: string
38+
format: date-time
39+
example: 2024-12-28T00:00:00.000Z
40+
_id:
41+
type: string
42+
example: 66741c62b175e7d059a2639e
43+
__v:
44+
type: number
45+
example: 0
46+
47+
AddSellerItemRq:
48+
type: object
49+
properties:
50+
_id:
51+
type: string
52+
example: 66741c62b175e7d059a2639e
53+
seller_id:
54+
type: string
55+
example: 0d367ba3-a2e8-4380-86c3-ab7c0b7890c0
56+
name:
57+
type: string
58+
example: Test Item
59+
description:
60+
type: string
61+
example: This is a sample seller item description.
62+
price:
63+
type: number
64+
format: double
65+
example: 0.01
66+
stock_level:
67+
$ref: '/api/docs/enum/StockLevelType.yml#/components/schemas/StockLevelType'
68+
image:
69+
type: string
70+
format: binary
71+
example: https://example.com/image.jpg
72+
duration:
73+
type: number
74+
example: 1
75+
76+
AddSellerItemRs:
77+
type: object
78+
properties:
79+
seller_id:
80+
type: string
81+
example: 0d367ba3-a2e8-4380-86c3-ab7c0b7890c0
82+
name:
83+
type: string
84+
example: Test Item
85+
description:
86+
type: string
87+
example: This is a sample seller item description.
88+
price:
89+
type: number
90+
format: double
91+
example: 0.01
92+
stock_level:
93+
$ref: '/api/docs/enum/StockLevelType.yml#/components/schemas/StockLevelType'
94+
image:
95+
type: string
96+
format: binary
97+
example: https://example.com/image.jpg
98+
duration:
99+
type: number
100+
example: 1
101+
created_at:
102+
type: string
103+
format: date-time
104+
example: 2024-12-21T00:00:00.000Z
105+
updated_at:
106+
type: string
107+
format: date-time
108+
example: 2024-12-21T00:00:00.000Z
109+
expired_by:
110+
type: string
111+
format: date-time
112+
example: 2024-12-28T00:00:00.000Z
113+
_id:
114+
type: string
115+
example: 66741c62b175e7d059a2639e
116+
__v:
117+
type: number
118+
example: 0
119+
120+
DeleteSellerItemRs:
121+
type: object
122+
properties:
123+
message:
124+
type: string
125+
example: Seller item deleted successfully.
126+
deletedSellerItem:
127+
type: object
128+
properties:
129+
seller_id:
130+
type: string
131+
example: 0d367ba3-a2e8-4380-86c3-ab7c0b7890c0
132+
name:
133+
type: string
134+
example: Test Item
135+
description:
136+
type: string
137+
example: This is a sample seller item description.
138+
price:
139+
type: number
140+
format: double
141+
example: 0.01
142+
stock_level:
143+
$ref: '/api/docs/enum/StockLevelType.yml#/components/schemas/StockLevelType'
144+
image:
145+
type: string
146+
format: binary
147+
example: https://example.com/image.jpg
148+
duration:
149+
type: number
150+
example: 1
151+
created_at:
152+
type: string
153+
format: date-time
154+
example: 2024-12-21T00:00:00.000Z
155+
updated_at:
156+
type: string
157+
format: date-time
158+
example: 2024-12-21T00:00:00.000Z
159+
expired_by:
160+
type: string
161+
format: date-time
162+
example: 2024-12-28T00:00:00.000Z
163+
_id:
164+
type: string
165+
example: 66741c62b175e7d059a2639e
166+
__v:
167+
type: number
168+
example: 0

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.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
components:
2+
schemas:
3+
StockLevelType:
4+
type: string
5+
enum:
6+
- 1 available
7+
- 2 available
8+
- 3 available
9+
- Many available
10+
- Made to order
11+
- Ongoing service
12+
- Sold
13+
description: The type of stock level.

src/controllers/sellerController.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Request, Response } from "express";
22
import * as sellerService from "../services/seller.service";
33
import { uploadImage } from "../services/misc/image.service";
44
import * as userSettingsService from '../services/userSettings.service';
5+
import { ISeller } from "../types";
56
import logger from "../config/loggingConfig";
67

78
export const fetchSellersByCriteria = async (req: Request, res: Response) => {
@@ -71,7 +72,7 @@ export const registerSeller = async (req: Request, res: Response) => {
7172
formData.image = image;
7273

7374
// Register or update seller
74-
const registeredSeller = await sellerService.registerOrUpdateSeller(authUser, formData, image);
75+
const registeredSeller = await sellerService.registerOrUpdateSeller(authUser, formData);
7576
logger.info(`Registered or updated seller for user ${authUser.pi_uid}`);
7677

7778
// Update UserSettings with email and phone_number
@@ -103,3 +104,63 @@ export const deleteSeller = async (req: Request, res: Response) => {
103104
return res.status(500).json({ message: 'An error occurred while deleting seller; please try again later' });
104105
}
105106
};
107+
108+
export const getSellerItems = async (req: Request, res: Response) => {
109+
const { seller_id } = req.params
110+
try {
111+
const items = await sellerService.getAllSellerItems(seller_id);
112+
113+
if (!items || items.length === 0) {
114+
logger.warn(`No items are found for seller: ${seller_id}`);
115+
return res.status(204).json({ message: 'Seller items not found' });
116+
}
117+
logger.info(`Fetched ${items.length} items for seller: ${seller_id}`);
118+
return res.status(200).json(items);
119+
} catch (error) {
120+
logger.error('Failed to fetch seller items:', error);
121+
return res.status(500).json({ message: 'An error occurred while fetching seller Items; please try again later' });
122+
}
123+
};
124+
125+
export const addOrUpdateSellerItem = async (req: Request, res: Response) => {
126+
const currentSeller = req.currentSeller as ISeller;
127+
128+
const formData = req.body;
129+
logger.debug('Received formData for seller item:', { formData });
130+
131+
try {
132+
// Image file handling
133+
const file = req.file;
134+
const image = file ? await uploadImage(currentSeller.seller_id, file, 'seller-item') : '';
135+
formData.image = image;
136+
137+
logger.debug('Form data being sent:', { formData });
138+
// Add or update Item
139+
const sellerItem = await sellerService.addOrUpdateSellerItem(currentSeller, formData);
140+
logger.info(`Added/ updated seller item for seller ${currentSeller.seller_id}`);
141+
142+
// Send response
143+
return res.status(200).json({
144+
sellerItem: sellerItem,
145+
});
146+
} catch (error) {
147+
logger.error(`Failed to add or update seller item for userID ${currentSeller.seller_id}:`, error);
148+
return res.status(500).json({
149+
message: 'An error occurred while adding/ updating seller item; please try again later',
150+
});
151+
}
152+
};
153+
154+
export const deleteSellerItem = async (req: Request, res: Response) => {
155+
try {
156+
const currentSeller = req.currentSeller as ISeller;
157+
158+
const { item_id } = req.params;
159+
const deletedSellerItem = await sellerService.deleteSellerItem(item_id);
160+
logger.info(`Deleted seller item with ID ${currentSeller.seller_id}`);
161+
res.status(200).json({ message: "Seller item deleted successfully", deletedSellerItem: deletedSellerItem });
162+
} catch (error) {
163+
logger.error(`Failed to delete seller item for userID ${ req.currentUser?.pi_uid }:`, error);
164+
return res.status(500).json({ message: 'An error occurred while deleting seller item; please try again later' });
165+
}
166+
};

src/models/Seller.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import mongoose, { Schema, Types } from "mongoose";
22

33
import { ISeller } from "../types";
4-
import { SellerType, VisibleSellerType } from "./enums/sellerType";
4+
import { SellerType, VisibleSellerType, FulfillmentType } from "./enums/sellerType";
55

66
const sellerSchema = new Schema<ISeller>(
77
{
@@ -53,6 +53,16 @@
5353
order_online_enabled_pref: {
5454
type: Boolean,
5555
required: false,
56+
},
57+
fulfillment_method: {
58+
type: String,
59+
enum: Object.values(FulfillmentType).filter(value => typeof value === 'string'),
60+
default: FulfillmentType.CollectionByBuyer
61+
},
62+
fulfillment_description: {
63+
type: String,
64+
default: null,
65+
required: false
5666
}
5767
},
5868
{ timestamps: true } // Adds timestamps to track creation and update times

0 commit comments

Comments
 (0)