File size: 1,226 Bytes
8c7b7ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const nodemailer = require('nodemailer');
const twilio = require('twilio');
const env = require('../config/env');

const mailTransport = env.smtp.host && env.smtp.user
  ? nodemailer.createTransport({
      host: env.smtp.host,
      port: env.smtp.port,
      secure: env.smtp.secure,
      auth: {
        user: env.smtp.user,
        pass: env.smtp.pass
      }
    })
  : null;

const twilioClient = env.twilio.sid && env.twilio.token
  ? twilio(env.twilio.sid, env.twilio.token)
  : null;

async function sendEmail(to, subject, text) {
  if (!mailTransport) {
    // Fallback for local development without SMTP configuration.
    console.log(`[EMAIL][SKIPPED] to=${to} subject=${subject} text=${text}`);
    return { skipped: true };
  }

  await mailTransport.sendMail({
    from: env.smtp.from,
    to,
    subject,
    text
  });

  return { skipped: false };
}

async function sendWhatsApp(to, body) {
  if (!twilioClient || !env.twilio.from || !to) {
    console.log(`[WHATSAPP][SKIPPED] to=${to} body=${body}`);
    return { skipped: true };
  }

  await twilioClient.messages.create({
    from: env.twilio.from,
    to,
    body
  });

  return { skipped: false };
}

module.exports = { sendEmail, sendWhatsApp };