76 lines
1.8 KiB
TypeScript
76 lines
1.8 KiB
TypeScript
import {
|
|
Entity,
|
|
Index,
|
|
ManyToOne,
|
|
OneToMany,
|
|
Property,
|
|
Collection,
|
|
Cascade,
|
|
Enum,
|
|
PrimaryKey,
|
|
} from '@mikro-orm/core';
|
|
import { BaseEntity } from '../../../common/entities/base.entity';
|
|
import { OrderStatus } from '../interface/order.interface';
|
|
import { User } from '../../user/entities/user.entity';
|
|
import { OrderItem } from './order-item.entity';
|
|
import { Payment } from 'src/modules/payment/entities/payment.entity';
|
|
import { ulid } from 'ulid';
|
|
|
|
@Entity({ tableName: 'orders' })
|
|
@Index({ properties: ['user', 'status'] })
|
|
@Index({ properties: ['status'] })
|
|
export class Order extends BaseEntity {
|
|
@PrimaryKey({type:'string',columnType:'char(26)'})
|
|
id:string=ulid()
|
|
|
|
@ManyToOne(() => User)
|
|
user!: User;
|
|
|
|
@OneToMany(() => Payment, payment => payment.order, {
|
|
cascade: [Cascade.ALL],
|
|
orphanRemoval: true,
|
|
})
|
|
payments = new Collection<Payment>(this);
|
|
|
|
@OneToMany(() => OrderItem, item => item.order, {
|
|
cascade: [Cascade.ALL],
|
|
orphanRemoval: true,
|
|
})
|
|
items = new Collection<OrderItem>(this);
|
|
|
|
@Property({ nullable: true })
|
|
userAddress?: string | null;
|
|
|
|
@Property({ type: 'bigint', autoincrement: true })
|
|
orderNumber: number;
|
|
|
|
|
|
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
|
discount: number = 0;
|
|
|
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
|
subTotal!: number;
|
|
|
|
|
|
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
|
deliveryFee: number = 0;
|
|
|
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
|
total!: number;
|
|
|
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
|
paidAmount!: number;
|
|
|
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
|
balance!: number;
|
|
|
|
@Property({ type: 'text', nullable: true })
|
|
description?: string;
|
|
|
|
|
|
@Enum(() => OrderStatus)
|
|
status!: OrderStatus;
|
|
|
|
|
|
}
|