refactoring: custom views service.

fix: constraints of delete item from storage.
fix: constraints of delete item category from storage.
fix: localize database seeds files.
fix: view meta data in accounts list response.
This commit is contained in:
Ahmed Bouhuolia
2020-10-05 19:09:56 +02:00
parent 0114ed9f8b
commit 99e6fe273f
64 changed files with 1593 additions and 1103 deletions

View File

@@ -30,7 +30,8 @@ export default class ExpensesController extends BaseController {
asyncMiddleware(this.newExpense.bind(this)),
this.catchServiceErrors,
);
router.post('/publish', [
router.post(
'/publish', [
...this.bulkSelectSchema,
],
this.bulkPublishExpenses.bind(this),
@@ -69,7 +70,9 @@ export default class ExpensesController extends BaseController {
this.catchServiceErrors,
);
router.get(
'/',
'/', [
...this.expensesListSchema,
],
asyncMiddleware(this.getExpensesList.bind(this)),
this.dynamicListService.handlerErrorsToResponse,
this.catchServiceErrors,
@@ -89,6 +92,7 @@ export default class ExpensesController extends BaseController {
check('currency_code').optional(),
check('exchange_rate').optional().isNumeric().toFloat(),
check('publish').optional().isBoolean().toBoolean(),
check('categories').exists().isArray({ min: 1 }),
check('categories.*.index').exists().isNumeric().toInt(),
check('categories.*.expense_account_id').exists().isNumeric().toInt(),
@@ -120,6 +124,20 @@ export default class ExpensesController extends BaseController {
];
}
get expensesListSchema() {
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 expense on
* @param {Request} req
@@ -240,12 +258,23 @@ export default class ExpensesController extends BaseController {
const filter = {
filterRoles: [],
sortOrder: 'asc',
columnSortBy: 'created_at',
page: 1,
pageSize: 12,
...this.matchedQueryData(req),
};
if (filter.stringifiedFilterRoles) {
filter.filterRoles = JSON.parse(filter.stringifiedFilterRoles);
}
try {
const expenses = await this.expensesService.getExpensesList(tenantId, filter);
return res.status(200).send({ expenses });
const { expenses, pagination, filterMeta } = await this.expensesService.getExpensesList(tenantId, filter);
return res.status(200).send({
expenses,
pagination: this.transfromToResponse(pagination),
filter_meta: this.transfromToResponse(filterMeta),
});
} catch (error) {
next(error);
}
@@ -259,29 +288,34 @@ export default class ExpensesController extends BaseController {
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', code: 100 }],
});
return res.boom.badRequest(
'Expense not found.',
{ errors: [{ type: 'EXPENSE_NOT_FOUND', code: 100 }] }
);
}
if (error.errorType === 'total_amount_equals_zero') {
return res.boom.badRequest(null, {
errors: [{ type: 'TOTAL.AMOUNT.EQUALS.ZERO', code: 200 }],
});
return res.boom.badRequest(
'Expense total should not equal zero.',
{ errors: [{ type: 'TOTAL.AMOUNT.EQUALS.ZERO', code: 200 }] },
);
}
if (error.errorType === 'payment_account_not_found') {
return res.boom.badRequest(null, {
errors: [{ type: 'PAYMENT.ACCOUNT.NOT.FOUND', code: 300 }],
});
return res.boom.badRequest(
'Payment account not found.',
{ errors: [{ type: 'PAYMENT.ACCOUNT.NOT.FOUND', code: 300 }] },
);
}
if (error.errorType === 'some_expenses_not_found') {
return res.boom.badRequest(null, {
errors: [{ type: 'SOME.EXPENSE.ACCOUNTS.NOT.FOUND', code: 400 }]
})
return res.boom.badRequest(
'Some expense accounts not found.',
{ errors: [{ type: 'SOME.EXPENSE.ACCOUNTS.NOT.FOUND', code: 400 }] },
);
}
if (error.errorType === 'payment_account_has_invalid_type') {
return res.boom.badRequest(null, {
errors: [{ type: 'PAYMENT.ACCOUNT.HAS.INVALID.TYPE', code: 500 }],
});
return res.boom.badRequest(
'Payment account has invalid type.',
{ errors: [{ type: 'PAYMENT.ACCOUNT.HAS.INVALID.TYPE', code: 500 }], },
);
}
if (error.errorType === 'expenses_account_has_invalid_type') {
return res.boom.badRequest(null, {