mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 12:17:37 +00:00
feat(notification): support for multiple phone numbers (#738)
This commit is contained in:
+2
-2
@@ -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
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user