nodejs-rigorous
Version:
Rigorous Framework
93 lines (58 loc) • 2.66 kB
JavaScript
/* eslint class-methods-use-this: 0 */
const _ = require('lodash');
const { RigorousError, errorTypes } = require('../../factory/RigorousError/index');
const RigorousRoute = require('../RigorousRoute');
const h_secure_user_input = require('../../helpers/h_secure_user_input');
const h_format_checker = require('../../helpers/h_format_checker');
class Route extends RigorousRoute {
constructor(middlewares, modelService, authorizationUserId) {
super(middlewares, {});
this.modelService = modelService;
this.authorizationUserId = authorizationUserId;
}
async secureInput(req) {
const body = _.get(req, 'body');
this.userIdAsking = req.authuser.user_id;
const inputs = {
ids: h_secure_user_input.escapeHtml(_.get(body, 'ids')),
};
return inputs;
}
async checkConformityInput(inputs) {
const checker = {};
const checkConformityIdInput = async (ids) => {
const promises = [];
for (let i = 0; i < ids.length; i += 1) {
const id = ids[i];
promises.push(this.modelService.readIt({ _id: id }, { selectAttributesReturned: { [this.authorizationUserId]: 1 } }));
}
const modelCheckers = await Promise.all(promises);
modelCheckers.forEach((modelChecker) => { if (h_format_checker.isNil(modelChecker)) { throw new RigorousError(errorTypes.RESPONSE_ERROR_DATA_NOT_CONFORM); } });
return modelCheckers;
};
checker.modelCheckers = await checkConformityIdInput(inputs.ids);
checker.modelCheckers.forEach((modelChecker) => { if (h_format_checker.isNil(modelChecker)) { throw new RigorousError(errorTypes.RESPONSE_ERROR_DATA_NOT_CONFORM); } });
return checker;
}
async checkAuthorization(checker) {
checker.modelCheckers.forEach((modelChecker) => {
if (!modelChecker[this.authorizationUserId].equals(this.userIdAsking)) { throw new RigorousError(errorTypes.RESPONSE_ERROR_DATA_NOT_CONFORM); }
});
}
async processData(inputs, checker) {
const promises = [];
for (let i = 0; i < inputs.ids.length; i += 1) {
const id = inputs.ids[i];
promises.push(this.modelService.deleteIt({ _id: id }));
}
await Promise.all(promises);
return {
result: null,
notificationParam: inputs,
};
}
async sendNotification(inputs, notificationParam) {
// None
}
}
module.exports = Route;