order item
This commit is contained in:
@@ -131,7 +131,7 @@ export class CreateOrderAsAdminDto {
|
|||||||
|
|
||||||
@ApiProperty({})
|
@ApiProperty({})
|
||||||
@IsBoolean()
|
@IsBoolean()
|
||||||
enableTax: Boolean
|
enableTax: boolean
|
||||||
|
|
||||||
@ApiProperty({})
|
@ApiProperty({})
|
||||||
@IsInt()
|
@IsInt()
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { BaseEntity } from 'src/common/entities/base.entity';
|
|||||||
|
|
||||||
@Entity({ tableName: 'order_items' })
|
@Entity({ tableName: 'order_items' })
|
||||||
@Index({ properties: ['order'] })
|
@Index({ properties: ['order'] })
|
||||||
export class OrderItem extends BaseEntity{
|
export class OrderItem extends BaseEntity {
|
||||||
[OptionalProps]?: 'createdAt' | 'deletedAt' | 'subTotal' | 'total';
|
[OptionalProps]?: 'createdAt' | 'deletedAt' | 'subTotal' | 'total';
|
||||||
|
|
||||||
@ManyToOne(() => Order)
|
@ManyToOne(() => Order)
|
||||||
@@ -29,18 +29,34 @@ export class OrderItem extends BaseEntity{
|
|||||||
@Property({ type: 'decimal', precision: 10, scale: 0, nullable: true, })
|
@Property({ type: 'decimal', precision: 10, scale: 0, nullable: true, })
|
||||||
unitPrice?: number;
|
unitPrice?: number;
|
||||||
|
|
||||||
@Formula(alias => `
|
// @Formula(alias => `
|
||||||
COALESCE(${alias}.unit_price, 0) * ${alias}.quantity
|
// COALESCE(${alias}.unit_price, 0) * ${alias}.quantity
|
||||||
`)
|
// `)
|
||||||
|
@Property({
|
||||||
|
type: 'decimal', precision: 10,
|
||||||
|
columnType: 'numeric(10,0) GENERATED ALWAYS AS (COALESCE(unit_price, 0) * quantity) STORED',
|
||||||
|
persist: false,
|
||||||
|
scale: 0,
|
||||||
|
nullable: true
|
||||||
|
})
|
||||||
subTotal!: number;
|
subTotal!: number;
|
||||||
|
|
||||||
@Property({ type: 'decimal', precision: 10, scale: 0, nullable: true })
|
@Property({
|
||||||
|
type: 'decimal', precision: 10,
|
||||||
|
scale: 0, nullable: true
|
||||||
|
})
|
||||||
discount?: number;
|
discount?: number;
|
||||||
|
|
||||||
@Formula(alias => `
|
// @Formula(alias => `
|
||||||
(COALESCE(${alias}.unit_price, 0) * ${alias}.quantity)
|
// (COALESCE(${alias}.unit_price, 0) * ${alias}.quantity)
|
||||||
- COALESCE(${alias}.discount, 0)
|
// - COALESCE(${alias}.discount, 0)
|
||||||
`)
|
// `)
|
||||||
|
@Property({
|
||||||
|
type: 'decimal', precision: 10,
|
||||||
|
scale: 0, nullable: true,
|
||||||
|
columnType: 'numeric(10,0) GENERATED ALWAYS AS ((COALESCE(unit_price, 0) * quantity) - COALESCE(discount, 0)) STORED',
|
||||||
|
persist: false
|
||||||
|
})
|
||||||
total!: number;
|
total!: number;
|
||||||
|
|
||||||
@Property({ type: 'text', nullable: true })
|
@Property({ type: 'text', nullable: true })
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ export type CreateOrderItemPopulated = Omit<CreateOrderItem, 'productId' | 'desi
|
|||||||
export interface CreateOrder {
|
export interface CreateOrder {
|
||||||
userId: string
|
userId: string
|
||||||
status: OrderStatusEnum
|
status: OrderStatusEnum
|
||||||
enableTax?: Boolean
|
enableTax?: boolean
|
||||||
paymentMethod?: string
|
paymentMethod?: string
|
||||||
adminId?: string
|
adminId?: string
|
||||||
estimatedDays?: number
|
estimatedDays?: number
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ export class OrderService {
|
|||||||
// return updateOrder
|
// return updateOrder
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO : clean
|
|
||||||
async updateOrderAsAdmin2(orderId: string, dto: UpdateOrderAsAdminDto) {
|
async updateOrderAsAdmin2(orderId: string, dto: UpdateOrderAsAdminDto) {
|
||||||
const order = await this.findOrderOrFail(orderId)
|
const order = await this.findOrderOrFail(orderId)
|
||||||
|
|
||||||
@@ -324,44 +324,6 @@ export class OrderService {
|
|||||||
return orders
|
return orders
|
||||||
}
|
}
|
||||||
|
|
||||||
// async calculateOrder(inputOrder?: Order, orderId?: string) {
|
|
||||||
// let order: undefined | Order = inputOrder
|
|
||||||
|
|
||||||
// if (!order && orderId) {
|
|
||||||
// order = await this.findOneOrFail(orderId)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (!order) {
|
|
||||||
// throw new BadRequestException("Order not found")
|
|
||||||
// }
|
|
||||||
// // calculate order financials
|
|
||||||
// const subTotal = order.items.reduce((sum, item) => {
|
|
||||||
// return sum + item.subTotal
|
|
||||||
// }, 0)
|
|
||||||
|
|
||||||
// const totalDiscount = order.items.reduce((sum, item) => {
|
|
||||||
// return sum + Number(item.discount)
|
|
||||||
// }, 0)
|
|
||||||
|
|
||||||
|
|
||||||
// const totalBeforeTax = subTotal - totalDiscount
|
|
||||||
// let tax = 0
|
|
||||||
|
|
||||||
// if (order.enableTax) {
|
|
||||||
// tax = 0.1 * totalBeforeTax
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const total = totalBeforeTax + tax
|
|
||||||
|
|
||||||
// // Update Order financial values
|
|
||||||
// // order.subTotal = subTotal
|
|
||||||
// // order.discount = totalDiscount
|
|
||||||
// // order.taxAmount = tax
|
|
||||||
// // order.total = total
|
|
||||||
// // order.balance = total - paidAmount
|
|
||||||
|
|
||||||
// return order
|
|
||||||
// }
|
|
||||||
|
|
||||||
async confirmOrderItem(userId: string, orderId: string, orderItemId: string) {
|
async confirmOrderItem(userId: string, orderId: string, orderItemId: string) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user