order
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { Entity, Index, ManyToOne, Property } from '@mikro-orm/core';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
import { Order } from './order.entity';
|
||||
import { product } from '../../products/entities/product.entity';
|
||||
import { product } from 'src/modules/product/entities/product.entity';
|
||||
|
||||
@Entity({ tableName: 'order_items' })
|
||||
@Index({ properties: ['order'] })
|
||||
@@ -24,4 +24,7 @@ export class OrderItem extends BaseEntity {
|
||||
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||
totalPrice!: number;
|
||||
|
||||
@Property({ nullable: true })
|
||||
description?: string;
|
||||
}
|
||||
|
||||
@@ -12,31 +12,19 @@ import {
|
||||
type EventArgs,
|
||||
} from '@mikro-orm/core';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
import { OrderCouponDetail, OrderStatus } from '../interface/order.interface';
|
||||
import { OrderStatus } from '../interface/order.interface';
|
||||
import { User } from '../../user/entities/user.entity';
|
||||
import { Restaurant } from '../../restaurants/entities/restaurant.entity';
|
||||
import { PaymentMethod } from '../../payment/entities/payment-method.entity';
|
||||
import { OrderItem } from './order-item.entity';
|
||||
import { Delivery } from '../../delivery/entities/delivery.entity';
|
||||
import { OrderUserAddress, OrderCarAddress } from '../interface/order.interface';
|
||||
import { OrderUserAddress } from '../interface/order.interface';
|
||||
import { Payment } from 'src/modules/payment/entities/payment.entity';
|
||||
|
||||
@Entity({ tableName: 'orders' })
|
||||
@Unique({ properties: ['restaurant', 'orderNumber'] })
|
||||
@Index({ properties: ['restaurant', 'status'] })
|
||||
@Index({ properties: ['user', 'status'] })
|
||||
@Index({ properties: ['restaurant', 'orderNumber'] })
|
||||
@Index({ properties: ['status'] })
|
||||
export class Order extends BaseEntity {
|
||||
@ManyToOne(() => User)
|
||||
user!: User;
|
||||
|
||||
@ManyToOne(() => Restaurant)
|
||||
restaurant!: Restaurant;
|
||||
|
||||
@ManyToOne(() => Delivery)
|
||||
deliveryMethod!: Delivery;
|
||||
|
||||
@OneToMany(() => Payment, payment => payment.order, {
|
||||
cascade: [Cascade.ALL],
|
||||
orphanRemoval: true,
|
||||
@@ -52,32 +40,17 @@ export class Order extends BaseEntity {
|
||||
@Property({ type: 'json', nullable: true })
|
||||
userAddress?: OrderUserAddress | null;
|
||||
// for car delivery
|
||||
@Property({ type: 'json', nullable: true })
|
||||
carAddress?: OrderCarAddress | null;
|
||||
|
||||
@ManyToOne(() => PaymentMethod)
|
||||
paymentMethod: PaymentMethod;
|
||||
|
||||
@Property({ type: 'int', nullable: true })
|
||||
orderNumber?: number;
|
||||
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
||||
couponDiscount: number = 0;
|
||||
|
||||
@Property({ type: 'jsonb', nullable: true })
|
||||
couponDetail?: OrderCouponDetail | null;
|
||||
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
||||
itemsDiscount: number = 0;
|
||||
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
||||
totalDiscount: number = 0;
|
||||
discount: number = 0;
|
||||
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||
subTotal!: number;
|
||||
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
||||
tax: number = 0;
|
||||
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
||||
deliveryFee: number = 0;
|
||||
@@ -85,39 +58,22 @@ export class Order extends BaseEntity {
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||
total!: number;
|
||||
|
||||
@Property({ type: 'int', default: 0 })
|
||||
totalItems: number = 0;
|
||||
|
||||
@Property({ type: 'text', nullable: true })
|
||||
description?: string;
|
||||
|
||||
@Property({ nullable: true })
|
||||
tableNumber?: string;
|
||||
|
||||
@Enum(() => OrderStatus)
|
||||
status!: OrderStatus;
|
||||
|
||||
@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 restaurant is loaded
|
||||
if (!order.restaurant) {
|
||||
throw new Error('Restaurant must be set before creating order');
|
||||
}
|
||||
|
||||
// Get the restaurant ID (handle both entity and ID cases)
|
||||
const restaurantId = typeof order.restaurant === 'string' ? order.restaurant : order.restaurant.id;
|
||||
|
||||
// Query for max orderNumber for this restaurant
|
||||
// Using findOne with orderBy to get the highest order number
|
||||
const maxOrder = await em.findOne(
|
||||
Order,
|
||||
{ restaurant: restaurantId },
|
||||
{
|
||||
orderBy: { orderNumber: 'DESC' },
|
||||
fields: ['orderNumber'],
|
||||
|
||||
@@ -1,42 +1,10 @@
|
||||
import type { CouponType } from 'src/modules/coupons/interface/coupon';
|
||||
|
||||
export interface OrderUserAddress {
|
||||
address?: string;
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
city: string;
|
||||
province: string;
|
||||
postalCode?: string;
|
||||
fullName: string;
|
||||
phone: string;
|
||||
}
|
||||
|
||||
export interface OrderCarAddress {
|
||||
carModel: string;
|
||||
carColor: string;
|
||||
plateNumber: string;
|
||||
phone: string;
|
||||
}
|
||||
|
||||
export enum OrderStatus {
|
||||
PENDING_PAYMENT = 'pendingPayment',
|
||||
PAID = 'paid',
|
||||
NEW = 'new',
|
||||
PREPARING = 'preparing',
|
||||
DELIVERED_TO_WAITER = 'deliveredToWaiter',
|
||||
DELIVERED_TO_RECEPTIONIST = 'deliveredToReceptionist',
|
||||
SHIPPED = 'shipped',
|
||||
COMPLETED = 'completed',
|
||||
CANCELED = 'canceled',
|
||||
SHIPPED = 'shipped',
|
||||
}
|
||||
|
||||
export interface OrderCouponDetail {
|
||||
couponId: string;
|
||||
couponName: string;
|
||||
couponCode: string;
|
||||
type: CouponType;
|
||||
value: number;
|
||||
|
||||
maxDiscount?: number;
|
||||
}
|
||||
|
||||
export type StatusTransitionRef = 'user' | 'admin';
|
||||
|
||||
Reference in New Issue
Block a user