fix: delete item category has associated items.

This commit is contained in:
Ahmed Bouhuolia
2020-11-29 13:16:12 +02:00
parent 130aa6a400
commit 7524edb3d2

View File

@@ -224,7 +224,11 @@ export default class ItemCategoriesService implements IItemCategoriesService {
*/ */
public async deleteItemCategory(tenantId: number, itemCategoryId: number, authorizedUser: ISystemUser) { public async deleteItemCategory(tenantId: number, itemCategoryId: number, authorizedUser: ISystemUser) {
this.logger.info('[item_category] trying to delete item category.', { tenantId, itemCategoryId }); this.logger.info('[item_category] trying to delete item category.', { tenantId, itemCategoryId });
// Retrieve item category or throw not found error.
await this.getItemCategoryOrThrowError(tenantId, itemCategoryId); await this.getItemCategoryOrThrowError(tenantId, itemCategoryId);
// Unassociate items with item category.
await this.unassociateItemsWithCategories(tenantId, itemCategoryId); await this.unassociateItemsWithCategories(tenantId, itemCategoryId);
const { ItemCategory } = this.tenancy.models(tenantId); const { ItemCategory } = this.tenancy.models(tenantId);
@@ -276,11 +280,14 @@ export default class ItemCategoriesService implements IItemCategoriesService {
* @param {number|number[]} itemCategoryId - * @param {number|number[]} itemCategoryId -
* @return {Promise<void>} * @return {Promise<void>}
*/ */
private async unassociateItemsWithCategories(tenantId: number, itemCategoryId: number|number[]): Promise<void> { private async unassociateItemsWithCategories(
tenantId: number,
itemCategoryId: number | number[],
): 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('id', ids).patch({ category_id: null }); await Item.query().whereIn('category_id', ids).patch({ category_id: null });
} }
/** /**
@@ -288,7 +295,11 @@ export default class ItemCategoriesService implements IItemCategoriesService {
* @param {number} tenantId * @param {number} tenantId
* @param {number[]} itemCategoriesIds * @param {number[]} itemCategoriesIds
*/ */
public async deleteItemCategories(tenantId: number, itemCategoriesIds: number[], authorizedUser: ISystemUser) { public async deleteItemCategories(
tenantId: number,
itemCategoriesIds: number[],
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);