Skip to content

Commit

Permalink
remove entity validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Orit Persik authored and Avraham Ostreicher committed Jun 10, 2019
1 parent 379697c commit 3285d16
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions server/controllers/system.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
remove,
tree,
updateTreeNames,
findEntity,
}

async function insert(userId, system) {
Expand Down Expand Up @@ -52,3 +53,9 @@ async function updateTreeNames(platforms, tree) {
return category;
});
}

async function findEntity(entityId) {
return await System.find({
$or: [{platform: entityId}, {equipment: {$in: [entityId]}}]
});
}
9 changes: 8 additions & 1 deletion server/routes/entity.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const passport = require('passport');
const httpError = require('http-errors');
const asyncHandler = require('express-async-handler')
const entityCtrl = require('../controllers/entity.controller');
const systemCtrl = require('../controllers/system.controller');
const requireAdmin = require('../middleware/require-admin');

const router = express.Router();
Expand All @@ -22,7 +23,7 @@ router.route('/clone/:entityId/:modeName')
router.route('/:entityId/:modeName?')
.get(asyncHandler(get))
.put(asyncHandler(findOne), asyncHandler(checkUniqueMode), asyncHandler(update))
.delete(asyncHandler(remove));
.delete(asyncHandler(validateRemove), asyncHandler(remove));

async function insert(req, res) {
let entityData = await entityCtrl.insert(req.user, req.body);
Expand Down Expand Up @@ -71,3 +72,9 @@ async function remove(req, res) {
if(!entity) throw new httpError(404);
res.json(entity);
}

async function validateRemove(req, res, next) {
let systems = await systemCtrl.findEntity(req.params.entityId);
if(systems && systems.length) throw new httpError(403, `This entity exists in ${systems[0].name} system.`);
next();
}

0 comments on commit 3285d16

Please sign in to comment.