mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
- feat: Optimize tenancy software architecture.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
70
server/src/services/Items/ItemsService.ts
Normal file
70
server/src/services/Items/ItemsService.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user