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
@@ -180,8 +180,8 @@ const notifications = {
['virgin', 'vmobl.com'],
['virgin-ca', 'vmobile.ca']
]),
carrier: envOrString(process.env.PHONE_CARRIER),
number: envOrString(process.env.PHONE_NUMBER)
carrier: envOrArray(process.env.PHONE_CARRIER),
number: envOrArray(process.env.PHONE_NUMBER)
},
playSound: envOrString(process.env.PLAY_SOUND),
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];
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) {
if (phone.number) {
logger.debug('↗ sending sms');
const carrier = phone.carrier;
for (let i = 0; i < Math.max(phone.number.length, phone.carrier.length); i++) {
const currentNumber = phone.number[i];
const currentCarrier = phone.carrier[i];
if (carrier && phone.availableCarriers.has(carrier)) {
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()
};
transporter.sendMail(mailOptions, error => {
if (error) {
logger.error('✖ couldn\'t send sms', error);
} else {
logger.info('✔ sms sent');
}
});
if (!currentNumber) {
logger.error(`${currentCarrier} is not associated with a number`);
continue;
} else if (!currentCarrier) {
logger.error(`${currentNumber} is not associated with a carrier`);
continue;
}
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() {
const carrier = phone.carrier;
function generateAddress(number: string, carrier: string) {
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);
}