From 149464fa7a0b0ef0b65f14a45c6c84f813ed5071 Mon Sep 17 00:00:00 2001 From: "a.bouhuolia" Date: Sat, 2 Jan 2021 15:13:18 +0200 Subject: [PATCH] fix: validate the not found items in delete items/item. --- server/src/api/controllers/Items.ts | 5 +++++ server/src/services/Items/ItemsService.ts | 23 +++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/server/src/api/controllers/Items.ts b/server/src/api/controllers/Items.ts index 88b480dc0..96cdc4ab4 100644 --- a/server/src/api/controllers/Items.ts +++ b/server/src/api/controllers/Items.ts @@ -428,6 +428,11 @@ export default class ItemsController extends BaseController { 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') { return res.status(400).send({ errors: [{ type: 'ITEM_CATEGORY.NOT.FOUND', code: 140 }], diff --git a/server/src/services/Items/ItemsService.ts b/server/src/services/Items/ItemsService.ts index b922b67aa..bec041acd 100644 --- a/server/src/services/Items/ItemsService.ts +++ b/server/src/services/Items/ItemsService.ts @@ -13,6 +13,8 @@ import InventoryService from 'services/Inventory/Inventory'; const ERRORS = { NOT_FOUND: 'NOT_FOUND', + ITEMS_NOT_FOUND: 'ITEMS_NOT_FOUND', + ITEM_NAME_EXISTS: 'ITEM_NAME_EXISTS', ITEM_CATEOGRY_NOT_FOUND: 'ITEM_CATEOGRY_NOT_FOUND', 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. - * @param {Array} itemsIDs - * @return {Array} + * Validates the given items IDs exists or throw not found service error. + * @param {number} tenantId - + * @param {number[]} itemsIDs - + * @return {Promise} */ - private async validateItemsIdsExists(tenantId: number, itemsIDs: number[]) { + private async validateItemsIdsExists( + tenantId: number, + itemsIDs: number[], + ): Promise { const { Item } = this.tenancy.models(tenantId); const storedItems = await Item.query().whereIn('id', itemsIDs); const storedItemsIds = storedItems.map((t: IItem) => t.id); 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, itemsIds, }); - /// Validates the given items exist on the storage. + // Validates the given items exist on the storage. await this.validateItemsIdsExists(tenantId, itemsIds); // Validate the items have no associated invoices or bills.