update order entity
This commit is contained in:
@@ -58,8 +58,8 @@ export class Order extends BaseEntity {
|
||||
@ManyToOne(() => PaymentMethod)
|
||||
paymentMethod: PaymentMethod;
|
||||
|
||||
@Property({ type: 'int', nullable: true })
|
||||
orderNumber?: number;
|
||||
@Property({ type: 'int', defaultRaw: `nextval('order_number_seq')` })
|
||||
orderNumber!: number;
|
||||
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
||||
couponDiscount: number = 0;
|
||||
@@ -100,31 +100,5 @@ export class Order extends BaseEntity {
|
||||
@Property({ type: 'json', nullable: true })
|
||||
history: Array<{ status: OrderStatus; changedAt: Date; desc: string | null }> = [];
|
||||
|
||||
@BeforeCreate()
|
||||
async generateOrderNumber(args: EventArgs<Order>) {
|
||||
const em = args.em;
|
||||
const order = args.entity;
|
||||
|
||||
// Ensure shop is loaded
|
||||
if (!order.shop) {
|
||||
throw new Error('Shop must be set before creating order');
|
||||
}
|
||||
|
||||
// Get the shop ID (handle both entity and ID cases)
|
||||
const restaurantId = typeof order.shop === 'string' ? order.shop : order.shop.id;
|
||||
|
||||
// Query for max orderNumber for this shop
|
||||
// Using findOne with orderBy to get the highest order number
|
||||
const maxOrder = await em.findOne(
|
||||
Order,
|
||||
{ shop: restaurantId },
|
||||
{
|
||||
orderBy: { orderNumber: 'DESC' },
|
||||
fields: ['orderNumber'],
|
||||
},
|
||||
);
|
||||
|
||||
// Set the next order number (1 if no orders exist, otherwise max + 1)
|
||||
order.orderNumber = maxOrder?.orderNumber ? maxOrder.orderNumber + 1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user