import { Collection, Entity, ManyToOne, OptionalProps, Property, OneToMany, Opt, Index } from '@mikro-orm/core'; import { Product } from 'src/modules/product/entities/product.entity'; import { BaseEntity } from 'src/common/entities/base.entity'; import { Invoice } from './invoice.entity'; import { Order } from 'src/modules/order/entities/order.entity'; @Entity({ tableName: 'invoice_items' }) @Index({ properties: ['invoice'] }) export class InvoiceItem extends BaseEntity { [OptionalProps]?: 'createdAt' | 'deletedAt' | 'discount' | 'description' | 'confirmedAt'; @ManyToOne(() => Invoice) invoice!: Invoice; @ManyToOne(() => Product) product!: Product; @OneToMany(() => Order, order => order.invoiceItem) orders = new Collection(this); @Property({ type: 'int' }) quantity!: number; @Property({ type: 'int', fieldName: 'unit_price' }) unitPrice!: number; @Property({ type: 'int', columnType: 'int GENERATED ALWAYS AS (COALESCE(unit_price, 0) * quantity) STORED', persist: false, }) subTotal!: number & Opt; @Property({ type: 'int', nullable: true }) discount?: number; @Property({ type: 'int', columnType: 'int GENERATED ALWAYS AS ((COALESCE(unit_price, 0) * quantity) - COALESCE(discount, 0)) STORED', persist: false }) total!: number & Opt; @Property({ type: 'text', nullable: true }) description?: string; @Property({ nullable: true, columnType: 'timestamptz' }) confirmedAt?: Date; }