mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 06:40:31 +00:00
feat(items): item type can not changing in inventory type.
This commit is contained in:
@@ -538,6 +538,15 @@ export default class ItemsController extends BaseController {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (error.errorType === 'ITEM_CANNOT_CHANGE_INVENTORY_TYPE') {
|
||||||
|
return res.status(400).send({
|
||||||
|
errors: [{
|
||||||
|
type: 'ITEM_CANNOT_CHANGE_INVENTORY_TYPE',
|
||||||
|
message: 'Cannot change inventory item type',
|
||||||
|
code: 340,
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.
|
* Creates a new item.
|
||||||
* @param {number} tenantId DTO
|
* @param {number} tenantId DTO
|
||||||
@@ -288,6 +319,11 @@ export default class ItemsService implements IItemsService {
|
|||||||
// Validates the given item existance on the storage.
|
// Validates the given item existance on the storage.
|
||||||
const oldItem = await this.getItemOrThrowError(tenantId, itemId);
|
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.
|
// Validate whether the given item name already exists on the storage.
|
||||||
await this.validateItemNameUniquiness(tenantId, itemDTO.name, itemId);
|
await this.validateItemNameUniquiness(tenantId, itemDTO.name, itemId);
|
||||||
|
|
||||||
@@ -318,7 +354,7 @@ export default class ItemsService implements IItemsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const newItem = await Item.query().patchAndFetchById(itemId, {
|
const newItem = await Item.query().patchAndFetchById(itemId, {
|
||||||
...itemDTO,
|
...itemModel,
|
||||||
});
|
});
|
||||||
this.logger.info('[items] item edited successfully.', {
|
this.logger.info('[items] item edited successfully.', {
|
||||||
tenantId,
|
tenantId,
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
export const ERRORS = {
|
export const ERRORS = {
|
||||||
NOT_FOUND: 'NOT_FOUND',
|
NOT_FOUND: 'NOT_FOUND',
|
||||||
ITEMS_NOT_FOUND: 'ITEMS_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_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT',
|
'ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT',
|
||||||
|
ITEM_CANNOT_CHANGE_INVENTORY_TYPE: 'ITEM_CANNOT_CHANGE_INVENTORY_TYPE',
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user