fix(server): shouldn't write GL entries when save transaction as draft.

This commit is contained in:
Ahmed Bouhuolia
2023-08-16 23:05:39 +02:00
parent 01f7effc71
commit 5b2be2ac19
16 changed files with 122 additions and 18 deletions

View File

@@ -20,6 +20,10 @@ export class BillGLEntriesSubscriber {
events.bill.onCreated,
this.handlerWriteJournalEntriesOnCreate
);
bus.subscribe(
events.bill.onOpened,
this.handlerWriteJournalEntriesOnCreate
);
bus.subscribe(
events.bill.onEdited,
this.handleOverwriteJournalEntriesOnEdit
@@ -34,8 +38,11 @@ export class BillGLEntriesSubscriber {
private handlerWriteJournalEntriesOnCreate = async ({
tenantId,
billId,
bill,
trx,
}: IBillCreatedPayload) => {
if (!bill.openedAt) return null;
await this.billGLEntries.writeBillGLEntries(tenantId, billId, trx);
};
@@ -46,8 +53,11 @@ export class BillGLEntriesSubscriber {
private handleOverwriteJournalEntriesOnEdit = async ({
tenantId,
billId,
bill,
trx,
}: IBillEditedPayload) => {
if (!bill.openedAt) return null;
await this.billGLEntries.rewriteBillGLEntries(tenantId, billId, trx);
};

View File

@@ -132,7 +132,7 @@ export class EditBill {
} as IBillEditingPayload);
// Update the bill transaction.
const bill = await Bill.query(trx).upsertGraph({
const bill = await Bill.query(trx).upsertGraphAndFetch({
id: billId,
...billObj,
});

View File

@@ -5,6 +5,9 @@ import { ERRORS } from './constants';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import UnitOfWork from '@/services/UnitOfWork';
import { BillsValidators } from './BillsValidators';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
import { IBillOpenedPayload, IBillOpeningPayload } from '@/interfaces';
@Service()
export class OpenBill {
@@ -17,6 +20,9 @@ export class OpenBill {
@Inject()
private validators: BillsValidators;
@Inject()
private eventPublisher: EventPublisher;
/**
* Mark the bill as open.
* @param {number} tenantId
@@ -37,10 +43,24 @@ export class OpenBill {
throw new ServiceError(ERRORS.BILL_ALREADY_OPEN);
}
return this.uow.withTransaction(tenantId, async (trx) => {
// Triggers `onBillCreating` event.
await this.eventPublisher.emitAsync(events.bill.onOpening, {
trx,
tenantId,
oldBill,
} as IBillOpeningPayload);
// Record the bill opened at on the storage.
await Bill.query(trx).findById(billId).patch({
const bill = await Bill.query(trx).patchAndFetchById(billId, {
openedAt: moment().toMySqlDateTime(),
});
// Triggers `onBillCreating` event.
await this.eventPublisher.emitAsync(events.bill.onOpened, {
trx,
bill,
oldBill,
tenantId,
} as IBillOpenedPayload);
});
}
}

View File

@@ -63,14 +63,17 @@ export class DeliverSaleInvoice {
// Record the delivered at on the storage.
const saleInvoice = await SaleInvoice.query(trx)
.where({ id: saleInvoiceId })
.update({ deliveredAt: moment().toMySqlDateTime() });
.patchAndFetchById(saleInvoiceId, {
deliveredAt: moment().toMySqlDateTime(),
})
.withGraphFetched('entries');
// Triggers `onSaleInvoiceDelivered` event.
await this.eventPublisher.emitAsync(events.saleInvoice.onDelivered, {
tenantId,
saleInvoiceId,
saleInvoice,
trx,
} as ISaleInvoiceEventDeliveredPayload);
});
}

View File

@@ -58,12 +58,12 @@ export class CloseSaleReceipt {
} as ISaleReceiptEventClosingPayload);
// Mark the sale receipt as closed on the storage.
const saleReceipt = await SaleReceipt.query(trx)
.findById(saleReceiptId)
.patch({
const saleReceipt = await SaleReceipt.query(trx).patchAndFetchById(
saleReceiptId,
{
closedAt: moment().toMySqlDateTime(),
});
}
);
// Triggers `onSaleReceiptClosed` event.
await this.eventPublisher.emitAsync(events.saleReceipt.onClosed, {
saleReceiptId,