issue #725 fixed

parent beb487da
......@@ -203,26 +203,40 @@ module.exports = {
//
update: function (req, res) {
if (!req.params.id_stu) {
res.badRequest();
function promisifyLicenseActivate(license_number, stu_id) {
return new Promise(function(resolve, reject) {
License.activate(license_number, stu_id, function(err, result) {
if (err) return reject(err);
resolve(result);
});
});
}
if (!req.params.id_stu)
return res.badRequest();
Student.findOne(req.params.id_stu)
.populate('license')
.then(function(stu) {
var k;
var current_license;
if (!stu)
throw new Error("Student not found");
console.log("->s1" + JSON.stringify(stu));
// Update license
if (req.body.license_number)
if (!stu.license[0] || req.body.license_number != stu.license[0].number) {
License.activate(req.body.license_number, stu.id, function(err, license) {
if (err)
return res.badRequest(err);
});
}
if (req.body.license_number && (!stu.license[0] || req.body.license_number != stu.license[0].number))
current_license = promisifyLicenseActivate(req.body.license_number, stu.id);
else
current_license = Promise.resolve(stu.license);
return ([stu, current_license]);
})
.spread(function(stu, license) {
console.log("->s3" + JSON.stringify(stu));
console.log("->l4" + JSON.stringify(license));
// copy attributes
for (k in req.body) stu[k] = req.body[k];
......@@ -230,9 +244,6 @@ module.exports = {
if (!req.body.password) // to avoid change password when no one is provided
delete stu.password;
// delete license attribute as this has been already handled
delete stu.license;
// delete username, as this should never be updated from requests
delete stu.username;
......@@ -240,6 +251,8 @@ module.exports = {
if (err)
throw err;
console.log("->s5" + JSON.stringify(stu));
stu.license = license;
res.ok(stu);
// Send websocket message
......@@ -249,6 +262,7 @@ module.exports = {
(req.isSocket) ? req.socket : undefined
);
});
})
.catch(function (err) {
res.notFound();
......
......@@ -138,9 +138,9 @@ dashboardControllers.controller('StudentSetupCtrl', function StudentSetupCtrl(
})
.error(function (err) {
console.log(err);
if (err.message.search('nvalid license'))
if (err.message && err.message.search('nvalid license'))
ngToast.danger({ content: $translate.instant('license_invalid') });
else if (err.message.search('in use'))
else if (err.message && err.message.search('in use'))
ngToast.danger({ content: $translate.instant('license_already_activated') });
else
ngToast.danger({ content: $translate.instant('student_not_updated') });
......
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