Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Product details endpoint #157

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'],
});

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
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
Loading