This commit is contained in:
@@ -19,6 +19,40 @@ class CategoryRepository extends BaseRepository<ICategory> {
|
||||
constructor() {
|
||||
super(CategoryModel);
|
||||
}
|
||||
|
||||
async getDescendantCategoryIds(rootCategoryId: string): Promise<string[]> {
|
||||
const docs = await this.model.aggregate<{ _id: Types.ObjectId; descendants: { _id: Types.ObjectId }[] }>([
|
||||
{
|
||||
$match: {
|
||||
_id: new ObjectId(rootCategoryId),
|
||||
deleted: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
$graphLookup: {
|
||||
from: "categories",
|
||||
startWith: "$_id",
|
||||
connectFromField: "_id",
|
||||
connectToField: "parent",
|
||||
as: "descendants",
|
||||
restrictSearchWithMatch: { deleted: false },
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 1,
|
||||
descendants: { _id: 1 },
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
if (!docs[0]) return [];
|
||||
|
||||
const ids = new Set<string>([docs[0]._id.toString()]);
|
||||
for (const child of docs[0].descendants || []) ids.add(child._id.toString());
|
||||
return Array.from(ids);
|
||||
}
|
||||
|
||||
async findByTitle(title_en: string, id?: string) {
|
||||
const query: Record<string, any> = { title_en, deleted: false };
|
||||
|
||||
|
||||
@@ -16,16 +16,22 @@ export class ProductRepository extends BaseRepository<IProduct> {
|
||||
super(ProductModel);
|
||||
}
|
||||
|
||||
async getSellersProducts(shopIds: Types.ObjectId[], queryDto: SellerProductQueryDTO) {
|
||||
async getSellersProducts(shopIds: Types.ObjectId[], queryDto: SellerProductQueryDTO & { categoryIds?: string[] }) {
|
||||
const { limit, skip } = paginationUtils(queryDto);
|
||||
|
||||
const categoryIds = queryDto.categoryIds?.length
|
||||
? queryDto.categoryIds.map((id) => new Types.ObjectId(id))
|
||||
: queryDto.categoryId
|
||||
? [new Types.ObjectId(queryDto.categoryId)]
|
||||
: null;
|
||||
|
||||
const products = await this.model.aggregate([
|
||||
{
|
||||
$match: {
|
||||
deleted: false,
|
||||
shop: { $in: shopIds },
|
||||
...(queryDto.categoryId && {
|
||||
category: new Types.ObjectId(queryDto.categoryId),
|
||||
...(categoryIds && {
|
||||
category: { $in: categoryIds },
|
||||
}),
|
||||
...(queryDto.status && {
|
||||
status: queryDto.status as ProductStatus,
|
||||
@@ -249,16 +255,22 @@ export class ProductRepository extends BaseRepository<IProduct> {
|
||||
};
|
||||
}
|
||||
|
||||
async getSellerProducts(shopId: string, queries: SellerProductQueryDTO) {
|
||||
async getSellerProducts(shopId: string, queries: SellerProductQueryDTO & { categoryIds?: string[] }) {
|
||||
const { skip, limit } = paginationUtils(queries);
|
||||
|
||||
const categoryIds = queries.categoryIds?.length
|
||||
? queries.categoryIds.map((id) => new Types.ObjectId(id))
|
||||
: queries.categoryId
|
||||
? [new Types.ObjectId(queries.categoryId)]
|
||||
: null;
|
||||
|
||||
const products = await this.model.aggregate([
|
||||
{
|
||||
$match: {
|
||||
deleted: false,
|
||||
shop: new Types.ObjectId(shopId),
|
||||
...(queries.categoryId && {
|
||||
category: new Types.ObjectId(queries.categoryId),
|
||||
...(categoryIds && {
|
||||
category: { $in: categoryIds },
|
||||
}),
|
||||
...(queries.status && {
|
||||
status: queries.status as ProductStatus,
|
||||
|
||||
@@ -853,7 +853,8 @@ class ProductService {
|
||||
async getAdminPanelProducts(ownerRef: OwnerRef, queryDto: SellerProductQueryDTO) {
|
||||
const shops = await this.shopeRepo.model.find({ ownerRef }).select("_id");
|
||||
const shopIds = shops.map((shop) => shop._id);
|
||||
const { docs, count } = await this.productRepo.getSellersProducts(shopIds, queryDto);
|
||||
const categoryIds = queryDto.categoryId ? await this.categoryRepo.getDescendantCategoryIds(queryDto.categoryId) : undefined;
|
||||
const { docs, count } = await this.productRepo.getSellersProducts(shopIds, { ...queryDto, categoryIds });
|
||||
const brands = docs.map(({ brand }: any) => BrandDTO.transformBrand(brand));
|
||||
const categories = docs.map(({ category }: any) => CategoryTreeDTO.transformCategory(category));
|
||||
const products = docs.map((doc: IProduct) => SellerPanelProductDTO.transformProduct(doc));
|
||||
@@ -870,7 +871,8 @@ class ProductService {
|
||||
const shop = await this.shopeRepo.model.findOne({ owner: ownerId });
|
||||
if (!shop) throw new BadRequestError(ShopMessage.ShopNotFound);
|
||||
|
||||
const { docs, count, category } = await this.productRepo.getSellerProducts(shop._id.toString(), queryDto);
|
||||
const categoryIds = queryDto.categoryId ? await this.categoryRepo.getDescendantCategoryIds(queryDto.categoryId) : undefined;
|
||||
const { docs, count, category } = await this.productRepo.getSellerProducts(shop._id.toString(), { ...queryDto, categoryIds });
|
||||
|
||||
const brands = docs.map(({ brand }: any) => BrandDTO.transformBrand(brand));
|
||||
const categories = category.map((cat: any) => CategoryTreeDTO.transformCategory(cat));
|
||||
|
||||
Reference in New Issue
Block a user