order
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Entity, Index, ManyToOne, Property } from '@mikro-orm/core';
|
import { Entity, Index, ManyToOne, Property } from '@mikro-orm/core';
|
||||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||||
import { Order } from './order.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' })
|
@Entity({ tableName: 'order_items' })
|
||||||
@Index({ properties: ['order'] })
|
@Index({ properties: ['order'] })
|
||||||
@@ -24,4 +24,7 @@ export class OrderItem extends BaseEntity {
|
|||||||
|
|
||||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||||
totalPrice!: number;
|
totalPrice!: number;
|
||||||
|
|
||||||
|
@Property({ nullable: true })
|
||||||
|
description?: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,31 +12,19 @@ import {
|
|||||||
type EventArgs,
|
type EventArgs,
|
||||||
} from '@mikro-orm/core';
|
} from '@mikro-orm/core';
|
||||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
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 { 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 { OrderItem } from './order-item.entity';
|
||||||
import { Delivery } from '../../delivery/entities/delivery.entity';
|
import { OrderUserAddress } from '../interface/order.interface';
|
||||||
import { OrderUserAddress, OrderCarAddress } from '../interface/order.interface';
|
|
||||||
import { Payment } from 'src/modules/payment/entities/payment.entity';
|
import { Payment } from 'src/modules/payment/entities/payment.entity';
|
||||||
|
|
||||||
@Entity({ tableName: 'orders' })
|
@Entity({ tableName: 'orders' })
|
||||||
@Unique({ properties: ['restaurant', 'orderNumber'] })
|
|
||||||
@Index({ properties: ['restaurant', 'status'] })
|
|
||||||
@Index({ properties: ['user', 'status'] })
|
@Index({ properties: ['user', 'status'] })
|
||||||
@Index({ properties: ['restaurant', 'orderNumber'] })
|
|
||||||
@Index({ properties: ['status'] })
|
@Index({ properties: ['status'] })
|
||||||
export class Order extends BaseEntity {
|
export class Order extends BaseEntity {
|
||||||
@ManyToOne(() => User)
|
@ManyToOne(() => User)
|
||||||
user!: User;
|
user!: User;
|
||||||
|
|
||||||
@ManyToOne(() => Restaurant)
|
|
||||||
restaurant!: Restaurant;
|
|
||||||
|
|
||||||
@ManyToOne(() => Delivery)
|
|
||||||
deliveryMethod!: Delivery;
|
|
||||||
|
|
||||||
@OneToMany(() => Payment, payment => payment.order, {
|
@OneToMany(() => Payment, payment => payment.order, {
|
||||||
cascade: [Cascade.ALL],
|
cascade: [Cascade.ALL],
|
||||||
orphanRemoval: true,
|
orphanRemoval: true,
|
||||||
@@ -52,32 +40,17 @@ export class Order extends BaseEntity {
|
|||||||
@Property({ type: 'json', nullable: true })
|
@Property({ type: 'json', nullable: true })
|
||||||
userAddress?: OrderUserAddress | null;
|
userAddress?: OrderUserAddress | null;
|
||||||
// for car delivery
|
// for car delivery
|
||||||
@Property({ type: 'json', nullable: true })
|
|
||||||
carAddress?: OrderCarAddress | null;
|
|
||||||
|
|
||||||
@ManyToOne(() => PaymentMethod)
|
|
||||||
paymentMethod: PaymentMethod;
|
|
||||||
|
|
||||||
@Property({ type: 'int', nullable: true })
|
@Property({ type: 'int', nullable: true })
|
||||||
orderNumber?: number;
|
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 })
|
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
||||||
itemsDiscount: number = 0;
|
discount: number = 0;
|
||||||
|
|
||||||
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
|
||||||
totalDiscount: number = 0;
|
|
||||||
|
|
||||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||||
subTotal!: number;
|
subTotal!: number;
|
||||||
|
|
||||||
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
|
||||||
tax: number = 0;
|
|
||||||
|
|
||||||
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
||||||
deliveryFee: number = 0;
|
deliveryFee: number = 0;
|
||||||
@@ -85,39 +58,22 @@ export class Order extends BaseEntity {
|
|||||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||||
total!: number;
|
total!: number;
|
||||||
|
|
||||||
@Property({ type: 'int', default: 0 })
|
|
||||||
totalItems: number = 0;
|
|
||||||
|
|
||||||
@Property({ type: 'text', nullable: true })
|
@Property({ type: 'text', nullable: true })
|
||||||
description?: string;
|
description?: string;
|
||||||
|
|
||||||
@Property({ nullable: true })
|
|
||||||
tableNumber?: string;
|
|
||||||
|
|
||||||
@Enum(() => OrderStatus)
|
@Enum(() => OrderStatus)
|
||||||
status!: OrderStatus;
|
status!: OrderStatus;
|
||||||
|
|
||||||
@Property({ type: 'json', nullable: true })
|
|
||||||
history: Array<{ status: OrderStatus; changedAt: Date; desc: string | null }> = [];
|
|
||||||
|
|
||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
async generateOrderNumber(args: EventArgs<Order>) {
|
async generateOrderNumber(args: EventArgs<Order>) {
|
||||||
const em = args.em;
|
const em = args.em;
|
||||||
const order = args.entity;
|
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(
|
const maxOrder = await em.findOne(
|
||||||
Order,
|
Order,
|
||||||
{ restaurant: restaurantId },
|
|
||||||
{
|
{
|
||||||
orderBy: { orderNumber: 'DESC' },
|
orderBy: { orderNumber: 'DESC' },
|
||||||
fields: ['orderNumber'],
|
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 {
|
export enum OrderStatus {
|
||||||
PENDING_PAYMENT = 'pendingPayment',
|
NEW = 'new',
|
||||||
PAID = 'paid',
|
|
||||||
PREPARING = 'preparing',
|
PREPARING = 'preparing',
|
||||||
DELIVERED_TO_WAITER = 'deliveredToWaiter',
|
|
||||||
DELIVERED_TO_RECEPTIONIST = 'deliveredToReceptionist',
|
|
||||||
SHIPPED = 'shipped',
|
|
||||||
COMPLETED = 'completed',
|
COMPLETED = 'completed',
|
||||||
CANCELED = 'canceled',
|
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