update order status

This commit is contained in:
2026-07-14 21:04:07 +03:30
parent 13f21d4d3f
commit e991215f5a
8 changed files with 608 additions and 54 deletions
@@ -17,8 +17,7 @@ export interface OrderCarAddress {
}
export enum OrderStatus {
PENDING_PAYMENT = 'pendingPayment',
PAID = 'paid',
NEW = 'new',
PREPARING = 'preparing',
DELIVERED_TO_WAITER = 'deliveredToWaiter',
DELIVERED_TO_RECEPTIONIST = 'deliveredToReceptionist',
@@ -35,8 +35,7 @@ export class OrderListeners {
private getStatusFarsi(status: OrderStatus): string {
const statusMap: Record<OrderStatus, string> = {
[OrderStatus.PENDING_PAYMENT]: ر انتظار پرداخت',
[OrderStatus.PAID]: 'پرداخت شده',
[OrderStatus.NEW]: 'جدید',
[OrderStatus.PREPARING]: 'در حال آماده‌سازی',
[OrderStatus.DELIVERED_TO_RECEPTIONIST]: 'تحویل به پذیرش',
[OrderStatus.DELIVERED_TO_WAITER]: 'تحویل به گارسون',
+8 -14
View File
@@ -47,8 +47,7 @@ export class OrdersService {
private readonly logger = new Logger(OrdersService.name);
private static readonly STATUS_TRANSITIONS: Record<OrderStatus, readonly OrderStatus[]> = {
[OrderStatus.PENDING_PAYMENT]: [OrderStatus.PAID, OrderStatus.CANCELED, OrderStatus.PREPARING],
[OrderStatus.PAID]: [OrderStatus.PREPARING, OrderStatus.CANCELED],
[OrderStatus.NEW]: [ OrderStatus.CANCELED, OrderStatus.PREPARING],
[OrderStatus.PREPARING]: [OrderStatus.DELIVERED_TO_RECEPTIONIST, OrderStatus.DELIVERED_TO_WAITER, OrderStatus.SHIPPED, OrderStatus.CANCELED],
[OrderStatus.DELIVERED_TO_WAITER]: [OrderStatus.COMPLETED, OrderStatus.CANCELED],
[OrderStatus.DELIVERED_TO_RECEPTIONIST]: [OrderStatus.COMPLETED, OrderStatus.CANCELED],
@@ -92,8 +91,8 @@ export class OrdersService {
description: cart.description,
printInvoice: cart.printInvoice ?? false,
tableNumber: cart.tableNumber,
status: OrderStatus.PENDING_PAYMENT,
history: [{ status: OrderStatus.PENDING_PAYMENT, changedAt: new Date() }],
status: OrderStatus.NEW,
history: [{ status: OrderStatus.NEW, changedAt: new Date() }],
});
em.persist(order);
@@ -206,8 +205,8 @@ export class OrdersService {
description: dto.description,
printInvoice: false,
tableNumber: dto.tableNumber,
status: OrderStatus.PENDING_PAYMENT,
history: [{ status: OrderStatus.PENDING_PAYMENT, changedAt: new Date() }],
status: OrderStatus.NEW,
history: [{ status: OrderStatus.NEW, changedAt: new Date() }],
});
em.persist(order);
@@ -431,8 +430,8 @@ export class OrdersService {
if (!OrdersService.STATUS_TRANSITIONS[from]?.includes(to)) return false;
if (to === OrderStatus.CANCELED) {
// only allow orders with status of PENDING_PAYMENT and PAID are allowed to be canceled by user
if (ref === 'user' && ![OrderStatus.PENDING_PAYMENT, OrderStatus.PAID].includes(from)) {
// only allow orders with status of NEW are allowed to be canceled by user
if (ref === 'user' && ![OrderStatus.NEW].includes(from)) {
return false;
} else if (ref === 'admin') {
return true;
@@ -441,7 +440,7 @@ export class OrdersService {
// only allow orders with status of PENDING_PAYMENT and payment
// method of cash are allowed to move to CONFIRMED directly
if (
from == OrderStatus.PENDING_PAYMENT &&
from == OrderStatus.NEW &&
to == OrderStatus.PREPARING &&
paymentMethod !== PaymentMethodEnum.Cash &&
paymentMethod !== PaymentMethodEnum.CreditCard
@@ -475,10 +474,6 @@ export class OrdersService {
return false;
}
if (paymentMethod === PaymentMethodEnum.Online) {
if (to === OrderStatus.PREPARING && from !== OrderStatus.PAID) return false;
}
return true;
}
@@ -709,7 +704,6 @@ export class OrdersService {
restaurant: { id: restId },
status: {
$in: [
OrderStatus.PAID,
OrderStatus.PREPARING,
OrderStatus.DELIVERED_TO_WAITER,
OrderStatus.DELIVERED_TO_RECEPTIONIST,
@@ -105,7 +105,7 @@ export class OrderRepository extends EntityRepository<Order> {
where.$or = searchConditions;
}
// Filter: Exclude orders with payment method Online and status pending_payment
// Filter: Exclude online orders that have no paid payment
if (excludeOnlinePendingPayment) {
const existingConditions = where.$and || [];
where.$and = [
@@ -113,7 +113,7 @@ export class OrderRepository extends EntityRepository<Order> {
{
$or: [
{ paymentMethod: { method: { $ne: PaymentMethodEnum.Online } } },
{ status: { $ne: OrderStatus.PENDING_PAYMENT } },
{ payments: { status: PaymentStatusEnum.Paid } },
],
},
];
@@ -28,7 +28,7 @@ export class PaymentsService {
const ctx = await this.loadAndValidateOrder(orderId);
// Idempotency: avoid creating/charging again for already-paid orders
if (ctx.order.status === OrderStatus.PAID) {
if (ctx.order.payments.find(p => p.status === PaymentStatusEnum.Paid)) {
return { paymentUrl: null };
}
@@ -112,7 +112,7 @@ export class PaymentsService {
const order = await em.findOne(Order, { id: ctx.order.id }, { populate: ['user', 'restaurant'] });
if (!order) throw new NotFoundException(OrderMessage.NOT_FOUND);
if (!order.user) throw new NotFoundException(OrderMessage.USER_NOT_FOUND);
if (order.status === OrderStatus.PAID) {
if (order.payments.find(p => p.status === PaymentStatusEnum.Paid)) {
return;
}
@@ -152,7 +152,6 @@ export class PaymentsService {
payment.status = PaymentStatusEnum.Paid;
payment.paidAt = new Date();
order.status = OrderStatus.PAID;
em.persist([ payment, order, newWalletTransaction]);
await em.flush();
@@ -232,9 +231,6 @@ export class PaymentsService {
}
this.markPaid(payment, result.referenceId);
if (payment.order.status === OrderStatus.PENDING_PAYMENT) {
payment.order.status = OrderStatus.PAID;
}
await em.flush();
return payment;
@@ -275,9 +271,7 @@ export class PaymentsService {
if (payment.status === PaymentStatusEnum.Paid) {
throw new BadRequestException(PaymentMessage.PAYMENT_ALREADY_PAID);
}
if (payment.order.status === OrderStatus.PENDING_PAYMENT) {
payment.order.status = OrderStatus.PAID;
}
payment.status = PaymentStatusEnum.Paid;
payment.paidAt = new Date();
em.persist(payment);
@@ -5,7 +5,7 @@ import { EntityManager } from '@mikro-orm/postgresql';
import { OrderStatus } from '../../orders/interface/order.interface';
const COUNTED_ORDER_STATUSES = [
OrderStatus.PAID,
OrderStatus.NEW,
OrderStatus.PREPARING,
OrderStatus.DELIVERED_TO_WAITER,
OrderStatus.DELIVERED_TO_RECEPTIONIST,