chore: add service to approve a invoice by user
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
import dayjs from "dayjs";
|
||||
import Decimal from "decimal.js";
|
||||
import { QueryRunner } from "typeorm";
|
||||
|
||||
@@ -59,7 +60,27 @@ export class InvoicesService {
|
||||
invoice,
|
||||
};
|
||||
}
|
||||
///********************************** */
|
||||
|
||||
async approveInvoiceByUser(invoiceId: string, userId: string) {
|
||||
const invoice = await this.invoiceRepository.findOneBy({ id: invoiceId, user: { id: userId } });
|
||||
if (!invoice) throw new BadRequestException(InvoiceMessage.NOT_FOUND_BY_ID);
|
||||
|
||||
if (invoice.status === InvoiceStatus.APPROVED) throw new BadRequestException(InvoiceMessage.ALREADY_APPROVED);
|
||||
if (invoice.status === InvoiceStatus.PAID) throw new BadRequestException(InvoiceMessage.INVOICE_ALREADY_PAID);
|
||||
if (dayjs().isAfter(invoice.dueDate)) throw new BadRequestException(InvoiceMessage.INVOICE_IS_OVERDUE);
|
||||
|
||||
if (invoice.status !== InvoiceStatus.PENDING) throw new BadRequestException(InvoiceMessage.INVOICE_CAN_NOT_APPROVED);
|
||||
|
||||
invoice.status = InvoiceStatus.APPROVED;
|
||||
|
||||
await this.invoiceRepository.save(invoice);
|
||||
|
||||
return {
|
||||
message: InvoiceMessage.APPROVED,
|
||||
invoice,
|
||||
};
|
||||
}
|
||||
///********************************** */
|
||||
|
||||
async createInvoiceForSubscription(userId: string, plan: SubscriptionPlan, dueDate: Date, queryRunner: QueryRunner) {
|
||||
|
||||
Reference in New Issue
Block a user