- feat: Optimize tenancy software architecture.

This commit is contained in:
Ahmed Bouhuolia
2020-08-30 22:11:14 +02:00
parent 74321a2886
commit ca251a2d28
53 changed files with 1581 additions and 1055 deletions

View File

@@ -1,59 +0,0 @@
import { difference } from "lodash";
import { Item, ItemTransaction } from '@/models';
export default class ItemsService {
static async newItem(item) {
const storedItem = await Item.tenant()
.query()
.insertAndFetch({
...item,
});
return storedItem;
}
static async editItem(item, itemId) {
const updateItem = await Item.tenant()
.query()
.findById(itemId)
.patch({
...item,
});
return updateItem;
}
static async deleteItem(itemId) {
return Item.tenant()
.query()
.findById(itemId)
.delete();
}
static async getItemWithMetadata(itemId) {
return Item.tenant()
.query()
.findById(itemId)
.withGraphFetched(
'costAccount',
'sellAccount',
'inventoryAccount',
'category'
);
}
/**
* Validates the given items IDs exists or not returns the not found ones.
* @param {Array} itemsIDs
* @return {Array}
*/
static async isItemsIdsExists(itemsIDs) {
const storedItems = await Item.tenant().query().whereIn('id', itemsIDs);
const storedItemsIds = storedItems.map((t) => t.id);
const notFoundItemsIds = difference(
itemsIDs,
storedItemsIds,
);
return notFoundItemsIds;
}
}

View File

@@ -0,0 +1,70 @@
import { difference } from "lodash";
import { Service, Inject } from "typedi";
import TenancyService from '@/services/Tenancy/TenancyService';
@Service()
export default class ItemsService {
@Inject()
tenancy: TenancyService;
async newItem(tenantId: number, item: any) {
const { Item } = this.tenancy.models(tenantId);
const storedItem = await Item.query()
.insertAndFetch({
...item,
});
return storedItem;
}
async editItem(tenantId: number, item: any, itemId: number) {
const { Item } = this.tenancy.models(tenantId);
const updateItem = await Item.query()
.findById(itemId)
.patch({
...item,
});
return updateItem;
}
async deleteItem(tenantId: number, itemId: number) {
const { Item } = this.tenancy.models(tenantId);
return Item.query()
.findById(itemId)
.delete();
}
/**
* Retrieve the item details of the given id with associated details.
* @param {number} tenantId
* @param {number} itemId
*/
async getItemWithMetadata(tenantId: number, itemId: number) {
const { Item } = this.tenancy.models(tenantId);
return Item.query()
.findById(itemId)
.withGraphFetched(
'costAccount',
'sellAccount',
'inventoryAccount',
'category'
);
}
/**
* Validates the given items IDs exists or not returns the not found ones.
* @param {Array} itemsIDs
* @return {Array}
*/
async isItemsIdsExists(tenantId: number, itemsIDs: number[]) {
const { Item } = this.tenancy.models(tenantId);
const storedItems = await Item.query().whereIn('id', itemsIDs);
const storedItemsIds = storedItems.map((t) => t.id);
const notFoundItemsIds = difference(
itemsIDs,
storedItemsIds,
);
return notFoundItemsIds;
}
}