mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
fix: resource advanced view filter.
This commit is contained in:
@@ -1,30 +1,21 @@
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { check, validationResult, param, query } from 'express-validator';
|
||||
import { difference } from 'lodash';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
import JournalPoster from 'services/Accounting/JournalPoster';
|
||||
import {
|
||||
mapViewRolesToConditionals,
|
||||
mapFilterRolesToDynamicFilter,
|
||||
} from 'lib/ViewRolesBuilder';
|
||||
import {
|
||||
DynamicFilter,
|
||||
DynamicFilterSortBy,
|
||||
DynamicFilterViews,
|
||||
DynamicFilterFilterRoles,
|
||||
} from 'lib/DynamicFilter';
|
||||
import BaseController from './BaseController';
|
||||
import { IAccountDTO, IAccount } from 'interfaces';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import AccountsService from 'services/Accounts/AccountsService';
|
||||
import { check, param, query } from 'express-validator';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
import BaseController from 'api/controllers/BaseController';
|
||||
import AccountsService from 'services/Accounts/AccountsService';
|
||||
import { IAccountDTO, IAccountsFilter } from 'interfaces';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import DynamicListingService from 'services/DynamicListing/DynamicListService';
|
||||
|
||||
@Service()
|
||||
export default class AccountsController extends BaseController{
|
||||
|
||||
@Inject()
|
||||
accountsService: AccountsService;
|
||||
|
||||
@Inject()
|
||||
dynamicListService: DynamicListingService;
|
||||
|
||||
/**
|
||||
* Router constructor method.
|
||||
*/
|
||||
@@ -39,13 +30,15 @@ export default class AccountsController extends BaseController{
|
||||
'/:id/activate', [
|
||||
...this.accountParamSchema,
|
||||
],
|
||||
asyncMiddleware(this.activateAccount.bind(this))
|
||||
asyncMiddleware(this.activateAccount.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.post(
|
||||
'/:id/inactivate', [
|
||||
...this.accountParamSchema,
|
||||
],
|
||||
asyncMiddleware(this.inactivateAccount.bind(this))
|
||||
asyncMiddleware(this.inactivateAccount.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.post(
|
||||
'/:id', [
|
||||
@@ -53,47 +46,48 @@ export default class AccountsController extends BaseController{
|
||||
...this.accountParamSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.editAccount.bind(this))
|
||||
asyncMiddleware(this.editAccount.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.post(
|
||||
'/', [
|
||||
...this.accountDTOSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.newAccount.bind(this))
|
||||
asyncMiddleware(this.newAccount.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.get(
|
||||
'/:id', [
|
||||
...this.accountParamSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.getAccount.bind(this))
|
||||
asyncMiddleware(this.getAccount.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.get(
|
||||
'/', [
|
||||
...this.accountsListSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.getAccountsList.bind(this)),
|
||||
this.dynamicListService.handlerErrorsToResponse,
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
// // router.get(
|
||||
// // '/', [
|
||||
// // ...this.accountsListSchema
|
||||
// // ],
|
||||
// // asyncMiddleware(this.getAccountsList.handler)
|
||||
// // );
|
||||
|
||||
router.delete(
|
||||
'/:id', [
|
||||
...this.accountParamSchema
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.deleteAccount.bind(this))
|
||||
asyncMiddleware(this.deleteAccount.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.delete(
|
||||
'/',
|
||||
this.bulkDeleteSchema,
|
||||
asyncMiddleware(this.deleteBulkAccounts.bind(this))
|
||||
asyncMiddleware(this.deleteBulkAccounts.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
// router.post(
|
||||
// '/:id/transfer_account/:toAccount',
|
||||
// this.transferToAnotherAccount.validation,
|
||||
// asyncMiddleware(this.transferToAnotherAccount.handler)
|
||||
// );
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -142,11 +136,7 @@ export default class AccountsController extends BaseController{
|
||||
*/
|
||||
get accountsListSchema() {
|
||||
return [
|
||||
query('display_type').optional().isIn(['tree', 'flat']),
|
||||
query('account_types').optional().isArray(),
|
||||
query('account_types.*').optional().isNumeric().toInt(),
|
||||
query('custom_view_id').optional().isNumeric().toInt(),
|
||||
|
||||
query('stringified_filter_roles').optional().isJSON(),
|
||||
|
||||
query('column_sort_by').optional(),
|
||||
@@ -176,10 +166,7 @@ export default class AccountsController extends BaseController{
|
||||
|
||||
return res.status(200).send({ id: account.id });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.transformServiceErrorToResponse(res, error);
|
||||
}
|
||||
next();
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,10 +185,7 @@ export default class AccountsController extends BaseController{
|
||||
const account = await this.accountsService.editAccount(tenantId, accountId, accountDTO);
|
||||
return res.status(200).send({ id: account.id });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.transformServiceErrorToResponse(res, error);
|
||||
}
|
||||
next();
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,10 +204,7 @@ export default class AccountsController extends BaseController{
|
||||
return res.status(200).send({ account });
|
||||
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.transformServiceErrorToResponse(res, error);
|
||||
}
|
||||
next();
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,14 +221,8 @@ export default class AccountsController extends BaseController{
|
||||
try {
|
||||
await this.accountsService.deleteAccount(tenantId, accountId);
|
||||
return res.status(200).send({ id: accountId });
|
||||
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.transformServiceErrorToResponse(res, error);
|
||||
|
||||
|
||||
}
|
||||
next();
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,10 +240,7 @@ export default class AccountsController extends BaseController{
|
||||
await this.accountsService.activateAccount(tenantId, accountId, true);
|
||||
return res.status(200).send({ id: accountId });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.transformServiceErrorToResponse(res, error);
|
||||
}
|
||||
next();
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,10 +259,7 @@ export default class AccountsController extends BaseController{
|
||||
return res.status(200).send({ id: accountId });
|
||||
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.transformServiceErrorToResponse(res, error);
|
||||
}
|
||||
next();
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,10 +279,7 @@ export default class AccountsController extends BaseController{
|
||||
await this.accountsService.activateAccounts(tenantId, accountsIds, isActive)
|
||||
return res.status(200).send({ ids: accountsIds });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.transformServiceErrorToResponse(res, error);
|
||||
}
|
||||
next();
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -332,211 +298,117 @@ export default class AccountsController extends BaseController{
|
||||
return res.status(200).send({ ids: accountsIds });
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
if (error instanceof ServiceError) {
|
||||
this.transformServiceErrorToResponse(res, error);
|
||||
}
|
||||
next();
|
||||
/**
|
||||
* Retrieve accounts datatable list.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async getAccountsList(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const filter: IAccountsFilter = {
|
||||
filterRoles: [],
|
||||
sortOrder: 'asc',
|
||||
columnSortBy: 'name',
|
||||
...this.matchedQueryData(req),
|
||||
};
|
||||
if (filter.stringifiedFilterRoles) {
|
||||
filter.filterRoles = JSON.parse(filter.stringifiedFilterRoles);
|
||||
}
|
||||
try {
|
||||
const accounts = await this.accountsService.getAccountsList(tenantId, filter);
|
||||
return res.status(200).send({ accounts });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms service errors to response.
|
||||
* @param {Error}
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {ServiceError} error
|
||||
*/
|
||||
transformServiceErrorToResponse(res: Response, error: ServiceError) {
|
||||
console.log(error.errorType);
|
||||
if (error.errorType === 'account_not_found') {
|
||||
return res.boom.notFound(
|
||||
'The given account not found.', {
|
||||
errors: [{ type: 'ACCOUNT.NOT.FOUND', code: 100 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_name_not_unqiue') {
|
||||
return res.boom.badRequest(
|
||||
'The given account not unique.',
|
||||
{ errors: [{ type: 'ACCOUNT.NAME.NOT.UNIQUE', code: 150 }], }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_type_not_found') {
|
||||
return res.boom.badRequest(
|
||||
'The given account type not found.', {
|
||||
errors: [{ type: 'ACCOUNT_TYPE_NOT_FOUND', code: 200 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_type_not_allowed_to_changed') {
|
||||
return res.boom.badRequest(
|
||||
'Not allowed to change account type of the account.',
|
||||
{ errors: [{ type: 'NOT.ALLOWED.TO.CHANGE.ACCOUNT.TYPE', code: 300 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'parent_account_not_found') {
|
||||
return res.boom.badRequest(
|
||||
'The parent account not found.',
|
||||
{ errors: [{ type: 'PARENT_ACCOUNT_NOT_FOUND', code: 400 }] },
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'parent_has_different_type') {
|
||||
return res.boom.badRequest(
|
||||
'The parent account has different type.',
|
||||
{ errors: [{ type: 'PARENT.ACCOUNT.HAS.DIFFERENT.ACCOUNT.TYPE', code: 500 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_code_not_unique') {
|
||||
return res.boom.badRequest(
|
||||
'The given account code is not unique.',
|
||||
{ errors: [{ type: 'NOT_UNIQUE_CODE', code: 600 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_has_children') {
|
||||
return res.boom.badRequest(
|
||||
'You could not delete account has children.',
|
||||
{ errors: [{ type: 'ACCOUNT.HAS.CHILD.ACCOUNTS', code: 700 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_has_associated_transactions') {
|
||||
return res.boom.badRequest(
|
||||
'You could not delete account has associated transactions.',
|
||||
{ errors: [{ type: 'ACCOUNT.HAS.ASSOCIATED.TRANSACTIONS', code: 800 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_predefined') {
|
||||
return res.boom.badRequest(
|
||||
'You could not delete predefined account',
|
||||
{ errors: [{ type: 'ACCOUNT.PREDEFINED', code: 900 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'accounts_not_found') {
|
||||
return res.boom.notFound(
|
||||
'Some of the given accounts not found.',
|
||||
{ errors: [{ type: 'SOME.ACCOUNTS.NOT_FOUND', code: 1000 }] },
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'predefined_accounts') {
|
||||
return res.boom.badRequest(
|
||||
'Some of the given accounts are predefined.',
|
||||
{ errors: [{ type: 'ACCOUNTS_PREDEFINED', code: 1100 }] }
|
||||
);
|
||||
catchServiceErrors(error, req: Request, res: Response, next: NextFunction) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'account_not_found') {
|
||||
return res.boom.notFound(
|
||||
'The given account not found.',
|
||||
{ errors: [{ type: 'ACCOUNT.NOT.FOUND', code: 100 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_name_not_unqiue') {
|
||||
return res.boom.badRequest(
|
||||
'The given account not unique.',
|
||||
{ errors: [{ type: 'ACCOUNT.NAME.NOT.UNIQUE', code: 150 }], }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_type_not_found') {
|
||||
return res.boom.badRequest(
|
||||
'The given account type not found.', {
|
||||
errors: [{ type: 'ACCOUNT_TYPE_NOT_FOUND', code: 200 }]
|
||||
}
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_type_not_allowed_to_changed') {
|
||||
return res.boom.badRequest(
|
||||
'Not allowed to change account type of the account.',
|
||||
{ errors: [{ type: 'NOT.ALLOWED.TO.CHANGE.ACCOUNT.TYPE', code: 300 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'parent_account_not_found') {
|
||||
return res.boom.badRequest(
|
||||
'The parent account not found.',
|
||||
{ errors: [{ type: 'PARENT_ACCOUNT_NOT_FOUND', code: 400 }] },
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'parent_has_different_type') {
|
||||
return res.boom.badRequest(
|
||||
'The parent account has different type.',
|
||||
{ errors: [{ type: 'PARENT.ACCOUNT.HAS.DIFFERENT.ACCOUNT.TYPE', code: 500 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_code_not_unique') {
|
||||
return res.boom.badRequest(
|
||||
'The given account code is not unique.',
|
||||
{ errors: [{ type: 'NOT_UNIQUE_CODE', code: 600 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_has_children') {
|
||||
return res.boom.badRequest(
|
||||
'You could not delete account has children.',
|
||||
{ errors: [{ type: 'ACCOUNT.HAS.CHILD.ACCOUNTS', code: 700 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_has_associated_transactions') {
|
||||
return res.boom.badRequest(
|
||||
'You could not delete account has associated transactions.',
|
||||
{ errors: [{ type: 'ACCOUNT.HAS.ASSOCIATED.TRANSACTIONS', code: 800 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'account_predefined') {
|
||||
return res.boom.badRequest(
|
||||
'You could not delete predefined account',
|
||||
{ errors: [{ type: 'ACCOUNT.PREDEFINED', code: 900 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'accounts_not_found') {
|
||||
return res.boom.notFound(
|
||||
'Some of the given accounts not found.',
|
||||
{ errors: [{ type: 'SOME.ACCOUNTS.NOT_FOUND', code: 1000 }] },
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'predefined_accounts') {
|
||||
return res.boom.badRequest(
|
||||
'Some of the given accounts are predefined.',
|
||||
{ errors: [{ type: 'ACCOUNTS_PREDEFINED', code: 1100 }] }
|
||||
);
|
||||
}
|
||||
}
|
||||
next(error)
|
||||
}
|
||||
|
||||
|
||||
// /**
|
||||
// * Retrieve accounts list.
|
||||
// */
|
||||
// getAccountsList(req, res) {
|
||||
// const validationErrors = validationResult(req);
|
||||
|
||||
// if (!validationErrors.isEmpty()) {
|
||||
// return res.boom.badData(null, {
|
||||
// code: 'validation_error',
|
||||
// ...validationErrors,
|
||||
// });
|
||||
// }
|
||||
// const filter = {
|
||||
// display_type: 'flat',
|
||||
// account_types: [],
|
||||
// filter_roles: [],
|
||||
// sort_order: 'asc',
|
||||
// ...req.query,
|
||||
// };
|
||||
// if (filter.stringified_filter_roles) {
|
||||
// filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
|
||||
// }
|
||||
// const { Resource, Account, View } = req.models;
|
||||
// const errorReasons = [];
|
||||
|
||||
// const accountsResource = await Resource.query()
|
||||
// .remember()
|
||||
// .where('name', 'accounts')
|
||||
// .withGraphFetched('fields')
|
||||
// .first();
|
||||
|
||||
// if (!accountsResource) {
|
||||
// return res.status(400).send({
|
||||
// errors: [{ type: 'ACCOUNTS_RESOURCE_NOT_FOUND', code: 200 }],
|
||||
// });
|
||||
// }
|
||||
// const resourceFieldsKeys = accountsResource.fields.map((c) => c.key);
|
||||
|
||||
// const view = await View.query().onBuild((builder) => {
|
||||
// if (filter.custom_view_id) {
|
||||
// builder.where('id', filter.custom_view_id);
|
||||
// } else {
|
||||
// builder.where('favourite', true);
|
||||
// }
|
||||
// // builder.where('resource_id', accountsResource.id);
|
||||
// builder.withGraphFetched('roles.field');
|
||||
// builder.withGraphFetched('columns');
|
||||
// builder.first();
|
||||
|
||||
// builder.remember();
|
||||
// });
|
||||
// const dynamicFilter = new DynamicFilter(Account.tableName);
|
||||
|
||||
// if (filter.column_sort_by) {
|
||||
// if (resourceFieldsKeys.indexOf(filter.column_sort_by) === -1) {
|
||||
// errorReasons.push({ type: 'COLUMN.SORT.ORDER.NOT.FOUND', code: 300 });
|
||||
// }
|
||||
// const sortByFilter = new DynamicFilterSortBy(
|
||||
// filter.column_sort_by,
|
||||
// filter.sort_order
|
||||
// );
|
||||
// dynamicFilter.setFilter(sortByFilter);
|
||||
// }
|
||||
// // View roles.
|
||||
// if (view && view.roles.length > 0) {
|
||||
// const viewFilter = new DynamicFilterViews(
|
||||
// mapViewRolesToConditionals(view.roles),
|
||||
// view.rolesLogicExpression
|
||||
// );
|
||||
// if (!viewFilter.validateFilterRoles()) {
|
||||
// errorReasons.push({
|
||||
// type: 'VIEW.LOGIC.EXPRESSION.INVALID',
|
||||
// code: 400,
|
||||
// });
|
||||
// }
|
||||
// dynamicFilter.setFilter(viewFilter);
|
||||
// }
|
||||
// // Filter roles.
|
||||
// if (filter.filter_roles.length > 0) {
|
||||
// // Validate the accounts resource fields.
|
||||
// const filterRoles = new DynamicFilterFilterRoles(
|
||||
// mapFilterRolesToDynamicFilter(filter.filter_roles),
|
||||
// accountsResource.fields
|
||||
// );
|
||||
// dynamicFilter.setFilter(filterRoles);
|
||||
|
||||
// if (filterRoles.validateFilterRoles().length > 0) {
|
||||
// errorReasons.push({
|
||||
// type: 'ACCOUNTS.RESOURCE.HAS.NO.GIVEN.FIELDS',
|
||||
// code: 500,
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// if (errorReasons.length > 0) {
|
||||
// return res.status(400).send({ errors: errorReasons });
|
||||
// }
|
||||
|
||||
// const accounts = await Account.query().onBuild((builder) => {
|
||||
// builder.modify('filterAccountTypes', filter.account_types);
|
||||
// builder.withGraphFetched('type');
|
||||
// builder.withGraphFetched('balance');
|
||||
|
||||
// dynamicFilter.buildQuery()(builder);
|
||||
// });
|
||||
// return res.status(200).send({
|
||||
// accounts:
|
||||
// filter.display_type === 'tree'
|
||||
// ? Account.toNestedArray(accounts)
|
||||
// : accounts,
|
||||
// ...(view
|
||||
// ? {
|
||||
// customViewId: view.id,
|
||||
// }
|
||||
// : {}),
|
||||
// });
|
||||
// }
|
||||
};
|
||||
|
||||
@@ -1,17 +1,28 @@
|
||||
import { Response, Request } from 'express';
|
||||
import { Response, Request, NextFunction } from 'express';
|
||||
import { matchedData, validationResult } from "express-validator";
|
||||
import { camelCase, omit } from "lodash";
|
||||
import { mapKeysDeep } from 'utils'
|
||||
|
||||
export default class BaseController {
|
||||
|
||||
private dataToCamelCase(data) {
|
||||
return mapKeysDeep(data, (v, k) => camelCase(k));
|
||||
}
|
||||
|
||||
matchedBodyData(req: Request, options: any = {}) {
|
||||
const data = matchedData(req, {
|
||||
locations: ['body'],
|
||||
includeOptionals: true,
|
||||
...omit(options, ['locations']), // override any propery except locations.
|
||||
});
|
||||
return mapKeysDeep(data, (v, k) => camelCase(k));
|
||||
return this.dataToCamelCase(data);
|
||||
}
|
||||
|
||||
matchedQueryData(req: Request) {
|
||||
const data = matchedData(req, {
|
||||
locations: ['query'],
|
||||
});
|
||||
return this.dataToCamelCase(data);
|
||||
}
|
||||
|
||||
validationResult(req: Request, res: Response, next: NextFunction) {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { Request, Response, Router, NextFunction } from 'express';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { check } from 'express-validator';
|
||||
import { check, query } from 'express-validator';
|
||||
import ContactsController from 'api/controllers/Contacts/Contacts';
|
||||
import VendorsService from 'services/Contacts/VendorsService';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import { IVendorNewDTO, IVendorEditDTO } from 'interfaces';
|
||||
import { IVendorNewDTO, IVendorEditDTO, IVendorsFilter } from 'interfaces';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
|
||||
@Service()
|
||||
@@ -52,6 +52,12 @@ export default class VendorsController extends ContactsController {
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.getVendor.bind(this))
|
||||
);
|
||||
router.get('/', [
|
||||
...this.vendorsListSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.getVendorsList.bind(this)),
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -64,6 +70,22 @@ export default class VendorsController extends ContactsController {
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Vendors datatable list validation schema.
|
||||
*/
|
||||
get vendorsListSchema() {
|
||||
return [
|
||||
query('custom_view_id').optional().isNumeric().toInt(),
|
||||
query('stringified_filter_roles').optional().isJSON(),
|
||||
|
||||
query('column_sort_by').optional(),
|
||||
query('sort_order').optional().isIn(['desc', 'asc']),
|
||||
|
||||
query('page').optional().isNumeric().toInt(),
|
||||
query('page_size').optional().isNumeric().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new vendor.
|
||||
* @param {Request} req
|
||||
@@ -192,4 +214,24 @@ export default class VendorsController extends ContactsController {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve vendors datatable list.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async getVendorsList(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const vendorsFilter: IVendorsFilter = {
|
||||
filterRoles: [],
|
||||
...this.matchedBodyData(req),
|
||||
};
|
||||
try {
|
||||
const vendors = await this.vendorsService.getVendorsList(tenantId, vendorsFilter);
|
||||
return res.status(200).send({ vendors });
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,16 @@ import BaseController from "api/controllers/BaseController";
|
||||
import ExpensesService from "services/Expenses/ExpensesService";
|
||||
import { IExpenseDTO } from 'interfaces';
|
||||
import { ServiceError } from "exceptions";
|
||||
import DynamicListingService from 'services/DynamicListing/DynamicListService';
|
||||
|
||||
@Service()
|
||||
export default class ExpensesController extends BaseController {
|
||||
@Inject()
|
||||
expensesService: ExpensesService;
|
||||
|
||||
@Inject()
|
||||
dynamicListService: DynamicListingService;
|
||||
|
||||
/**
|
||||
* Express router.
|
||||
*/
|
||||
@@ -23,19 +27,22 @@ export default class ExpensesController extends BaseController {
|
||||
...this.expenseDTOSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.newExpense.bind(this))
|
||||
asyncMiddleware(this.newExpense.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.post('/publish', [
|
||||
...this.bulkSelectSchema,
|
||||
],
|
||||
this.bulkPublishExpenses.bind(this)
|
||||
this.bulkPublishExpenses.bind(this),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.post(
|
||||
'/:id/publish', [
|
||||
...this.expenseParamSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.publishExpense.bind(this))
|
||||
asyncMiddleware(this.publishExpense.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.post(
|
||||
'/:id', [
|
||||
@@ -44,19 +51,28 @@ export default class ExpensesController extends BaseController {
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.editExpense.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.delete(
|
||||
'/:id', [
|
||||
...this.expenseParamSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.deleteExpense.bind(this))
|
||||
asyncMiddleware(this.deleteExpense.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.delete('/', [
|
||||
...this.bulkSelectSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.bulkDeleteExpenses.bind(this))
|
||||
asyncMiddleware(this.bulkDeleteExpenses.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.get(
|
||||
'/',
|
||||
asyncMiddleware(this.getExpensesList.bind(this)),
|
||||
this.dynamicListService.handlerErrorsToResponse,
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
return router;
|
||||
}
|
||||
@@ -118,9 +134,6 @@ export default class ExpensesController extends BaseController {
|
||||
const expense = await this.expensesService.newExpense(tenantId, expenseDTO, user);
|
||||
return res.status(200).send({ id: expense.id });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.serviceErrorsTransformer(res, error);
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
@@ -140,9 +153,6 @@ export default class ExpensesController extends BaseController {
|
||||
await this.expensesService.editExpense(tenantId, expenseId, expenseDTO, user);
|
||||
return res.status(200).send({ id: expenseId });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.serviceErrorsTransformer(res, error);
|
||||
}
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
@@ -161,9 +171,6 @@ export default class ExpensesController extends BaseController {
|
||||
await this.expensesService.deleteExpense(tenantId, expenseId)
|
||||
return res.status(200).send({ id: expenseId });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.serviceErrorsTransformer(res, error);
|
||||
}
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
@@ -182,9 +189,6 @@ export default class ExpensesController extends BaseController {
|
||||
await this.expensesService.publishExpense(tenantId, expenseId)
|
||||
return res.status(200).send({ });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.serviceErrorsTransformer(req, error);
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
@@ -203,9 +207,6 @@ export default class ExpensesController extends BaseController {
|
||||
await this.expensesService.deleteBulkExpenses(tenantId, expensesIds);
|
||||
return res.status(200).send({ ids: expensesIds });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.serviceErrorsTransformer(req, error);
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
@@ -218,9 +219,23 @@ export default class ExpensesController extends BaseController {
|
||||
await this.expensesService.publishBulkExpenses(tenantId,);
|
||||
return res.status(200).send({});
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
this.serviceErrorsTransformer(req, error);
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async getExpensesList(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const filter = {
|
||||
filterRoles: [],
|
||||
sortOrder: 'asc',
|
||||
...this.matchedQueryData(req),
|
||||
};
|
||||
|
||||
try {
|
||||
const expenses = await this.expensesService.getExpensesList(tenantId, filter);
|
||||
return res.status(200).send({ expenses });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
@@ -230,36 +245,39 @@ export default class ExpensesController extends BaseController {
|
||||
* @param {Response} res
|
||||
* @param {ServiceError} error
|
||||
*/
|
||||
serviceErrorsTransformer(res, error: ServiceError) {
|
||||
if (error.errorType === 'expense_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'EXPENSE_NOT_FOUND' }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'total_amount_equals_zero') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'TOTAL.AMOUNT.EQUALS.ZERO' }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'payment_account_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'PAYMENT.ACCOUNT.NOT.FOUND', }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'some_expenses_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'SOME.EXPENSE.ACCOUNTS.NOT.FOUND', code: 200 }]
|
||||
})
|
||||
}
|
||||
if (error.errorType === 'payment_account_has_invalid_type') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'PAYMENT.ACCOUNT.HAS.INVALID.TYPE' }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'expenses_account_has_invalid_type') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'EXPENSES.ACCOUNT.HAS.INVALID.TYPE' }]
|
||||
});
|
||||
catchServiceErrors(error: Error, req: Request, res: Response, next: NextFunction) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'expense_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'EXPENSE_NOT_FOUND' }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'total_amount_equals_zero') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'TOTAL.AMOUNT.EQUALS.ZERO' }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'payment_account_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'PAYMENT.ACCOUNT.NOT.FOUND', }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'some_expenses_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'SOME.EXPENSE.ACCOUNTS.NOT.FOUND', code: 200 }]
|
||||
})
|
||||
}
|
||||
if (error.errorType === 'payment_account_has_invalid_type') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'PAYMENT.ACCOUNT.HAS.INVALID.TYPE' }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'expenses_account_has_invalid_type') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'EXPENSES.ACCOUNT.HAS.INVALID.TYPE' }]
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
@@ -8,14 +8,6 @@ import { difference } from 'lodash';
|
||||
import { Service } from 'typedi';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
import validateMiddleware from 'api/middleware/validateMiddleware';
|
||||
import {
|
||||
DynamicFilter,
|
||||
DynamicFilterSortBy,
|
||||
DynamicFilterFilterRoles,
|
||||
} from 'lib/DynamicFilter';
|
||||
import {
|
||||
mapFilterRolesToDynamicFilter,
|
||||
} from 'lib/ViewRolesBuilder';
|
||||
import { IItemCategory, IItemCategoryOTD } from 'interfaces';
|
||||
import BaseController from 'api/controllers/BaseController';
|
||||
|
||||
@@ -336,67 +328,7 @@ export default class ItemsCategoriesController extends BaseController {
|
||||
* @return {Response}
|
||||
*/
|
||||
async getList(req: Request, res: Response) {
|
||||
const { Resource, ItemCategory } = req.models;
|
||||
const categoriesResource = await Resource.query()
|
||||
.where('name', 'items_categories')
|
||||
.withGraphFetched('fields')
|
||||
.first();
|
||||
|
||||
if (!categoriesResource) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'ITEMS.CATEGORIES.RESOURCE.NOT.FOUND', code: 200 }],
|
||||
});
|
||||
}
|
||||
const filter = {
|
||||
column_sort_order: '',
|
||||
sort_order: '',
|
||||
filter_roles: [],
|
||||
...req.query,
|
||||
};
|
||||
if (filter.stringified_filter_roles) {
|
||||
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
|
||||
}
|
||||
const errorReasons = [];
|
||||
const resourceFieldsKeys = categoriesResource.fields.map((c) => c.key);
|
||||
const dynamicFilter = new DynamicFilter(ItemCategory.tableName);
|
||||
|
||||
// Dynamic filter with filter roles.
|
||||
if (filter.filter_roles.length > 0) {
|
||||
// Validate the accounts resource fields.
|
||||
const filterRoles = new DynamicFilterFilterRoles(
|
||||
mapFilterRolesToDynamicFilter(filter.filter_roles),
|
||||
categoriesResource.fields,
|
||||
);
|
||||
categoriesResource.setFilter(filterRoles);
|
||||
|
||||
if (filterRoles.validateFilterRoles().length > 0) {
|
||||
errorReasons.push({ type: 'ITEMS.RESOURCE.HAS.NO.FIELDS', code: 500 });
|
||||
}
|
||||
}
|
||||
// Dynamic filter with column sort order.
|
||||
if (filter.column_sort_order) {
|
||||
if (resourceFieldsKeys.indexOf(filter.column_sort_order) === -1) {
|
||||
errorReasons.push({ type: 'COLUMN.SORT.ORDER.NOT.FOUND', code: 300 });
|
||||
}
|
||||
const sortByFilter = new DynamicFilterSortBy(
|
||||
filter.column_sort_order,
|
||||
filter.sort_order,
|
||||
);
|
||||
dynamicFilter.setFilter(sortByFilter);
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.status(400).send({ errors: errorReasons });
|
||||
}
|
||||
const categories = await ItemCategory.query().onBuild((builder) => {
|
||||
dynamicFilter.buildQuery()(builder);
|
||||
|
||||
builder.select([
|
||||
'*',
|
||||
ItemCategory.relatedQuery('items').count().as('count'),
|
||||
]);
|
||||
});
|
||||
|
||||
return res.status(200).send({ categories });
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { check, param, query, ValidationChain, matchedData } from 'express-validator';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
import validateMiddleware from 'api/middleware/validateMiddleware';
|
||||
import ItemsService from 'services/Items/ItemsService';
|
||||
import DynamicListing from 'services/DynamicListing/DynamicListing';
|
||||
import DynamicListingBuilder from 'services/DynamicListing/DynamicListingBuilder';
|
||||
import { dynamicListingErrorsToResponse } from 'services/DynamicListing/hasDynamicListing';
|
||||
import BaseController from 'api/controllers/BaseController';
|
||||
import DynamicListingService from 'services/DynamicListing/DynamicListService';
|
||||
|
||||
@Service()
|
||||
export default class ItemsController {
|
||||
export default class ItemsController extends BaseController {
|
||||
@Inject()
|
||||
itemsService: ItemsService;
|
||||
|
||||
@Inject()
|
||||
dynamicListService: DynamicListingService;
|
||||
|
||||
/**
|
||||
* Router constructor.
|
||||
*/
|
||||
@@ -62,7 +64,8 @@ export default class ItemsController {
|
||||
'/',
|
||||
this.validateListQuerySchema,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.listItems.bind(this)),
|
||||
asyncMiddleware(this.getItemsList.bind(this)),
|
||||
this.dynamicListService.handlerErrorsToResponse,
|
||||
);
|
||||
return router;
|
||||
}
|
||||
@@ -131,7 +134,7 @@ export default class ItemsController {
|
||||
*/
|
||||
get validateListQuerySchema() {
|
||||
return [
|
||||
query('column_sort_order').optional().isIn(['created_at', 'name', 'amount', 'sku']),
|
||||
query('column_sort_order').optional().trim().escape(),
|
||||
query('sort_order').optional().isIn(['desc', 'asc']),
|
||||
query('page').optional().isNumeric().toInt(),
|
||||
query('page_size').optional().isNumeric().toInt(),
|
||||
@@ -353,79 +356,25 @@ export default class ItemsController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Listing items with pagination metadata.
|
||||
* Retrieve items datatable list.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async listItems(req: Request, res: Response) {
|
||||
async getItemsList(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const filter = {
|
||||
filter_roles: [],
|
||||
sort_order: 'asc',
|
||||
page: 1,
|
||||
page_size: 10,
|
||||
...req.query,
|
||||
filterRoles: [],
|
||||
sortOrder: 'asc',
|
||||
...this.matchedQueryData(req),
|
||||
};
|
||||
if (filter.stringified_filter_roles) {
|
||||
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
|
||||
if (filter.stringifiedFilterRoles) {
|
||||
filter.filterRoles = JSON.parse(filter.stringifiedFilterRoles);
|
||||
}
|
||||
const { Resource, Item, View } = req.models;
|
||||
const resource = await Resource.query()
|
||||
.remember()
|
||||
.where('name', 'items')
|
||||
.withGraphFetched('fields')
|
||||
.first();
|
||||
|
||||
if (!resource) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'ITEMS.RESOURCE.NOT_FOUND', code: 200 }],
|
||||
});
|
||||
try {
|
||||
const items = await this.itemsService.getItemsList(tenantId, filter);
|
||||
return res.status(200).send({ items });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
const viewMeta = await View.query()
|
||||
.modify('allMetadata')
|
||||
.modify('specificOrFavourite', filter.custom_view_id)
|
||||
.where('resource_id', resource.id)
|
||||
.first();
|
||||
|
||||
const listingBuilder = new DynamicListingBuilder();
|
||||
const errorReasons = [];
|
||||
|
||||
listingBuilder.addModelClass(Item);
|
||||
listingBuilder.addCustomViewId(filter.custom_view_id);
|
||||
listingBuilder.addFilterRoles(filter.filter_roles);
|
||||
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
|
||||
listingBuilder.addView(viewMeta);
|
||||
|
||||
const dynamicListing = new DynamicListing(listingBuilder);
|
||||
|
||||
if (dynamicListing instanceof Error) {
|
||||
const errors = dynamicListingErrorsToResponse(dynamicListing);
|
||||
errorReasons.push(...errors);
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.status(400).send({ errors: errorReasons });
|
||||
}
|
||||
const items = await Item.query().onBuild((builder: any) => {
|
||||
builder.withGraphFetched('costAccount');
|
||||
builder.withGraphFetched('sellAccount');
|
||||
builder.withGraphFetched('inventoryAccount');
|
||||
builder.withGraphFetched('category');
|
||||
|
||||
dynamicListing.buildQuery()(builder);
|
||||
return builder;
|
||||
}).pagination(filter.page - 1, filter.page_size);
|
||||
|
||||
return res.status(200).send({
|
||||
items: {
|
||||
...items,
|
||||
...(viewMeta
|
||||
? {
|
||||
viewMeta: {
|
||||
custom_view_id: viewMeta.id,
|
||||
view_columns: viewMeta.columns,
|
||||
}
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,16 +68,15 @@ export default class OrganizationController extends BaseController{
|
||||
});
|
||||
}
|
||||
}
|
||||
console.log(error);
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Seeds initial data to tenant database.
|
||||
* @param req
|
||||
* @param res
|
||||
* @param next
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async seed(req: Request, res: Response, next: Function) {
|
||||
const { organizationId } = req.tenant;
|
||||
|
||||
@@ -9,9 +9,6 @@ import BillsService from 'services/Purchases/Bills';
|
||||
import BaseController from 'api/controllers/BaseController';
|
||||
import ItemsService from 'services/Items/ItemsService';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import DynamicListingBuilder from 'services/DynamicListing/DynamicListingBuilder';
|
||||
import DynamicListing from 'services/DynamicListing/DynamicListing';
|
||||
import { dynamicListingErrorsToResponse } from 'services/DynamicListing/HasDynamicListing';
|
||||
|
||||
@Service()
|
||||
export default class BillsController extends BaseController {
|
||||
@@ -330,71 +327,6 @@ export default class BillsController extends BaseController {
|
||||
* @return {Response}
|
||||
*/
|
||||
async listingBills(req: Request, res: Response) {
|
||||
const filter = {
|
||||
filter_roles: [],
|
||||
sort_order: 'asc',
|
||||
page: 1,
|
||||
page_size: 10,
|
||||
...req.query,
|
||||
};
|
||||
if (filter.stringified_filter_roles) {
|
||||
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
|
||||
}
|
||||
const { Bill, View, Resource } = req.models;
|
||||
const resource = await Resource.query()
|
||||
.remember()
|
||||
.where('name', 'bills')
|
||||
.withGraphFetched('fields')
|
||||
.first();
|
||||
|
||||
if (!resource) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'BILLS_RESOURCE_NOT_FOUND', code: 200 }],
|
||||
});
|
||||
}
|
||||
const viewMeta = await View.query()
|
||||
.modify('allMetadata')
|
||||
.modify('specificOrFavourite', filter.custom_view_id)
|
||||
.where('resource_id', resource.id)
|
||||
.first();
|
||||
|
||||
const listingBuilder = new DynamicListingBuilder();
|
||||
const errorReasons = [];
|
||||
|
||||
listingBuilder.addModelClass(Bill);
|
||||
listingBuilder.addCustomViewId(filter.custom_view_id);
|
||||
listingBuilder.addFilterRoles(filter.filter_roles);
|
||||
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
|
||||
listingBuilder.addView(viewMeta);
|
||||
|
||||
const dynamicListing = new DynamicListing(listingBuilder);
|
||||
|
||||
if (dynamicListing instanceof Error) {
|
||||
const errors = dynamicListingErrorsToResponse(dynamicListing);
|
||||
errorReasons.push(...errors);
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.status(400).send({ errors: errorReasons });
|
||||
}
|
||||
const bills = await Bill.query()
|
||||
.onBuild((builder) => {
|
||||
dynamicListing.buildQuery()(builder);
|
||||
builder.withGraphFetched('vendor');
|
||||
return builder;
|
||||
})
|
||||
.pagination(filter.page - 1, filter.page_size);
|
||||
|
||||
return res.status(200).send({
|
||||
bills: {
|
||||
...bills,
|
||||
...(viewMeta
|
||||
? {
|
||||
view_meta: {
|
||||
customViewId: viewMeta.id,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
import { Router } from 'express';
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { check, param, query, ValidationChain, matchedData } from 'express-validator';
|
||||
import { difference } from 'lodash';
|
||||
@@ -8,9 +8,6 @@ import validateMiddleware from 'api/middleware/validateMiddleware';
|
||||
import BaseController from 'api/controllers/BaseController';
|
||||
import BillPaymentsService from 'services/Purchases/BillPayments';
|
||||
import AccountsService from 'services/Accounts/AccountsService';
|
||||
import DynamicListingBuilder from 'services/DynamicListing/DynamicListingBuilder';
|
||||
import DynamicListing from 'services/DynamicListing/DynamicListing';
|
||||
import { dynamicListingErrorsToResponse } from 'services/DynamicListing/hasDynamicListing';
|
||||
|
||||
/**
|
||||
* Bills payments controller.
|
||||
@@ -384,70 +381,6 @@ export default class BillsPayments extends BaseController {
|
||||
* @return {Response}
|
||||
*/
|
||||
async getBillsPayments(req: Request, res: Response) {
|
||||
const filter = {
|
||||
filter_roles: [],
|
||||
sort_order: 'asc',
|
||||
page: 1,
|
||||
page_size: 10,
|
||||
...req.query,
|
||||
};
|
||||
if (filter.stringified_filter_roles) {
|
||||
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
|
||||
}
|
||||
const { BillPayment, View, Resource } = req.models;
|
||||
|
||||
const resource = await Resource.query()
|
||||
.where('name', 'bill_payments')
|
||||
.withGraphFetched('fields')
|
||||
.first();
|
||||
|
||||
if (!resource) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'BILL.PAYMENTS.RESOURCE.NOT_FOUND', code: 200 }],
|
||||
});
|
||||
}
|
||||
const viewMeta = await View.query()
|
||||
.modify('allMetadata')
|
||||
.modify('specificOrFavourite', filter.custom_view_id)
|
||||
.where('resource_id', resource.id)
|
||||
.first();
|
||||
|
||||
const listingBuilder = new DynamicListingBuilder();
|
||||
const errorReasons = [];
|
||||
|
||||
listingBuilder.addModelClass(BillPayment);
|
||||
listingBuilder.addCustomViewId(filter.custom_view_id);
|
||||
listingBuilder.addFilterRoles(filter.filter_roles);
|
||||
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
|
||||
listingBuilder.addView(viewMeta);
|
||||
|
||||
const dynamicListing = new DynamicListing(listingBuilder);
|
||||
|
||||
if (dynamicListing instanceof Error) {
|
||||
const errors = dynamicListingErrorsToResponse(dynamicListing);
|
||||
errorReasons.push(...errors);
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.status(400).send({ errors: errorReasons });
|
||||
}
|
||||
const billPayments = await BillPayment.query().onBuild((builder) => {
|
||||
dynamicListing.buildQuery()(builder);
|
||||
builder.withGraphFetched('vendor');
|
||||
builder.withGraphFetched('paymentAccount');
|
||||
return builder;
|
||||
}).pagination(filter.page - 1, filter.page_size);
|
||||
|
||||
return res.status(200).send({
|
||||
bill_payments: {
|
||||
...billPayments,
|
||||
...(viewMeta
|
||||
? {
|
||||
view_meta: {
|
||||
customViewId: viewMeta.id,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,6 @@ import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
import PaymentReceiveService from 'services/Sales/PaymentsReceives';
|
||||
import SaleInvoiceService from 'services/Sales/SalesInvoices';
|
||||
import AccountsService from 'services/Accounts/AccountsService';
|
||||
import DynamicListing from 'services/DynamicListing/DynamicListing';
|
||||
import DynamicListingBuilder from 'services/DynamicListing/DynamicListingBuilder';
|
||||
import { dynamicListingErrorsToResponse } from 'services/DynamicListing/hasDynamicListing';
|
||||
|
||||
/**
|
||||
* Payments receives controller.
|
||||
@@ -406,71 +403,6 @@ export default class PaymentReceivesController extends BaseController {
|
||||
* @return {Response}
|
||||
*/
|
||||
async getPaymentReceiveList(req: Request, res: Response) {
|
||||
const filter = {
|
||||
filter_roles: [],
|
||||
sort_order: 'asc',
|
||||
page: 1,
|
||||
page_size: 10,
|
||||
...req.query,
|
||||
};
|
||||
if (filter.stringified_filter_roles) {
|
||||
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
|
||||
}
|
||||
const { Resource, PaymentReceive, View, Bill } = req.models;
|
||||
const resource = await Resource.query()
|
||||
.remember()
|
||||
.where('name', 'payment_receives')
|
||||
.withGraphFetched('fields')
|
||||
.first();
|
||||
|
||||
if (!resource) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'PAYMENT_RECEIVES_RESOURCE_NOT_FOUND', code: 200 }],
|
||||
});
|
||||
}
|
||||
const viewMeta = await View.query()
|
||||
.modify('allMetadata')
|
||||
.modify('specificOrFavourite', filter.custom_view_id)
|
||||
.where('resource_id', resource.id)
|
||||
.first();
|
||||
|
||||
const listingBuilder = new DynamicListingBuilder();
|
||||
const errorReasons = [];
|
||||
|
||||
listingBuilder.addModelClass(Bill);
|
||||
listingBuilder.addCustomViewId(filter.custom_view_id);
|
||||
listingBuilder.addFilterRoles(filter.filter_roles);
|
||||
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
|
||||
listingBuilder.addView(viewMeta);
|
||||
|
||||
const dynamicListing = new DynamicListing(listingBuilder);
|
||||
|
||||
if (dynamicListing instanceof Error) {
|
||||
const errors = dynamicListingErrorsToResponse(dynamicListing);
|
||||
errorReasons.push(...errors);
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.status(400).send({ errors: errorReasons });
|
||||
}
|
||||
const paymentReceives = await PaymentReceive.query().onBuild((builder) => {
|
||||
builder.withGraphFetched('customer');
|
||||
builder.withGraphFetched('depositAccount');
|
||||
|
||||
dynamicListing.buildQuery()(builder);
|
||||
return builder;
|
||||
}).pagination(filter.page - 1, filter.page_size);
|
||||
|
||||
return res.status(200).send({
|
||||
payment_receives: {
|
||||
...paymentReceives,
|
||||
...(viewMeta
|
||||
? {
|
||||
viewMeta: {
|
||||
customViewId: viewMeta.id,
|
||||
}
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,6 @@ import validateMiddleware from 'api/middleware/validateMiddleware';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
import SaleEstimateService from 'services/Sales/SalesEstimate';
|
||||
import ItemsService from 'services/Items/ItemsService';
|
||||
import DynamicListingBuilder from 'services/DynamicListing/DynamicListingBuilder';
|
||||
import DynamicListing from 'services/DynamicListing/DynamicListing';
|
||||
|
||||
@Service()
|
||||
export default class SalesEstimatesController extends BaseController {
|
||||
@@ -301,69 +299,6 @@ export default class SalesEstimatesController extends BaseController {
|
||||
* @param {Response} res
|
||||
*/
|
||||
async getEstimates(req: Request, res: Response) {
|
||||
const filter = {
|
||||
filter_roles: [],
|
||||
sort_order: 'asc',
|
||||
page: 1,
|
||||
page_size: 10,
|
||||
...req.query,
|
||||
};
|
||||
if (filter.stringified_filter_roles) {
|
||||
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
|
||||
}
|
||||
const { SaleEstimate, Resource, View } = req.models;
|
||||
const resource = await Resource.query()
|
||||
.remember()
|
||||
.where('name', 'sales_estimates')
|
||||
.withGraphFetched('fields')
|
||||
.first();
|
||||
|
||||
if (!resource) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'RESOURCE.NOT.FOUND', code: 200, }],
|
||||
});
|
||||
}
|
||||
const viewMeta = await View.query()
|
||||
.modify('allMetadata')
|
||||
.modify('specificOrFavourite', filter.custom_view_id)
|
||||
.where('resource_id', resource.id)
|
||||
.first();
|
||||
|
||||
const listingBuilder = new DynamicListingBuilder();
|
||||
const errorReasons = [];
|
||||
|
||||
listingBuilder.addView(viewMeta);
|
||||
listingBuilder.addModelClass(SaleEstimate);
|
||||
listingBuilder.addCustomViewId(filter.custom_view_id);
|
||||
listingBuilder.addFilterRoles(filter.filter_roles);
|
||||
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
|
||||
|
||||
const dynamicListing = new DynamicListing(listingBuilder);
|
||||
|
||||
if (dynamicListing instanceof Error) {
|
||||
const errors = dynamicListingErrorsToResponse(dynamicListing);
|
||||
errorReasons.push(...errors);
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.status(400).send({ errors: errorReasons });
|
||||
}
|
||||
|
||||
const salesEstimates = await SaleEstimate.query().onBuild((builder) => {
|
||||
dynamicListing.buildQuery()(builder);
|
||||
builder.withGraphFetched('customer');
|
||||
return builder;
|
||||
}).pagination(filter.page - 1, filter.page_size);
|
||||
|
||||
return res.status(200).send({
|
||||
sales_estimates: {
|
||||
...salesEstimates,
|
||||
...(viewMeta ? {
|
||||
viewMeta: {
|
||||
custom_view_id: viewMeta.id,
|
||||
},
|
||||
} : {}),
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
@@ -7,9 +7,6 @@ import validateMiddleware from 'api/middleware/validateMiddleware';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
import SaleInvoiceService from 'services/Sales/SalesInvoices';
|
||||
import ItemsService from 'services/Items/ItemsService';
|
||||
import DynamicListing from 'services/DynamicListing/DynamicListing';
|
||||
import DynamicListingBuilder from 'services/DynamicListing/DynamicListingBuilder';
|
||||
import { dynamicListingErrorsToResponse } from 'services/DynamicListing/hasDynamicListing';
|
||||
import { ISaleInvoiceOTD } from 'interfaces';
|
||||
|
||||
@Service()
|
||||
@@ -415,69 +412,6 @@ export default class SaleInvoicesController {
|
||||
* @param {Function} next
|
||||
*/
|
||||
async getSalesInvoices(req, res) {
|
||||
const filter = {
|
||||
filter_roles: [],
|
||||
sort_order: 'asc',
|
||||
page: 1,
|
||||
page_size: 10,
|
||||
...req.query,
|
||||
};
|
||||
if (filter.stringified_filter_roles) {
|
||||
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
|
||||
}
|
||||
const { SaleInvoice, View, Resource } = req.models;
|
||||
const resource = await Resource.query()
|
||||
.remember()
|
||||
.where('name', 'sales_invoices')
|
||||
.withGraphFetched('fields')
|
||||
.first();
|
||||
|
||||
if (!resource) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'SALES_INVOICES_RESOURCE_NOT_FOUND', code: 200 }],
|
||||
});
|
||||
}
|
||||
const viewMeta = await View.query()
|
||||
.modify('allMetadata')
|
||||
.modify('specificOrFavourite', filter.custom_view_id)
|
||||
.where('resource_id', resource.id)
|
||||
.first();
|
||||
|
||||
const listingBuilder = new DynamicListingBuilder();
|
||||
const errorReasons = [];
|
||||
|
||||
listingBuilder.addModelClass(SaleInvoice);
|
||||
listingBuilder.addCustomViewId(filter.custom_view_id);
|
||||
listingBuilder.addFilterRoles(filter.filter_roles);
|
||||
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
|
||||
listingBuilder.addView(viewMeta);
|
||||
|
||||
const dynamicListing = new DynamicListing(listingBuilder);
|
||||
|
||||
if (dynamicListing instanceof Error) {
|
||||
const errors = dynamicListingErrorsToResponse(dynamicListing);
|
||||
errorReasons.push(...errors);
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.status(400).send({ errors: errorReasons });
|
||||
}
|
||||
const salesInvoices = await SaleInvoice.query().onBuild((builder) => {
|
||||
builder.withGraphFetched('entries');
|
||||
builder.withGraphFetched('customer');
|
||||
dynamicListing.buildQuery()(builder);
|
||||
}).pagination(filter.page - 1, filter.page_size);
|
||||
|
||||
return res.status(200).send({
|
||||
sales_invoices: {
|
||||
...salesInvoices,
|
||||
...(viewMeta
|
||||
? {
|
||||
view_meta: {
|
||||
customViewId: viewMeta.id,
|
||||
}
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,6 @@ import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
import AccountsService from 'services/Accounts/AccountsService';
|
||||
import ItemsService from 'services/Items/ItemsService';
|
||||
import SaleReceiptService from 'services/Sales/SalesReceipts';
|
||||
import DynamicListingBuilder from 'services/DynamicListing/DynamicListingBuilder';
|
||||
import DynamicListing from 'services/DynamicListing/DynamicListing';
|
||||
import {
|
||||
dynamicListingErrorsToResponse
|
||||
} from 'services/DynamicListing/HasDynamicListing';
|
||||
|
||||
@Service()
|
||||
export default class SalesReceiptsController {
|
||||
@@ -302,65 +297,6 @@ export default class SalesReceiptsController {
|
||||
* @param {Response} res
|
||||
*/
|
||||
async listingSalesReceipts(req: Request, res: Response) {
|
||||
const filter = {
|
||||
filter_roles: [],
|
||||
sort_order: 'asc',
|
||||
page: 1,
|
||||
page_size: 10,
|
||||
...req.query,
|
||||
};
|
||||
if (filter.stringified_filter_roles) {
|
||||
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
|
||||
}
|
||||
const { SaleReceipt, Resource, View } = req.models;
|
||||
const resource = await Resource.query()
|
||||
.remember()
|
||||
.where('name', 'sales_receipts')
|
||||
.withGraphFetched('fields')
|
||||
.first();
|
||||
|
||||
if (!resource) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'RESOURCE.NOT.FOUND', code: 200, }],
|
||||
});
|
||||
}
|
||||
const viewMeta = await View.query()
|
||||
.modify('allMetadata')
|
||||
.modify('specificOrFavourite', filter.custom_view_id)
|
||||
.where('resource_id', resource.id)
|
||||
.first();
|
||||
|
||||
const listingBuilder = new DynamicListingBuilder();
|
||||
const errorReasons = [];
|
||||
|
||||
listingBuilder.addView(viewMeta);
|
||||
listingBuilder.addModelClass(SaleReceipt);
|
||||
listingBuilder.addCustomViewId(filter.custom_view_id);
|
||||
listingBuilder.addFilterRoles(filter.filter_roles);
|
||||
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
|
||||
|
||||
const dynamicListing = new DynamicListing(listingBuilder);
|
||||
|
||||
if (dynamicListing instanceof Error) {
|
||||
const errors = dynamicListingErrorsToResponse(dynamicListing);
|
||||
errorReasons.push(...errors);
|
||||
}
|
||||
const salesReceipts = await SaleReceipt.query().onBuild((builder) => {
|
||||
builder.withGraphFetched('customer');
|
||||
builder.withGraphFetched('depositAccount');
|
||||
builder.withGraphFetched('entries');
|
||||
dynamicListing.buildQuery()(builder);
|
||||
}).pagination(filter.page - 1, filter.page_size);
|
||||
|
||||
return res.status(200).send({
|
||||
sales_receipts: {
|
||||
...salesReceipts,
|
||||
...(viewMeta ? {
|
||||
view_meta: {
|
||||
customViewId: viewMeta.id,
|
||||
}
|
||||
} : {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -178,7 +178,6 @@ export default class LicensesController extends BaseController {
|
||||
message: 'The licenses have been generated successfully.'
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,7 +134,6 @@ export default class UsersController extends BaseController{
|
||||
await this.usersService.deleteUser(tenantId, id);
|
||||
return res.status(200).send({ id });
|
||||
} catch (error) {
|
||||
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'user_not_found') {
|
||||
return res.boom.notFound(null, {
|
||||
@@ -142,7 +141,7 @@ export default class UsersController extends BaseController{
|
||||
});
|
||||
}
|
||||
}
|
||||
next();
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,8 +159,6 @@ export default class UsersController extends BaseController{
|
||||
const user = await this.usersService.getUser(tenantId, userId);
|
||||
return res.status(200).send({ user });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'user_not_found') {
|
||||
return res.boom.notFound(null, {
|
||||
@@ -169,7 +166,7 @@ export default class UsersController extends BaseController{
|
||||
});
|
||||
}
|
||||
}
|
||||
next();
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,7 +183,7 @@ export default class UsersController extends BaseController{
|
||||
|
||||
return res.status(200).send({ users });
|
||||
} catch (error) {
|
||||
next();
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,8 +201,6 @@ export default class UsersController extends BaseController{
|
||||
await this.usersService.activateUser(tenantId, userId);
|
||||
return res.status(200).send({ id: userId });
|
||||
} catch(error) {
|
||||
console.log(error);
|
||||
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'user_not_found') {
|
||||
return res.status(404).send({
|
||||
@@ -218,7 +213,7 @@ export default class UsersController extends BaseController{
|
||||
});
|
||||
}
|
||||
}
|
||||
next();
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,7 +243,7 @@ export default class UsersController extends BaseController{
|
||||
});
|
||||
}
|
||||
}
|
||||
next();
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user