Skip to content

Commit

Permalink
feat(network): add handling of Antenna Criteria (#662)
Browse files Browse the repository at this point in the history
Co-authored-by: Rik Smale <[email protected]>
  • Loading branch information
LeonVreling and WikiRik authored Sep 15, 2024
1 parent 3135bd6 commit 7bd43e1
Show file tree
Hide file tree
Showing 9 changed files with 441 additions and 3 deletions.
42 changes: 42 additions & 0 deletions lib/antenna_criteria.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { AntennaCriterion } = require('../models');
const errors = require('./errors');

exports.listCriteria = async (req, res) => {
if (!req.permissions.manage_antenna_criteria) {
return errors.makeForbiddenError(res, 'You are not allowed to list Antenna Criteria.');
}

const criteria = await AntennaCriterion.findAll({
where: { agora_id: Number(req.params.agora_id) }
});

return res.json({
success: true,
data: criteria
});
};

exports.setCriterion = async (req, res) => {
if (!['communication', 'board election', 'members list', 'membership fee', 'events', 'agora attendance', 'development plan', 'fulfilment report'].includes(req.body.antenna_criterion)) {
return errors.makeValidationError(res, 'This is not a valid criterion.');
}

const criterion = req.body.antenna_criterion.replace(/ /g, '_');

if (!req.permissions.antenna_criteria[criterion]) {
return errors.makeForbiddenError(res, 'You are not allowed to set fulfilment of the criterion ' + criterion + '.');
}

if (req.body.antenna_criterion === 'communication' && req.body.value === 'exception' && !req.permissions.antenna_criteria.communication_exception) {
return errors.makeForbiddenError(res, 'You are not allowed to give exceptions to the Antenna Criterion `communication`.');
}

// The `.upsert()` method returns an object [result, created],
// where `created` is a boolen whether the result was created or updated
const result = await AntennaCriterion.upsert(req.body);

return res.json({
success: true,
data: result[0]
});
};
15 changes: 14 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ function getBodiesListFromPermissions(result) {

exports.getPermissions = (user, corePermissions, managePermissions) => {
const permissions = {
view_board: hasPermission(corePermissions, 'view:board')
view_board: hasPermission(corePermissions, 'view:board'),
manage_antenna_criteria: hasPermission(corePermissions, 'global:manage_network:antenna_criteria')
};

permissions.manage_boards = {
Expand All @@ -62,6 +63,18 @@ exports.getPermissions = (user, corePermissions, managePermissions) => {
permissions.manage_boards[body.id] = manageBoardsList.includes(body.id);
}

permissions.antenna_criteria = {
communication: hasPermission(corePermissions, 'global:manage_network:communication'),
communication_exception: hasPermission(corePermissions, 'global:manage_network:communication_exception'),
board_election: hasPermission(corePermissions, 'global:manage_network:board_election'),
members_list: hasPermission(corePermissions, 'global:manage_network:members_list'),
membership_fee: hasPermission(corePermissions, 'global:manage_network:membership_fee'),
events: hasPermission(corePermissions, 'global:manage_network:events'),
agora_attendance: hasPermission(corePermissions, 'global:manage_network:agora_attendance'),
development_plan: hasPermission(corePermissions, 'global:manage_network:development_plan'),
fulfilment_report: hasPermission(corePermissions, 'global:manage_network:fulfilment_report'),
};

return permissions;
};

Expand Down
4 changes: 4 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Bugsnag = require('./bugsnag');
const morgan = require('./morgan');
const middlewares = require('./middlewares');
const boards = require('./boards');
const antennaCriteria = require('./antenna_criteria');
const metrics = require('./metrics');
const endpointsMetrics = require('./endpoints_metrics');
const db = require('./sequelize');
Expand Down Expand Up @@ -40,6 +41,9 @@ GeneralRouter.get('/bodies/:body_id/boards/:board_id', boards.findBoard, boards.
GeneralRouter.put('/bodies/:body_id/boards/:board_id', boards.findBoard, boards.updateBoard);
GeneralRouter.delete('/bodies/:body_id/boards/:board_id', boards.findBoard, boards.deleteBoard);

GeneralRouter.get('/antennaCriteria/:agora_id', antennaCriteria.listCriteria);
GeneralRouter.put('/antennaCriteria', antennaCriteria.setCriterion);

server.use(endpointsMetrics.addEndpointMetrics);
server.use('/', GeneralRouter);
server.use(middlewares.notFound);
Expand Down
36 changes: 36 additions & 0 deletions migrations/20240811135640-create-antenna-criterion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('antennaCriteria', {
agora_id: {
allowNull: false,
type: Sequelize.INTEGER,
primaryKey: true
},
body_id: {
allowNull: false,
type: Sequelize.INTEGER,
primaryKey: true
},
antenna_criterion: {
allowNull: false,
type: Sequelize.ENUM('communication', 'board election', 'members list', 'membership fee', 'events', 'agora attendance', 'development plan', 'fulfilment report'),
primaryKey: true
},
value: {
allowNull: true,
type: Sequelize.ENUM('true', 'false', 'exception')
},
comment: {
allowNull: true,
type: Sequelize.TEXT
},
created_at: {
allowNull: false,
type: Sequelize.DATE
},
updated_at: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: (queryInterface) => queryInterface.dropTable('antennaCriteria')
};
55 changes: 55 additions & 0 deletions models/AntennaCriterion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { Sequelize, sequelize } = require('../lib/sequelize');

const AntennaCriterion = sequelize.define('antennaCriterion', {
agora_id: {
allowNull: false,
primaryKey: true,
type: Sequelize.INTEGER,
validate: {
notEmpty: { msg: 'Agora should be set.' },
isInt: { msg: 'Agora ID should be a number.' }
}
},
body_id: {
allowNull: false,
primaryKey: true,
type: Sequelize.INTEGER,
validate: {
notEmpty: { msg: 'Body should be set.' },
isInt: { msg: 'Body ID should be a number.' }
}
},
antenna_criterion: {
allowNull: false,
primaryKey: true,
type: Sequelize.ENUM('communication', 'board election', 'members list', 'membership fee', 'events', 'agora attendance', 'development plan', 'fulfilment report'),
validate: {
isIn: {
args: [['communication', 'board election', 'members list', 'membership fee', 'events', 'agora attendance', 'development plan', 'fulfilment report']],
msg: 'Antenna criterion must be one of these: "communication", "board election", "members list", "membership fee", "events", "agora attendance", "development plan", "fulfilment report".'
}
}
},
value: {
allowNull: true,
type: Sequelize.ENUM('true', 'false', 'exception'),
validate: {
isIn: {
args: [['true', 'false', 'exception']],
msg: 'Value must be one of these: "true", "false", "exception", or null.'
}
}
},
comment: {
allowNull: true,
type: Sequelize.TEXT
}
}, {
underscored: true,
tableName: 'antennaCriteria',
createdAt: 'created_at',
updatedAt: 'updated_at',
primaryKey: ['agora_id', 'body_id', 'antenna_criterion']
});

module.exports = AntennaCriterion;
3 changes: 2 additions & 1 deletion models/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const Board = require('./Board');
const AntennaCriterion = require('./AntennaCriterion');

module.exports = { Board };
module.exports = { Board, AntennaCriterion };
Loading

0 comments on commit 7bd43e1

Please sign in to comment.