fix: sync inventory items quantity with services.

This commit is contained in:
a.bouhuolia
2021-02-28 14:45:13 +02:00
parent 0fd5eb34c6
commit fb9d68c2cf
9 changed files with 147 additions and 210 deletions

View File

@@ -183,7 +183,7 @@ export default class InventoryAdjustmentService {
inventoryAdjustmentId: number
): Promise<void> {
// Retrieve the inventory adjustment or throw not found service error.
const adjustment = await this.getInventoryAdjustmentOrThrowError(
const oldInventoryAdjustment = await this.getInventoryAdjustmentOrThrowError(
tenantId,
inventoryAdjustmentId
);
@@ -208,6 +208,7 @@ export default class InventoryAdjustmentService {
await this.eventDispatcher.dispatch(events.inventoryAdjustment.onDeleted, {
tenantId,
inventoryAdjustmentId,
oldInventoryAdjustment
});
this.logger.info(
'[inventory_adjustment] the adjustment deleted successfully.',
@@ -317,7 +318,7 @@ export default class InventoryAdjustmentService {
});
});
// Saves the given inventory transactions to the storage.
this.inventoryService.recordInventoryTransactions(
await this.inventoryService.recordInventoryTransactions(
tenantId,
inventoryTransactions,
override

View File

@@ -0,0 +1,92 @@
import { Inject, Service } from 'typedi';
import { toSafeInteger } from 'lodash';
import { IInventoryTransaction, IItemsQuantityChanges } from 'interfaces';
import HasTenancyService from 'services/Tenancy/TenancyService';
/**
* Syncs the inventory transactions with inventory items quantity.
*/
@Service()
export default class InventoryItemsQuantitySync {
@Inject()
tenancy: HasTenancyService;
/**
* Reverse the given inventory transactions.
* @param {IInventoryTransaction[]} inventroyTransactions
* @return {IInventoryTransaction[]}
*/
reverseInventoryTransactions(
inventroyTransactions: IInventoryTransaction[]
): IInventoryTransaction[] {
return inventroyTransactions.map((transaction) => ({
...transaction,
direction: transaction.direction === 'OUT' ? 'IN' : 'OUT',
}));
}
/**
* Reverses the inventory transactions.
* @param {IInventoryTransaction[]} inventroyTransactions -
* @return {IItemsQuantityChanges[]}
*/
getReverseItemsQuantityChanges(
inventroyTransactions: IInventoryTransaction[]
): IItemsQuantityChanges[] {
const reversedTransactions = this.reverseInventoryTransactions(
inventroyTransactions
);
return this.getItemsQuantityChanges(reversedTransactions);
}
/**
* Retrieve the items quantity changes from the given inventory transactions.
* @param {IInventoryTransaction[]} inventroyTransactions - Inventory transactions.
* @return {IItemsQuantityChanges[]}
*/
getItemsQuantityChanges(
inventroyTransactions: IInventoryTransaction[]
): IItemsQuantityChanges[] {
const balanceMap: { [itemId: number]: number } = {};
inventroyTransactions.forEach(
(inventoryTransaction: IInventoryTransaction) => {
const { itemId, direction, quantity } = inventoryTransaction;
if (!balanceMap[itemId]) {
balanceMap[itemId] = 0;
}
balanceMap[itemId] += direction === 'IN' ? quantity : 0;
balanceMap[itemId] -= direction === 'OUT' ? quantity : 0;
}
);
return Object.entries(balanceMap).map(([itemId, balanceChange]) => ({
itemId: toSafeInteger(itemId),
balanceChange,
}));
}
/**
* Changes the items quantity changes.
* @param {IItemsQuantityChanges[]} itemsQuantity - Items quantity changes.
* @return {Promise<void>}
*/
async changeItemsQuantity(
tenantId: number,
itemsQuantity: IItemsQuantityChanges[]
): Promise<void> {
const { itemRepository } = this.tenancy.repositories(tenantId);
const opers = [];
itemsQuantity.forEach((itemQuantity: IItemsQuantityChanges) => {
const changeQuantityOper = itemRepository.changeNumber(
{ id: itemQuantity.itemId, type: 'inventory' },
'quantityOnHand',
itemQuantity.balanceChange
);
opers.push(changeQuantityOper);
});
await Promise.all(opers);
}
}