refactoring: custom views service.

fix: constraints of delete item from storage.
fix: constraints of delete item category from storage.
fix: localize database seeds files.
fix: view meta data in accounts list response.
This commit is contained in:
Ahmed Bouhuolia
2020-10-05 19:09:56 +02:00
parent 0114ed9f8b
commit 99e6fe273f
64 changed files with 1593 additions and 1103 deletions

View File

@@ -8,7 +8,6 @@ import {
IItemCategoriesFilter,
ISystemUser,
} from "interfaces";
import ItemCategory from "models/ItemCategory";
import DynamicListingService from 'services/DynamicListing/DynamicListService';
import TenancyService from 'services/Tenancy/TenancyService';
@@ -21,6 +20,7 @@ const ERRORS = {
SELL_ACCOUNT_NOT_FOUND: 'SELL_ACCOUNT_NOT_FOUND',
INVENTORY_ACCOUNT_NOT_FOUND: 'INVENTORY_ACCOUNT_NOT_FOUND',
INVENTORY_ACCOUNT_NOT_INVENTORY: 'INVENTORY_ACCOUNT_NOT_INVENTORY',
CATEGORY_HAVE_ITEMS: 'CATEGORY_HAVE_ITEMS'
};
export default class ItemCategoriesService implements IItemCategoriesService {
@@ -202,6 +202,7 @@ 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 });
await this.getItemCategoryOrThrowError(tenantId, itemCategoryId);
await this.unassociateItemsWithCategories(tenantId, itemCategoryId);
const { ItemCategory } = this.tenancy.models(tenantId);
await ItemCategory.query().findById(itemCategoryId).delete();
@@ -233,10 +234,16 @@ export default class ItemCategoriesService implements IItemCategoriesService {
const dynamicList = await this.dynamicListService.dynamicList(tenantId, ItemCategory, filter);
const itemCategories = await ItemCategory.query().onBuild((query) => {
query.orderBy('createdAt', 'ASC');
dynamicList.buildQuery()(query);
});
return itemCategories;
return { itemCategories, filterMeta: dynamicList.getResponseMeta() };
}
private async unassociateItemsWithCategories(tenantId: number, itemCategoryId: number|number[]) {
const { Item } = this.tenancy.models(tenantId);
const ids = Array.isArray(itemCategoryId) ? itemCategoryId : [itemCategoryId];
await Item.query().whereIn('id', ids).patch({ category_id: null });
}
/**
@@ -247,7 +254,8 @@ export default class ItemCategoriesService implements IItemCategoriesService {
public async deleteItemCategories(tenantId: number, itemCategoriesIds: number[], authorizedUser: ISystemUser) {
this.logger.info('[item_category] trying to delete item categories.', { tenantId, itemCategoriesIds });
await this.getItemCategoriesOrThrowError(tenantId, itemCategoriesIds);
await this.unassociateItemsWithCategories(tenantId, itemCategoriesIds);
await ItemCategory.query().whereIn('id', itemCategoriesIds).delete();
this.logger.info('[item_category] item categories deleted successfully.', { tenantId, itemCategoriesIds });
}