From 8b90ce5f6cbe7b8206541b3019329e81922a8b06 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Wed, 21 Aug 2024 19:32:59 +0200 Subject: [PATCH] fix: cannot import items income and cost accounts --- packages/server/src/models/Item.Settings.ts | 8 ++--- .../server/src/services/Items/CreateItem.ts | 10 ++++++ .../server/src/services/Items/EditItem.ts | 10 ++++++ .../src/services/Items/ItemValidators.ts | 36 +++++++++++++++++++ .../server/src/services/Items/constants.ts | 5 +++ 5 files changed, 65 insertions(+), 4 deletions(-) diff --git a/packages/server/src/models/Item.Settings.ts b/packages/server/src/models/Item.Settings.ts index af8968366..2c88be0d5 100644 --- a/packages/server/src/models/Item.Settings.ts +++ b/packages/server/src/models/Item.Settings.ts @@ -257,25 +257,25 @@ export default { name: 'item.field.sell_price', fieldType: 'number', }, - cost_price: { + costPrice: { name: 'item.field.cost_price', fieldType: 'number', }, - costAccount: { + costAccountId: { name: 'item.field.cost_account', fieldType: 'relation', relationModel: 'Account', relationImportMatch: ['name', 'code'], importHint: 'Matches the account name or code.', }, - sellAccount: { + sellAccountId: { name: 'item.field.sell_account', fieldType: 'relation', relationModel: 'Account', relationImportMatch: ['name', 'code'], importHint: 'Matches the account name or code.', }, - inventoryAccount: { + inventoryAccountId: { name: 'item.field.inventory_account', fieldType: 'relation', relationModel: 'Account', diff --git a/packages/server/src/services/Items/CreateItem.ts b/packages/server/src/services/Items/CreateItem.ts index 5de01aec9..3ce935ee3 100644 --- a/packages/server/src/services/Items/CreateItem.ts +++ b/packages/server/src/services/Items/CreateItem.ts @@ -43,12 +43,22 @@ export class CreateItem { itemDTO.sellAccountId ); } + // Validate the income account id existance if the item is sellable. + this.validators.validateIncomeAccountExistance( + itemDTO.sellable, + itemDTO.sellAccountId + ); if (itemDTO.costAccountId) { await this.validators.validateItemCostAccountExistance( tenantId, itemDTO.costAccountId ); } + // Validate the cost account id existance if the item is purchasable. + this.validators.validateCostAccountExistance( + itemDTO.purchasable, + itemDTO.costAccountId + ); if (itemDTO.inventoryAccountId) { await this.validators.validateItemInventoryAccountExistance( tenantId, diff --git a/packages/server/src/services/Items/EditItem.ts b/packages/server/src/services/Items/EditItem.ts index 6e230921f..17ac8240e 100644 --- a/packages/server/src/services/Items/EditItem.ts +++ b/packages/server/src/services/Items/EditItem.ts @@ -55,6 +55,11 @@ export class EditItem { itemDTO.categoryId ); } + // Validate the income account id existance if the item is sellable. + this.validators.validateIncomeAccountExistance( + itemDTO.sellable, + itemDTO.sellAccountId + ); // Validate the sell account existance on the storage. if (itemDTO.sellAccountId) { await this.validators.validateItemSellAccountExistance( @@ -62,6 +67,11 @@ export class EditItem { itemDTO.sellAccountId ); } + // Validate the cost account id existance if the item is purchasable. + this.validators.validateCostAccountExistance( + itemDTO.purchasable, + itemDTO.costAccountId + ); // Validate the cost account existance on the storage. if (itemDTO.costAccountId) { await this.validators.validateItemCostAccountExistance( diff --git a/packages/server/src/services/Items/ItemValidators.ts b/packages/server/src/services/Items/ItemValidators.ts index c81ac9c7e..3468ad1d0 100644 --- a/packages/server/src/services/Items/ItemValidators.ts +++ b/packages/server/src/services/Items/ItemValidators.ts @@ -85,6 +85,42 @@ export class ItemsValidators { } } + /** + * Validates income account existance. + * @param {number|null} sellable - Detarmines if the item sellable. + * @param {number|null} incomeAccountId - Income account id. + * @throws {ServiceError(ERRORS.INCOME_ACCOUNT_REQUIRED_WITH_SELLABLE_ITEM)} + */ + public validateIncomeAccountExistance( + sellable?: boolean, + incomeAccountId?: number + ) { + if (sellable && !incomeAccountId) { + throw new ServiceError( + ERRORS.INCOME_ACCOUNT_REQUIRED_WITH_SELLABLE_ITEM, + 'Income account is require with sellable item.' + ); + } + } + + /** + * Validates the cost account existance. + * @param {boolean|null} purchasable - Detarmines if the item purchasble. + * @param {number|null} costAccountId - Cost account id. + * @throws {ServiceError(ERRORS.COST_ACCOUNT_REQUIRED_WITH_PURCHASABLE_ITEM)} + */ + public validateCostAccountExistance( + purchasable: boolean, + costAccountId?: number + ) { + if (purchasable && !costAccountId) { + throw new ServiceError( + ERRORS.COST_ACCOUNT_REQUIRED_WITH_PURCHASABLE_ITEM, + 'The cost account is required with purchasable item.' + ); + } + } + /** * Validate item inventory account existance and type. * @param {number} tenantId diff --git a/packages/server/src/services/Items/constants.ts b/packages/server/src/services/Items/constants.ts index 3d571c527..9b4089687 100644 --- a/packages/server/src/services/Items/constants.ts +++ b/packages/server/src/services/Items/constants.ts @@ -26,6 +26,11 @@ export const ERRORS = { PURCHASE_TAX_RATE_NOT_FOUND: 'PURCHASE_TAX_RATE_NOT_FOUND', SELL_TAX_RATE_NOT_FOUND: 'SELL_TAX_RATE_NOT_FOUND', + + INCOME_ACCOUNT_REQUIRED_WITH_SELLABLE_ITEM: + 'INCOME_ACCOUNT_REQUIRED_WITH_SELLABLE_ITEM', + COST_ACCOUNT_REQUIRED_WITH_PURCHASABLE_ITEM: + 'COST_ACCOUNT_REQUIRED_WITH_PURCHASABLE_ITEM', }; export const DEFAULT_VIEW_COLUMNS = [];