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

Add vendors name and product category in best selling response #152

Merged
merged 1 commit into from
Jul 18, 2024
Merged
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
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
Loading