mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +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,
|
||||
// }
|
||||
// : {}),
|
||||
// });
|
||||
// }
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user