feat: item categories events.

This commit is contained in:
a.bouhuolia
2021-01-02 11:07:42 +02:00
parent 871ca5a569
commit 717747981e
3 changed files with 266 additions and 87 deletions

View File

@@ -147,7 +147,10 @@ export default class ItemsCategoriesController extends BaseController {
itemCategoryOTD, itemCategoryOTD,
user user
); );
return res.status(200).send({ id: itemCategory.id }); return res.status(200).send({
id: itemCategory.id,
message: 'The item category has been created successfully.',
});
} catch (error) { } catch (error) {
next(error); next(error);
} }
@@ -171,7 +174,10 @@ export default class ItemsCategoriesController extends BaseController {
itemCategoryOTD, itemCategoryOTD,
user user
); );
return res.status(200).send({ id: itemCategoryId }); return res.status(200).send({
id: itemCategoryId,
message: 'The item category has been edited successfully.',
});
} catch (error) { } catch (error) {
next(error); next(error);
} }
@@ -193,7 +199,10 @@ export default class ItemsCategoriesController extends BaseController {
itemCategoryId, itemCategoryId,
user user
); );
return res.status(200).send({ id: itemCategoryId }); return res.status(200).send({
id: itemCategoryId,
message: 'The item category has been deleted successfully.',
});
} catch (error) { } catch (error) {
next(error); next(error);
} }
@@ -270,7 +279,10 @@ export default class ItemsCategoriesController extends BaseController {
itemCategoriesIds, itemCategoriesIds,
user user
); );
return res.status(200).send({ ids: itemCategoriesIds }); return res.status(200).send({
ids: itemCategoriesIds,
message: 'The item categories have been deleted successfully.',
});
} catch (error) { } catch (error) {
next(error); next(error);
} }
@@ -297,37 +309,42 @@ export default class ItemsCategoriesController extends BaseController {
} }
if (error.errorType === 'ITEM_CATEGORIES_NOT_FOUND') { if (error.errorType === 'ITEM_CATEGORIES_NOT_FOUND') {
return res.boom.badRequest(null, { return res.boom.badRequest(null, {
errors: [{ type: 'ITEM_CATEGORIES_NOT_FOUND', code: 120 }], errors: [{ type: 'ITEM_CATEGORIES_NOT_FOUND', code: 200 }],
});
}
if (error.errorType === 'CATEGORY_NAME_EXISTS') {
return res.boom.badRequest(null, {
errors: [{ type: 'CATEGORY_NAME_EXISTS', code: 300 }],
}); });
} }
if (error.errorType === 'COST_ACCOUNT_NOT_FOUMD') { if (error.errorType === 'COST_ACCOUNT_NOT_FOUMD') {
return res.boom.badRequest(null, { return res.boom.badRequest(null, {
errors: [{ type: 'COST.ACCOUNT.NOT.FOUND', code: 120 }], errors: [{ type: 'COST.ACCOUNT.NOT.FOUND', code: 400 }],
}); });
} }
if (error.errorType === 'COST_ACCOUNT_NOT_COGS') { if (error.errorType === 'COST_ACCOUNT_NOT_COGS') {
return res.boom.badRequest(null, { return res.boom.badRequest(null, {
errors: [{ type: 'COST.ACCOUNT.NOT.COGS.TYPE', code: 220 }], errors: [{ type: 'COST.ACCOUNT.NOT.COGS.TYPE', code: 500 }],
}); });
} }
if (error.errorType === 'SELL_ACCOUNT_NOT_INCOME') { if (error.errorType === 'SELL_ACCOUNT_NOT_INCOME') {
return res.boom.badRequest(null, { return res.boom.badRequest(null, {
errors: [{ type: 'SELL.ACCOUNT.NOT.FOUND', code: 130 }], errors: [{ type: 'SELL.ACCOUNT.NOT.FOUND', code: 600 }],
}); });
} }
if (error.errorType === 'SELL_ACCOUNT_NOT_FOUND') { if (error.errorType === 'SELL_ACCOUNT_NOT_FOUND') {
return res.boom.badRequest(null, { return res.boom.badRequest(null, {
errors: [{ type: 'SELL.ACCOUNT.NOT.INCOME.TYPE', code: 230 }], errors: [{ type: 'SELL.ACCOUNT.NOT.INCOME.TYPE', code: 700 }],
}); });
} }
if (error.errorType === 'INVENTORY_ACCOUNT_NOT_FOUND') { if (error.errorType === 'INVENTORY_ACCOUNT_NOT_FOUND') {
return res.boom.badRequest(null, { return res.boom.badRequest(null, {
errors: [{ type: 'INVENTORY.ACCOUNT.NOT.FOUND', code: 200 }], errors: [{ type: 'INVENTORY.ACCOUNT.NOT.FOUND', code: 800 }],
}); });
} }
if (error.errorType === 'INVENTORY_ACCOUNT_NOT_INVENTORY') { if (error.errorType === 'INVENTORY_ACCOUNT_NOT_INVENTORY') {
return res.boom.badRequest(null, { return res.boom.badRequest(null, {
errors: [{ type: 'INVENTORY.ACCOUNT.NOT.CURRENT.ASSET', code: 300 }], errors: [{ type: 'INVENTORY.ACCOUNT.NOT.CURRENT.ASSET', code: 900 }],
}); });
} }
} }

View File

@@ -12,13 +12,14 @@ import {
IItemCategoriesFilter, IItemCategoriesFilter,
ISystemUser, ISystemUser,
IFilterMeta, IFilterMeta,
} from "interfaces"; } from 'interfaces';
import DynamicListingService from 'services/DynamicListing/DynamicListService'; import DynamicListingService from 'services/DynamicListing/DynamicListService';
import TenancyService from 'services/Tenancy/TenancyService'; import TenancyService from 'services/Tenancy/TenancyService';
import events from 'subscribers/events'; import events from 'subscribers/events';
const ERRORS = { const ERRORS = {
ITEM_CATEGORIES_NOT_FOUND: 'ITEM_CATEGORIES_NOT_FOUND', ITEM_CATEGORIES_NOT_FOUND: 'ITEM_CATEGORIES_NOT_FOUND',
CATEGORY_NAME_EXISTS: 'CATEGORY_NAME_EXISTS',
CATEGORY_NOT_FOUND: 'CATEGORY_NOT_FOUND', CATEGORY_NOT_FOUND: 'CATEGORY_NOT_FOUND',
COST_ACCOUNT_NOT_FOUMD: 'COST_ACCOUNT_NOT_FOUMD', COST_ACCOUNT_NOT_FOUMD: 'COST_ACCOUNT_NOT_FOUMD',
COST_ACCOUNT_NOT_COGS: 'COST_ACCOUNT_NOT_COGS', COST_ACCOUNT_NOT_COGS: 'COST_ACCOUNT_NOT_COGS',
@@ -26,7 +27,7 @@ const ERRORS = {
SELL_ACCOUNT_NOT_FOUND: 'SELL_ACCOUNT_NOT_FOUND', SELL_ACCOUNT_NOT_FOUND: 'SELL_ACCOUNT_NOT_FOUND',
INVENTORY_ACCOUNT_NOT_FOUND: 'INVENTORY_ACCOUNT_NOT_FOUND', INVENTORY_ACCOUNT_NOT_FOUND: 'INVENTORY_ACCOUNT_NOT_FOUND',
INVENTORY_ACCOUNT_NOT_INVENTORY: 'INVENTORY_ACCOUNT_NOT_INVENTORY', INVENTORY_ACCOUNT_NOT_INVENTORY: 'INVENTORY_ACCOUNT_NOT_INVENTORY',
CATEGORY_HAVE_ITEMS: 'CATEGORY_HAVE_ITEMS' CATEGORY_HAVE_ITEMS: 'CATEGORY_HAVE_ITEMS',
}; };
export default class ItemCategoriesService implements IItemCategoriesService { export default class ItemCategoriesService implements IItemCategoriesService {
@@ -47,12 +48,15 @@ export default class ItemCategoriesService implements IItemCategoriesService {
* @param {number} tenantId * @param {number} tenantId
* @param {number} itemCategoryId * @param {number} itemCategoryId
*/ */
private async getItemCategoryOrThrowError(tenantId: number, itemCategoryId: number) { private async getItemCategoryOrThrowError(
tenantId: number,
itemCategoryId: number
) {
const { ItemCategory } = this.tenancy.models(tenantId); const { ItemCategory } = this.tenancy.models(tenantId);
const category = await ItemCategory.query().findById(itemCategoryId); const category = await ItemCategory.query().findById(itemCategoryId);
if (!category) { if (!category) {
throw new ServiceError(ERRORS.CATEGORY_NOT_FOUND) throw new ServiceError(ERRORS.CATEGORY_NOT_FOUND);
} }
return category; return category;
} }
@@ -62,7 +66,10 @@ export default class ItemCategoriesService implements IItemCategoriesService {
* @param {IItemCategoryOTD} itemCategoryOTD * @param {IItemCategoryOTD} itemCategoryOTD
* @param {ISystemUser} authorizedUser * @param {ISystemUser} authorizedUser
*/ */
private transformOTDToObject(itemCategoryOTD: IItemCategoryOTD, authorizedUser: ISystemUser) { private transformOTDToObject(
itemCategoryOTD: IItemCategoryOTD,
authorizedUser: ISystemUser
) {
return { ...itemCategoryOTD, userId: authorizedUser.id }; return { ...itemCategoryOTD, userId: authorizedUser.id };
} }
@@ -72,10 +79,44 @@ export default class ItemCategoriesService implements IItemCategoriesService {
* @param {number} itemCategoryId - * @param {number} itemCategoryId -
* @returns {IItemCategory} * @returns {IItemCategory}
*/ */
public async getItemCategory(tenantId: number, itemCategoryId: number, user: ISystemUser) { public async getItemCategory(
tenantId: number,
itemCategoryId: number,
user: ISystemUser
) {
return this.getItemCategoryOrThrowError(tenantId, itemCategoryId); return this.getItemCategoryOrThrowError(tenantId, itemCategoryId);
} }
/**
* Validates the category name uniquiness.
* @param {number} tenantId - Tenant id.
* @param {string} categoryName - Category name.
* @param {number} notAccountId - Ignore the account id.
*/
private async validateCategoryNameUniquiness(
tenantId: number,
categoryName: string,
notCategoryId?: number
) {
const { ItemCategory } = this.tenancy.models(tenantId);
this.logger.info('[item_category] validating category name uniquiness.', {
tenantId,
categoryName,
notCategoryId,
});
const foundItemCategory = await ItemCategory.query()
.findOne('name', categoryName)
.onBuild((query) => {
if (notCategoryId) {
query.whereNot('id', notCategoryId);
}
});
if (foundItemCategory) {
throw new ServiceError(ERRORS.CATEGORY_NAME_EXISTS);
}
}
/** /**
* Inserts a new item category. * Inserts a new item category.
* @param {number} tenantId * @param {number} tenantId
@@ -88,7 +129,11 @@ export default class ItemCategoriesService implements IItemCategoriesService {
authorizedUser: ISystemUser authorizedUser: ISystemUser
): Promise<IItemCategory> { ): Promise<IItemCategory> {
const { ItemCategory } = this.tenancy.models(tenantId); const { ItemCategory } = this.tenancy.models(tenantId);
this.logger.info('[item_category] trying to insert a new item category.', { tenantId }); this.logger.info('[item_category] trying to insert a new item category.', {
tenantId,
});
// Validate the category name uniquiness.
await this.validateCategoryNameUniquiness(tenantId, itemCategoryOTD.name);
if (itemCategoryOTD.sellAccountId) { if (itemCategoryOTD.sellAccountId) {
await this.validateSellAccount(tenantId, itemCategoryOTD.sellAccountId); await this.validateSellAccount(tenantId, itemCategoryOTD.sellAccountId);
@@ -97,14 +142,25 @@ export default class ItemCategoriesService implements IItemCategoriesService {
await this.validateCostAccount(tenantId, itemCategoryOTD.costAccountId); await this.validateCostAccount(tenantId, itemCategoryOTD.costAccountId);
} }
if (itemCategoryOTD.inventoryAccountId) { if (itemCategoryOTD.inventoryAccountId) {
await this.validateInventoryAccount(tenantId, itemCategoryOTD.inventoryAccountId); await this.validateInventoryAccount(
tenantId,
itemCategoryOTD.inventoryAccountId
);
} }
const itemCategoryObj = this.transformOTDToObject(itemCategoryOTD, authorizedUser); const itemCategoryObj = this.transformOTDToObject(
const itemCategory = await ItemCategory.query().insert({ ...itemCategoryObj }); itemCategoryOTD,
authorizedUser
);
const itemCategory = await ItemCategory.query().insert({
...itemCategoryObj,
});
await this.eventDispatcher.dispatch(events.items.onCreated); await this.eventDispatcher.dispatch(events.itemCategory.onCreated);
this.logger.info('[item_category] item category inserted successfully.', { tenantId, itemCategoryOTD }); this.logger.info('[item_category] item category inserted successfully.', {
tenantId,
itemCategoryOTD,
});
return itemCategory; return itemCategory;
} }
@@ -116,17 +172,29 @@ export default class ItemCategoriesService implements IItemCategoriesService {
* @return {Promise<void>} * @return {Promise<void>}
*/ */
private async validateSellAccount(tenantId: number, sellAccountId: number) { private async validateSellAccount(tenantId: number, sellAccountId: number) {
const { accountRepository, accountTypeRepository } = this.tenancy.repositories(tenantId); const {
accountRepository,
accountTypeRepository,
} = this.tenancy.repositories(tenantId);
this.logger.info('[items] validate sell account existance.', { tenantId, sellAccountId }); this.logger.info('[items] validate sell account existance.', {
tenantId,
sellAccountId,
});
const incomeType = await accountTypeRepository.getByKey('income'); const incomeType = await accountTypeRepository.getByKey('income');
const foundAccount = await accountRepository.findOneById(sellAccountId); const foundAccount = await accountRepository.findOneById(sellAccountId);
if (!foundAccount) { if (!foundAccount) {
this.logger.info('[items] sell account not found.', { tenantId, sellAccountId }); this.logger.info('[items] sell account not found.', {
throw new ServiceError(ERRORS.SELL_ACCOUNT_NOT_FOUND) tenantId,
sellAccountId,
});
throw new ServiceError(ERRORS.SELL_ACCOUNT_NOT_FOUND);
} else if (foundAccount.accountTypeId !== incomeType.id) { } else if (foundAccount.accountTypeId !== incomeType.id) {
this.logger.info('[items] sell account not income type.', { tenantId, sellAccountId }); this.logger.info('[items] sell account not income type.', {
tenantId,
sellAccountId,
});
throw new ServiceError(ERRORS.SELL_ACCOUNT_NOT_INCOME); throw new ServiceError(ERRORS.SELL_ACCOUNT_NOT_INCOME);
} }
} }
@@ -138,17 +206,29 @@ export default class ItemCategoriesService implements IItemCategoriesService {
* @return {Promise<void>} * @return {Promise<void>}
*/ */
private async validateCostAccount(tenantId: number, costAccountId: number) { private async validateCostAccount(tenantId: number, costAccountId: number) {
const { accountRepository, accountTypeRepository } = this.tenancy.repositories(tenantId); const {
accountRepository,
accountTypeRepository,
} = this.tenancy.repositories(tenantId);
this.logger.info('[items] validate cost account existance.', { tenantId, costAccountId }); this.logger.info('[items] validate cost account existance.', {
tenantId,
costAccountId,
});
const COGSType = await accountTypeRepository.getByKey('cost_of_goods_sold'); const COGSType = await accountTypeRepository.getByKey('cost_of_goods_sold');
const foundAccount = await accountRepository.findOneById(costAccountId) const foundAccount = await accountRepository.findOneById(costAccountId);
if (!foundAccount) { if (!foundAccount) {
this.logger.info('[items] cost account not found.', { tenantId, costAccountId }); this.logger.info('[items] cost account not found.', {
tenantId,
costAccountId,
});
throw new ServiceError(ERRORS.COST_ACCOUNT_NOT_FOUMD); throw new ServiceError(ERRORS.COST_ACCOUNT_NOT_FOUMD);
} else if (foundAccount.accountTypeId !== COGSType.id) { } else if (foundAccount.accountTypeId !== COGSType.id) {
this.logger.info('[items] validate cost account not COGS type.', { tenantId, costAccountId }); this.logger.info('[items] validate cost account not COGS type.', {
tenantId,
costAccountId,
});
throw new ServiceError(ERRORS.COST_ACCOUNT_NOT_COGS); throw new ServiceError(ERRORS.COST_ACCOUNT_NOT_COGS);
} }
} }
@@ -159,18 +239,35 @@ export default class ItemCategoriesService implements IItemCategoriesService {
* @param {number} inventoryAccountId * @param {number} inventoryAccountId
* @return {Promise<void>} * @return {Promise<void>}
*/ */
private async validateInventoryAccount(tenantId: number, inventoryAccountId: number) { private async validateInventoryAccount(
const { accountTypeRepository, accountRepository } = this.tenancy.repositories(tenantId); tenantId: number,
inventoryAccountId: number
) {
const {
accountTypeRepository,
accountRepository,
} = this.tenancy.repositories(tenantId);
this.logger.info('[items] validate inventory account existance.', { tenantId, inventoryAccountId }); this.logger.info('[items] validate inventory account existance.', {
tenantId,
inventoryAccountId,
});
const otherAsset = await accountTypeRepository.getByKey('other_asset'); const otherAsset = await accountTypeRepository.getByKey('other_asset');
const foundAccount = await accountRepository.findOneById(inventoryAccountId); const foundAccount = await accountRepository.findOneById(
inventoryAccountId
);
if (!foundAccount) { if (!foundAccount) {
this.logger.info('[items] inventory account not found.', { tenantId, inventoryAccountId }); this.logger.info('[items] inventory account not found.', {
throw new ServiceError(ERRORS.INVENTORY_ACCOUNT_NOT_FOUND) tenantId,
inventoryAccountId,
});
throw new ServiceError(ERRORS.INVENTORY_ACCOUNT_NOT_FOUND);
} else if (otherAsset.id !== foundAccount.accountTypeId) { } else if (otherAsset.id !== foundAccount.accountTypeId) {
this.logger.info('[items] inventory account not inventory type.', { tenantId, inventoryAccountId }); this.logger.info('[items] inventory account not inventory type.', {
tenantId,
inventoryAccountId,
});
throw new ServiceError(ERRORS.INVENTORY_ACCOUNT_NOT_INVENTORY); throw new ServiceError(ERRORS.INVENTORY_ACCOUNT_NOT_INVENTORY);
} }
} }
@@ -186,11 +283,20 @@ export default class ItemCategoriesService implements IItemCategoriesService {
tenantId: number, tenantId: number,
itemCategoryId: number, itemCategoryId: number,
itemCategoryOTD: IItemCategoryOTD, itemCategoryOTD: IItemCategoryOTD,
authorizedUser: ISystemUser, authorizedUser: ISystemUser
): Promise<IItemCategory> { ): Promise<IItemCategory> {
const { ItemCategory } = this.tenancy.models(tenantId); const { ItemCategory } = this.tenancy.models(tenantId);
const oldItemCategory = await this.getItemCategoryOrThrowError(tenantId, itemCategoryId); const oldItemCategory = await this.getItemCategoryOrThrowError(
tenantId,
itemCategoryId
);
// Validate the category name whether unique on the storage.
await this.validateCategoryNameUniquiness(
tenantId,
itemCategoryOTD.name,
itemCategoryId
);
if (itemCategoryOTD.sellAccountId) { if (itemCategoryOTD.sellAccountId) {
await this.validateSellAccount(tenantId, itemCategoryOTD.sellAccountId); await this.validateSellAccount(tenantId, itemCategoryOTD.sellAccountId);
} }
@@ -198,13 +304,26 @@ export default class ItemCategoriesService implements IItemCategoriesService {
await this.validateCostAccount(tenantId, itemCategoryOTD.costAccountId); await this.validateCostAccount(tenantId, itemCategoryOTD.costAccountId);
} }
if (itemCategoryOTD.inventoryAccountId) { if (itemCategoryOTD.inventoryAccountId) {
await this.validateInventoryAccount(tenantId, itemCategoryOTD.inventoryAccountId); await this.validateInventoryAccount(
tenantId,
itemCategoryOTD.inventoryAccountId
);
} }
const itemCategoryObj = this.transformOTDToObject(itemCategoryOTD, authorizedUser); const itemCategoryObj = this.transformOTDToObject(
const itemCategory = await ItemCategory.query().patchAndFetchById(itemCategoryId, { ...itemCategoryObj }); itemCategoryOTD,
authorizedUser
);
const itemCategory = await ItemCategory.query().patchAndFetchById(
itemCategoryId,
{ ...itemCategoryObj }
);
await this.eventDispatcher.dispatch(events.items.onEdited); await this.eventDispatcher.dispatch(events.itemCategory.onEdited);
this.logger.info('[item_category] edited successfully.', { tenantId, itemCategoryId, itemCategoryOTD }); this.logger.info('[item_category] edited successfully.', {
tenantId,
itemCategoryId,
itemCategoryOTD,
});
return itemCategory; return itemCategory;
} }
@@ -215,8 +334,15 @@ export default class ItemCategoriesService implements IItemCategoriesService {
* @param {number} itemCategoryId - Item category id. * @param {number} itemCategoryId - Item category id.
* @return {Promise<void>} * @return {Promise<void>}
*/ */
public async deleteItemCategory(tenantId: number, itemCategoryId: number, authorizedUser: ISystemUser) { public async deleteItemCategory(
this.logger.info('[item_category] trying to delete item category.', { tenantId, itemCategoryId }); tenantId: number,
itemCategoryId: number,
authorizedUser: ISystemUser
) {
this.logger.info('[item_category] trying to delete item category.', {
tenantId,
itemCategoryId,
});
// Retrieve item category or throw not found error. // Retrieve item category or throw not found error.
await this.getItemCategoryOrThrowError(tenantId, itemCategoryId); await this.getItemCategoryOrThrowError(tenantId, itemCategoryId);
@@ -226,9 +352,12 @@ export default class ItemCategoriesService implements IItemCategoriesService {
const { ItemCategory } = this.tenancy.models(tenantId); const { ItemCategory } = this.tenancy.models(tenantId);
await ItemCategory.query().findById(itemCategoryId).delete(); await ItemCategory.query().findById(itemCategoryId).delete();
this.logger.info('[item_category] deleted successfully.', { tenantId, itemCategoryId }); this.logger.info('[item_category] deleted successfully.', {
tenantId,
itemCategoryId,
});
await this.eventDispatcher.dispatch(events.items.onDeleted); await this.eventDispatcher.dispatch(events.itemCategory.onDeleted);
} }
/** /**
@@ -236,12 +365,23 @@ export default class ItemCategoriesService implements IItemCategoriesService {
* @param {number} tenantId * @param {number} tenantId
* @param {number[]} itemCategoriesIds * @param {number[]} itemCategoriesIds
*/ */
private async getItemCategoriesOrThrowError(tenantId: number, itemCategoriesIds: number[]) { private async getItemCategoriesOrThrowError(
tenantId: number,
itemCategoriesIds: number[]
) {
const { ItemCategory } = this.tenancy.models(tenantId); const { ItemCategory } = this.tenancy.models(tenantId);
const itemCategories = await ItemCategory.query().whereIn('id', itemCategoriesIds); const itemCategories = await ItemCategory.query().whereIn(
'id',
itemCategoriesIds
);
const storedItemCategoriesIds = itemCategories.map((category: IItemCategory) => category.id); const storedItemCategoriesIds = itemCategories.map(
const notFoundCategories = difference(itemCategoriesIds, storedItemCategoriesIds); (category: IItemCategory) => category.id
);
const notFoundCategories = difference(
itemCategoriesIds,
storedItemCategoriesIds
);
if (notFoundCategories.length > 0) { if (notFoundCategories.length > 0) {
throw new ServiceError(ERRORS.ITEM_CATEGORIES_NOT_FOUND); throw new ServiceError(ERRORS.ITEM_CATEGORIES_NOT_FOUND);
@@ -257,9 +397,13 @@ export default class ItemCategoriesService implements IItemCategoriesService {
tenantId: number, tenantId: number,
filter: IItemCategoriesFilter, filter: IItemCategoriesFilter,
authorizedUser: ISystemUser authorizedUser: ISystemUser
): Promise<{ itemCategories: IItemCategory[], filterMeta: IFilterMeta }> { ): Promise<{ itemCategories: IItemCategory[]; filterMeta: IFilterMeta }> {
const { ItemCategory } = this.tenancy.models(tenantId); const { ItemCategory } = this.tenancy.models(tenantId);
const dynamicList = await this.dynamicListService.dynamicList(tenantId, ItemCategory, filter); const dynamicList = await this.dynamicListService.dynamicList(
tenantId,
ItemCategory,
filter
);
const itemCategories = await ItemCategory.query().onBuild((query) => { const itemCategories = await ItemCategory.query().onBuild((query) => {
// Subquery to calculate sumation of assocaited items to the item category. // Subquery to calculate sumation of assocaited items to the item category.
@@ -278,10 +422,12 @@ export default class ItemCategoriesService implements IItemCategoriesService {
*/ */
private async unassociateItemsWithCategories( private async unassociateItemsWithCategories(
tenantId: number, tenantId: number,
itemCategoryId: number | number[], itemCategoryId: number | number[]
): Promise<void> { ): Promise<void> {
const { Item } = this.tenancy.models(tenantId); const { Item } = this.tenancy.models(tenantId);
const ids = Array.isArray(itemCategoryId) ? itemCategoryId : [itemCategoryId]; const ids = Array.isArray(itemCategoryId)
? itemCategoryId
: [itemCategoryId];
await Item.query().whereIn('category_id', ids).patch({ category_id: null }); await Item.query().whereIn('category_id', ids).patch({ category_id: null });
} }
@@ -294,9 +440,12 @@ export default class ItemCategoriesService implements IItemCategoriesService {
public async deleteItemCategories( public async deleteItemCategories(
tenantId: number, tenantId: number,
itemCategoriesIds: number[], itemCategoriesIds: number[],
authorizedUser: ISystemUser, authorizedUser: ISystemUser
) { ) {
this.logger.info('[item_category] trying to delete item categories.', { tenantId, itemCategoriesIds }); this.logger.info('[item_category] trying to delete item categories.', {
tenantId,
itemCategoriesIds,
});
const { ItemCategory } = this.tenancy.models(tenantId); const { ItemCategory } = this.tenancy.models(tenantId);
await this.getItemCategoriesOrThrowError(tenantId, itemCategoriesIds); await this.getItemCategoriesOrThrowError(tenantId, itemCategoriesIds);
@@ -304,7 +453,10 @@ export default class ItemCategoriesService implements IItemCategoriesService {
await ItemCategory.query().whereIn('id', itemCategoriesIds).delete(); await ItemCategory.query().whereIn('id', itemCategoriesIds).delete();
await this.eventDispatcher.dispatch(events.items.onBulkDeleted); await this.eventDispatcher.dispatch(events.itemCategory.onBulkDeleted);
this.logger.info('[item_category] item categories deleted successfully.', { tenantId, itemCategoriesIds }); this.logger.info('[item_category] item categories deleted successfully.', {
tenantId,
itemCategoriesIds,
});
} }
} }

View File

@@ -175,6 +175,16 @@ export default {
onBulkDeleted: 'onItemBulkDeleted', onBulkDeleted: 'onItemBulkDeleted',
}, },
/**
* Item category service.
*/
itemCategory: {
onCreated: 'onItemCategoryCreated',
onEdited: 'onItemCategoryEdited',
onDeleted: 'onItemCategoryDeleted',
onBulkDeleted: 'onItemCategoryBulkDeleted',
},
/** /**
* Inventory service. * Inventory service.
*/ */