Guest User

Untitled

a guest
Mar 15th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. const User = require("../models/User");
  2. const nodemailer = require("nodemailer");
  3. const Myusername = require("../config/keys").SendGridUsername;
  4. const Mypassword = require("../config/keys").SendGridPassword;
  5. const webpush = require('web-push');
  6.  
  7.  
  8. module.exports.getEmergency = (req, res) => {
  9. res.render("emergency");
  10. };
  11.  
  12.  
  13. module.exports.postEmergency = (req, res) => {
  14.  
  15. // We have the req.user
  16. console.log(req.user);
  17. // We have the req.body
  18. console.log(req.body);
  19.  
  20. function getSubscriptionsFromDatabase(){
  21. let subscription_array = []
  22. emergencyContact.forEach((element) => {
  23. // finding if the emergency contact is in the database
  24. User.findOne({email:element.contactemail}).then(user =>{
  25. subscription_array.push(user.pushSubscription)
  26. })
  27. .catch(err => {
  28. return;
  29. })
  30. })
  31. console.log(subscription_array);
  32. return subscription_array;
  33. }
  34. const triggerPushMsg = function(subscription,dataToSend){
  35. return webpush.sendNotification(subscription,dataToSend)
  36. .catch((err) => {
  37. if (err.statusCode === 410) {
  38. console.log("Error 410")
  39. } else {
  40. console.log('Subscription is no longer valid: ', err);
  41. }
  42. });
  43. }
  44.  
  45. const smtpTransport = nodemailer.createTransport({
  46. service: 'gmail',
  47. auth: {
  48. user: Myusername,
  49. pass: Mypassword
  50. }
  51. });
  52.  
  53.  
  54. const { contact, latitude, longitude } = req.body;
  55.  
  56. const emergencyContact = req.user.emergencyContact || req.session.user.emergencyContact;
  57.  
  58. emergencyContact.forEach((element) => {
  59.  
  60. const mailOptions = {
  61. to: element.contactemail,
  62. from: 'thesarthakarora@gmail.com',
  63. subject: 'Emergency for '+ req.user.name,
  64. text: "Hey " + element.contactname + " there is an emergency!!!!, Please help "+ req.user.name + ". The Google maps link to reach him is https://www.google.com/maps?daddr="+ latitude +"," + longitude
  65. };
  66. smtpTransport.sendMail(mailOptions);
  67. console.log("mail sent")
  68.  
  69. });
  70.  
  71.  
  72. const link = "https://api.whatsapp.com/send?phone=91" + contact + "&text=Pleasehelpme!https://thesafeplusapp.herokuapp.com/mylocation";
  73.  
  74. return getSubscriptionsFromDatabase()
  75. .then((subscription_array) => {
  76. const payload = JSON.stringify({ title:`${req.user.name} needs your help!`,link:`${link}`})
  77. const selfData = JSON.stringify({title: "Unable to send push notifications to your contact",link:"https://thesafeplus.herokuapp.com/dashboard"})
  78. let promiseChain = Promise.resolve();
  79. if(subscription_array.length > 0){
  80. subscription_array.forEach((subscription) => {
  81. promiseChain = promiseChain.then(() => {
  82. triggerPushMsg(req.user.pushSubscription,selfData)
  83. return triggerPushMsg(subscription,payload);
  84. });
  85. })
  86. }
  87. else {
  88. return triggerPushMsg(req.user.pushSubscription,selfData)
  89. }
  90. }).then(() => {
  91. console.log("Some notification was sent")
  92. res.render(res.render('LocationShare',{link,contact}))
  93. })
  94. .catch(function(err) {
  95. res.status(500);
  96. res.setHeader('Content-Type', 'application/json');
  97. res.send(JSON.stringify({
  98. error: {
  99. id: 'unable-to-send-messages',
  100. message: `We were unable to send messages to all subscriptions : ` +
  101. `'${err.message}'`
  102. }
  103. }));
  104. })
  105.  
  106. // const link = "https://api.whatsapp.com/send?phone=91" + contact + "&text=Pleasehelpme!/mylocation";
  107.  
  108.  
  109. };
Add Comment
Please, Sign In to add comment