Skip to content

Commit

Permalink
add vendors name and product category in best selling response
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyShimwa committed Jul 17, 2024
1 parent 734e842 commit 4646a55
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/controller/productController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ export const checkProductAvailability = async (req: Request, res: Response) => {
res.json({ availability, productId });
};

// Best seller API endpoints
export const getBestSellingProducts = async (req: Request, res: Response) => {
const orderDetailsRepository = dbConnection.getRepository(OrderDetails);

Expand All @@ -478,10 +477,12 @@ export const getBestSellingProducts = async (req: Request, res: Response) => {

const productIds = bestSellingProducts.map((item) => item.productId);

const products = await productRepository.findBy({
id: In(productIds),
});

const products = await productRepository
.createQueryBuilder('product')
.leftJoinAndSelect('product.vendor', 'vendor')
.leftJoinAndSelect('product.category', 'category')
.where('product.id IN (:...ids)', { ids: productIds })
.getMany();
if (!products || products.length === 0) {
return res
.status(404)
Expand All @@ -494,6 +495,11 @@ export const getBestSellingProducts = async (req: Request, res: Response) => {
);
return {
...product,
category: product.category.name,
vendor: {
firstName: product.vendor.firstName,
lastName: product.vendor.lastName,
},
sales: parseInt(productData.totalQuantity, 10),
};
});
Expand Down

0 comments on commit 4646a55

Please sign in to comment.