feat: items bulk delete.

refactor: items to service design.
This commit is contained in:
Ahmed Bouhuolia
2020-09-29 16:04:13 +02:00
parent d47d36bb0e
commit 9fbad4ac46
4 changed files with 470 additions and 243 deletions

View File

@@ -2,10 +2,12 @@ import { Inject, Service } from 'typedi';
import { Router, Request, Response, NextFunction } from 'express'; import { Router, Request, Response, NextFunction } from 'express';
import { check, param, query, ValidationChain, matchedData } from 'express-validator'; import { check, param, query, ValidationChain, matchedData } from 'express-validator';
import asyncMiddleware from 'api/middleware/asyncMiddleware'; import asyncMiddleware from 'api/middleware/asyncMiddleware';
import validateMiddleware from 'api/middleware/validateMiddleware';
import ItemsService from 'services/Items/ItemsService'; import ItemsService from 'services/Items/ItemsService';
import BaseController from 'api/controllers/BaseController'; import BaseController from 'api/controllers/BaseController';
import DynamicListingService from 'services/DynamicListing/DynamicListService'; import DynamicListingService from 'services/DynamicListing/DynamicListService';
import { ServiceError } from 'exceptions';
import { IItemDTO } from 'interfaces';
import { Request } from 'express-validator/src/base';
@Service() @Service()
export default class ItemsController extends BaseController { export default class ItemsController extends BaseController {
@@ -22,50 +24,53 @@ export default class ItemsController extends BaseController {
const router = Router(); const router = Router();
router.post( router.post(
'/', '/', [
this.validateItemSchema, ...this.validateItemSchema,
validateMiddleware, ],
asyncMiddleware(this.validateCategoryExistance.bind(this)), this.validationResult,
asyncMiddleware(this.validateCostAccountExistance.bind(this)),
asyncMiddleware(this.validateSellAccountExistance.bind(this)),
asyncMiddleware(this.validateInventoryAccountExistance.bind(this)),
asyncMiddleware(this.validateItemNameExistance.bind(this)),
asyncMiddleware(this.newItem.bind(this)), asyncMiddleware(this.newItem.bind(this)),
this.handlerServiceErrors,
); );
router.post( router.post(
'/:id', [ '/:id', [
...this.validateItemSchema, ...this.validateItemSchema,
...this.validateSpecificItemSchema, ...this.validateSpecificItemSchema,
], ],
validateMiddleware, this.validationResult,
asyncMiddleware(this.validateItemExistance.bind(this)),
asyncMiddleware(this.validateCategoryExistance.bind(this)),
asyncMiddleware(this.validateCostAccountExistance.bind(this)),
asyncMiddleware(this.validateSellAccountExistance.bind(this)),
asyncMiddleware(this.validateInventoryAccountExistance.bind(this)),
asyncMiddleware(this.validateItemNameExistance.bind(this)),
asyncMiddleware(this.editItem.bind(this)), asyncMiddleware(this.editItem.bind(this)),
this.handlerServiceErrors,
);
router.delete('/', [
...this.validateBulkSelectSchema,
],
this.validationResult,
asyncMiddleware(this.bulkDeleteItems.bind(this)),
this.handlerServiceErrors
); );
router.delete( router.delete(
'/:id', '/:id', [
this.validateSpecificItemSchema, ...this.validateSpecificItemSchema,
validateMiddleware, ],
asyncMiddleware(this.validateItemExistance.bind(this)), this.validationResult,
asyncMiddleware(this.deleteItem.bind(this)), asyncMiddleware(this.deleteItem.bind(this)),
this.handlerServiceErrors,
); );
router.get( router.get(
'/:id', '/:id', [
this.validateSpecificItemSchema, ...this.validateSpecificItemSchema,
validateMiddleware, ],
asyncMiddleware(this.validateItemExistance.bind(this)), this.validationResult,
asyncMiddleware(this.getItem.bind(this)), asyncMiddleware(this.getItem.bind(this)),
this.handlerServiceErrors,
); );
router.get( router.get(
'/', '/', [
this.validateListQuerySchema, ...this.validateListQuerySchema,
validateMiddleware, ],
this.validationResult,
asyncMiddleware(this.getItemsList.bind(this)), asyncMiddleware(this.getItemsList.bind(this)),
this.dynamicListService.handlerErrorsToResponse, this.dynamicListService.handlerErrorsToResponse,
this.handlerServiceErrors,
); );
return router; return router;
} }
@@ -121,6 +126,7 @@ export default class ItemsController extends BaseController {
/** /**
* Validate specific item params schema. * Validate specific item params schema.
* @return {ValidationChain[]}
*/ */
get validateSpecificItemSchema(): ValidationChain[] { get validateSpecificItemSchema(): ValidationChain[] {
return [ return [
@@ -128,6 +134,16 @@ export default class ItemsController extends BaseController {
]; ];
} }
/**
* Bulk select validation schema.
* @return {ValidationChain[]}
*/
get validateBulkSelectSchema(): ValidationChain[] {
return [
query('ids').isArray({ min: 2 }),
query('ids.*').isNumeric().toInt(),
];
}
/** /**
* Validate list query schema * Validate list query schema
@@ -143,169 +159,21 @@ export default class ItemsController extends BaseController {
] ]
} }
/**
* Validates the given item existance on the storage.
* @param {Request} req -
* @param {Response} res -
* @param {NextFunction} next -
*/
async validateItemExistance(req: Request, res: Response, next: Function) {
const { Item } = req.models;
const itemId: number = req.params.id;
const foundItem = await Item.query().findById(itemId);
if (!foundItem) {
return res.status(400).send({
errors: [{ type: 'ITEM.NOT.FOUND', code: 100 }],
});
}
next();
}
/**
* Validate wether the given item name already exists on the storage.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async validateItemNameExistance(req: Request, res: Response, next: Function) {
const { Item } = req.models;
const item = req.body;
const itemId: number = req.params.id;
const foundItems: [] = await Item.query().onBuild((builder: any) => {
builder.where('name', item.name);
if (itemId) {
builder.whereNot('id', itemId);
}
});
if (foundItems.length > 0) {
return res.status(400).send({
errors: [{ type: 'ITEM.NAME.ALREADY.EXISTS', code: 210 }],
});
}
next();
}
/**
* Validate wether the given category existance on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
async validateCategoryExistance(req: Request, res: Response, next: Function) {
const { ItemCategory } = req.models;
const item = req.body;
if (item.category_id) {
const foundCategory = await ItemCategory.query().findById(item.category_id);
if (!foundCategory) {
return res.status(400).send({
errors: [{ type: 'ITEM_CATEGORY.NOT.FOUND', code: 140 }],
});
}
}
next();
}
/**
* Validate wether the given cost account exists on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
async validateCostAccountExistance(req: Request, res: Response, next: Function) {
const { Account, AccountType } = req.models;
const item = req.body;
if (item.cost_account_id) {
const COGSType = await AccountType.query().findOne('key', 'cost_of_goods_sold');
const foundAccount = await Account.query().findById(item.cost_account_id)
if (!foundAccount) {
return res.status(400).send({
errors: [{ type: 'COST.ACCOUNT.NOT.FOUND', code: 120 }],
});
} else if (foundAccount.accountTypeId !== COGSType.id) {
return res.status(400).send({
errors: [{ type: 'COST.ACCOUNT.NOT.COGS.TYPE', code: 220 }],
});
}
}
next();
}
/**
* Validate wether the given sell account exists on the storage.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async validateSellAccountExistance(req: Request, res: Response, next: Function) {
const { Account, AccountType } = req.models;
const item = req.body;
if (item.sell_account_id) {
const incomeType = await AccountType.query().findOne('key', 'income');
const foundAccount = await Account.query().findById(item.sell_account_id);
if (!foundAccount) {
return res.status(400).send({
errors: [{ type: 'SELL.ACCOUNT.NOT.FOUND', code: 130 }],
});
} else if (foundAccount.accountTypeId !== incomeType.id) {
return res.status(400).send({
errors: [{ type: 'SELL.ACCOUNT.NOT.INCOME.TYPE', code: 230 }],
})
}
}
next();
}
/**
* Validates wether the given inventory account exists on the storage.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async validateInventoryAccountExistance(req: Request, res: Response, next: Function) {
const { Account, AccountType } = req.models;
const item = req.body;
if (item.inventory_account_id) {
const otherAsset = await AccountType.query().findOne('key', 'other_asset');
const foundAccount = await Account.query().findById(item.inventory_account_id);
if (!foundAccount) {
return res.status(400).send({
errors: [{ type: 'INVENTORY.ACCOUNT.NOT.FOUND', code: 200}],
});
} else if (otherAsset.id !== foundAccount.accountTypeId) {
return res.status(400).send({
errors: [{ type: 'INVENTORY.ACCOUNT.NOT.CURRENT.ASSET', code: 300 }],
});
}
}
next();
}
/** /**
* Stores the given item details to the storage. * Stores the given item details to the storage.
* @param {Request} req * @param {Request} req
* @param {Response} res * @param {Response} res
*/ */
async newItem(req: Request, res: Response,) { async newItem(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req; const { tenantId } = req;
const itemDTO: IItemDTO = this.matchedBodyData(req);
const item = matchedData(req, { try {
locations: ['body'], const storedItem = await this.itemsService.newItem(tenantId, itemDTO);
includeOptionals: true
});
const storedItem = await this.itemsService.newItem(tenantId, item);
return res.status(200).send({ id: storedItem.id }); return res.status(200).send({ id: storedItem.id });
} catch (error) {
next(error);
}
} }
/** /**
@@ -313,17 +181,17 @@ export default class ItemsController extends BaseController {
* @param {Request} req * @param {Request} req
* @param {Response} res * @param {Response} res
*/ */
async editItem(req: Request, res: Response) { async editItem(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req; const { tenantId } = req;
const itemId: number = req.params.id; const itemId: number = req.params.id;
const item = matchedData(req, { const item: IItemDTO = this.matchedBodyData(req);
locations: ['body'],
includeOptionals: true
});
const updatedItem = await this.itemsService.editItem(tenantId, item, itemId);
try {
await this.itemsService.editItem(tenantId, itemId, item);
return res.status(200).send({ id: itemId }); return res.status(200).send({ id: itemId });
} catch (error) {
next(error);
}
} }
/** /**
@@ -331,13 +199,16 @@ export default class ItemsController extends BaseController {
* @param {Request} req * @param {Request} req
* @param {Response} res * @param {Response} res
*/ */
async deleteItem(req: Request, res: Response) { async deleteItem(req: Request, res: Response, next: NextFunction) {
const itemId: number = req.params.id; const itemId: number = req.params.id;
const { tenantId } = req; const { tenantId } = req;
try {
await this.itemsService.deleteItem(tenantId, itemId); await this.itemsService.deleteItem(tenantId, itemId);
return res.status(200).send({ id: itemId }); return res.status(200).send({ id: itemId });
} catch (error) {
next(error);
}
} }
/** /**
@@ -346,13 +217,17 @@ export default class ItemsController extends BaseController {
* @param {Response} res * @param {Response} res
* @return {Response} * @return {Response}
*/ */
async getItem(req: Request, res: Response) { async getItem(req: Request, res: Response, next: NextFunction) {
const itemId: number = req.params.id; const itemId: number = req.params.id;
const { tenantId } = req; const { tenantId } = req;
const storedItem = await this.itemsService.getItemWithMetadata(tenantId, itemId); try {
const storedItem = await this.itemsService.getItem(tenantId, itemId);
return res.status(200).send({ item: storedItem }); return res.status(200).send({ item: storedItem });
} catch (error) {
next(error)
}
} }
/** /**
@@ -371,10 +246,105 @@ export default class ItemsController extends BaseController {
filter.filterRoles = JSON.parse(filter.stringifiedFilterRoles); filter.filterRoles = JSON.parse(filter.stringifiedFilterRoles);
} }
try { try {
const items = await this.itemsService.getItemsList(tenantId, filter); const items = await this.itemsService.itemsList(tenantId, filter);
return res.status(200).send({ items }); return res.status(200).send({ items });
} catch (error) { } catch (error) {
next(error); next(error);
} }
} }
/**
* Deletes items in bulk.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async bulkDeleteItems(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { ids: itemsIds } = req.query;
try {
await this.itemsService.bulkDeleteItems(tenantId, itemsIds);
return res.status(200).send({ ids: itemsIds });
} catch (error) {
next(error);
}
}
/**
* Handles service errors.
* @param {Error} error
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
handlerServiceErrors(error: Error, req: Request, res: Response, next: NextFunction) {
if (error instanceof ServiceError) {
if (error.errorType === 'NOT_FOUND') {
return res.status(400).send({
errors: [{ type: 'ITEM.NOT.FOUND', code: 140 }],
});
}
if (error.errorType === 'ITEM_CATEOGRY_NOT_FOUND') {
return res.status(400).send({
errors: [{ type: 'ITEM_CATEGORY.NOT.FOUND', code: 140 }],
});
}
if (error.errorType === 'ITEM_NAME_EXISTS') {
return res.status(400).send({
errors: [{ type: 'ITEM.NAME.ALREADY.EXISTS', code: 210 }],
});
}
if (error.errorType === 'COST_ACCOUNT_NOT_FOUMD') {
return res.status(400).send({
errors: [{ type: 'COST.ACCOUNT.NOT.FOUND', code: 120 }],
});
}
if (error.errorType === 'COST_ACCOUNT_NOT_COGS') {
return res.status(400).send({
errors: [{ type: 'COST.ACCOUNT.NOT.COGS.TYPE', code: 220 }],
});
}
if (error.errorType === 'SELL_ACCOUNT_NOT_FOUND') {
return res.status(400).send({
errors: [{ type: 'SELL.ACCOUNT.NOT.FOUND', code: 130 }],
});
}
if (error.errorType === 'SELL_ACCOUNT_NOT_INCOME') {
return res.status(400).send({
errors: [{ type: 'SELL.ACCOUNT.NOT.INCOME.TYPE', code: 230 }],
});
}
if (error.errorType === 'COST_ACCOUNT_NOT_FOUMD') {
return res.status(400).send({
errors: [{ type: 'COST.ACCOUNT.NOT.FOUND', code: 120 }],
});
}
if (error.errorType === 'COST_ACCOUNT_NOT_COGS') {
return res.status(400).send({
errors: [{ type: 'COST.ACCOUNT.NOT.COGS.TYPE', code: 220 }],
});
}
if (error.errorType === 'SELL_ACCOUNT_NOT_FOUND') {
return res.status(400).send({
errors: [{ type: 'SELL.ACCOUNT.NOT.FOUND', code: 130 }],
});
}
if (error.errorType === 'INVENTORY_ACCOUNT_NOT_FOUND') {
return res.status(400).send({
errors: [{ type: 'INVENTORY.ACCOUNT.NOT.FOUND', code: 200 }],
});
}
if (error.errorType === 'SELL_ACCOUNT_NOT_INCOME') {
return res.status(400).send({
errors: [{ type: 'SELL.ACCOUNT.NOT.INCOME.TYPE', code: 230 }],
});
}
if (error.errorType === 'INVENTORY_ACCOUNT_NOT_INVENTORY') {
return res.status(400).send({
errors: [{ type: 'INVENTORY.ACCOUNT.NOT.CURRENT.ASSET', code: 300 }],
});
}
}
}
} }

View File

@@ -4,10 +4,68 @@ export interface IItem{
id: number, id: number,
name: string, name: string,
type: string, type: string,
sku: string,
sellable: boolean,
purchasable: boolean,
costPrice: number,
sellPrice: number,
currencyCode: string,
costAccountId: number,
sellAccountId: number,
inventoryAccountId: number,
sellDescription: string,
purchaseDescription: string,
quantityOnHand: number,
note: string,
categoryId: number,
userId: number,
createdAt: Date,
updatedAt: Date,
}
export interface IItemDTO {
name: string,
type: string,
sku: string,
sellable: boolean,
purchasable: boolean,
costPrice: number,
sellPrice: number,
currencyCode: string,
costAccountId: number,
sellAccountId: number,
inventoryAccountId: number,
sellDescription: string,
purchaseDescription: string,
quantityOnHand: number,
note: string,
categoryId: number,
} }
export interface IItemsService { export interface IItemsService {
bulkDeleteItems(tenantId: number, itemsIds: number[]): Promise<void>;
getItem(tenantId: number, itemId: number): Promise<IItem>;
deleteItem(tenantId: number, itemId: number): Promise<void>;
editItem(tenantId: number, itemId: number, itemDTO: IItemDTO): Promise<IItem>;
newItem(tenantId: number, itemDTO: IItemDTO): Promise<IItem>;
itemsList(tenantId: number, itemsFilter: IItemsFilter): Promise<{items: IItem[]}>;
} }
export interface IItemsFilter extends IDynamicListFilter { export interface IItemsFilter extends IDynamicListFilter {

View File

@@ -5,6 +5,7 @@ import AccountTransaction from 'models/AccountTransaction';
import AccountType from 'models/AccountType'; import AccountType from 'models/AccountType';
import Item from 'models/Item'; import Item from 'models/Item';
import ItemEntry from 'models/ItemEntry'; import ItemEntry from 'models/ItemEntry';
import ItemCategory from 'models/ItemCategory';
import Bill from 'models/Bill'; import Bill from 'models/Bill';
import BillPayment from 'models/BillPayment'; import BillPayment from 'models/BillPayment';
import BillPaymentEntry from 'models/BillPaymentEntry'; import BillPaymentEntry from 'models/BillPaymentEntry';
@@ -41,6 +42,7 @@ export default (knex) => {
AccountTransaction, AccountTransaction,
AccountType, AccountType,
Item, Item,
ItemCategory,
ItemEntry, ItemEntry,
ManualJournal, ManualJournal,
Bill, Bill,

View File

@@ -1,41 +1,230 @@
import { difference } from "lodash"; import { difference } from "lodash";
import { Service, Inject } from "typedi"; import { Service, Inject } from "typedi";
import { IItemsFilter } from 'interfaces'; import { IItemsFilter, IItemsService, IItemDTO, IItem } from 'interfaces';
import DynamicListingService from 'services/DynamicListing/DynamicListService'; import DynamicListingService from 'services/DynamicListing/DynamicListService';
import TenancyService from 'services/Tenancy/TenancyService'; import TenancyService from 'services/Tenancy/TenancyService';
import { ServiceError } from "exceptions";
import { Item } from "models";
const ERRORS = {
NOT_FOUND: 'NOT_FOUND',
ITEM_NAME_EXISTS: 'ITEM_NAME_EXISTS',
ITEM_CATEOGRY_NOT_FOUND: 'ITEM_CATEOGRY_NOT_FOUND',
COST_ACCOUNT_NOT_COGS: 'COST_ACCOUNT_NOT_COGS',
COST_ACCOUNT_NOT_FOUMD: 'COST_ACCOUNT_NOT_FOUMD',
SELL_ACCOUNT_NOT_FOUND: 'SELL_ACCOUNT_NOT_FOUND',
SELL_ACCOUNT_NOT_INCOME: 'SELL_ACCOUNT_NOT_INCOME',
INVENTORY_ACCOUNT_NOT_FOUND: 'INVENTORY_ACCOUNT_NOT_FOUND',
INVENTORY_ACCOUNT_NOT_INVENTORY: 'INVENTORY_ACCOUNT_NOT_INVENTORY',
}
@Service() @Service()
export default class ItemsService { export default class ItemsService implements IItemsService {
@Inject() @Inject()
tenancy: TenancyService; tenancy: TenancyService;
@Inject() @Inject()
dynamicListService: DynamicListingService; dynamicListService: DynamicListingService;
async newItem(tenantId: number, item: any) { @Inject('logger')
logger: any;
/**
* Retrieve item details or throw not found error.
* @param {number} tenantId
* @param {number} itemId
* @return {Promise<void>}
*/
private async getItemOrThrowError(tenantId: number, itemId: number): Promise<void> {
const { Item } = this.tenancy.models(tenantId); const { Item } = this.tenancy.models(tenantId);
const storedItem = await Item.query()
.insertAndFetch({ this.logger.info('[items] validate item id existance.', { itemId });
...item, const foundItem = await Item.query().findById(itemId);
if (!foundItem) {
this.logger.info('[items] item not found.', { itemId });
throw new ServiceError(ERRORS.NOT_FOUND);
}
return foundItem;
}
/**
* Validate wether the given item name already exists on the storage.
* @param {number} tenantId
* @param {string} itemName
* @param {number} notItemId
* @return {Promise<void>}
*/
private async validateItemNameUniquiness(tenantId: number, itemName: string, notItemId?: number): Promise<void> {
const { Item } = this.tenancy.models(tenantId);
this.logger.info('[items] validate item name uniquiness.', { itemName, tenantId });
const foundItems: [] = await Item.query().onBuild((builder: any) => {
builder.where('name', itemName);
if (notItemId) {
builder.whereNot('id', notItemId);
}
}); });
if (foundItems.length > 0) {
this.logger.info('[items] item name already exists.', { itemName, tenantId });
throw new ServiceError(ERRORS.ITEM_NAME_EXISTS);
}
}
/**
* Validate item COGS account existance and type.
* @param {number} tenantId
* @param {number} costAccountId
* @return {Promise<void>}
*/
private async validateItemCostAccountExistance(tenantId: number, costAccountId: number): Promise<void> {
const { accountRepository, accountTypeRepository } = this.tenancy.repositories(tenantId);
this.logger.info('[items] validate cost account existance.', { tenantId, costAccountId });
const COGSType = await accountTypeRepository.getByKey('cost_of_goods_sold');
const foundAccount = await accountRepository.getById(costAccountId)
if (!foundAccount) {
this.logger.info('[items] cost account not found.', { tenantId, costAccountId });
throw new ServiceError(ERRORS.COST_ACCOUNT_NOT_FOUMD);
} else if (foundAccount.accountTypeId !== COGSType.id) {
this.logger.info('[items] validate cost account not COGS type.', { tenantId, costAccountId });
throw new ServiceError(ERRORS.COST_ACCOUNT_NOT_COGS);
}
}
/**
* Validate item sell account existance and type.
* @param {number} tenantId - Tenant id.
* @param {number} sellAccountId - Sell account id.
*/
private async validateItemSellAccountExistance(tenantId: number, sellAccountId: number) {
const { accountRepository, accountTypeRepository } = this.tenancy.repositories(tenantId);
this.logger.info('[items] validate sell account existance.', { tenantId, sellAccountId });
const incomeType = await accountTypeRepository.getByKey('income');
const foundAccount = await accountRepository.getById(sellAccountId);
if (!foundAccount) {
this.logger.info('[items] sell account not found.', { tenantId, sellAccountId });
throw new ServiceError(ERRORS.SELL_ACCOUNT_NOT_FOUND)
} else if (foundAccount.accountTypeId !== incomeType.id) {
this.logger.info('[items] sell account not income type.', { tenantId, sellAccountId });
throw new ServiceError(ERRORS.SELL_ACCOUNT_NOT_INCOME);
}
}
/**
* Validate item inventory account existance and type.
* @param {number} tenantId
* @param {number} inventoryAccountId
*/
private async validateItemInventoryAccountExistance(tenantId: number, inventoryAccountId: number) {
const { accountTypeRepository, accountRepository } = this.tenancy.repositories(tenantId);
this.logger.info('[items] validate inventory account existance.', { tenantId, inventoryAccountId });
const otherAsset = await accountTypeRepository.getByKey('other_asset');
const foundAccount = await accountRepository.getById(inventoryAccountId);
if (!foundAccount) {
this.logger.info('[items] inventory account not found.', { tenantId, inventoryAccountId });
throw new ServiceError(ERRORS.INVENTORY_ACCOUNT_NOT_FOUND)
} else if (otherAsset.id !== foundAccount.accountTypeId) {
this.logger.info('[items] inventory account not inventory type.', { tenantId, inventoryAccountId });
throw new ServiceError(ERRORS.INVENTORY_ACCOUNT_NOT_INVENTORY);
}
}
/**
* Validate item category existance.
* @param {number} tenantId
* @param {number} itemCategoryId
*/
private async validateItemCategoryExistance(tenantId: number, itemCategoryId: number) {
const { ItemCategory } = this.tenancy.models(tenantId);
const foundCategory = await ItemCategory.query().findById(itemCategoryId);
if (!foundCategory) {
throw new ServiceError(ERRORS.ITEM_CATEOGRY_NOT_FOUND);
}
}
/**
* Creates a new item.
* @param {number} tenantId DTO
* @param {IItemDTO} item
* @return {Promise<IItem>}
*/
public async newItem(tenantId: number, itemDTO: IItemDTO): Promise<IItem> {
const { Item } = this.tenancy.models(tenantId);
// Validate whether the given item name already exists on the storage.
await this.validateItemNameUniquiness(tenantId, itemDTO.name);
if (itemDTO.categoryId) {
await this.validateItemCategoryExistance(tenantId, itemDTO.categoryId);
}
if (itemDTO.sellAccountId) {
await this.validateItemSellAccountExistance(tenantId, itemDTO.sellAccountId);
}
if (itemDTO.costAccountId) {
await this.validateItemCostAccountExistance(tenantId, itemDTO.costAccountId);
}
if (itemDTO.inventoryAccountId) {
await this.validateItemInventoryAccountExistance(tenantId, itemDTO.inventoryAccountId);
}
const storedItem = await Item.query().insertAndFetch({ ...itemDTO });
this.logger.info('[items] item inserted successfully.', { tenantId, itemDTO });
return storedItem; return storedItem;
} }
async editItem(tenantId: number, item: any, itemId: number) { /**
* Edits the item metadata.
* @param {number} tenantId
* @param {number} itemId
* @param {IItemDTO} itemDTO
*/
public async editItem(tenantId: number, itemId: number, itemDTO: IItemDTO) {
const { Item } = this.tenancy.models(tenantId); const { Item } = this.tenancy.models(tenantId);
const updateItem = await Item.query()
.findById(itemId) // Validates the given item existance on the storage.
.patch({ const oldItem = await this.getItemOrThrowError(tenantId, itemId);
...item,
}); if (itemDTO.categoryId) {
return updateItem; await this.validateItemCategoryExistance(tenantId, itemDTO.categoryId);
}
if (itemDTO.sellAccountId) {
await this.validateItemSellAccountExistance(tenantId, itemDTO.sellAccountId);
}
if (itemDTO.costAccountId) {
await this.validateItemCostAccountExistance(tenantId, itemDTO.costAccountId);
}
if (itemDTO.inventoryAccountId) {
await this.validateItemInventoryAccountExistance(tenantId, itemDTO.inventoryAccountId);
} }
async deleteItem(tenantId: number, itemId: number) { const newItem = await Item.query().patchAndFetchById(itemId, { ...itemDTO });
this.logger.info('[items] item edited successfully.', { tenantId, itemId, itemDTO });
return newItem;
}
/**
* Delete the given item from the storage.
* @param {number} tenantId - Tenant id.
* @param {number} itemId - Item id.
* @return {Promise<void>}
*/
public async deleteItem(tenantId: number, itemId: number) {
const { Item } = this.tenancy.models(tenantId); const { Item } = this.tenancy.models(tenantId);
return Item.query()
.findById(itemId) this.logger.info('[items] trying to delete item.', { tenantId, itemId });
.delete(); await this.getItemOrThrowError(tenantId, itemId);
await Item.query().findById(itemId).delete();
this.logger.info('[items] deleted successfully.', { tenantId, itemId });
} }
/** /**
@@ -43,16 +232,16 @@ export default class ItemsService {
* @param {number} tenantId * @param {number} tenantId
* @param {number} itemId * @param {number} itemId
*/ */
async getItemWithMetadata(tenantId: number, itemId: number) { public async getItem(tenantId: number, itemId: number): Promise<IItem> {
const { Item } = this.tenancy.models(tenantId); const { Item } = this.tenancy.models(tenantId);
return Item.query()
.findById(itemId) const item = Item.query().findById(itemId)
.withGraphFetched( .withGraphFetched('costAccount', 'sellAccount', 'inventoryAccount', 'category');
'costAccount',
'sellAccount', if (!item) {
'inventoryAccount', throw new ServiceError(ERRORS.NOT_FOUND);
'category' }
); return item;
} }
/** /**
@@ -60,21 +249,29 @@ export default class ItemsService {
* @param {Array} itemsIDs * @param {Array} itemsIDs
* @return {Array} * @return {Array}
*/ */
async isItemsIdsExists(tenantId: number, itemsIDs: number[]) { private async validateItemsIdsExists(tenantId: number, itemsIDs: number[]) {
const { Item } = this.tenancy.models(tenantId); const { Item } = this.tenancy.models(tenantId);
const storedItems = await Item.query().whereIn('id', itemsIDs); const storedItems = await Item.query().whereIn('id', itemsIDs);
const storedItemsIds = storedItems.map((t) => t.id); const storedItemsIds = storedItems.map((t) => t.id);
const notFoundItemsIds = difference( const notFoundItemsIds = difference(itemsIDs, storedItemsIds);
itemsIDs,
storedItemsIds,
);
return notFoundItemsIds; return notFoundItemsIds;
} }
writeItemInventoryOpeningQuantity(tenantId: number, itemId: number, openingQuantity: number, averageCost: number) { /**
* Deletes items in bulk.
* @param {number} tenantId
* @param {number[]} itemsIds
*/
public async bulkDeleteItems(tenantId: number, itemsIds: number[]) {
const { Item } = this.tenancy.models(tenantId);
this.logger.info('[items] trying to delete items in bulk.', { tenantId, itemsIds });
await this.validateItemsIdsExists(tenantId, itemsIds);
await Item.query().whereIn('id', itemsIds).delete();
this.logger.info('[items] deleted successfully in bulk.', { tenantId, itemsIds });
} }
/** /**
@@ -82,7 +279,7 @@ export default class ItemsService {
* @param {number} tenantId * @param {number} tenantId
* @param {IItemsFilter} itemsFilter * @param {IItemsFilter} itemsFilter
*/ */
async getItemsList(tenantId: number, itemsFilter: IItemsFilter) { public async itemsList(tenantId: number, itemsFilter: IItemsFilter) {
const { Item } = this.tenancy.models(tenantId); const { Item } = this.tenancy.models(tenantId);
const dynamicFilter = await this.dynamicListService.dynamicList(tenantId, Item, itemsFilter); const dynamicFilter = await this.dynamicListService.dynamicList(tenantId, Item, itemsFilter);