fix bug in category products
Build and Deploy / build_and_deploy (push) Has been cancelled

This commit is contained in:
2026-07-13 22:35:12 +03:30
parent 9a97e9b0c7
commit 67800c20ce
+32 -19
View File
@@ -1,4 +1,4 @@
import { Types } from "mongoose"; import { PipelineStage, Types } from "mongoose";
import { BaseRepository } from "../../../common/base/repository"; import { BaseRepository } from "../../../common/base/repository";
import { ProductMarketStatus, ProductStatus } from "../../../common/enums/product.enum"; import { ProductMarketStatus, ProductStatus } from "../../../common/enums/product.enum";
@@ -130,6 +130,11 @@ export class ProductRepository extends BaseRepository<IProduct> {
variantId: "$default_variant._id", variantId: "$default_variant._id",
}, },
}, },
{
$sort: {
createdAt: -1,
},
},
{ {
$facet: { $facet: {
data: [{ $skip: skip }, { $limit: limit }], data: [{ $skip: skip }, { $limit: limit }],
@@ -141,11 +146,6 @@ export class ProductRepository extends BaseRepository<IProduct> {
totalCount: { $arrayElemAt: ["$totalCount.count", 0] }, totalCount: { $arrayElemAt: ["$totalCount.count", 0] },
}, },
}, },
{
$sort: {
createdAt: -1,
},
},
]); ]);
return { return {
count: (products[0]?.totalCount as number) || 0, count: (products[0]?.totalCount as number) || 0,
@@ -226,6 +226,11 @@ export class ProductRepository extends BaseRepository<IProduct> {
popular: { $cond: [{ $gt: [{ $size: "$popularData" }, 0] }, true, false] }, popular: { $cond: [{ $gt: [{ $size: "$popularData" }, 0] }, true, false] },
}, },
}, },
{
$sort: {
createdAt: -1,
},
},
{ {
$facet: { $facet: {
data: [{ $skip: skip }, { $limit: limit }], data: [{ $skip: skip }, { $limit: limit }],
@@ -237,11 +242,6 @@ export class ProductRepository extends BaseRepository<IProduct> {
totalCount: { $arrayElemAt: ["$totalCount.count", 0] }, totalCount: { $arrayElemAt: ["$totalCount.count", 0] },
}, },
}, },
{
$sort: {
createdAt: -1,
},
},
]); ]);
return { return {
count: (products[0]?.totalCount as number) || 0, count: (products[0]?.totalCount as number) || 0,
@@ -361,6 +361,11 @@ export class ProductRepository extends BaseRepository<IProduct> {
variantId: "$default_variant._id", variantId: "$default_variant._id",
}, },
}, },
{
$sort: {
createdAt: -1,
},
},
{ {
$facet: { $facet: {
data: [{ $skip: skip }, { $limit: limit }], data: [{ $skip: skip }, { $limit: limit }],
@@ -380,11 +385,6 @@ export class ProductRepository extends BaseRepository<IProduct> {
totalCount: { $arrayElemAt: ["$totalCount.count", 0] }, totalCount: { $arrayElemAt: ["$totalCount.count", 0] },
}, },
}, },
{
$sort: {
createdAt: -1,
},
},
]); ]);
return { return {
count: (products?.[0]?.totalCount as number) || 0, count: (products?.[0]?.totalCount as number) || 0,
@@ -1625,6 +1625,7 @@ export class ProductRepository extends BaseRepository<IProduct> {
break; break;
} }
} }
const sortStage = { ...sortMatch, _id: 1 };
const pipeline = [ const pipeline = [
{ {
$match: productMatch, $match: productMatch,
@@ -1762,11 +1763,23 @@ export class ProductRepository extends BaseRepository<IProduct> {
preserveNullAndEmptyArrays: true, preserveNullAndEmptyArrays: true,
}, },
}, },
{
$group: {
_id: {
productId: "$_id",
variantId: "$variants._id",
},
doc: { $first: "$$ROOT" },
},
},
{
$replaceRoot: { newRoot: "$doc" },
},
{ {
$group: { $group: {
_id: "$_id", _id: "$_id",
count: { $sum: 1 }, count: { $sum: 1 },
createdAt: { $first: "$createdAt" },
title_fa: { $first: "$title_fa" }, title_fa: { $first: "$title_fa" },
title_en: { $first: "$title_en" }, title_en: { $first: "$title_en" },
seoTitle: { $first: "$seoTitle" }, seoTitle: { $first: "$seoTitle" },
@@ -1819,7 +1832,7 @@ export class ProductRepository extends BaseRepository<IProduct> {
}, },
{ {
$facet: { $facet: {
data: [{ $skip: skip }, { $limit: limit }, { $sort: sortMatch }], data: [{ $sort: sortStage }, { $skip: skip }, { $limit: limit }],
totalCount: [{ $count: "count" }], totalCount: [{ $count: "count" }],
}, },
}, },
@@ -1830,7 +1843,7 @@ export class ProductRepository extends BaseRepository<IProduct> {
}, },
]; ];
const docs = await this.model.aggregate(pipeline); const docs = await this.model.aggregate(pipeline as PipelineStage[]);
return { return {
count: (docs[0]?.totalCount as number) || 0, count: (docs[0]?.totalCount as number) || 0,
docs: docs[0]?.data || [], docs: docs[0]?.data || [],