Skip to content

Commit 4d6ea27

Browse files
ft(relatedProducts):buyer should see related products
1 parent 23ef274 commit 4d6ea27

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/__test__/product.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { generateAccessToken } from "../helpers/security.helpers";
2020
import { read_function } from "../utils/db_methods";
2121
import { response } from "express";
2222
import searchProduct from "../controllers/searchProduct";
23+
import { Console } from "node:console";
2324

2425
jest.setTimeout(100000);
2526

@@ -35,6 +36,7 @@ function logErrors(
3536

3637
const Jest_request = request(app.use(logErrors));
3738
let seller_token: string;
39+
let buyer_token: string;
3840
let category_id: string;
3941
let product_id: any;
4042
describe("PRODUCT API TEST", () => {
@@ -188,7 +190,6 @@ describe("PRODUCT API TEST", () => {
188190
const { body } = await Jest_request.get(`/api/v1/products/${product_id}`)
189191
.set("Authorization", `Bearer ${seller_token}`)
190192
.expect(200);
191-
192193
expect(body.status).toStrictEqual("SUCCESS");
193194
expect(body.message).toStrictEqual("Product fetched successfully!");
194195
expect(body.data).toBeDefined();

src/controllers/productController.ts

+54
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { insert_function, read_function } from "../utils/db_methods";
1313
import { category_utils } from "../utils/controller";
1414
import { Info, Message } from "../types/upload";
1515
import { isAvailable } from "../utils/nodeEvents";
16+
import { Op, OrderItem } from "sequelize";
1617

1718
let product_id;
1819
const include = [
@@ -206,6 +207,7 @@ const read_single_product = async (req: Request, res: Response) => {
206207
"Product not found or not owned!",
207208
);
208209
}
210+
209211
return sendResponse(
210212
res,
211213
200,
@@ -225,6 +227,57 @@ const read_single_product = async (req: Request, res: Response) => {
225227
}
226228
};
227229

230+
const getRecommendedProducts = async (req: Request, res: Response) => {
231+
try {
232+
const productId = req.params.id;
233+
const product = await read_function<ProductAttributes>(
234+
"Product",
235+
"findOne",
236+
{ where: { id: productId }, include },
237+
);
238+
239+
if (!product) {
240+
return sendResponse(
241+
res,
242+
404,
243+
"NOT FOUND",
244+
"Product not found or not owned!",
245+
);
246+
}
247+
248+
const condition = {
249+
where: {
250+
categoryId: product.categoryId,
251+
isAvailable: true,
252+
id: { [Op.ne]: productId },
253+
},
254+
include,
255+
limit: 5,
256+
order: [["createdAt", "DESC"]] as OrderItem[],
257+
};
258+
const recommended = await read_function<ProductAttributes>(
259+
"Product",
260+
"findAll",
261+
condition,
262+
);
263+
return sendResponse(
264+
res,
265+
200,
266+
"SUCCESS",
267+
"Recommended Products fetched successfully!",
268+
recommended,
269+
);
270+
} catch (error: unknown) {
271+
return sendResponse(
272+
res,
273+
500,
274+
"SERVER ERROR",
275+
"Something went wrong!",
276+
error as Error,
277+
);
278+
}
279+
};
280+
228281
const update_product = async (req: Request, res: Response) => {
229282
try {
230283
product_id = category_utils(req, res).getId;
@@ -530,4 +583,5 @@ export default {
530583
delete_product,
531584
guest_read_all_products,
532585
guest_read_single_product,
586+
getRecommendedProducts,
533587
};

src/routes/productRoutes.ts

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ productRouter.get(
3636
productController.read_single_product,
3737
);
3838

39+
productRouter.get(
40+
"/:id/recommended",
41+
userAuthentication.authenticateUser,
42+
productController.getRecommendedProducts,
43+
);
44+
3945
productRouter.patch(
4046
"/:id",
4147
userAuthentication.isSeller,

0 commit comments

Comments
 (0)