Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(network): add NetCom assignment and mail building and sending for Antenna Criteria fulfilment #687

Draft
wants to merge 8 commits into
base: stable
Choose a base branch
from
21 changes: 21 additions & 0 deletions lib/antenna_criteria.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { AntennaCriterion } = require('../models');
const errors = require('./errors');
const mailer = require('./mailer');

exports.listCriteria = async (req, res) => {
if (!req.permissions.manage_antenna_criteria) {
Expand Down Expand Up @@ -40,3 +41,23 @@
data: result[0]
});
};

exports.sendFulfilmentMail = async (req, res) => {
if (!req.permissions.send_mails) {
return errors.makeForbiddenError(res, 'You are not allowed to send Antenna Criteria fulfilment mails.');

Check warning on line 47 in lib/antenna_criteria.js

View check run for this annotation

Codecov / codecov/patch

lib/antenna_criteria.js#L47

Added line #L47 was not covered by tests
}

await mailer.sendMail({

Check warning on line 50 in lib/antenna_criteria.js

View check run for this annotation

Codecov / codecov/patch

lib/antenna_criteria.js#L50

Added line #L50 was not covered by tests
from: req.body.from,
to: req.body.to,
cc: req.body.cc,
subject: req.body.subject,
template: req.body.template,
reply_to: req.body.reply_to
});

return res.json({

Check warning on line 59 in lib/antenna_criteria.js

View check run for this annotation

Codecov / codecov/patch

lib/antenna_criteria.js#L59

Added line #L59 was not covered by tests
success: true,
message: 'Successfully sent Antenna Criteria fulfilment mail',
});
};
4 changes: 3 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ function getBodiesListFromPermissions(result) {
exports.getPermissions = (user, corePermissions, managePermissions) => {
const permissions = {
view_board: hasPermission(corePermissions, 'view:board'),
manage_antenna_criteria: hasPermission(corePermissions, 'global:manage_network:antenna_criteria')
manage_antenna_criteria: hasPermission(corePermissions, 'global:manage_network:antenna_criteria'),
manage_netcom_assignment: hasPermission(corePermissions, 'global:manage_network:netcom_assignment'),
send_mails: hasPermission(corePermissions, 'global:manage_network:fulfilment_email')
};

permissions.manage_boards = {
Expand Down
34 changes: 34 additions & 0 deletions lib/mail_component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { MailComponent } = require('../models');
const errors = require('./errors');

exports.listMailComponents = async (req, res) => {
if (!req.permissions.send_mails) {
return errors.makeForbiddenError(res, 'You are not allowed to list mail components.');

Check warning on line 6 in lib/mail_component.js

View check run for this annotation

Codecov / codecov/patch

lib/mail_component.js#L6

Added line #L6 was not covered by tests
}

const components = await MailComponent.findAll({

Check warning on line 9 in lib/mail_component.js

View check run for this annotation

Codecov / codecov/patch

lib/mail_component.js#L9

Added line #L9 was not covered by tests
where: { agora_id: Number(req.params.agora_id) }
});

return res.json({

Check warning on line 13 in lib/mail_component.js

View check run for this annotation

Codecov / codecov/patch

lib/mail_component.js#L13

Added line #L13 was not covered by tests
success: true,
data: components
});
};

exports.setMailComponent = async (req, res) => {
if (!req.permissions.send_mails) {
return errors.makeForbiddenError(res, 'You are not allowed to set mail components.');

Check warning on line 21 in lib/mail_component.js

View check run for this annotation

Codecov / codecov/patch

lib/mail_component.js#L21

Added line #L21 was not covered by tests
}

if (!['introduction', 'communication', 'board election', 'members list', 'membership fee', 'events', 'agora attendance', 'development plan', 'fulfilment report', 'closing'].includes(req.body.mail_component)) {
return errors.makeValidationError(res, 'This is not a valid mail component.');

Check warning on line 25 in lib/mail_component.js

View check run for this annotation

Codecov / codecov/patch

lib/mail_component.js#L25

Added line #L25 was not covered by tests
}

const result = await MailComponent.upsert(req.body);

Check warning on line 28 in lib/mail_component.js

View check run for this annotation

Codecov / codecov/patch

lib/mail_component.js#L28

Added line #L28 was not covered by tests

return res.json({

Check warning on line 30 in lib/mail_component.js

View check run for this annotation

Codecov / codecov/patch

lib/mail_component.js#L30

Added line #L30 was not covered by tests
success: true,
data: result[0]
});
};
4 changes: 3 additions & 1 deletion lib/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ module.exports.sendMail = async (options) => {
body: {
from: options.from,
to: options.to,
cc: options.cc,
subject: options.subject,
template: options.template,
parameters: options.parameters
parameters: options.parameters,
reply_to: options.reply_to
}
});

Expand Down
47 changes: 47 additions & 0 deletions lib/netcom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { Netcom } = require('../models');
const errors = require('./errors');

exports.listNetcomAssignment = async (req, res) => {
if (!req.permissions.manage_netcom_assignment) {
return errors.makeForbiddenError(res, 'You are not allowed to list NetCom assignment.');

Check warning on line 6 in lib/netcom.js

View check run for this annotation

Codecov / codecov/patch

lib/netcom.js#L6

Added line #L6 was not covered by tests
}

const netcom = await Netcom.findAll();

Check warning on line 9 in lib/netcom.js

View check run for this annotation

Codecov / codecov/patch

lib/netcom.js#L9

Added line #L9 was not covered by tests

return res.json({

Check warning on line 11 in lib/netcom.js

View check run for this annotation

Codecov / codecov/patch

lib/netcom.js#L11

Added line #L11 was not covered by tests
success: true,
data: netcom
});
};

exports.setNetcomAssignment = async (req, res) => {
if (!req.permissions.manage_netcom_assignment) {
return errors.makeForbiddenError(res, 'You are not allowed to set NetCom assignment.');

Check warning on line 19 in lib/netcom.js

View check run for this annotation

Codecov / codecov/patch

lib/netcom.js#L19

Added line #L19 was not covered by tests
}

const result = await Netcom.upsert(req.body);

Check warning on line 22 in lib/netcom.js

View check run for this annotation

Codecov / codecov/patch

lib/netcom.js#L22

Added line #L22 was not covered by tests

return res.json({

Check warning on line 24 in lib/netcom.js

View check run for this annotation

Codecov / codecov/patch

lib/netcom.js#L24

Added line #L24 was not covered by tests
success: true,
data: result[0]
});
};

exports.removeNetcomAssignment = async (req, res) => {
if (!req.permissions.manage_netcom_assignment) {
return errors.makeForbiddenError(res, 'You are not allowed to remove NetCom assignment.');

Check warning on line 32 in lib/netcom.js

View check run for this annotation

Codecov / codecov/patch

lib/netcom.js#L32

Added line #L32 was not covered by tests
}

const assignment = await Netcom.findOne({

Check warning on line 35 in lib/netcom.js

View check run for this annotation

Codecov / codecov/patch

lib/netcom.js#L35

Added line #L35 was not covered by tests
where: {
body_id: req.params.body_id
}
});

await assignment.destroy();

Check warning on line 41 in lib/netcom.js

View check run for this annotation

Codecov / codecov/patch

lib/netcom.js#L41

Added line #L41 was not covered by tests

return res.json({

Check warning on line 43 in lib/netcom.js

View check run for this annotation

Codecov / codecov/patch

lib/netcom.js#L43

Added line #L43 was not covered by tests
success: true,
message: 'NetCom assignment was deleted.'
});
};
10 changes: 10 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const morgan = require('./morgan');
const middlewares = require('./middlewares');
const boards = require('./boards');
const antennaCriteria = require('./antenna_criteria');
const netcom = require('./netcom');
const mailComponent = require('./mail_component');
const metrics = require('./metrics');
const endpointsMetrics = require('./endpoints_metrics');
const db = require('./sequelize');
Expand Down Expand Up @@ -44,6 +46,14 @@ GeneralRouter.delete('/bodies/:body_id/boards/:board_id', boards.findBoard, boar

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

GeneralRouter.get('/netcom', netcom.listNetcomAssignment);
GeneralRouter.put('/netcom', netcom.setNetcomAssignment);
GeneralRouter.delete('/netcom/:body_id', netcom.removeNetcomAssignment);

GeneralRouter.get('/mailComponent/:agora_id', mailComponent.listMailComponents);
GeneralRouter.put('/mailComponent', mailComponent.setMailComponent);

server.use(endpointsMetrics.addEndpointMetrics);
server.use('/', GeneralRouter);
Expand Down
22 changes: 22 additions & 0 deletions migrations/20240923115900-create-netcom-assignment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('netcom', {
body_id: {
allowNull: false,
type: Sequelize.INTEGER,
primaryKey: true
},
netcom_id: {
allowNull: false,
type: Sequelize.INTEGER
},
created_at: {
allowNull: false,
type: Sequelize.DATE
},
updated_at: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: (queryInterface) => queryInterface.dropTable('netcom')
};
27 changes: 27 additions & 0 deletions migrations/20240924202000-create-mail-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('mailComponent', {
agora_id: {
allowNull: false,
type: Sequelize.INTEGER,
primaryKey: true
},
mail_component: {
allowNull: false,
type: Sequelize.ENUM('introduction', 'communication', 'board election', 'members list', 'membership fee', 'events', 'agora attendance', 'development plan', 'fulfilment report', 'closing'),
primaryKey: true
},
text: {
allowNull: false,
type: Sequelize.TEXT
},
created_at: {
allowNull: false,
type: Sequelize.DATE
},
updated_at: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: (queryInterface) => queryInterface.dropTable('mailComponent')
};
36 changes: 36 additions & 0 deletions models/MailComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { Sequelize, sequelize } = require('../lib/sequelize');

const MailComponent = sequelize.define('mailComponent', {
agora_id: {
allowNull: false,
primaryKey: true,
type: Sequelize.INTEGER,
validate: {
notEmpty: { msg: 'Agora should be set.' },
isInt: { msg: 'Agora ID should be a number.' }
}
},
mail_component: {
allowNull: false,
primaryKey: true,
type: Sequelize.ENUM('introduction', 'communication', 'board election', 'members list', 'membership fee', 'events', 'agora attendance', 'development plan', 'fulfilment report', 'closing'),
validate: {
isIn: {
args: [['introduction', 'communication', 'board election', 'members list', 'membership fee', 'events', 'agora attendance', 'development plan', 'fulfilment report', 'closing']],
msg: 'Message component must be one of these: "introduction", "communication", "board election", "members list", "membership fee", "events", "agora attendance", "development plan", "fulfilment report", "closing".'
}
}
},
text: {
allowNull: false,
type: Sequelize.TEXT
}
}, {
underscored: true,
tableName: 'mailComponent',
createdAt: 'created_at',
updatedAt: 'updated_at',
primaryKey: ['agora_id', 'mail_component']
});

module.exports = MailComponent;
27 changes: 27 additions & 0 deletions models/Netcom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { Sequelize, sequelize } = require('../lib/sequelize');

const Netcom = sequelize.define('netcom', {
body_id: {
allowNull: false,
primaryKey: true,
type: Sequelize.INTEGER,
validate: {
notEmpty: { msg: 'Body should be set.' },
isInt: { msg: 'Body ID should be a number.' }
}
},
netcom_id: {
allowNull: false,
type: Sequelize.INTEGER,
validate: {
isInt: { msg: 'Netcom ID should be a number.' }
}
}
}, {
underscored: true,
tableName: 'netcom',
createdAt: 'created_at',
updatedAt: 'updated_at'
});

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

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