feat(items): item type can not changing in inventory type.

This commit is contained in:
a.bouhuolia
2021-03-03 14:25:16 +02:00
parent 9fef91b965
commit 4f98db4c4b
3 changed files with 47 additions and 2 deletions

View File

@@ -227,6 +227,37 @@ export default class ItemsService implements IItemsService {
};
}
/**
*
* @param {IItemDTO} itemDTO - Item DTO.
* @param {IItem} oldItem -
*/
private transformEditItemDTOToModel(itemDTO: IItemDTO, oldItem: IItem) {
return {
...itemDTO,
...(itemDTO.type === 'inventory' && oldItem.type !== 'inventory'
? {
quantityOnHand: 0,
}
: {}),
};
}
/**
* Validate item type in edit item mode, cannot change item inventory type.
* @param {IItemDTO} itemDTO
* @param {IItem} oldItem
*/
private validateEditItemInventoryType(itemDTO: IItemDTO, oldItem: IItem) {
if (
itemDTO.type &&
oldItem.type === 'inventory' &&
itemDTO.type !== oldItem.type
) {
throw new ServiceError(ERRORS.ITEM_CANNOT_CHANGE_INVENTORY_TYPE);
}
}
/**
* Creates a new item.
* @param {number} tenantId DTO
@@ -288,6 +319,11 @@ export default class ItemsService implements IItemsService {
// Validates the given item existance on the storage.
const oldItem = await this.getItemOrThrowError(tenantId, itemId);
this.validateEditItemInventoryType(itemDTO, oldItem);
// Transform the edit item DTO to model.
const itemModel = this.transformEditItemDTOToModel(itemDTO, oldItem);
// Validate whether the given item name already exists on the storage.
await this.validateItemNameUniquiness(tenantId, itemDTO.name, itemId);
@@ -318,7 +354,7 @@ export default class ItemsService implements IItemsService {
}
const newItem = await Item.query().patchAndFetchById(itemId, {
...itemDTO,
...itemModel,
});
this.logger.info('[items] item edited successfully.', {
tenantId,

View File

@@ -1,4 +1,3 @@
export const ERRORS = {
NOT_FOUND: 'NOT_FOUND',
ITEMS_NOT_FOUND: 'ITEMS_NOT_FOUND',
@@ -18,4 +17,5 @@ export const ERRORS = {
ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT:
'ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT',
ITEM_CANNOT_CHANGE_INVENTORY_TYPE: 'ITEM_CANNOT_CHANGE_INVENTORY_TYPE',
};