fix(server): wirte GL entries only when publish transaction

This commit is contained in:
Ahmed Bouhuolia
2023-08-17 21:49:07 +02:00
parent 5b2be2ac19
commit 0fcee0eaa7
13 changed files with 65 additions and 43 deletions

View File

@@ -4,6 +4,7 @@ import {
IBillCreatedPayload,
IBillEditedPayload,
IBIllEventDeletedPayload,
IBillOpenedPayload,
} from '@/interfaces';
import { BillGLEntries } from './BillGLEntries';
@@ -37,13 +38,12 @@ export class BillGLEntriesSubscriber {
*/
private handlerWriteJournalEntriesOnCreate = async ({
tenantId,
billId,
bill,
trx,
}: IBillCreatedPayload) => {
}: IBillCreatedPayload | IBillOpenedPayload) => {
if (!bill.openedAt) return null;
await this.billGLEntries.writeBillGLEntries(tenantId, billId, trx);
await this.billGLEntries.writeBillGLEntries(tenantId, bill.id, trx);
};
/**

View File

@@ -50,10 +50,13 @@ export class OpenBill {
oldBill,
} as IBillOpeningPayload);
// Record the bill opened at on the storage.
const bill = await Bill.query(trx).patchAndFetchById(billId, {
openedAt: moment().toMySqlDateTime(),
});
// Save the bill opened at on the storage.
const bill = await Bill.query(trx)
.patchAndFetchById(billId, {
openedAt: moment().toMySqlDateTime(),
})
.withGraphFetched('entries');
// Triggers `onBillCreating` event.
await this.eventPublisher.emitAsync(events.bill.onOpened, {
trx,

View File

@@ -10,7 +10,7 @@ import VendorCreditInventoryTransactions from './VendorCreditInventoryTransactio
@Service()
export default class VendorCreditInventoryTransactionsSubscriber {
@Inject()
inventoryTransactions: VendorCreditInventoryTransactions;
private inventoryTransactions: VendorCreditInventoryTransactions;
/**
* Attaches events with handlers.
@@ -21,6 +21,10 @@ export default class VendorCreditInventoryTransactionsSubscriber {
events.vendorCredit.onCreated,
this.writeInventoryTransactionsOnceCreated
);
bus.subscribe(
events.vendorCredit.onOpened,
this.writeInventoryTransactionsOnceCreated
);
bus.subscribe(
events.vendorCredit.onEdited,
this.rewriteInventroyTransactionsOnceEdited
@@ -40,6 +44,9 @@ export default class VendorCreditInventoryTransactionsSubscriber {
vendorCredit,
trx,
}: IVendorCreditCreatedPayload) => {
// Can't continue if vendor credit is not opened.
if (!vendorCredit.openedAt) return null;
await this.inventoryTransactions.createInventoryTransactions(
tenantId,
vendorCredit,
@@ -57,6 +64,9 @@ export default class VendorCreditInventoryTransactionsSubscriber {
vendorCredit,
trx,
}: IVendorCreditEditedPayload) => {
// Can't continue if vendor credit is not opened.
if (!vendorCredit.openedAt) return null;
await this.inventoryTransactions.editInventoryTransactions(
tenantId,
vendorCreditId,