adding email notification to chat submissions

parent 60236681
...@@ -24,30 +24,62 @@ module.exports = { ...@@ -24,30 +24,62 @@ module.exports = {
* Saves a new message in the student's chat * Saves a new message in the student's chat
*/ */
send: function(req, res) { send: function(req, res) {
var msg = req.param('msg'); var params = req.param('msg');
if (!msg.id_sup || !msg.id_stu || !msg.content) if (!params.id_sup || !params.id_stu || !params.content)
return res.badRequest("Invalid message"); return res.badRequest("Invalid message");
Message.create({ Message.create({
'supervisor': msg.id_sup, 'supervisor': params.id_sup,
'student': msg.id_stu, 'student': params.id_stu,
'content': msg.content, 'content': params.content,
'ts': new Date().toISOString() 'ts': new Date().toISOString()
}) })
.then((msg) => Message.findOne({id: msg.id}).populateAll())
.then((msg) => { .then((msg) => {
Supervisor.findOne({id: msg.supervisor}) // Send sockets notification
.then((sup) => { sails.sockets.broadcast('studentRoom' + params.id_stu, 'chatmessage', msg.toJSON(), req.socket);
var obj = msg.toJSON(); return StuSup.find({id_stu: params.id_stu, id_sup: params.id_sup}).populate('supervisor');
obj.supervisor = sup.toJSON(); })
.then((stusups) => {
var emails = "";
for (var i=0; i<stusups.length; i++) {
if (stusups[i].supervisor.id != params.id_sup)
emails += " " + stusups[i].supervisor.email;
}
// Email body
var message = sails.__(
{
phrase: 'new_chat_message',
locale: msg.supervisor.lang
},
{
stuName: msg.student.name,
stuSurname: msg.student.surname,
supName: msg.supervisor.name,
supSurname: msg.supervisor.surname,
content: msg.content
});
sails.sockets.broadcast('studentRoom' + obj.student, 'chatmessage', obj, req.socket); // Email subject
var subject = sails.__({
phrase: 'notification_from_pictogram',
locale: msg.supervisor.lang || 'es-es'
});
return res.ok(obj); // Send Emails
mailService.mailer()
.send({
to: emails,
text: message,
subject: subject
}) })
.catch((err) => { .then(() => {})
if (err) throw err; .catch((err) => {});
});
// Ready, we respond with an Ok!
return res.ok(obj);
}) })
.catch((err) => { .catch((err) => {
console.log("Error adding new message: " + err); console.log("Error adding new message: " + err);
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
"license_annual": "Pictogram 12-months License", "license_annual": "Pictogram 12-months License",
"license_pro": "Pictogram PRO license", "license_pro": "Pictogram PRO license",
"login": "login", "login": "login",
"new_chat_message": "A new message was sent by {{ supName }} {{ supSurname }} about the student {{ stuName }} {{ stuSurname }}:\n
\" {{ content }} \"\n\n -- Pictogram chat service notification",
"notification_from_pictogram": "Notification from Pictogram", "notification_from_pictogram": "Notification from Pictogram",
"no_name": "No name", "no_name": "No name",
"no_surname": "No surname", "no_surname": "No surname",
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
"license_annual": "Pictogram licencia 12 meses", "license_annual": "Pictogram licencia 12 meses",
"license_pro": "Pictogram licencia PRO", "license_pro": "Pictogram licencia PRO",
"login": "acceder", "login": "acceder",
"new_chat_message": "Un nuevo mensaje ha sido enviado por {{ supName }} {{ supSurname }} acerca del estudiante {{ stuName }} {{ stuSurname }}:\n
\" {{ content }} \"\n\n -- Notificación del servicio de chat de Pictogram",
"notification_from_pictogram": "Notificación desde Pictogram", "notification_from_pictogram": "Notificación desde Pictogram",
"no_name": "Sin nombre", "no_name": "Sin nombre",
"no_surname": "Sin apellidos", "no_surname": "Sin apellidos",
......
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