chore: add the order count to user porfile
This commit is contained in:
@@ -14,6 +14,105 @@ class OrderRepository extends BaseRepository<IOrder> {
|
||||
super(OrderModel);
|
||||
}
|
||||
|
||||
async getUserOrderCount(userId: string) {
|
||||
const pipeline = [
|
||||
{
|
||||
$match: {
|
||||
user: new Types.ObjectId(userId),
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "orderitems",
|
||||
localField: "_id",
|
||||
foreignField: "order",
|
||||
as: "orderItems",
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: "$orderItems",
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: {
|
||||
orderId: "$_id",
|
||||
orderStatus: "$orderStatus",
|
||||
itemStatus: "$orderItems.status",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: null,
|
||||
processing: {
|
||||
$sum: {
|
||||
$cond: [
|
||||
{
|
||||
$or: [
|
||||
{ $eq: ["$_id.orderStatus", OrdersStatus.process_by_seller] },
|
||||
{ $eq: ["$_id.orderStatus", OrdersStatus.wait_payment] },
|
||||
],
|
||||
},
|
||||
{
|
||||
$cond: [
|
||||
{
|
||||
$or: [
|
||||
{ $eq: ["$_id.itemStatus", OrderItemsStatus.Processing] },
|
||||
{ $eq: ["$_id.itemStatus", OrderItemsStatus.Shipped] },
|
||||
],
|
||||
},
|
||||
1,
|
||||
0,
|
||||
],
|
||||
},
|
||||
0,
|
||||
],
|
||||
},
|
||||
},
|
||||
delivered: {
|
||||
$sum: {
|
||||
$cond: [
|
||||
{
|
||||
$and: [
|
||||
{ $eq: ["$_id.orderStatus", OrdersStatus.process_by_seller] },
|
||||
{ $eq: ["$_id.itemStatus", OrderItemsStatus.Delivered] },
|
||||
],
|
||||
},
|
||||
1,
|
||||
0,
|
||||
],
|
||||
},
|
||||
},
|
||||
cancelled: {
|
||||
$sum: {
|
||||
$cond: [
|
||||
{
|
||||
$and: [
|
||||
{ $eq: ["$_id.orderStatus", OrdersStatus.cancelled_system] },
|
||||
{ $eq: ["$_id.itemStatus", OrderItemsStatus.cancelled_system] },
|
||||
],
|
||||
},
|
||||
1,
|
||||
0,
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0,
|
||||
processing: 1,
|
||||
delivered: 1,
|
||||
cancelled: 1,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const result = await this.model.aggregate(pipeline);
|
||||
return result[0] || { processing: 0, delivered: 0, cancelled: 0 };
|
||||
}
|
||||
|
||||
async getUserOrders(userId: string, statusQuery: OrderStatusQuery) {
|
||||
const matchQuery: { [k: string]: unknown } = {
|
||||
user: new Types.ObjectId(userId),
|
||||
|
||||
@@ -333,11 +333,11 @@ class OrderService {
|
||||
//#######################################################
|
||||
//#######################################################
|
||||
async getUserOrders(userId: string, statusQuery: OrderStatusQuery) {
|
||||
// const orders = await this.orderRepo.model.find({ user: userId });
|
||||
const docs = await this.orderRepo.getUserOrders(userId, statusQuery);
|
||||
const orders = docs.map((doc) => UserOrderDTO.transformOrder(doc));
|
||||
const mappedOrders = orders.map((order) => this.mapUserOrderItems(order));
|
||||
return { mappedOrders, orders };
|
||||
const userOrderCount = await this.orderRepo.getUserOrderCount(userId);
|
||||
return { mappedOrders, orders, userOrderCount };
|
||||
}
|
||||
|
||||
//helper methods
|
||||
|
||||
@@ -15,7 +15,7 @@ export class TokenService {
|
||||
private logger = new Logger(TokenService.name);
|
||||
@inject(IOCTYPES.TokenRepository) private tokenRepository: TokenRepository;
|
||||
private jwtSecret: string = process.env.JWT_SECRET;
|
||||
private jwt_access_expire: string = process.env.JWT_EXPIRE_ACCESS || "2"; //hours
|
||||
private jwt_access_expire: string = process.env.JWT_EXPIRE_ACCESS || "2"; //minutes
|
||||
private jwt_refresh_expire: string = process.env.JWT_EXPIRE_REFRESH || "2"; //day
|
||||
|
||||
public _generateToken(type: TokenType, payload: AuthTokenPayload, option?: SignOptions) {
|
||||
@@ -24,7 +24,7 @@ export class TokenService {
|
||||
return refToken;
|
||||
}
|
||||
public async generateAuthTokens(payload: AuthTokenPayload) {
|
||||
const accessToken = this._generateToken("Access", payload, { expiresIn: `${this.jwt_access_expire}h` });
|
||||
const accessToken = this._generateToken("Access", payload, { expiresIn: `${this.jwt_access_expire}m` });
|
||||
const refreshToken = this._generateToken("Refresh", payload);
|
||||
const RefreshExpire = DayJs().add(+this.jwt_refresh_expire, "day").toDate();
|
||||
|
||||
@@ -37,7 +37,7 @@ export class TokenService {
|
||||
const tokens = {
|
||||
accessToken: {
|
||||
token: accessToken,
|
||||
expires: DayJs().add(+this.jwt_access_expire, "h").unix().toString(),
|
||||
expires: DayJs().add(+this.jwt_access_expire, "m").unix().toString(),
|
||||
},
|
||||
refreshToken: {
|
||||
token: refreshToken,
|
||||
|
||||
Reference in New Issue
Block a user