This commit is contained in:
elforjani3
2021-01-02 15:36:18 +02:00
2 changed files with 22 additions and 6 deletions

View File

@@ -428,6 +428,11 @@ export default class ItemsController extends BaseController {
errors: [{ type: 'ITEM.NOT.FOUND', code: 140 }], errors: [{ type: 'ITEM.NOT.FOUND', code: 140 }],
}); });
} }
if (error.errorType === 'ITEMS_NOT_FOUND') {
return res.status(400).send({
errors: [{ type: 'ITEMS_NOT_FOUND', code: 130 }],
});
}
if (error.errorType === 'ITEM_CATEOGRY_NOT_FOUND') { if (error.errorType === 'ITEM_CATEOGRY_NOT_FOUND') {
return res.status(400).send({ return res.status(400).send({
errors: [{ type: 'ITEM_CATEGORY.NOT.FOUND', code: 140 }], errors: [{ type: 'ITEM_CATEGORY.NOT.FOUND', code: 140 }],

View File

@@ -13,6 +13,8 @@ import InventoryService from 'services/Inventory/Inventory';
const ERRORS = { const ERRORS = {
NOT_FOUND: 'NOT_FOUND', NOT_FOUND: 'NOT_FOUND',
ITEMS_NOT_FOUND: 'ITEMS_NOT_FOUND',
ITEM_NAME_EXISTS: 'ITEM_NAME_EXISTS', ITEM_NAME_EXISTS: 'ITEM_NAME_EXISTS',
ITEM_CATEOGRY_NOT_FOUND: 'ITEM_CATEOGRY_NOT_FOUND', ITEM_CATEOGRY_NOT_FOUND: 'ITEM_CATEOGRY_NOT_FOUND',
COST_ACCOUNT_NOT_COGS: 'COST_ACCOUNT_NOT_COGS', COST_ACCOUNT_NOT_COGS: 'COST_ACCOUNT_NOT_COGS',
@@ -457,18 +459,27 @@ export default class ItemsService implements IItemsService {
} }
/** /**
* Validates the given items IDs exists or not returns the not found ones. * Validates the given items IDs exists or throw not found service error.
* @param {Array} itemsIDs * @param {number} tenantId -
* @return {Array} * @param {number[]} itemsIDs -
* @return {Promise<void>}
*/ */
private async validateItemsIdsExists(tenantId: number, itemsIDs: number[]) { private async validateItemsIdsExists(
tenantId: number,
itemsIDs: number[],
): Promise<void> {
const { Item } = this.tenancy.models(tenantId); const { Item } = this.tenancy.models(tenantId);
const storedItems = await Item.query().whereIn('id', itemsIDs); const storedItems = await Item.query().whereIn('id', itemsIDs);
const storedItemsIds = storedItems.map((t: IItem) => t.id); const storedItemsIds = storedItems.map((t: IItem) => t.id);
const notFoundItemsIds = difference(itemsIDs, storedItemsIds); const notFoundItemsIds = difference(itemsIDs, storedItemsIds);
return notFoundItemsIds;
if (notFoundItemsIds.length > 0) {
throw new ServiceError(ERRORS.ITEMS_NOT_FOUND, null, {
notFoundItemsIds,
});
}
} }
/** /**
@@ -483,7 +494,7 @@ export default class ItemsService implements IItemsService {
tenantId, tenantId,
itemsIds, itemsIds,
}); });
/// Validates the given items exist on the storage. // Validates the given items exist on the storage.
await this.validateItemsIdsExists(tenantId, itemsIds); await this.validateItemsIdsExists(tenantId, itemsIds);
// Validate the items have no associated invoices or bills. // Validate the items have no associated invoices or bills.