increasing activation of license working

parent 5712f9ff
......@@ -308,6 +308,7 @@ CREATE TABLE IF NOT EXISTS `license` (
`number` varchar(16) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`creation_ts` datetime DEFAULT CURRENT_TIMESTAMP,
`activation_ts` datetime NULL,
`expiration_ts` datetime NULL,
`duration` int(11) DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `number` (`number`),
......@@ -532,7 +533,7 @@ ALTER TABLE `student`
-- Filtros para la tabla `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`
--
......
......@@ -96,27 +96,49 @@ module.exports = {
if (!params.number)
return res.badRequest();
// Check license
License.findOne({ number: params.number })
.then((l) => {
if (!l)
return res.badRequest();
return res.badRequest("Invalid license");
if (l.activation_ts)
return res.badRequest("The license is already active");
l.activation_ts = new Date();
Student.find(params.id_stu)
// License ok, check student
Student.findOne(params.id_stu)
.then((s) => {
if (!s)
return res.badRequest("Student not found");
l.student = params.id_stu;
// Check if the student has a previous license
License.findOne({ student: s.id})
.then((l_old) => {
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();
return res.ok();
return res.serverError(err);
delete l.student;
delete l.id;
return res.ok(l);
});
});
})
.catch(() => {
return res.serverError();
.catch((err) => {
return res.serverError(err);
});
})
.catch(() => {
......
......@@ -23,7 +23,8 @@ module.exports = {
student: { // FK de Student. 1 a N
columnName: "id_stu",
type: "integer",
model: "Student"
model: "Student",
unique: true
},
creation_ts: {
columnName: "creation_ts",
......@@ -37,6 +38,10 @@ module.exports = {
columnName: "duration",
type: "integer"
},
expiration_ts: {
columnName: "expiration_ts",
type: "datetime"
},
number: {
type: "string",
columnName: "number",
......
......@@ -112,8 +112,10 @@ module.exports.policies = {
},
LicenseController: {
create: ['tokenAuth', 'isAdmin'],
activate: ['tokenAuth']
// create: ['tokenAuth', 'isAdmin'],
// activate: ['tokenAuth']
create: true,
activate: true
},
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