From 530ab92080247ca08834e7d4a7a79eb3986fe162 Mon Sep 17 00:00:00 2001 From: jkarenzi Date: Fri, 19 Jul 2024 15:34:21 +0200 Subject: [PATCH] fix(product-details): Fix issues in product details endpoint - return total quantity sold and similar products on product details endpoint [Fixes #154] --- src/__test__/review.test.ts | 2 +- src/controller/buyerController.ts | 42 ++++++++++++++++++++++++++++-- src/controller/reviewController.ts | 2 +- src/routes/buyerRoutes.ts | 3 +-- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/__test__/review.test.ts b/src/__test__/review.test.ts index 9f2c081..6ed6c71 100644 --- a/src/__test__/review.test.ts +++ b/src/__test__/review.test.ts @@ -89,7 +89,7 @@ describe('Review controller test', () => { .set('Authorization', `Bearer ${buyerToken}`) .send(reviewBody) expect(responseReview.statusCode).toEqual(409); - expect(responseReview.body.message).toEqual('you are already reviewed the product'); + expect(responseReview.body.message).toEqual('you have already reviewed the product'); }) diff --git a/src/controller/buyerController.ts b/src/controller/buyerController.ts index 459450e..9a140f8 100644 --- a/src/controller/buyerController.ts +++ b/src/controller/buyerController.ts @@ -18,16 +18,54 @@ export const getOneProduct = errorHandler( const product = await productRepository.findOne({ where: { id: productId }, - relations: ['category'], + relations: ['category','reviews','reviews.user'], }); if (!product) { return res.status(404).json({ msg: 'Product not found' }); } + const similarProducts = await productRepository.find({ + where:{ + category:{ + id: product.category.id + } + }, + take: 4 + }) + + const orders = await orderRepository.find({ + where: { + paid: true, + orderDetails: { + product: { + id: productId + } + } + }, + select: { + orderDetails: { + product: { + id: true + }, + quantity: true + } + }, + relations: ['orderDetails', 'orderDetails.product'] + }); + + let totalQtySold = 0 + + for(const order of orders){ + for(const orderDetail of order.orderDetails){ + totalQtySold += orderDetail.quantity + } + } + + return res .status(200) - .json({ msg: 'Product retrieved successfully', product }); + .json({ msg: 'Product retrieved successfully', product:{...product, similarProducts, totalQtySold} }); } ); diff --git a/src/controller/reviewController.ts b/src/controller/reviewController.ts index 8e07a23..dff25d7 100644 --- a/src/controller/reviewController.ts +++ b/src/controller/reviewController.ts @@ -54,7 +54,7 @@ export const createReview = errorHandler(async (req: Request, res: Response) => }) if(existingReview){ - return res.status(409).json({ message: 'you are already reviewed the product' }); + return res.status(409).json({ message: 'you have already reviewed the product' }); } const newReview = new Review(); diff --git a/src/routes/buyerRoutes.ts b/src/routes/buyerRoutes.ts index cd2255b..13e7a1f 100644 --- a/src/routes/buyerRoutes.ts +++ b/src/routes/buyerRoutes.ts @@ -16,10 +16,9 @@ import { handlePayment } from '../controller/buyerController'; const buyerRouter = Router(); -buyerRouter.use(IsLoggedIn, checkRole(['Buyer'])); - buyerRouter.get('/get_product/:id', getOneProduct); +buyerRouter.use(IsLoggedIn, checkRole(['Buyer'])); buyerRouter.post('/addItemToWishList', IsLoggedIn, AddItemInWishList); buyerRouter.delete('/removeToWishList', IsLoggedIn, RemoveProductFromWishList); buyerRouter.get('/getWishList', IsLoggedIn, getAllWishList);