Skip to content

Commit

Permalink
fix(product-details): Fix issues in product details endpoint
Browse files Browse the repository at this point in the history
- return total quantity sold and similar products on product details endpoint

[Fixes #154]
  • Loading branch information
jkarenzi committed Jul 20, 2024
1 parent 6121d7f commit 60dfe4c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/__test__/review.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

})

Expand Down
42 changes: 40 additions & 2 deletions src/controller/buyerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,54 @@ export const getOneProduct = errorHandler(

const product = await productRepository.findOne({
where: { id: productId },
relations: ['category'],
relations: ['category','reviews','reviews.user','vendor'],
});

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} });
}
);

Expand Down
4 changes: 3 additions & 1 deletion src/controller/cartController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export const getCartItems = errorHandler(

const cartItems = await cartRepository.find({
where: {
user: user!,
user: {
id: userId
},
},
select: {
user: {
Expand Down
2 changes: 1 addition & 1 deletion src/controller/reviewController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions src/routes/buyerRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 60dfe4c

Please sign in to comment.