feat(notification): support for multiple phone numbers (#738)

This commit is contained in:
Ben Wu
2020-11-10 06:21:54 -08:00
committed by GitHub
parent ab1fddf20c
commit 9f28fe5803
4 changed files with 49 additions and 35 deletions
+2 -2
View File
@@ -308,8 +308,8 @@ environment variables are **optional**._
| `EMAIL_PASSWORD` | Gmail password | See below if you have MFA | | `EMAIL_PASSWORD` | Gmail password | See below if you have MFA |
| `EMAIL_TO` | Destination Email | Defaults to username if not set. Can be comma separated | | `EMAIL_TO` | Destination Email | Defaults to username if not set. Can be comma separated |
| `EMAIL_USERNAME` | Gmail address | E.g.: `jensen.robbed.us@gmail.com` | | `EMAIL_USERNAME` | Gmail address | E.g.: `jensen.robbed.us@gmail.com` |
| `PHONE_CARRIER` | [Supported carriers](#supported-carriers) for SMS | Email configuration required | | `PHONE_CARRIER` | [Supported carriers](#supported-carriers) for SMS | E.g.: `att` or `att,verizon,google`, email configuration required. If multiple phone numbers are listed, enter a carrier for each phone number |
| `PHONE_NUMBER` | 10 digit phone number | E.g.: `1234567890`, email configuration required | | `PHONE_NUMBER` | 10 digit phone number(s) | E.g.: `1234567890` or `1234567890,0987654321,11112223333`, email configuration required |
| `SMTP_ADDRESS` | IP Address or fqdn of smtp server | | `SMTP_ADDRESS` | IP Address or fqdn of smtp server |
| `SMTP_PORT` | TCP Port number on which the smtp server is listening for connections | Default: `25` | | `SMTP_PORT` | TCP Port number on which the smtp server is listening for connections | Default: `25` |
+2 -1
View File
@@ -73,7 +73,8 @@
"rules": { "rules": {
"sort-imports": "error", "sort-imports": "error",
"sort-keys": "error", "sort-keys": "error",
"sort-vars": "error" "sort-vars": "error",
"max-params": 0
} }
}, },
"husky": { "husky": {
+2 -2
View File
@@ -180,8 +180,8 @@ const notifications = {
['virgin', 'vmobl.com'], ['virgin', 'vmobl.com'],
['virgin-ca', 'vmobile.ca'] ['virgin-ca', 'vmobile.ca']
]), ]),
carrier: envOrString(process.env.PHONE_CARRIER), carrier: envOrArray(process.env.PHONE_CARRIER),
number: envOrString(process.env.PHONE_NUMBER) number: envOrArray(process.env.PHONE_NUMBER)
}, },
playSound: envOrString(process.env.PLAY_SOUND), playSound: envOrString(process.env.PLAY_SOUND),
pushbullet: envOrString(process.env.PUSHBULLET), pushbullet: envOrString(process.env.PUSHBULLET),
+43 -30
View File
@@ -10,42 +10,55 @@ if (config.notifications.phone.number && !config.notifications.email.username) {
const [email, phone] = [config.notifications.email, config.notifications.phone]; const [email, phone] = [config.notifications.email, config.notifications.phone];
if (phone.carrier.length !== phone.number.length) {
logger.warn('✖ the number of carriers must match the number of phone numbers');
}
export function sendSms(link: Link, store: Store) { export function sendSms(link: Link, store: Store) {
if (phone.number) { for (let i = 0; i < Math.max(phone.number.length, phone.carrier.length); i++) {
logger.debug('↗ sending sms'); const currentNumber = phone.number[i];
const carrier = phone.carrier; const currentCarrier = phone.carrier[i];
if (carrier && phone.availableCarriers.has(carrier)) { if (!currentNumber) {
const mailOptions: Mail.Options = { logger.error(`${currentCarrier} is not associated with a number`);
attachments: link.screenshot ? [ continue;
{ } else if (!currentCarrier) {
filename: link.screenshot, logger.error(`${currentNumber} is not associated with a carrier`);
path: `./${link.screenshot}` continue;
}
] : undefined,
from: email.username,
subject: Print.inStock(link, store, false, true),
text: link.cartUrl ? link.cartUrl : link.url,
to: generateAddress()
};
transporter.sendMail(mailOptions, error => {
if (error) {
logger.error('✖ couldn\'t send sms', error);
} else {
logger.info('✔ sms sent');
}
});
} }
if (!phone.availableCarriers.has(currentCarrier)) {
logger.error(`✖ unknown carrier ${currentCarrier}`);
continue;
}
logger.debug('↗ sending sms');
const mailOptions: Mail.Options = {
attachments: link.screenshot ? [
{
filename: link.screenshot,
path: `./${link.screenshot}`
}
] : undefined,
from: email.username,
subject: Print.inStock(link, store, false, true),
text: link.cartUrl ? link.cartUrl : link.url,
to: generateAddress(currentNumber, currentCarrier)
};
transporter.sendMail(mailOptions, error => {
if (error) {
logger.error(`✖ couldn't send sms to ${currentNumber} for carrier ${currentCarrier}`, error);
} else {
logger.info('✔ sms sent');
}
});
} }
} }
function generateAddress() { function generateAddress(number: string, carrier: string) {
const carrier = phone.carrier;
if (carrier && phone.availableCarriers.has(carrier)) { if (carrier && phone.availableCarriers.has(carrier)) {
return [phone.number, phone.availableCarriers.get(carrier)].join('@'); return [number, phone.availableCarriers.get(carrier)].join('@');
} }
logger.error('✖ unknown carrier', carrier);
} }