Skip to content

Commit

Permalink
add vendors name and product category in best selling response (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyShimwa authored Jul 18, 2024
1 parent 734e842 commit 2a1f852
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/controller/productController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Request, Response } from 'express';
import { In } from 'typeorm';
import Product from '../database/models/productEntity';
import Category from '../database/models/categoryEntity';
import { OrderDetails } from '../database/models/orderDetailsEntity';
Expand Down Expand Up @@ -458,7 +457,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 +476,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 +494,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 2a1f852

Please sign in to comment.