mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
fix: delete item category has associated items.
This commit is contained in:
@@ -224,7 +224,11 @@ export default class ItemCategoriesService implements IItemCategoriesService {
|
||||
*/
|
||||
public async deleteItemCategory(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.
|
||||
await this.getItemCategoryOrThrowError(tenantId, itemCategoryId);
|
||||
|
||||
// Unassociate items with item category.
|
||||
await this.unassociateItemsWithCategories(tenantId, itemCategoryId);
|
||||
|
||||
const { ItemCategory } = this.tenancy.models(tenantId);
|
||||
@@ -276,11 +280,14 @@ export default class ItemCategoriesService implements IItemCategoriesService {
|
||||
* @param {number|number[]} itemCategoryId -
|
||||
* @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 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[]} 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 });
|
||||
const { ItemCategory } = this.tenancy.models(tenantId);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user