-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(network): add handling of Antenna Criteria (#662)
Co-authored-by: Rik Smale <[email protected]>
- Loading branch information
1 parent
3135bd6
commit 7bd43e1
Showing
9 changed files
with
441 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.