This commit is contained in:
@@ -19,6 +19,40 @@ class CategoryRepository extends BaseRepository<ICategory> {
|
|||||||
constructor() {
|
constructor() {
|
||||||
super(CategoryModel);
|
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) {
|
async findByTitle(title_en: string, id?: string) {
|
||||||
const query: Record<string, any> = { title_en, deleted: false };
|
const query: Record<string, any> = { title_en, deleted: false };
|
||||||
|
|
||||||
|
|||||||
@@ -16,16 +16,22 @@ export class ProductRepository extends BaseRepository<IProduct> {
|
|||||||
super(ProductModel);
|
super(ProductModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSellersProducts(shopIds: Types.ObjectId[], queryDto: SellerProductQueryDTO) {
|
async getSellersProducts(shopIds: Types.ObjectId[], queryDto: SellerProductQueryDTO & { categoryIds?: string[] }) {
|
||||||
const { limit, skip } = paginationUtils(queryDto);
|
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([
|
const products = await this.model.aggregate([
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
deleted: false,
|
deleted: false,
|
||||||
shop: { $in: shopIds },
|
shop: { $in: shopIds },
|
||||||
...(queryDto.categoryId && {
|
...(categoryIds && {
|
||||||
category: new Types.ObjectId(queryDto.categoryId),
|
category: { $in: categoryIds },
|
||||||
}),
|
}),
|
||||||
...(queryDto.status && {
|
...(queryDto.status && {
|
||||||
status: queryDto.status as ProductStatus,
|
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 { 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([
|
const products = await this.model.aggregate([
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
deleted: false,
|
deleted: false,
|
||||||
shop: new Types.ObjectId(shopId),
|
shop: new Types.ObjectId(shopId),
|
||||||
...(queries.categoryId && {
|
...(categoryIds && {
|
||||||
category: new Types.ObjectId(queries.categoryId),
|
category: { $in: categoryIds },
|
||||||
}),
|
}),
|
||||||
...(queries.status && {
|
...(queries.status && {
|
||||||
status: queries.status as ProductStatus,
|
status: queries.status as ProductStatus,
|
||||||
|
|||||||
@@ -853,7 +853,8 @@ class ProductService {
|
|||||||
async getAdminPanelProducts(ownerRef: OwnerRef, queryDto: SellerProductQueryDTO) {
|
async getAdminPanelProducts(ownerRef: OwnerRef, queryDto: SellerProductQueryDTO) {
|
||||||
const shops = await this.shopeRepo.model.find({ ownerRef }).select("_id");
|
const shops = await this.shopeRepo.model.find({ ownerRef }).select("_id");
|
||||||
const shopIds = shops.map((shop) => shop._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 brands = docs.map(({ brand }: any) => BrandDTO.transformBrand(brand));
|
||||||
const categories = docs.map(({ category }: any) => CategoryTreeDTO.transformCategory(category));
|
const categories = docs.map(({ category }: any) => CategoryTreeDTO.transformCategory(category));
|
||||||
const products = docs.map((doc: IProduct) => SellerPanelProductDTO.transformProduct(doc));
|
const products = docs.map((doc: IProduct) => SellerPanelProductDTO.transformProduct(doc));
|
||||||
@@ -870,7 +871,8 @@ class ProductService {
|
|||||||
const shop = await this.shopeRepo.model.findOne({ owner: ownerId });
|
const shop = await this.shopeRepo.model.findOne({ owner: ownerId });
|
||||||
if (!shop) throw new BadRequestError(ShopMessage.ShopNotFound);
|
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 brands = docs.map(({ brand }: any) => BrandDTO.transformBrand(brand));
|
||||||
const categories = category.map((cat: any) => CategoryTreeDTO.transformCategory(cat));
|
const categories = category.map((cat: any) => CategoryTreeDTO.transformCategory(cat));
|
||||||
|
|||||||
Reference in New Issue
Block a user