@@ -6,6 +6,7 @@ export enum PaymentStatus {
|
|||||||
export enum OrdersStatus {
|
export enum OrdersStatus {
|
||||||
wait_payment = "wait_payment",
|
wait_payment = "wait_payment",
|
||||||
process_by_seller = "process_by_sellers",
|
process_by_seller = "process_by_sellers",
|
||||||
|
shipped = "shipped",
|
||||||
cancelled_system = "cancelled_system",
|
cancelled_system = "cancelled_system",
|
||||||
Cancelled = "cancelled",
|
Cancelled = "cancelled",
|
||||||
Delivered = "Delivered",
|
Delivered = "Delivered",
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ export type SellerOrdersQueries = {
|
|||||||
limit: number;
|
limit: number;
|
||||||
shipperId: string;
|
shipperId: string;
|
||||||
status: string;
|
status: string;
|
||||||
|
paymentStatus: string;
|
||||||
since: number;
|
since: number;
|
||||||
maxPrice: number;
|
maxPrice: number;
|
||||||
minPrice: number;
|
minPrice: number;
|
||||||
|
|||||||
@@ -159,7 +159,11 @@ export class AdminOrderController extends BaseController {
|
|||||||
@ApiQuery("limit", "the limit of return data")
|
@ApiQuery("limit", "the limit of return data")
|
||||||
@ApiQuery("page", "the page want to get")
|
@ApiQuery("page", "the page want to get")
|
||||||
@ApiQuery("shipperId", "shipper id")
|
@ApiQuery("shipperId", "shipper id")
|
||||||
@ApiQuery("status", "status of payment ==> value = Completed | Cancelled | Pending ")
|
@ApiQuery(
|
||||||
|
"status",
|
||||||
|
"status of payment ==> value = wait_payment | process_by_sellers | shipped | cancelled_system | cancelled | Delivered",
|
||||||
|
)
|
||||||
|
@ApiQuery("paymentStatus", "status of order ==> value = Completed | Cancelled | Pending ")
|
||||||
@ApiQuery(
|
@ApiQuery(
|
||||||
"since",
|
"since",
|
||||||
"time of order created ==> value should be standard timestamp since that time user want like == > Date.now() - (7 * 24 * 60 * 60 * 1000) ==> this mean from a week ago till now ",
|
"time of order created ==> value should be standard timestamp since that time user want like == > Date.now() - (7 * 24 * 60 * 60 * 1000) ==> this mean from a week ago till now ",
|
||||||
@@ -174,6 +178,7 @@ export class AdminOrderController extends BaseController {
|
|||||||
@queryParam("page") page: string,
|
@queryParam("page") page: string,
|
||||||
@queryParam("shipperId") shipperId: string,
|
@queryParam("shipperId") shipperId: string,
|
||||||
@queryParam("status") status: string,
|
@queryParam("status") status: string,
|
||||||
|
@queryParam("paymentStatus") paymentStatus: string,
|
||||||
@queryParam("since") since: string,
|
@queryParam("since") since: string,
|
||||||
@queryParam("maxPrice") maxPrice: string,
|
@queryParam("maxPrice") maxPrice: string,
|
||||||
@queryParam("minPrice") minPrice: string,
|
@queryParam("minPrice") minPrice: string,
|
||||||
@@ -183,6 +188,7 @@ export class AdminOrderController extends BaseController {
|
|||||||
page: parseInt(page),
|
page: parseInt(page),
|
||||||
shipperId,
|
shipperId,
|
||||||
status,
|
status,
|
||||||
|
paymentStatus,
|
||||||
since: +since,
|
since: +since,
|
||||||
maxPrice: +maxPrice,
|
maxPrice: +maxPrice,
|
||||||
minPrice: +minPrice,
|
minPrice: +minPrice,
|
||||||
@@ -198,7 +204,11 @@ export class AdminOrderController extends BaseController {
|
|||||||
@ApiQuery("limit", "the limit of return data")
|
@ApiQuery("limit", "the limit of return data")
|
||||||
@ApiQuery("page", "the page want to get")
|
@ApiQuery("page", "the page want to get")
|
||||||
@ApiQuery("shipperId", "shipper id")
|
@ApiQuery("shipperId", "shipper id")
|
||||||
@ApiQuery("status", "status of payment ==> value = Completed | Cancelled | Pending ")
|
@ApiQuery(
|
||||||
|
"status",
|
||||||
|
"status of payment ==> value = wait_payment | process_by_sellers | shipped | cancelled_system | cancelled | Delivered",
|
||||||
|
)
|
||||||
|
@ApiQuery("paymentStatus", "status of order ==> value = Completed | Cancelled | Pending ")
|
||||||
@ApiQuery(
|
@ApiQuery(
|
||||||
"since",
|
"since",
|
||||||
"time of order created ==> value should be standard timestamp since that time user want like == > Date.now() - (7 * 24 * 60 * 60 * 1000) ==> this mean from a week ago till now ",
|
"time of order created ==> value should be standard timestamp since that time user want like == > Date.now() - (7 * 24 * 60 * 60 * 1000) ==> this mean from a week ago till now ",
|
||||||
@@ -212,6 +222,7 @@ export class AdminOrderController extends BaseController {
|
|||||||
@queryParam("page") page: string,
|
@queryParam("page") page: string,
|
||||||
@queryParam("shipperId") shipperId: string,
|
@queryParam("shipperId") shipperId: string,
|
||||||
@queryParam("status") status: string,
|
@queryParam("status") status: string,
|
||||||
|
@queryParam("paymentStatus") paymentStatus: string,
|
||||||
@queryParam("since") since: string,
|
@queryParam("since") since: string,
|
||||||
@queryParam("maxPrice") maxPrice: string,
|
@queryParam("maxPrice") maxPrice: string,
|
||||||
@queryParam("minPrice") minPrice: string,
|
@queryParam("minPrice") minPrice: string,
|
||||||
@@ -221,6 +232,7 @@ export class AdminOrderController extends BaseController {
|
|||||||
page: parseInt(page),
|
page: parseInt(page),
|
||||||
shipperId,
|
shipperId,
|
||||||
status,
|
status,
|
||||||
|
paymentStatus,
|
||||||
since: +since,
|
since: +since,
|
||||||
maxPrice: +maxPrice,
|
maxPrice: +maxPrice,
|
||||||
minPrice: +minPrice,
|
minPrice: +minPrice,
|
||||||
|
|||||||
@@ -768,7 +768,10 @@ class OrderItemRepo extends BaseRepository<IOrderItem> {
|
|||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
...(queries.status && {
|
...(queries.status && {
|
||||||
"order.payment.paymentStatus": queries.status,
|
"order.orderStatus": queries.status,
|
||||||
|
}),
|
||||||
|
...(queries.paymentStatus && {
|
||||||
|
"order.payment.paymentStatus": queries.paymentStatus,
|
||||||
}),
|
}),
|
||||||
...(queries.shipperId && {
|
...(queries.shipperId && {
|
||||||
shipper: +queries.shipperId,
|
shipper: +queries.shipperId,
|
||||||
@@ -882,7 +885,7 @@ class OrderItemRepo extends BaseRepository<IOrderItem> {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
$sort: {
|
$sort: {
|
||||||
"order.createdAt": -1,
|
createdAt: -1,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -210,15 +210,26 @@ class OrderService {
|
|||||||
//#######################################################
|
//#######################################################
|
||||||
//#######################################################
|
//#######################################################
|
||||||
async insertNativeShopTrackCode(orderId: number, adminId: string, insertDto: InsertTrackCodeDTO) {
|
async insertNativeShopTrackCode(orderId: number, adminId: string, insertDto: InsertTrackCodeDTO) {
|
||||||
const { orderItem } = await this.validateSellerOrder(orderId, adminId);
|
const { orderItem, order } = await this.validateSellerOrder(orderId, adminId);
|
||||||
|
order.orderStatus = OrdersStatus.shipped;
|
||||||
|
|
||||||
orderItem.trackCode = insertDto.trackCode;
|
orderItem.trackCode = insertDto.trackCode;
|
||||||
orderItem.postingDate = insertDto.postingDate;
|
orderItem.postingDate = insertDto.postingDate;
|
||||||
orderItem.postingReceipt = insertDto.postingReceipt;
|
orderItem.postingReceipt = insertDto.postingReceipt;
|
||||||
orderItem.status = OrderItemsStatus.Shipped;
|
orderItem.status = OrderItemsStatus.Shipped;
|
||||||
|
|
||||||
await orderItem.save();
|
const session = await startSession();
|
||||||
|
session.startTransaction();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await orderItem.save();
|
||||||
|
await order.save();
|
||||||
|
} catch (error) {
|
||||||
|
await session.abortTransaction();
|
||||||
|
throw error;
|
||||||
|
} finally {
|
||||||
|
await session.endSession();
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
message: OrderMessage.TrackingDetailInserted,
|
message: OrderMessage.TrackingDetailInserted,
|
||||||
orderItem,
|
orderItem,
|
||||||
|
|||||||
@@ -64,12 +64,18 @@ export class ReturnOrderDTO {
|
|||||||
@Expose()
|
@Expose()
|
||||||
total_price: number;
|
total_price: number;
|
||||||
|
|
||||||
@Expose()
|
@Transform(({ value }) => {
|
||||||
@Transform(({ value }) => new Date(value).toISOString())
|
if (!value) return null;
|
||||||
|
const date = new Date(value);
|
||||||
|
return isNaN(date.getTime()) ? null : date.toISOString();
|
||||||
|
})
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
|
|
||||||
@Expose()
|
@Transform(({ value }) => {
|
||||||
@Transform(({ value }) => new Date(value).toISOString())
|
if (!value) return null;
|
||||||
|
const date = new Date(value);
|
||||||
|
return isNaN(date.getTime()) ? null : date.toISOString();
|
||||||
|
})
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
|
|
||||||
@Expose()
|
@Expose()
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ export class ReturnOrderRepo extends BaseRepository<IReturnOrder> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getAllReturnsForAdmin(skip: number, limit: number) {
|
async getAllReturnsForAdmin(skip: number, limit: number) {
|
||||||
const [result] = await this.model.aggregate([
|
const docs = await this.model.aggregate([
|
||||||
// --- 1. Lookup order ---
|
// 1) lookup order
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: "orders",
|
from: "orders",
|
||||||
@@ -26,7 +26,7 @@ export class ReturnOrderRepo extends BaseRepository<IReturnOrder> {
|
|||||||
},
|
},
|
||||||
{ $unwind: { path: "$order", preserveNullAndEmptyArrays: true } },
|
{ $unwind: { path: "$order", preserveNullAndEmptyArrays: true } },
|
||||||
|
|
||||||
// --- 2. Lookup return order items ---
|
// 2) lookup return order items
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: "returnorderitems",
|
from: "returnorderitems",
|
||||||
@@ -36,8 +36,22 @@ export class ReturnOrderRepo extends BaseRepository<IReturnOrder> {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ $unwind: { path: "$returnOrderItems", preserveNullAndEmptyArrays: true } },
|
{ $unwind: { path: "$returnOrderItems", preserveNullAndEmptyArrays: true } },
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "returnreasons", // collection name of ReturnReasonModel
|
||||||
|
localField: "returnOrderItems.reason",
|
||||||
|
foreignField: "_id",
|
||||||
|
as: "returnOrderItems.reason",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$unwind: {
|
||||||
|
path: "$returnOrderItems.reason",
|
||||||
|
preserveNullAndEmptyArrays: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
// --- 3. Lookup order items ---
|
// 4) lookup orderItem (the original order item)
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: "orderitems",
|
from: "orderitems",
|
||||||
@@ -49,7 +63,7 @@ export class ReturnOrderRepo extends BaseRepository<IReturnOrder> {
|
|||||||
{ $unwind: { path: "$orderItem", preserveNullAndEmptyArrays: true } },
|
{ $unwind: { path: "$orderItem", preserveNullAndEmptyArrays: true } },
|
||||||
{ $addFields: { "returnOrderItems.orderItem": "$orderItem" } },
|
{ $addFields: { "returnOrderItems.orderItem": "$orderItem" } },
|
||||||
|
|
||||||
// --- 4. Lookup shop ---
|
// 5) lookup shop from the orderItem
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: "shops",
|
from: "shops",
|
||||||
@@ -61,10 +75,24 @@ export class ReturnOrderRepo extends BaseRepository<IReturnOrder> {
|
|||||||
{ $unwind: { path: "$shop", preserveNullAndEmptyArrays: true } },
|
{ $unwind: { path: "$shop", preserveNullAndEmptyArrays: true } },
|
||||||
{ $addFields: { shopName: "$shop.shopName" } },
|
{ $addFields: { shopName: "$shop.shopName" } },
|
||||||
|
|
||||||
// --- 5. Sort before pagination ---
|
// 6) group back so each return order is a single document with array of items
|
||||||
|
{
|
||||||
|
$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" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// 7) sort (by createdAt) before pagination
|
||||||
{ $sort: { createdAt: -1 } },
|
{ $sort: { createdAt: -1 } },
|
||||||
|
|
||||||
// --- 6. Facet for pagination + count ---
|
// 8) facet for pagination + count
|
||||||
{
|
{
|
||||||
$facet: {
|
$facet: {
|
||||||
data: [
|
data: [
|
||||||
@@ -85,10 +113,7 @@ export class ReturnOrderRepo extends BaseRepository<IReturnOrder> {
|
|||||||
comment: 1,
|
comment: 1,
|
||||||
reason: 1,
|
reason: 1,
|
||||||
imagesUrl: 1,
|
imagesUrl: 1,
|
||||||
orderItem: {
|
orderItem: 1,
|
||||||
_id: 1,
|
|
||||||
totalSellingPrice: 1,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -97,7 +122,7 @@ export class ReturnOrderRepo extends BaseRepository<IReturnOrder> {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
// --- 7. Flatten count field ---
|
// // 9) flatten count field
|
||||||
{
|
{
|
||||||
$addFields: {
|
$addFields: {
|
||||||
totalCount: { $ifNull: [{ $arrayElemAt: ["$totalCount.count", 0] }, 0] },
|
totalCount: { $ifNull: [{ $arrayElemAt: ["$totalCount.count", 0] }, 0] },
|
||||||
@@ -105,9 +130,11 @@ export class ReturnOrderRepo extends BaseRepository<IReturnOrder> {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const result = docs[0] || { data: [], totalCount: 0 };
|
||||||
|
|
||||||
return {
|
return {
|
||||||
count: result?.totalCount || 0,
|
count: result.totalCount || 0,
|
||||||
docs: result?.data || [],
|
docs: result.data || [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -75,7 +75,11 @@ class SellerController extends BaseController {
|
|||||||
@ApiQuery("limit", "the limit of return data")
|
@ApiQuery("limit", "the limit of return data")
|
||||||
@ApiQuery("page", "the page want to get")
|
@ApiQuery("page", "the page want to get")
|
||||||
@ApiQuery("shipperId", "shipper id")
|
@ApiQuery("shipperId", "shipper id")
|
||||||
@ApiQuery("status", "status of payment ==> value = Completed | Cancelled | Pending ")
|
@ApiQuery(
|
||||||
|
"status",
|
||||||
|
"status of payment ==> value = wait_payment | process_by_sellers | shipped | cancelled_system | cancelled | Delivered",
|
||||||
|
)
|
||||||
|
@ApiQuery("paymentStatus", "status of order ==> value = Completed | Cancelled | Pending ")
|
||||||
@ApiQuery(
|
@ApiQuery(
|
||||||
"since",
|
"since",
|
||||||
"time of order created ==> value should be standard timestamp since that time user want like == > Date.now() - (7 * 24 * 60 * 60 * 1000) ==> this mean from a week ago till now ",
|
"time of order created ==> value should be standard timestamp since that time user want like == > Date.now() - (7 * 24 * 60 * 60 * 1000) ==> this mean from a week ago till now ",
|
||||||
@@ -91,6 +95,7 @@ class SellerController extends BaseController {
|
|||||||
@queryParam("page") page: string,
|
@queryParam("page") page: string,
|
||||||
@queryParam("shipperId") shipperId: string,
|
@queryParam("shipperId") shipperId: string,
|
||||||
@queryParam("status") status: string,
|
@queryParam("status") status: string,
|
||||||
|
@queryParam("paymentStatus") paymentStatus: string,
|
||||||
@queryParam("since") since: string,
|
@queryParam("since") since: string,
|
||||||
@queryParam("maxPrice") maxPrice: string,
|
@queryParam("maxPrice") maxPrice: string,
|
||||||
@queryParam("minPrice") minPrice: string,
|
@queryParam("minPrice") minPrice: string,
|
||||||
@@ -101,6 +106,7 @@ class SellerController extends BaseController {
|
|||||||
page: parseInt(page),
|
page: parseInt(page),
|
||||||
shipperId,
|
shipperId,
|
||||||
status,
|
status,
|
||||||
|
paymentStatus,
|
||||||
since: +since,
|
since: +since,
|
||||||
maxPrice: +maxPrice,
|
maxPrice: +maxPrice,
|
||||||
minPrice: +minPrice,
|
minPrice: +minPrice,
|
||||||
|
|||||||
Reference in New Issue
Block a user