Skip to content

Commit

Permalink
feat(notification): support for multiple phone numbers (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
benw25 committed Nov 10, 2020
1 parent ab1fddf commit 9f28fe5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -308,8 +308,8 @@ environment variables are **optional**._
| `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_USERNAME` | Gmail address | E.g.: `jensen.robbed.us@gmail.com` |
| `PHONE_CARRIER` | [Supported carriers](#supported-carriers) for SMS | Email configuration required |
| `PHONE_NUMBER` | 10 digit phone number | E.g.: `1234567890`, 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(s) | E.g.: `1234567890` or `1234567890,0987654321,11112223333`, email configuration required |
| `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` |

Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -73,7 +73,8 @@
"rules": {
"sort-imports": "error",
"sort-keys": "error",
"sort-vars": "error"
"sort-vars": "error",
"max-params": 0
}
},
"husky": {
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Expand Up @@ -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),
Expand Down
73 changes: 43 additions & 30 deletions src/notification/sms.ts
Expand Up @@ -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) {
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 (!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 carrier = phone.carrier;

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');

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);
}

0 comments on commit 9f28fe5

Please sign in to comment.