mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 22:30:31 +00:00
fear: implement interface to dynamic list service.
This commit is contained in:
@@ -17,4 +17,9 @@ export interface IDynamicListFilterDTO {
|
|||||||
filterRoles?: IFilterRole[],
|
filterRoles?: IFilterRole[],
|
||||||
columnSortBy: string,
|
columnSortBy: string,
|
||||||
sortOrder: string,
|
sortOrder: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IDynamicListService {
|
||||||
|
dynamicList(tenantId: number, model: any, filter: IDynamicListFilterDTO): Promise<any>;
|
||||||
|
handlerErrorsToResponse(error, req, res, next): void;
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ import {
|
|||||||
} from 'lib/ViewRolesBuilder';
|
} from 'lib/ViewRolesBuilder';
|
||||||
|
|
||||||
import TenancyService from 'services/Tenancy/TenancyService';
|
import TenancyService from 'services/Tenancy/TenancyService';
|
||||||
import { IDynamicListFilterDTO, IFilterRole } from 'interfaces';
|
import { IDynamicListFilterDTO, IFilterRole, IDynamicListService } from 'interfaces';
|
||||||
|
|
||||||
const ERRORS = {
|
const ERRORS = {
|
||||||
VIEW_NOT_FOUND: 'view_not_found',
|
VIEW_NOT_FOUND: 'view_not_found',
|
||||||
@@ -23,50 +23,17 @@ const ERRORS = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class DynamicListService {
|
export default class DynamicListService implements IDynamicListService {
|
||||||
@Inject()
|
@Inject()
|
||||||
tenancy: TenancyService;
|
tenancy: TenancyService;
|
||||||
|
|
||||||
/**
|
|
||||||
* Middleware to catch services errors
|
|
||||||
* @param {Error} error
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
*/
|
|
||||||
handlerErrorsToResponse(error, req: Request, res: Response, next: NextFunction) {
|
|
||||||
if (error instanceof ServiceError) {
|
|
||||||
if (error.errorType === 'sort_column_not_found') {
|
|
||||||
return res.boom.badRequest(null, {
|
|
||||||
errors: [{ type: 'SORT.COLUMN.NOT.FOUND', code: 200 }],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (error.errorType === 'view_not_found') {
|
|
||||||
return res.boom.badRequest(null, {
|
|
||||||
errors: [{ type: 'CUSTOM.VIEW.NOT.FOUND', code: 100 }]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (error.errorType === 'filter_roles_fields_not_found') {
|
|
||||||
return res.boom.badRequest(null, {
|
|
||||||
errors: [{ type: 'FILTER.ROLES.FIELDS.NOT.FOUND', code: 300 }],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (error.errorType === 'stringified_filter_roles_invalid') {
|
|
||||||
return res.boom.badRequest(null, {
|
|
||||||
errors: [{ type: 'STRINGIFIED_FILTER_ROLES_INVALID', code: 400 }],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retreive custom view or throws error not found.
|
* Retreive custom view or throws error not found.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
* @param {number} viewId
|
* @param {number} viewId
|
||||||
* @return {Promise<IView>}
|
* @return {Promise<IView>}
|
||||||
*/
|
*/
|
||||||
async getCustomViewOrThrowError(tenantId: number, viewId: number) {
|
private async getCustomViewOrThrowError(tenantId: number, viewId: number) {
|
||||||
const { viewRepository } = this.tenancy.repositories(tenantId);
|
const { viewRepository } = this.tenancy.repositories(tenantId);
|
||||||
const view = await viewRepository.getById(viewId);
|
const view = await viewRepository.getById(viewId);
|
||||||
|
|
||||||
@@ -82,7 +49,7 @@ export default class DynamicListService {
|
|||||||
* @param {string} columnSortBy - Sort column
|
* @param {string} columnSortBy - Sort column
|
||||||
* @throws {ServiceError}
|
* @throws {ServiceError}
|
||||||
*/
|
*/
|
||||||
validateSortColumnExistance(model: any, columnSortBy: string) {
|
private validateSortColumnExistance(model: any, columnSortBy: string) {
|
||||||
const notExistsField = validateFieldKeyExistance(model.tableName, columnSortBy);
|
const notExistsField = validateFieldKeyExistance(model.tableName, columnSortBy);
|
||||||
|
|
||||||
if (notExistsField) {
|
if (notExistsField) {
|
||||||
@@ -96,7 +63,7 @@ export default class DynamicListService {
|
|||||||
* @param {IFilterRole[]} filterRoles
|
* @param {IFilterRole[]} filterRoles
|
||||||
* @throws {ServiceError}
|
* @throws {ServiceError}
|
||||||
*/
|
*/
|
||||||
validateRolesFieldsExistance(model: any, filterRoles: IFilterRole[]) {
|
private validateRolesFieldsExistance(model: any, filterRoles: IFilterRole[]) {
|
||||||
const invalidFieldsKeys = validateFilterRolesFieldsExistance(model.tableName, filterRoles);
|
const invalidFieldsKeys = validateFilterRolesFieldsExistance(model.tableName, filterRoles);
|
||||||
|
|
||||||
if (invalidFieldsKeys.length > 0) {
|
if (invalidFieldsKeys.length > 0) {
|
||||||
@@ -108,7 +75,7 @@ export default class DynamicListService {
|
|||||||
* Validates filter roles schema.
|
* Validates filter roles schema.
|
||||||
* @param {IFilterRole[]} filterRoles
|
* @param {IFilterRole[]} filterRoles
|
||||||
*/
|
*/
|
||||||
validateFilterRolesSchema(filterRoles: IFilterRole[]) {
|
private validateFilterRolesSchema(filterRoles: IFilterRole[]) {
|
||||||
const validate = validator({
|
const validate = validator({
|
||||||
required: true,
|
required: true,
|
||||||
type: 'object',
|
type: 'object',
|
||||||
@@ -164,4 +131,37 @@ export default class DynamicListService {
|
|||||||
}
|
}
|
||||||
return dynamicFilter;
|
return dynamicFilter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Middleware to catch services errors
|
||||||
|
* @param {Error} error
|
||||||
|
* @param {Request} req
|
||||||
|
* @param {Response} res
|
||||||
|
* @param {NextFunction} next
|
||||||
|
*/
|
||||||
|
handlerErrorsToResponse(error, req: Request, res: Response, next: NextFunction) {
|
||||||
|
if (error instanceof ServiceError) {
|
||||||
|
if (error.errorType === 'sort_column_not_found') {
|
||||||
|
return res.boom.badRequest(null, {
|
||||||
|
errors: [{ type: 'SORT.COLUMN.NOT.FOUND', code: 200 }],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (error.errorType === 'view_not_found') {
|
||||||
|
return res.boom.badRequest(null, {
|
||||||
|
errors: [{ type: 'CUSTOM.VIEW.NOT.FOUND', code: 100 }]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if (error.errorType === 'filter_roles_fields_not_found') {
|
||||||
|
return res.boom.badRequest(null, {
|
||||||
|
errors: [{ type: 'FILTER.ROLES.FIELDS.NOT.FOUND', code: 300 }],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (error.errorType === 'stringified_filter_roles_invalid') {
|
||||||
|
return res.boom.badRequest(null, {
|
||||||
|
errors: [{ type: 'STRINGIFIED_FILTER_ROLES_INVALID', code: 400 }],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user