This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Types } from "mongoose";
|
||||
import { PipelineStage, Types } from "mongoose";
|
||||
|
||||
import { BaseRepository } from "../../../common/base/repository";
|
||||
import { ProductMarketStatus, ProductStatus } from "../../../common/enums/product.enum";
|
||||
@@ -130,6 +130,11 @@ export class ProductRepository extends BaseRepository<IProduct> {
|
||||
variantId: "$default_variant._id",
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
createdAt: -1,
|
||||
},
|
||||
},
|
||||
{
|
||||
$facet: {
|
||||
data: [{ $skip: skip }, { $limit: limit }],
|
||||
@@ -141,11 +146,6 @@ export class ProductRepository extends BaseRepository<IProduct> {
|
||||
totalCount: { $arrayElemAt: ["$totalCount.count", 0] },
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
createdAt: -1,
|
||||
},
|
||||
},
|
||||
]);
|
||||
return {
|
||||
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] },
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
createdAt: -1,
|
||||
},
|
||||
},
|
||||
{
|
||||
$facet: {
|
||||
data: [{ $skip: skip }, { $limit: limit }],
|
||||
@@ -237,11 +242,6 @@ export class ProductRepository extends BaseRepository<IProduct> {
|
||||
totalCount: { $arrayElemAt: ["$totalCount.count", 0] },
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
createdAt: -1,
|
||||
},
|
||||
},
|
||||
]);
|
||||
return {
|
||||
count: (products[0]?.totalCount as number) || 0,
|
||||
@@ -361,6 +361,11 @@ export class ProductRepository extends BaseRepository<IProduct> {
|
||||
variantId: "$default_variant._id",
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
createdAt: -1,
|
||||
},
|
||||
},
|
||||
{
|
||||
$facet: {
|
||||
data: [{ $skip: skip }, { $limit: limit }],
|
||||
@@ -380,11 +385,6 @@ export class ProductRepository extends BaseRepository<IProduct> {
|
||||
totalCount: { $arrayElemAt: ["$totalCount.count", 0] },
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
createdAt: -1,
|
||||
},
|
||||
},
|
||||
]);
|
||||
return {
|
||||
count: (products?.[0]?.totalCount as number) || 0,
|
||||
@@ -1625,6 +1625,7 @@ export class ProductRepository extends BaseRepository<IProduct> {
|
||||
break;
|
||||
}
|
||||
}
|
||||
const sortStage = { ...sortMatch, _id: 1 };
|
||||
const pipeline = [
|
||||
{
|
||||
$match: productMatch,
|
||||
@@ -1762,11 +1763,23 @@ export class ProductRepository extends BaseRepository<IProduct> {
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
$group: {
|
||||
_id: {
|
||||
productId: "$_id",
|
||||
variantId: "$variants._id",
|
||||
},
|
||||
doc: { $first: "$$ROOT" },
|
||||
},
|
||||
},
|
||||
{
|
||||
$replaceRoot: { newRoot: "$doc" },
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: "$_id",
|
||||
count: { $sum: 1 },
|
||||
createdAt: { $first: "$createdAt" },
|
||||
title_fa: { $first: "$title_fa" },
|
||||
title_en: { $first: "$title_en" },
|
||||
seoTitle: { $first: "$seoTitle" },
|
||||
@@ -1819,7 +1832,7 @@ export class ProductRepository extends BaseRepository<IProduct> {
|
||||
},
|
||||
{
|
||||
$facet: {
|
||||
data: [{ $skip: skip }, { $limit: limit }, { $sort: sortMatch }],
|
||||
data: [{ $sort: sortStage }, { $skip: skip }, { $limit: limit }],
|
||||
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 {
|
||||
count: (docs[0]?.totalCount as number) || 0,
|
||||
docs: docs[0]?.data || [],
|
||||
|
||||
Reference in New Issue
Block a user