WIP pass the failed tests.

This commit is contained in:
Ahmed Bouhuolia
2020-05-17 07:08:12 +02:00
parent 00de156c9f
commit 10f636d2bc
77 changed files with 2164 additions and 1403 deletions

View File

@@ -68,6 +68,10 @@ export default {
this.transferToAnotherAccount.validation,
asyncMiddleware(this.transferToAnotherAccount.handler));
router.post('/bulk/:type(activate|inactivate)',
this.bulkInactivateAccounts.validation,
asyncMiddleware(this.bulkInactivateAccounts.handler));
return router;
},
@@ -541,4 +545,47 @@ export default {
return res.status(200).send();
},
},
/**
* Bulk acvtivate/inactivate the given accounts.
*/
bulkInactivateAccounts: {
validation: [
query('ids').isArray({ min: 2 }),
query('ids.*').isNumeric().toInt(),
param('type').exists().isIn(['activate', 'inactivate']),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const filter = {
ids: [],
...req.query,
};
const { Account } = req.models;
const { type } = req.params;
const storedAccounts = await Account.query().whereIn('id', filter.ids);
const storedAccountsIds = storedAccounts.map((account) => account.id);
const notFoundAccounts = difference(filter.ids, storedAccountsIds);
if (notFoundAccounts.length > 0) {
return res.status(400).send({
errors: [{ type: 'ACCOUNTS.NOT.FOUND', code: 200 }],
});
}
const updatedAccounts = await Account.query()
.whereIn('id', storedAccountsIds)
.patch({
active: type === 'activate' ? 1 : 0,
});
return res.status(200).send({ ids: storedAccountsIds });
}
}
};