order
This commit is contained in:
@@ -7,15 +7,11 @@ import {
|
|||||||
Collection,
|
Collection,
|
||||||
Cascade,
|
Cascade,
|
||||||
Enum,
|
Enum,
|
||||||
BeforeCreate,
|
|
||||||
Unique,
|
|
||||||
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 { 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 { OrderItem } from './order-item.entity';
|
import { OrderItem } from './order-item.entity';
|
||||||
import { OrderUserAddress } 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' })
|
||||||
@@ -37,12 +33,11 @@ export class Order extends BaseEntity {
|
|||||||
})
|
})
|
||||||
items = new Collection<OrderItem>(this);
|
items = new Collection<OrderItem>(this);
|
||||||
|
|
||||||
@Property({ type: 'json', nullable: true })
|
@Property({ nullable: true })
|
||||||
userAddress?: OrderUserAddress | null;
|
userAddress?: string | null;
|
||||||
// for car delivery
|
|
||||||
|
|
||||||
@Property({ type: 'int', nullable: true })
|
@Property({ type: 'bigint', autoincrement: true })
|
||||||
orderNumber?: number;
|
orderNumber: number;
|
||||||
|
|
||||||
|
|
||||||
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
||||||
@@ -58,6 +53,11 @@ 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: 'decimal', precision: 10, scale: 0 })
|
||||||
|
paidAmount!: number;
|
||||||
|
|
||||||
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||||
|
balance!: number;
|
||||||
|
|
||||||
@Property({ type: 'text', nullable: true })
|
@Property({ type: 'text', nullable: true })
|
||||||
description?: string;
|
description?: string;
|
||||||
@@ -67,20 +67,4 @@ export class Order extends BaseEntity {
|
|||||||
status!: OrderStatus;
|
status!: OrderStatus;
|
||||||
|
|
||||||
|
|
||||||
@BeforeCreate()
|
|
||||||
async generateOrderNumber(args: EventArgs<Order>) {
|
|
||||||
const em = args.em;
|
|
||||||
const order = args.entity;
|
|
||||||
|
|
||||||
const maxOrder = await em.findOne(
|
|
||||||
Order,
|
|
||||||
{
|
|
||||||
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