feat: close specific account.

This commit is contained in:
Ahmed Bouhuolia
2020-10-05 22:05:04 +02:00
parent 63c4567e08
commit 681fa0560e
3 changed files with 118 additions and 3 deletions

View File

@@ -43,6 +43,15 @@ export default class AccountsController extends BaseController{
asyncMiddleware(this.inactivateAccount.bind(this)),
this.catchServiceErrors,
);
router.post(
'/:id/close', [
...this.accountParamSchema,
...this.closingAccountSchema,
],
this.validationResult,
asyncMiddleware(this.closeAccount.bind(this)),
this.catchServiceErrors,
)
router.post(
'/:id', [
...this.accountDTOSchema,
@@ -150,6 +159,13 @@ export default class AccountsController extends BaseController{
];
}
get closingAccountSchema() {
return [
check('to_account_id').exists().isNumeric().toInt(),
check('delete_after_closing').exists().isBoolean(),
]
}
/**
* Creates a new account.
* @param {Request} req -
@@ -330,6 +346,30 @@ export default class AccountsController extends BaseController{
}
}
/**
* Closes the given account.
* @param {Request} req
* @param {Response} res
* @param next
*/
async closeAccount(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { id: accountId } = req.params;
const closeAccountQuery = this.matchedBodyData(req);
try {
await this.accountsService.closeAccount(
tenantId,
accountId,
closeAccountQuery.toAccountId,
closeAccountQuery.deleteAfterClosing
);
return res.status(200).send({ id: accountId });
} catch (error) {
next(error);
}
}
/**
* Transforms service errors to response.
* @param {Error}
@@ -411,6 +451,12 @@ export default class AccountsController extends BaseController{
{ errors: [{ type: 'ACCOUNTS_PREDEFINED', code: 1100 }] }
);
}
if (error.errorType === 'close_account_and_to_account_not_same_type') {
return res.boom.badRequest(
'The close account has different root type with to account.',
{ errors: [{ type: 'CLOSE_ACCOUNT_AND_TO_ACCOUNT_NOT_SAME_TYPE', code: 1200 }] },
);
}
}
next(error)
}