chore: complete the the invoice module - not tested

This commit is contained in:
mahyargdz
2025-02-11 14:25:49 +03:30
parent bbdc5996b5
commit 49c62018f0
24 changed files with 674 additions and 74 deletions
@@ -1,7 +1,10 @@
import { Column, Entity, ManyToOne } from "typeorm";
// eslint-disable-next-line import/no-named-as-default
import Decimal from "decimal.js";
import { Column, Entity, ManyToOne, OneToMany } from "typeorm";
import { InvoiceItem } from "./invoice-item.entity";
import { BaseEntity } from "../../../common/entities/base.entity";
import { SubscriptionPlan } from "../../subscriptions/entities/subscription.entity";
import { DecimalTransformer } from "../../../common/transformers/decimal.transformer";
import { User } from "../../users/entities/user.entity";
import { InvoiceStatus } from "../enums/invoice-status.enum";
@@ -10,15 +13,25 @@ export class Invoice extends BaseEntity {
@ManyToOne(() => User, (user) => user.invoices, { nullable: true, onDelete: "RESTRICT" })
user: User;
@ManyToOne(() => SubscriptionPlan, { nullable: true, onDelete: "SET NULL" })
subscriptionPlan?: SubscriptionPlan;
@Column({ type: "decimal", precision: 10, scale: 2, nullable: false })
amount: number;
@Column({ type: "decimal", precision: 16, scale: 2, nullable: false, transformer: new DecimalTransformer() })
totalPrice: Decimal;
@Column({ type: "enum", enum: InvoiceStatus, default: InvoiceStatus.PENDING })
status: InvoiceStatus;
@Column({ type: "timestamp", nullable: false })
dueDate: Date;
@Column({ type: "timestamp", nullable: true })
paidAt?: Date;
paidAt: Date | null;
@Column({ type: "int", default: 0 })
tax: number;
@OneToMany(() => InvoiceItem, (invoiceItem) => invoiceItem.invoice, { cascade: true })
items: InvoiceItem[];
get isOverdue(): boolean {
return this.status === InvoiceStatus.PENDING && new Date() > this.dueDate;
}
}