From 9e9de11d070ebb9420ab967ecfa00ec80636e634 Mon Sep 17 00:00:00 2001 From: Vuthy Date: Mon, 12 Aug 2019 20:18:25 +0700 Subject: [PATCH] Enable Email --- lib/email.js | 18 +++++++++--------- routes/admin.js | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/lib/email.js b/lib/email.js index 117d4ed..d9e1348 100644 --- a/lib/email.js +++ b/lib/email.js @@ -8,16 +8,16 @@ var credentials = { pass:'yoywnomlwolmysjk' }; -var sEmail = false; +var sEmail = true; function sendEmail(recipient, subject, text){ var send = require('gmail-send')({ - user: credentials.user, // Your GMail account used to send emails - pass: credentials.pass, // Application-specific password - to: recipient, // Send to yourself + user: credentials.user, // Your GMail account used to send emails + pass: credentials.pass, // Application-specific password + to: recipient, // Send to yourself subject: subject, - html: text, // Plain text - })({}); // Send email without any check + html: text, // Plain text + })({}); // Send email without any check } function notSendEmail(recipient, subject, text){ @@ -25,9 +25,9 @@ function notSendEmail(recipient, subject, text){ } if(sEmail){ - module.exports = { - sendEmail: sendEmail - } + module.exports = { + sendEmail: sendEmail + } }else{ module.exports = { sendEmail: notSendEmail diff --git a/routes/admin.js b/routes/admin.js index 978bffd..8a13976 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -354,12 +354,30 @@ module.exports.set = function(router, pool) { var apartmentFromDatabase = apartment.rows[0].apartment_list; // apartment_list: ['a1', 'a2', 'b3', ...] + + // Check apartment from req.body.apartment_list array if it contains in database for (var i = 0; i < requestBody.apartment_list.length; i++) { if (apartmentFromDatabase.indexOf(requestBody.apartment_list[i]) == -1 ) { return res.send(requestBody.apartment_list[i] + " apartment is not exists") } } + + var userToSendEmail = []; + + var fetchEmailQuery = { + text: "SELECT email, apartment, username FROM account;" + } + + var emailList = await pool.query(fetchEmailQuery); + for (var i = 0; i < emailList.rows.length; i++) { + if (requestBody.apartment_list.indexOf(emailList.rows[i].apartment) != -1) { + userToSendEmail.push(emailList.rows[i]); + } + } + + + // If n if (isNaN(req.body.amount)) { return res.status(500).send("Amount is in valid number;") @@ -400,7 +418,28 @@ module.exports.set = function(router, pool) { console.log(err) res.send(err); } else { - res.send("Sent") + res.send("Sent"); + + var email = require('../lib/email.js'); + // send email to trasferer #shudsdf + + // send email to trasferer #shudsdf + for (var i = 0; i < userToSendEmail.length; i++ ) { + console.log("Email to send to: " + userToSendEmail[i].email) + var contentToRecipient = "Hello, "+userToSendEmail[i].username+"

You have recieved "+parseFloat(req.body.amount)+"P from "+req.user.displayName+"

Reason: "+req.body.reason+"

"; + email.sendEmail(userToSendEmail[i].email,"Pedro Recieved",contentToRecipient); + } + } })