mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 11:07:43 +00:00
feat: sms notification for usa carriers (#40)
Co-authored-by: Jef LeCompte <jeffreylec@gmail.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {Config} from '../config';
|
||||
import sendEmail from './email';
|
||||
import sendSlaskMessage from './slack';
|
||||
import sendSMS from './sms';
|
||||
|
||||
export default function sendNotification(cartUrl: string) {
|
||||
if (Config.notifications.email.username && Config.notifications.email.password) {
|
||||
@@ -10,4 +11,10 @@ export default function sendNotification(cartUrl: string) {
|
||||
if (Config.notifications.slack.channel && Config.notifications.slack.token) {
|
||||
sendSlaskMessage(cartUrl);
|
||||
}
|
||||
|
||||
if (Config.notifications.phone.number && Config.notifications.phone.carrier) {
|
||||
if (Config.notifications.availableCarriers.includes(Config.notifications.phone.carrier.toLowerCase())) {
|
||||
sendSMS(cartUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import nodemailer from 'nodemailer';
|
||||
import Mail from 'nodemailer/lib/mailer';
|
||||
import {Config} from '../config';
|
||||
import {Logger} from '../logger';
|
||||
|
||||
const subject = 'NVIDIA - BUY NOW';
|
||||
|
||||
enum carrierAddress {
|
||||
sprint = 'messaging.sprintpcs.com',
|
||||
verizon = 'vtext.com',
|
||||
tmobile = 'tmomail.net',
|
||||
att = 'txt.att.net'
|
||||
}
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
service: 'gmail',
|
||||
auth: {
|
||||
user: Config.email.username,
|
||||
pass: Config.email.password
|
||||
}
|
||||
});
|
||||
|
||||
const mailOptions: Mail.Options = {
|
||||
from: Config.email.username,
|
||||
to: generateAddress(),
|
||||
subject
|
||||
};
|
||||
|
||||
export default function sendSMS(text: string) {
|
||||
mailOptions.text = text;
|
||||
|
||||
transporter.sendMail(mailOptions, (error, info) => {
|
||||
if (error) {
|
||||
Logger.error(error);
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
||||
Logger.info(`✔ email sent: ${info.response}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function generateAddress() {
|
||||
for (const carrier of Object.keys(carrierAddress)) {
|
||||
if (Config.phone.carrier && carrier === Config.phone.carrier.toLowerCase()) {
|
||||
// @ts-expect-error
|
||||
return [Config.phone.number, carrierAddress[carrier]].join('@');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user