fix: bill
This commit is contained in:
@@ -19,7 +19,10 @@ import { CompaniesService } from "../companies/services/companies.service";
|
||||
import { InvoiceItem } from "../invoices/entities/invoice-item.entity";
|
||||
import { Invoice } from "../invoices/entities/invoice.entity";
|
||||
import { NotificationQueue } from "../notifications/queue/notification.queue";
|
||||
import { InvoicesService } from "../invoices/providers/invoices.service";
|
||||
import { InjectQueue } from "@nestjs/bullmq";
|
||||
import { INVOICE } from "../invoices/constants";
|
||||
import { Queue } from "bullmq";
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class BillsService {
|
||||
@@ -27,8 +30,9 @@ export class BillsService {
|
||||
private readonly em: EntityManager,
|
||||
private readonly companiesService: CompaniesService,
|
||||
private readonly billRepository: BillsRepository,
|
||||
@InjectQueue(INVOICE.QUEUE_NAME)
|
||||
private readonly invoiceQueue: Queue,
|
||||
private readonly notificationQueue: NotificationQueue,
|
||||
private readonly invoicesService: InvoicesService,
|
||||
|
||||
) { }
|
||||
|
||||
@@ -76,35 +80,14 @@ export class BillsService {
|
||||
});
|
||||
|
||||
invoice.items.add(invoiceItem);
|
||||
|
||||
await this.notificationQueue.addInvoiceCreationNotification(company.user.id, {
|
||||
invoiceId: invoice.numericId.toString(),
|
||||
dueDate: invoice.dueDate,
|
||||
createDate: invoice.createdAt,
|
||||
price: new Decimal(invoice.totalPrice).toNumber(),
|
||||
userPhone: company.user.phone,
|
||||
userEmail: company.user.email,
|
||||
items: invoiceItem.name,
|
||||
paidAt: invoice.paidAt,
|
||||
});
|
||||
|
||||
await this.invoicesService.scheduleInvoiceJobs(invoice, {
|
||||
companyId: company.id,
|
||||
items: [
|
||||
{
|
||||
name: invoiceItem.name,
|
||||
count: invoiceItem.count,
|
||||
unitPrice: new Decimal(invoiceItem.unitPrice).toNumber(),
|
||||
discount: new Decimal(invoiceItem.discount).toNumber(),
|
||||
},
|
||||
],
|
||||
});
|
||||
em.persist(invoice);
|
||||
}
|
||||
|
||||
await em.flush();
|
||||
await em.commit();
|
||||
|
||||
await this.addInvoiceReminderJobs(bill.id);
|
||||
|
||||
return bill;
|
||||
} catch (error) {
|
||||
await em.rollback();
|
||||
@@ -112,6 +95,34 @@ export class BillsService {
|
||||
}
|
||||
}
|
||||
|
||||
private async addInvoiceReminderJobs(billId: string) {
|
||||
const invoices = await this.em.find(Invoice, { bill: { id: billId } }, { populate: ["items", "company","company.user"] });
|
||||
|
||||
for (const invoice of invoices) {
|
||||
await this.notificationQueue.addInvoiceCreationNotification(invoice.company.user.id, {
|
||||
invoiceId: invoice.numericId.toString(),
|
||||
dueDate: invoice.dueDate,
|
||||
createDate: invoice.createdAt,
|
||||
price: new Decimal(invoice.totalPrice).toNumber(),
|
||||
userPhone: invoice.company.user.phone,
|
||||
userEmail: invoice.company.user.email,
|
||||
items: invoice.items[0].name,
|
||||
paidAt: invoice.paidAt,
|
||||
});
|
||||
|
||||
await this.invoiceQueue.add(
|
||||
INVOICE.REMINDER_JOB_NAME,
|
||||
{ invoiceId: invoice.id },
|
||||
{
|
||||
delay: dayjs(invoice.dueDate).subtract(INVOICE.DAYS_BEFORE_OVERDUE, "day").diff(dayjs()),
|
||||
attempts: INVOICE.REMINDER_JOB_ATTEMPTS,
|
||||
backoff: { type: "exponential", delay: INVOICE.REMINDER_JOB_BACKOFF },
|
||||
priority: INVOICE.REMINDER_JOB_PRIORITY,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async createWaterBillByExcel(file: IFile, dto: CreateWaterBillFromExcelDto, business: Business) {
|
||||
const values = await this.parseWaterBillExcel(file.buffer);
|
||||
if (values.length === 0) {
|
||||
@@ -155,7 +166,7 @@ export class BillsService {
|
||||
const companies = await this.companiesService.findAllBybusinessId(business.id, em);
|
||||
|
||||
for (const company of companies) {
|
||||
const subtotal = new Decimal(chargeRate);
|
||||
const subtotal = new Decimal(chargeRate).mul(company.metrage);
|
||||
const tax = subtotal.mul(0.1);
|
||||
const totalPrice = subtotal.add(tax);
|
||||
|
||||
@@ -185,6 +196,8 @@ export class BillsService {
|
||||
await em.flush();
|
||||
await em.commit();
|
||||
|
||||
await this.addInvoiceReminderJobs(bill.id)
|
||||
|
||||
return bill;
|
||||
} catch (error) {
|
||||
await em.rollback();
|
||||
|
||||
Reference in New Issue
Block a user