@@ -14,7 +14,8 @@ export class ReturnOrderRepo extends BaseRepository<IReturnOrder> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getAllReturnsForAdmin(skip: number, limit: number) {
|
async getAllReturnsForAdmin(skip: number, limit: number) {
|
||||||
const docs = await this.model.aggregate([
|
const [result] = await this.model.aggregate([
|
||||||
|
// --- 1. Lookup order ---
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: "orders",
|
from: "orders",
|
||||||
@@ -23,9 +24,9 @@ export class ReturnOrderRepo extends BaseRepository<IReturnOrder> {
|
|||||||
as: "order",
|
as: "order",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{ $unwind: { path: "$order", preserveNullAndEmptyArrays: true } },
|
||||||
$unwind: "$order",
|
|
||||||
},
|
// --- 2. Lookup return order items ---
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: "returnorderitems",
|
from: "returnorderitems",
|
||||||
@@ -34,117 +35,79 @@ export class ReturnOrderRepo extends BaseRepository<IReturnOrder> {
|
|||||||
as: "returnOrderItems",
|
as: "returnOrderItems",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{ $unwind: { path: "$returnOrderItems", preserveNullAndEmptyArrays: true } },
|
||||||
$unwind: "$returnOrderItems",
|
|
||||||
},
|
// --- 3. Lookup order items ---
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: "orderitems",
|
from: "orderitems",
|
||||||
localField: "returnOrderItems.orderItem",
|
localField: "returnOrderItems.orderItem",
|
||||||
foreignField: "_id",
|
foreignField: "_id",
|
||||||
as: "returnOrderItems.orderItem",
|
as: "orderItem",
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$unwind: "$returnOrderItems.orderItem",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$lookup: {
|
|
||||||
from: "products",
|
|
||||||
localField: "returnOrderItems.orderItem.shipmentItems.product",
|
|
||||||
foreignField: "_id",
|
|
||||||
as: "product",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$unwind: "$product",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$addFields: {
|
|
||||||
product: "$product",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{ $unwind: { path: "$orderItem", preserveNullAndEmptyArrays: true } },
|
||||||
|
{ $addFields: { "returnOrderItems.orderItem": "$orderItem" } },
|
||||||
|
|
||||||
|
// --- 4. Lookup shop ---
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: "shops",
|
from: "shops",
|
||||||
localField: "returnOrderItems.orderItem.shop",
|
localField: "orderItem.shop",
|
||||||
foreignField: "_id",
|
foreignField: "_id",
|
||||||
as: "shop",
|
as: "shop",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{ $unwind: { path: "$shop", preserveNullAndEmptyArrays: true } },
|
||||||
$unwind: "$shop",
|
{ $addFields: { shopName: "$shop.shopName" } },
|
||||||
},
|
|
||||||
{
|
// --- 5. Sort before pagination ---
|
||||||
$addFields: {
|
{ $sort: { createdAt: -1 } },
|
||||||
shopName: "$shop.shopName",
|
|
||||||
},
|
// --- 6. Facet for pagination + count ---
|
||||||
},
|
|
||||||
{
|
|
||||||
$lookup: {
|
|
||||||
from: "sellers",
|
|
||||||
localField: "shop.owner",
|
|
||||||
foreignField: "_id",
|
|
||||||
as: "seller",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$unwind: "$seller",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$addFields: {
|
|
||||||
seller: "$seller.fullName",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$project: {
|
|
||||||
_id: 1,
|
|
||||||
order: 1,
|
|
||||||
status: 1,
|
|
||||||
total_price: 1,
|
|
||||||
shopName: 1,
|
|
||||||
seller: 1,
|
|
||||||
product: {
|
|
||||||
_id: 1,
|
|
||||||
title_fa: 1,
|
|
||||||
imagesUrl: 1,
|
|
||||||
},
|
|
||||||
createdAt: 1,
|
|
||||||
updatedAt: 1,
|
|
||||||
returnOrderItems: {
|
|
||||||
_id: 1,
|
|
||||||
quantity: 1,
|
|
||||||
comment: 1,
|
|
||||||
reason: 1,
|
|
||||||
imagesUrl: 1,
|
|
||||||
orderItem: {
|
|
||||||
_id: 1,
|
|
||||||
totalSellingPrice: 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
$facet: {
|
$facet: {
|
||||||
data: [{ $skip: skip }, { $limit: limit }],
|
data: [
|
||||||
|
{ $skip: skip },
|
||||||
|
{ $limit: limit },
|
||||||
|
{
|
||||||
|
$project: {
|
||||||
|
_id: 1,
|
||||||
|
order: 1,
|
||||||
|
status: 1,
|
||||||
|
total_price: 1,
|
||||||
|
shopName: 1,
|
||||||
|
createdAt: 1,
|
||||||
|
updatedAt: 1,
|
||||||
|
returnOrderItems: {
|
||||||
|
_id: 1,
|
||||||
|
quantity: 1,
|
||||||
|
comment: 1,
|
||||||
|
reason: 1,
|
||||||
|
imagesUrl: 1,
|
||||||
|
orderItem: {
|
||||||
|
_id: 1,
|
||||||
|
totalSellingPrice: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
totalCount: [{ $count: "count" }],
|
totalCount: [{ $count: "count" }],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// --- 7. Flatten count field ---
|
||||||
{
|
{
|
||||||
$addFields: {
|
$addFields: {
|
||||||
totalCount: { $arrayElemAt: ["$totalCount.count", 0] },
|
totalCount: { $ifNull: [{ $arrayElemAt: ["$totalCount.count", 0] }, 0] },
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$sort: {
|
|
||||||
createdAt: -1,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
count: (docs?.[0]?.totalCount as number) || 0,
|
count: result?.totalCount || 0,
|
||||||
docs: docs?.[0]?.data || [],
|
docs: result?.data || [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user