mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 06:10:31 +00:00
refactor: dynamic list to nestjs
This commit is contained in:
@@ -15,6 +15,8 @@ import { InventoryAdjustmentsGLSubscriber } from './subscribers/InventoryAdjustm
|
||||
import { InventoryAdjustmentsGLEntries } from './commands/ledger/InventoryAdjustmentsGLEntries';
|
||||
import { LedgerModule } from '../Ledger/Ledger.module';
|
||||
import { TenancyContext } from '../Tenancy/TenancyContext.service';
|
||||
import { InventoryAdjustmentInventoryTransactionsSubscriber } from './inventory/InventoryAdjustmentInventoryTransactionsSubscriber';
|
||||
import { InventoryAdjustmentInventoryTransactions } from './inventory/InventoryAdjustmentInventoryTransactions';
|
||||
|
||||
const models = [
|
||||
RegisterTenancyModel(InventoryAdjustment),
|
||||
@@ -34,6 +36,8 @@ const models = [
|
||||
InventoryAdjustmentsGLSubscriber,
|
||||
InventoryAdjustmentsGLEntries,
|
||||
TenancyContext,
|
||||
InventoryAdjustmentInventoryTransactionsSubscriber,
|
||||
InventoryAdjustmentInventoryTransactions
|
||||
],
|
||||
exports: [...models],
|
||||
})
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import { Knex } from "knex";
|
||||
import { InventoryAdjustment } from "../models/InventoryAdjustment";
|
||||
import { InventoryTransaction } from "@/modules/InventoryCost/models/InventoryTransaction";
|
||||
import { InventoryService } from "@/modules/InventoryCost/Inventory";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
|
||||
@Injectable()
|
||||
export class InventoryAdjustmentInventoryTransactions {
|
||||
constructor(
|
||||
private readonly inventoryService: InventoryService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Writes the inventory transactions from the inventory adjustment transaction.
|
||||
* @param {number} tenantId -
|
||||
* @param {IInventoryAdjustment} inventoryAdjustment -
|
||||
* @param {boolean} override -
|
||||
* @param {Knex.Transaction} trx -
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async writeInventoryTransactions(
|
||||
inventoryAdjustment: InventoryAdjustment,
|
||||
override: boolean = false,
|
||||
trx?: Knex.Transaction
|
||||
): Promise<void> {
|
||||
const commonTransaction = {
|
||||
direction: inventoryAdjustment.inventoryDirection,
|
||||
date: inventoryAdjustment.date,
|
||||
transactionType: 'InventoryAdjustment',
|
||||
transactionId: inventoryAdjustment.id,
|
||||
createdAt: inventoryAdjustment.createdAt,
|
||||
costAccountId: inventoryAdjustment.adjustmentAccountId,
|
||||
|
||||
branchId: inventoryAdjustment.branchId,
|
||||
warehouseId: inventoryAdjustment.warehouseId,
|
||||
};
|
||||
const inventoryTransactions = [];
|
||||
|
||||
inventoryAdjustment.entries.forEach((entry) => {
|
||||
inventoryTransactions.push({
|
||||
...commonTransaction,
|
||||
itemId: entry.itemId,
|
||||
quantity: entry.quantity,
|
||||
rate: entry.cost,
|
||||
});
|
||||
});
|
||||
// Saves the given inventory transactions to the storage.
|
||||
await this.inventoryService.recordInventoryTransactions(
|
||||
inventoryTransactions,
|
||||
override,
|
||||
trx
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverts the inventory transactions from the inventory adjustment transaction.
|
||||
* @param {number} inventoryAdjustmentId
|
||||
*/
|
||||
async revertInventoryTransactions(
|
||||
inventoryAdjustmentId: number,
|
||||
trx?: Knex.Transaction
|
||||
): Promise<{ oldInventoryTransactions: InventoryTransaction[] }> {
|
||||
return this.inventoryService.deleteInventoryTransactions(
|
||||
inventoryAdjustmentId,
|
||||
'InventoryAdjustment',
|
||||
trx
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import {
|
||||
IInventoryAdjustmentEventCreatedPayload,
|
||||
IInventoryAdjustmentEventPublishedPayload,
|
||||
} from '../types/InventoryAdjustments.types';
|
||||
import { IInventoryAdjustmentEventDeletedPayload } from '../types/InventoryAdjustments.types';
|
||||
import { InventoryAdjustmentInventoryTransactions } from './InventoryAdjustmentInventoryTransactions';
|
||||
import { events } from '@/common/events/events';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
|
||||
@Injectable()
|
||||
export class InventoryAdjustmentInventoryTransactionsSubscriber {
|
||||
constructor(
|
||||
private readonly inventoryTransactions: InventoryAdjustmentInventoryTransactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Handles writing inventory transactions once the quick adjustment created.
|
||||
* @param {IInventoryAdjustmentEventPublishedPayload} payload
|
||||
* @param {IInventoryAdjustmentEventCreatedPayload} payload -
|
||||
*/
|
||||
@OnEvent(events.inventoryAdjustment.onQuickCreated)
|
||||
public async handleWriteInventoryTransactionsOncePublished({
|
||||
inventoryAdjustment,
|
||||
trx,
|
||||
}:
|
||||
| IInventoryAdjustmentEventPublishedPayload
|
||||
| IInventoryAdjustmentEventCreatedPayload) {
|
||||
await this.inventoryTransactions.writeInventoryTransactions(
|
||||
inventoryAdjustment,
|
||||
false,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles reverting invetory transactions once the inventory adjustment deleted.
|
||||
* @param {IInventoryAdjustmentEventDeletedPayload} payload -
|
||||
*/
|
||||
@OnEvent(events.inventoryAdjustment.onDeleted)
|
||||
public async handleRevertInventoryTransactionsOnceDeleted({
|
||||
inventoryAdjustmentId,
|
||||
oldInventoryAdjustment,
|
||||
trx,
|
||||
}: IInventoryAdjustmentEventDeletedPayload) {
|
||||
// Can't continue if the inventory adjustment is not published.
|
||||
if (!oldInventoryAdjustment.isPublished) {
|
||||
return;
|
||||
}
|
||||
// Reverts the inventory transactions of adjustment transaction.
|
||||
await this.inventoryTransactions.revertInventoryTransactions(
|
||||
inventoryAdjustmentId,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user