fix: store opening quantity, cost and date in item inventory type only.

This commit is contained in:
a.bouhuolia
2021-01-02 16:42:21 +02:00
parent ed4c49883c
commit 29bf723f9b

View File

@@ -1,4 +1,4 @@
import { defaultTo, difference, omit } from 'lodash'; import { defaultTo, difference, omit, pick } from 'lodash';
import { Service, Inject } from 'typedi'; import { Service, Inject } from 'typedi';
import { import {
EventDispatcher, EventDispatcher,
@@ -229,6 +229,25 @@ export default class ItemsService implements IItemsService {
} }
} }
/**
* Transforms the item DTO to model.
* @param {IItemDTO} itemDTO - Item DTO.
* @return {IItem}
*/
private transformNewItemDTOToModel(itemDTO: IItemDTO) {
const inventoryAttrs = ['openingQuantity', 'openingCost', 'openingDate'];
return {
...omit(itemDTO, inventoryAttrs),
...(itemDTO.type === 'inventory' ? pick(itemDTO, inventoryAttrs) : {}),
active: defaultTo(itemDTO.active, 1),
quantityOnHand:
itemDTO.type === 'inventory'
? defaultTo(itemDTO.openingQuantity, 0)
: null,
};
}
/** /**
* Creates a new item. * Creates a new item.
* @param {number} tenantId DTO * @param {number} tenantId DTO
@@ -263,12 +282,7 @@ export default class ItemsService implements IItemsService {
); );
} }
const item = await Item.query().insertAndFetch({ const item = await Item.query().insertAndFetch({
...itemDTO, ...this.transformNewItemDTOToModel(itemDTO)
active: defaultTo(itemDTO.active, 1),
quantityOnHand:
itemDTO.type === 'inventory'
? defaultTo(itemDTO.openingQuantity, 0)
: null,
}); });
this.logger.info('[items] item inserted successfully.', { this.logger.info('[items] item inserted successfully.', {
tenantId, tenantId,
@@ -469,7 +483,7 @@ export default class ItemsService implements IItemsService {
*/ */
private async validateItemsIdsExists( private async validateItemsIdsExists(
tenantId: number, tenantId: number,
itemsIDs: number[], itemsIDs: number[]
): Promise<void> { ): Promise<void> {
const { Item } = this.tenancy.models(tenantId); const { Item } = this.tenancy.models(tenantId);