add : return order detail
This commit is contained in:
@@ -43,6 +43,16 @@ export class AdminOrderController extends BaseController {
|
|||||||
return this.response({ pager, returnsOrders: data.returnsOrders });
|
return this.response({ pager, returnsOrders: data.returnsOrders });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation("get detail of order that returned ==> login as admin")
|
||||||
|
@ApiResponse("successful", HttpStatus.Ok)
|
||||||
|
@ApiParam("returnOrderId", "id of return order", true)
|
||||||
|
@ApiAuth()
|
||||||
|
@httpGet("/returns/:returnOrderId", Guard.authAdmin(), ValidationMiddleware.validateQuery(PaginationDTO))
|
||||||
|
public async getReturnOrder(@requestParam("returnOrderId") returnOrderId: string) {
|
||||||
|
const returnOrder = await this.returnService.getReturnOrderDetail(returnOrderId);
|
||||||
|
return this.response({ returnOrder });
|
||||||
|
}
|
||||||
|
|
||||||
@ApiOperation("approve a return order request with return order id")
|
@ApiOperation("approve a return order request with return order id")
|
||||||
@ApiResponse("successful", HttpStatus.Ok)
|
@ApiResponse("successful", HttpStatus.Ok)
|
||||||
@ApiParam("returnOrderId", "id of return order", true)
|
@ApiParam("returnOrderId", "id of return order", true)
|
||||||
|
|||||||
@@ -171,6 +171,124 @@ export class ReturnOrderRepo extends BaseRepository<IReturnOrder> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getReturnOrderDetail(returnOrderId: string) {
|
||||||
|
const [returnOrder] = await this.model.aggregate([
|
||||||
|
{
|
||||||
|
$match: {
|
||||||
|
_id: new Types.ObjectId(returnOrderId),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// 1) lookup order
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "orders",
|
||||||
|
localField: "order",
|
||||||
|
foreignField: "_id",
|
||||||
|
as: "order",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ $unwind: { path: "$order", preserveNullAndEmptyArrays: true } },
|
||||||
|
|
||||||
|
// 2) lookup return order items
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "returnorderitems",
|
||||||
|
localField: "_id",
|
||||||
|
foreignField: "returnOrderId",
|
||||||
|
as: "returnOrderItems",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ $unwind: { path: "$returnOrderItems", preserveNullAndEmptyArrays: true } },
|
||||||
|
|
||||||
|
// 3) lookup return reason
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "returnreasons",
|
||||||
|
localField: "returnOrderItems.reason",
|
||||||
|
foreignField: "_id",
|
||||||
|
as: "returnOrderItems.reason",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$unwind: {
|
||||||
|
path: "$returnOrderItems.reason",
|
||||||
|
preserveNullAndEmptyArrays: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// 4) lookup orderItem (the original order item)
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "orderitems",
|
||||||
|
localField: "returnOrderItems.orderItem",
|
||||||
|
foreignField: "_id",
|
||||||
|
as: "orderItem",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ $unwind: { path: "$orderItem", preserveNullAndEmptyArrays: true } },
|
||||||
|
|
||||||
|
// ✅ 5) populate shipmentItem from orderItem.shipmentItems
|
||||||
|
{
|
||||||
|
$addFields: {
|
||||||
|
"returnOrderItems.shipmentItem": {
|
||||||
|
$first: {
|
||||||
|
$filter: {
|
||||||
|
input: "$orderItem.shipmentItems",
|
||||||
|
as: "si",
|
||||||
|
cond: { $eq: ["$$si._id", "$returnOrderItems.shipmentItem"] },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// 6) attach orderItem itself
|
||||||
|
{ $addFields: { "returnOrderItems.orderItem": "$orderItem" } },
|
||||||
|
// ✅ populate product inside shipmentItem
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "products", // name of your product collection
|
||||||
|
localField: "returnOrderItems.shipmentItem.product",
|
||||||
|
foreignField: "_id", // or "productId" if your product._id isn’t numeric
|
||||||
|
as: "returnOrderItems.shipmentItem.product",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$unwind: {
|
||||||
|
path: "$returnOrderItems.shipmentItem.product",
|
||||||
|
preserveNullAndEmptyArrays: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// 7) lookup shop
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "shops",
|
||||||
|
localField: "orderItem.shop",
|
||||||
|
foreignField: "_id",
|
||||||
|
as: "shop",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ $unwind: { path: "$shop", preserveNullAndEmptyArrays: true } },
|
||||||
|
{ $addFields: { shopName: "$shop.shopName" } },
|
||||||
|
|
||||||
|
// 8) regroup all return items per return order
|
||||||
|
{
|
||||||
|
$group: {
|
||||||
|
_id: "$_id",
|
||||||
|
order: { $first: "$order" },
|
||||||
|
status: { $first: "$status" },
|
||||||
|
total_price: { $first: "$total_price" },
|
||||||
|
shopName: { $first: "$shopName" },
|
||||||
|
createdAt: { $first: "$createdAt" },
|
||||||
|
updatedAt: { $first: "$updatedAt" },
|
||||||
|
returnOrderItems: { $push: "$returnOrderItems" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
return returnOrder;
|
||||||
|
}
|
||||||
|
|
||||||
async getAllReturnsForUser(userId: string) {
|
async getAllReturnsForUser(userId: string) {
|
||||||
const docs = await this.model.aggregate([
|
const docs = await this.model.aggregate([
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -37,6 +37,12 @@ class ReturnService {
|
|||||||
returnOrders,
|
returnOrders,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
//#######################################################
|
||||||
|
|
||||||
|
async getReturnOrderDetail(returnOrderId: string) {
|
||||||
|
const returnOrder = await this.returnOrderRepo.getReturnOrderDetail(returnOrderId);
|
||||||
|
return returnOrder;
|
||||||
|
}
|
||||||
|
|
||||||
//#######################################################
|
//#######################################################
|
||||||
async getAllReturnOrders(queryDto: PaginationDTO) {
|
async getAllReturnOrders(queryDto: PaginationDTO) {
|
||||||
|
|||||||
Reference in New Issue
Block a user