Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const User = require("../models/User");
- const nodemailer = require("nodemailer");
- const Myusername = require("../config/keys").SendGridUsername;
- const Mypassword = require("../config/keys").SendGridPassword;
- const webpush = require('web-push');
- module.exports.getEmergency = (req, res) => {
- res.render("emergency");
- };
- module.exports.postEmergency = (req, res) => {
- // We have the req.user
- console.log(req.user);
- // We have the req.body
- console.log(req.body);
- function getSubscriptionsFromDatabase(){
- let subscription_array = []
- emergencyContact.forEach((element) => {
- // finding if the emergency contact is in the database
- User.findOne({email:element.contactemail}).then(user =>{
- subscription_array.push(user.pushSubscription)
- })
- .catch(err => {
- return;
- })
- })
- console.log(subscription_array);
- return subscription_array;
- }
- const triggerPushMsg = function(subscription,dataToSend){
- return webpush.sendNotification(subscription,dataToSend)
- .catch((err) => {
- if (err.statusCode === 410) {
- console.log("Error 410")
- } else {
- console.log('Subscription is no longer valid: ', err);
- }
- });
- }
- const smtpTransport = nodemailer.createTransport({
- service: 'gmail',
- auth: {
- user: Myusername,
- pass: Mypassword
- }
- });
- const { contact, latitude, longitude } = req.body;
- const emergencyContact = req.user.emergencyContact || req.session.user.emergencyContact;
- emergencyContact.forEach((element) => {
- const mailOptions = {
- to: element.contactemail,
- from: 'thesarthakarora@gmail.com',
- subject: 'Emergency for '+ req.user.name,
- 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
- };
- smtpTransport.sendMail(mailOptions);
- console.log("mail sent")
- });
- const link = "https://api.whatsapp.com/send?phone=91" + contact + "&text=Pleasehelpme!https://thesafeplusapp.herokuapp.com/mylocation";
- return getSubscriptionsFromDatabase()
- .then((subscription_array) => {
- const payload = JSON.stringify({ title:`${req.user.name} needs your help!`,link:`${link}`})
- const selfData = JSON.stringify({title: "Unable to send push notifications to your contact",link:"https://thesafeplus.herokuapp.com/dashboard"})
- let promiseChain = Promise.resolve();
- if(subscription_array.length > 0){
- subscription_array.forEach((subscription) => {
- promiseChain = promiseChain.then(() => {
- triggerPushMsg(req.user.pushSubscription,selfData)
- return triggerPushMsg(subscription,payload);
- });
- })
- }
- else {
- return triggerPushMsg(req.user.pushSubscription,selfData)
- }
- }).then(() => {
- console.log("Some notification was sent")
- res.render(res.render('LocationShare',{link,contact}))
- })
- .catch(function(err) {
- res.status(500);
- res.setHeader('Content-Type', 'application/json');
- res.send(JSON.stringify({
- error: {
- id: 'unable-to-send-messages',
- message: `We were unable to send messages to all subscriptions : ` +
- `'${err.message}'`
- }
- }));
- })
- // const link = "https://api.whatsapp.com/send?phone=91" + contact + "&text=Pleasehelpme!/mylocation";
- };
Add Comment
Please, Sign In to add comment