increasing activation of license working

parent 5712f9ff
...@@ -308,6 +308,7 @@ CREATE TABLE IF NOT EXISTS `license` ( ...@@ -308,6 +308,7 @@ CREATE TABLE IF NOT EXISTS `license` (
`number` varchar(16) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `number` varchar(16) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`creation_ts` datetime DEFAULT CURRENT_TIMESTAMP, `creation_ts` datetime DEFAULT CURRENT_TIMESTAMP,
`activation_ts` datetime NULL, `activation_ts` datetime NULL,
`expiration_ts` datetime NULL,
`duration` int(11) DEFAULT 0, `duration` int(11) DEFAULT 0,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE KEY `number` (`number`), UNIQUE KEY `number` (`number`),
...@@ -532,7 +533,7 @@ ALTER TABLE `student` ...@@ -532,7 +533,7 @@ ALTER TABLE `student`
-- Filtros para la tabla `license` -- Filtros para la tabla `license`
-- --
ALTER TABLE `license` ALTER TABLE `license`
ADD CONSTRAINT `license_fk_1` FOREIGN KEY (`id_stu`) REFERENCES `student` (`id`); ADD CONSTRAINT `license_fk_1` FOREIGN KEY (`id_stu`) REFERENCES `student` (`id`) ON DELETE CASCADE;
-- --
-- Filtros para la tabla `supervisor` -- Filtros para la tabla `supervisor`
-- --
......
...@@ -96,27 +96,49 @@ module.exports = { ...@@ -96,27 +96,49 @@ module.exports = {
if (!params.number) if (!params.number)
return res.badRequest(); return res.badRequest();
// Check license
License.findOne({ number: params.number }) License.findOne({ number: params.number })
.then((l) => { .then((l) => {
if (!l) if (!l)
return res.badRequest(); return res.badRequest("Invalid license");
if (l.activation_ts) if (l.activation_ts)
return res.badRequest("The license is already active"); return res.badRequest("The license is already active");
l.activation_ts = new Date(); // License ok, check student
Student.find(params.id_stu) Student.findOne(params.id_stu)
.then((s) => { .then((s) => {
if (!s) if (!s)
return res.badRequest("Student not found"); return res.badRequest("Student not found");
l.student = params.id_stu;
l.save((err) => { // Check if the student has a previous license
if (err) License.findOne({ student: s.id})
return res.serverError(); .then((l_old) => {
return res.ok(); var left = 0;
if (l_old) {
// He had a license, if not expired, get remaining time
left = l_old.expiration_ts - new Date();
left = left < 0 ? 0 : left;
License.destroy({id: l_old.id}).exec((err) => {
if (err) throw err;
});
}
// Compute parameters for license to activate
var now = new Date();
l.student = s.id;
l.activation_ts = now;
l.expiration_ts = new Date(new Date(now.getFullYear(), now.getMonth()+l.duration, now.getDate()+1).getTime() + left);
l.save((err) => {
if (err)
return res.serverError(err);
delete l.student;
delete l.id;
return res.ok(l);
});
}); });
}) })
.catch(() => { .catch((err) => {
return res.serverError(); return res.serverError(err);
}); });
}) })
.catch(() => { .catch(() => {
......
...@@ -23,7 +23,8 @@ module.exports = { ...@@ -23,7 +23,8 @@ module.exports = {
student: { // FK de Student. 1 a N student: { // FK de Student. 1 a N
columnName: "id_stu", columnName: "id_stu",
type: "integer", type: "integer",
model: "Student" model: "Student",
unique: true
}, },
creation_ts: { creation_ts: {
columnName: "creation_ts", columnName: "creation_ts",
...@@ -37,6 +38,10 @@ module.exports = { ...@@ -37,6 +38,10 @@ module.exports = {
columnName: "duration", columnName: "duration",
type: "integer" type: "integer"
}, },
expiration_ts: {
columnName: "expiration_ts",
type: "datetime"
},
number: { number: {
type: "string", type: "string",
columnName: "number", columnName: "number",
......
...@@ -112,8 +112,10 @@ module.exports.policies = { ...@@ -112,8 +112,10 @@ module.exports.policies = {
}, },
LicenseController: { LicenseController: {
create: ['tokenAuth', 'isAdmin'], // create: ['tokenAuth', 'isAdmin'],
activate: ['tokenAuth'] // activate: ['tokenAuth']
create: true,
activate: true
}, },
SupervisorController: { SupervisorController: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment