@dibs-tech/mail-client
Advanced tools
Comparing version 4.2.1 to 4.2.2
745
index.js
@@ -1,2 +0,2 @@ | ||
const SparkPost = require('sparkpost'); | ||
// const SparkPost = require('sparkpost'); | ||
const fs = require('fs'); | ||
@@ -11,3 +11,6 @@ const handlebars = require('handlebars'); | ||
const validator = require('validator'); | ||
const { sendTemplatedEmailNew } = require('./lib'); | ||
require('./globals'); | ||
require('./helpers/handlebars/classOrClasses')(handlebars); | ||
@@ -27,43 +30,44 @@ /* eslint-disable no-param-reassign */ | ||
function MailClient(apiKey = process.env.SPARKPOST_API_KEY) { | ||
this.apiKey = apiKey; | ||
this.client = new SparkPost(apiKey); | ||
this.sparkPostPrefix = 'https://api.sparkpost.com/api/v1'; | ||
this.apiKey = apiKey; | ||
// this.client = new SparkPost(apiKey); | ||
// Authorization: Basic <credentials> | ||
this.sparkPostPrefix = 'https://api.sparkpost.com/api/v1'; | ||
} | ||
MailClient.EmailTypes = new Enum([ | ||
'BOOKING_CONFIRMATION', | ||
'ADDED_TO_WAITLIST', | ||
'WAITLIST_CONFIRMATION', | ||
'OFF_WAITLIST_CONFIRMATION', | ||
'PACKAGE_CONFIRMATION', | ||
'PACKAGE_CONFIRMATION_AUTOPAY', | ||
'CHECKOUT_RECEIPT', | ||
'DROP_CONFIRMATION', | ||
'CLASS_CANCELED', | ||
'TRAINER_CHANGED', | ||
'GIFT_CARD', | ||
'FLASH_CREDIT_DEFAULT', | ||
'FLASH_CREDIT_SECRET', | ||
'FLASH_CREDIT_SECRET_UNLOCKED', | ||
'REFER_A_FRIEND', | ||
'REFERRAL_COMPLETED', | ||
'BOOKING_CONFIRMATION', | ||
'ADDED_TO_WAITLIST', | ||
'WAITLIST_CONFIRMATION', | ||
'OFF_WAITLIST_CONFIRMATION', | ||
'PACKAGE_CONFIRMATION', | ||
'PACKAGE_CONFIRMATION_AUTOPAY', | ||
'CHECKOUT_RECEIPT', | ||
'DROP_CONFIRMATION', | ||
'CLASS_CANCELED', | ||
'TRAINER_CHANGED', | ||
'GIFT_CARD', | ||
'FLASH_CREDIT_DEFAULT', | ||
'FLASH_CREDIT_SECRET', | ||
'FLASH_CREDIT_SECRET_UNLOCKED', | ||
'REFER_A_FRIEND', | ||
'REFERRAL_COMPLETED', | ||
]); | ||
MailClient.WhiteLabelTemplateMap = { | ||
[MailClient.EmailTypes.ADDED_TO_WAITLIST]: 'waitlist-confirm', | ||
[MailClient.EmailTypes.OFF_WAITLIST_CONFIRMATION]: 'confirmation-from-waitlist', | ||
[MailClient.EmailTypes.WAITLIST_CONFIRMATION]: 'confirmation', | ||
[MailClient.EmailTypes.BOOKING_CONFIRMATION]: 'confirmation', | ||
[MailClient.EmailTypes.PACKAGE_CONFIRMATION]: 'package-purchase-no-autopay', | ||
[MailClient.EmailTypes.PACKAGE_CONFIRMATION_AUTOPAY]: 'package-purchase-with-autopay', | ||
[MailClient.EmailTypes.CHECKOUT_RECEIPT]: 'checkout-receipt', | ||
[MailClient.EmailTypes.DROP_CONFIRMATION]: 'drop-confirmation', | ||
[MailClient.EmailTypes.CLASS_CANCELED]: 'class-canceled', | ||
[MailClient.EmailTypes.TRAINER_CHANGED]: 'trainer-changed', | ||
[MailClient.EmailTypes.GIFT_CARD]: 'giftcard-recipient-confirmation', | ||
[MailClient.EmailTypes.FLASH_CREDIT_DEFAULT]: 'flash-credit-default', | ||
[MailClient.EmailTypes.FLASH_CREDIT_SECRET]: 'flash-credit-secret', | ||
[MailClient.EmailTypes.FLASH_CREDIT_SECRET_UNLOCKED]: 'flash-credit-secret-unlock', | ||
[MailClient.EmailTypes.REFER_A_FRIEND]: 'friend-referral', | ||
[MailClient.EmailTypes.REFERRAL_COMPLETED]: 'friend-referral-completed', | ||
[MailClient.EmailTypes.ADDED_TO_WAITLIST]: 'waitlist-confirm', | ||
[MailClient.EmailTypes.OFF_WAITLIST_CONFIRMATION]: 'confirmation-from-waitlist', | ||
[MailClient.EmailTypes.WAITLIST_CONFIRMATION]: 'confirmation', | ||
[MailClient.EmailTypes.BOOKING_CONFIRMATION]: 'confirmation', | ||
[MailClient.EmailTypes.PACKAGE_CONFIRMATION]: 'package-purchase-no-autopay', | ||
[MailClient.EmailTypes.PACKAGE_CONFIRMATION_AUTOPAY]: 'package-purchase-with-autopay', | ||
[MailClient.EmailTypes.CHECKOUT_RECEIPT]: 'checkout-receipt', | ||
[MailClient.EmailTypes.DROP_CONFIRMATION]: 'drop-confirmation', | ||
[MailClient.EmailTypes.CLASS_CANCELED]: 'class-canceled', | ||
[MailClient.EmailTypes.TRAINER_CHANGED]: 'trainer-changed', | ||
[MailClient.EmailTypes.GIFT_CARD]: 'giftcard-recipient-confirmation', | ||
[MailClient.EmailTypes.FLASH_CREDIT_DEFAULT]: 'flash-credit-default', | ||
[MailClient.EmailTypes.FLASH_CREDIT_SECRET]: 'flash-credit-secret', | ||
[MailClient.EmailTypes.FLASH_CREDIT_SECRET_UNLOCKED]: 'flash-credit-secret-unlock', | ||
[MailClient.EmailTypes.REFER_A_FRIEND]: 'friend-referral', | ||
[MailClient.EmailTypes.REFERRAL_COMPLETED]: 'friend-referral-completed', | ||
}; | ||
@@ -77,10 +81,13 @@ | ||
MailClient.validateEmail = async function validateEmail(emailAddr) { | ||
try { | ||
if (!validator.isEmail(emailAddr)) throw new Error('Invalid email address'); | ||
const addresses = await new Promise((resolve, reject) => | ||
dns.resolveMx(emailAddr.split('@')[1], (err, result) => (err ? reject(err) : resolve(result)))); | ||
return { success: addresses.length > 0 }; | ||
} catch (err) { | ||
return { success: false }; | ||
} | ||
try { | ||
if (!validator.isEmail(emailAddr)) throw new Error('Invalid email address'); | ||
const addresses = await new Promise((resolve, reject) => | ||
dns.resolveMx(emailAddr.split('@')[1], (err, result) => | ||
err ? reject(err) : resolve(result) | ||
) | ||
); | ||
return { success: addresses.length > 0 }; | ||
} catch (err) { | ||
return { success: false }; | ||
} | ||
}; | ||
@@ -101,58 +108,59 @@ | ||
MailClient.prototype.sendEmail = function sendEmail(to, subject, message, options, cb) { | ||
// options = options || {force: false} | ||
let force; | ||
const recipients = Array.isArray(to) ? to : [{ address: { email: to } }]; | ||
// options = options || {force: false} | ||
let force; | ||
const recipients = Array.isArray(to) ? to : [{ address: { email: to } }]; | ||
/* taking into account shorthand forcing, | ||
/* taking into account shorthand forcing, | ||
or if no force argument is passed, | ||
or if it comes from the ops call */ | ||
if (typeof options === 'boolean' || (!options && !cb) || options === undefined) { | ||
force = options || false; | ||
options = {}; | ||
// short-hand to only use the callback | ||
} else if (cb === undefined && typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
force = false; | ||
// if it is the full argument list | ||
} else { | ||
force = options.force; | ||
delete options.force; | ||
} | ||
if (typeof options === 'boolean' || (!options && !cb) || options === undefined) { | ||
force = options || false; | ||
options = {}; | ||
// short-hand to only use the callback | ||
} else if (cb === undefined && typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
force = false; | ||
// if it is the full argument list | ||
} else { | ||
force = options.force; | ||
delete options.force; | ||
} | ||
const transmissionObj = { | ||
options: { | ||
transactional: true, | ||
}, | ||
recipients, | ||
content: { | ||
from: options.from || { | ||
name: 'Dibs', | ||
email: 'no-reply@ondibs.com', | ||
}, | ||
subject, | ||
text: message, | ||
}, | ||
}; | ||
const transmissionObj = { | ||
options: { | ||
transactional: true, | ||
}, | ||
recipients, | ||
content: { | ||
from: options.from || { | ||
name: 'Dibs', | ||
email: 'no-reply@ondibs.com', | ||
}, | ||
subject, | ||
text: message, | ||
}, | ||
}; | ||
if (options.html) transmissionObj.content.html = options.html; | ||
if (options.attachments) transmissionObj.content.attachments = options.attachments; | ||
if (options.pool) transmissionObj.options.pool = options.pool; | ||
if (options.html) transmissionObj.content.html = options.html; | ||
if (options.attachments) transmissionObj.content.attachments = options.attachments; | ||
if (options.pool) transmissionObj.options.pool = options.pool; | ||
if (process.env.NODE_ENV === 'production' || force) { | ||
if (cb) { | ||
this.client.transmissions.send(transmissionObj).then(cb.bind(null, null)) | ||
.catch(cb); | ||
} else { | ||
this.client.transmissions.send(transmissionObj); | ||
} | ||
} else if (process.env.NODE_ENV !== 'test') { | ||
console.log(`Subject: ${subject} | ||
if (process.env.NODE_ENV === 'production' || force) { | ||
console.log('cb is line 146:', cb); | ||
if (cb) { | ||
// this.client.transmissions.send(transmissionObj).then(cb.bind(null, null)).catch(cb); | ||
sendTemplatedEmailNew(transmissionObj).then((cb) => console.log('cb is', cb)); | ||
} else { | ||
// this.client.transmissions.send(transmissionObj); | ||
sendTemplatedEmailNew(transmissionObj); | ||
} | ||
} else if (process.env.NODE_ENV !== 'test') { | ||
console.log(`Subject: ${subject} | ||
Body: ${message} | ||
Options: ${options}`); | ||
if (cb) cb(null); | ||
} | ||
if (cb) cb(null); | ||
} | ||
}; | ||
/** | ||
@@ -170,6 +178,5 @@ * ops - Description | ||
MailClient.prototype.ops = function ops(subject, message, options, cb) { | ||
this.sendEmail('ops@ondibs.com', `[ATTN-DIBS] ${subject}`, message, options, cb); | ||
this.sendEmail('ops@ondibs.com', `[ATTN-DIBS] ${subject}`, message, options, cb); | ||
}; | ||
/** | ||
@@ -187,3 +194,3 @@ * info - Description | ||
MailClient.prototype.info = function info(subject, message, options, cb) { | ||
this.sendEmail('info@ondibs.com', subject, message, options, cb); | ||
this.sendEmail('info@ondibs.com', subject, message, options, cb); | ||
}; | ||
@@ -203,3 +210,3 @@ | ||
MailClient.prototype.studios = function studio(subject, message, options, cb) { | ||
this.sendEmail('studios@ondibs.com', subject, message, options, cb); | ||
this.sendEmail('studios@ondibs.com', subject, message, options, cb); | ||
}; | ||
@@ -220,25 +227,42 @@ | ||
*/ | ||
MailClient.prototype.sendTemplatedEmail = async function sendTemplatedEmail(recipient, subject, templateName, data, options, cb) { // eslint-disable-line max-len | ||
// check if templateName is a link | ||
const templateIsCustom = isURL(templateName); | ||
const source = templateIsCustom ? (await axios.get(templateName)).data : fs.readFileSync(path.join(path.resolve('.'), 'email/templates/', `${templateName}.hbs`), 'utf-8'); | ||
const template = handlebars.compile(source); | ||
const html = template(data); | ||
MailClient.prototype.sendTemplatedEmail = async function sendTemplatedEmail( | ||
recipient, | ||
subject, | ||
templateName, | ||
data, | ||
options, | ||
cb | ||
) { | ||
// eslint-disable-line max-len | ||
// check if templateName is a link | ||
const templateIsCustom = isURL(templateName); | ||
const source = templateIsCustom | ||
? (await axios.get(templateName)).data | ||
: fs.readFileSync( | ||
path.join(path.resolve('.'), 'email/templates/', `${templateName}.hbs`), | ||
'utf-8' | ||
); | ||
const template = handlebars.compile(source); | ||
const html = template(data); | ||
let optionsObj; | ||
let optionsObj; | ||
if (typeof options === 'function') { | ||
cb = options; | ||
optionsObj = { html }; | ||
} else { | ||
optionsObj = { force: typeof options === 'boolean' ? options : (options.force || false), html }; | ||
if (typeof options !== 'boolean' && options.attachments) optionsObj.attachments = options.attachments; | ||
if (typeof options !== 'boolean' && options.from) optionsObj.from = options.from; | ||
} | ||
if (typeof options === 'function') { | ||
cb = options; | ||
optionsObj = { html }; | ||
} else { | ||
optionsObj = { | ||
force: typeof options === 'boolean' ? options : options.force || false, | ||
html, | ||
}; | ||
if (typeof options !== 'boolean' && options.attachments) | ||
optionsObj.attachments = options.attachments; | ||
if (typeof options !== 'boolean' && options.from) optionsObj.from = options.from; | ||
} | ||
this.sendEmail(recipient, subject, '', optionsObj, cb); | ||
this.sendEmail(recipient, subject, '', optionsObj, cb); | ||
}; | ||
MailClient.getDefaultTemplateName = function getDefaultTemplateName(emailType) { | ||
return `white-label/${this.WhiteLabelTemplateMap[emailType]}`; | ||
return `white-label/${this.WhiteLabelTemplateMap[emailType]}`; | ||
}; | ||
@@ -253,195 +277,231 @@ | ||
MailClient.prototype.getEmailDataForStudio = function getEmailDataForStudio(studio, emailType) { | ||
let template = MailClient.getDefaultTemplateName(emailType); | ||
let customEmailText = ''; | ||
let template = MailClient.getDefaultTemplateName(emailType); | ||
let customEmailText = ''; | ||
if (studio.custom_email_template || studio.custom_email_text) { | ||
switch (emailType) { | ||
case MailClient.EmailTypes.ADDED_TO_WAITLIST: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.waitlisted.link | ||
) || template; | ||
customEmailText = ( | ||
studio.custom_email_text | ||
&& (studio.custom_email_text.find(cet => cet.template === 'waitlist') || {}).text | ||
) || ''; | ||
break; | ||
if (studio.custom_email_template || studio.custom_email_text) { | ||
switch (emailType) { | ||
case MailClient.EmailTypes.ADDED_TO_WAITLIST: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.waitlisted.link) || | ||
template; | ||
customEmailText = | ||
(studio.custom_email_text && | ||
(studio.custom_email_text.find((cet) => cet.template === 'waitlist') || {}) | ||
.text) || | ||
''; | ||
break; | ||
case MailClient.EmailTypes.OFF_WAITLIST_CONFIRMATION: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.offWaitlistConfirm.link | ||
) || template; | ||
customEmailText = ( | ||
studio.custom_email_text | ||
&& (studio.custom_email_text.find(cet => cet.template === 'confirmation-from-waitlist') || {}).text | ||
) || ''; | ||
break; | ||
case MailClient.EmailTypes.OFF_WAITLIST_CONFIRMATION: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.offWaitlistConfirm.link) || | ||
template; | ||
customEmailText = | ||
(studio.custom_email_text && | ||
( | ||
studio.custom_email_text.find( | ||
(cet) => cet.template === 'confirmation-from-waitlist' | ||
) || {} | ||
).text) || | ||
''; | ||
break; | ||
case MailClient.EmailTypes.WAITLIST_CONFIRMATION: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.waitlistConfirm.link | ||
) || template; | ||
customEmailText = ( | ||
studio.custom_email_text | ||
&& (studio.custom_email_text.find(cet => cet.template === 'confirmation') || {}).text | ||
) || ''; | ||
break; | ||
case MailClient.EmailTypes.WAITLIST_CONFIRMATION: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.waitlistConfirm.link) || | ||
template; | ||
customEmailText = | ||
(studio.custom_email_text && | ||
( | ||
studio.custom_email_text.find( | ||
(cet) => cet.template === 'confirmation' | ||
) || {} | ||
).text) || | ||
''; | ||
break; | ||
case MailClient.EmailTypes.PACKAGE_CONFIRMATION: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.autoPayExpire.link | ||
) || template; | ||
break; | ||
case MailClient.EmailTypes.PACKAGE_CONFIRMATION: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.autoPayExpire.link) || | ||
template; | ||
break; | ||
case MailClient.EmailTypes.PACKAGE_CONFIRMATION_AUTOPAY: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.autoPayNoExpire.link | ||
) || template; | ||
break; | ||
case MailClient.EmailTypes.PACKAGE_CONFIRMATION_AUTOPAY: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.autoPayNoExpire.link) || | ||
template; | ||
break; | ||
case MailClient.EmailTypes.BOOKING_CONFIRMATION: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.confirmation.link | ||
) || template; | ||
customEmailText = ( | ||
studio.custom_email_text | ||
&& (studio.custom_email_text.find(cet => cet.template === 'confirmation') || {}).text | ||
) || ''; | ||
break; | ||
case MailClient.EmailTypes.BOOKING_CONFIRMATION: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.confirmation.link) || | ||
template; | ||
customEmailText = | ||
(studio.custom_email_text && | ||
( | ||
studio.custom_email_text.find( | ||
(cet) => cet.template === 'confirmation' | ||
) || {} | ||
).text) || | ||
''; | ||
break; | ||
case MailClient.EmailTypes.CHECKOUT_RECEIPT: | ||
template = (((studio.custom_email_template || {}).receipt || {}).link) || template; | ||
customEmailText = ( | ||
(studio.custom_email_text || []).find( | ||
cet => cet.template === 'confirmation') || {}).text || ''; // i wish optional chaining was a thing T_T | ||
break; | ||
case MailClient.EmailTypes.CHECKOUT_RECEIPT: | ||
template = ((studio.custom_email_template || {}).receipt || {}).link || template; | ||
customEmailText = | ||
( | ||
(studio.custom_email_text || []).find( | ||
(cet) => cet.template === 'confirmation' | ||
) || {} | ||
).text || ''; // i wish optional chaining was a thing T_T | ||
break; | ||
case MailClient.EmailTypes.DROP_CONFIRMATION: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.classDropped.link | ||
) || template; | ||
customEmailText = ( | ||
studio.custom_email_text | ||
&& (studio.custom_email_text.find(cet => cet.template === 'drop') || {}).text | ||
) || ''; | ||
break; | ||
case MailClient.EmailTypes.DROP_CONFIRMATION: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.classDropped.link) || | ||
template; | ||
customEmailText = | ||
(studio.custom_email_text && | ||
(studio.custom_email_text.find((cet) => cet.template === 'drop') || {}) | ||
.text) || | ||
''; | ||
break; | ||
case MailClient.EmailTypes.CLASS_CANCELED: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.classCanceled.link | ||
) || template; | ||
customEmailText = ( | ||
studio.custom_email_text | ||
&& (studio.custom_email_text.find(cet => cet.template === 'class-canceled') || {}).text | ||
) || ''; | ||
break; | ||
case MailClient.EmailTypes.CLASS_CANCELED: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.classCanceled.link) || | ||
template; | ||
customEmailText = | ||
(studio.custom_email_text && | ||
( | ||
studio.custom_email_text.find( | ||
(cet) => cet.template === 'class-canceled' | ||
) || {} | ||
).text) || | ||
''; | ||
break; | ||
case MailClient.EmailTypes.TRAINER_CHANGED: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.trainerChanged.link | ||
) || template; | ||
customEmailText = ( | ||
studio.custom_email_text | ||
&& (studio.custom_email_text.find(cet => cet.template === 'trainer-changed') || {}).text | ||
) || ''; | ||
break; | ||
case MailClient.EmailTypes.TRAINER_CHANGED: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.trainerChanged.link) || | ||
template; | ||
customEmailText = | ||
(studio.custom_email_text && | ||
( | ||
studio.custom_email_text.find( | ||
(cet) => cet.template === 'trainer-changed' | ||
) || {} | ||
).text) || | ||
''; | ||
break; | ||
case MailClient.EmailTypes.GIFT_CARD: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.giftCard.link | ||
) || template; | ||
customEmailText = ( | ||
studio.custom_email_text | ||
&& (studio.custom_email_text.find(cet => cet.template === 'gift-card') || {}).text | ||
) || ''; | ||
break; | ||
case MailClient.EmailTypes.GIFT_CARD: | ||
template = | ||
(studio.custom_email_template && studio.custom_email_template.giftCard.link) || | ||
template; | ||
customEmailText = | ||
(studio.custom_email_text && | ||
(studio.custom_email_text.find((cet) => cet.template === 'gift-card') || {}) | ||
.text) || | ||
''; | ||
break; | ||
case MailClient.EmailTypes.FLASH_CREDIT_DEFAULT: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.flashCredit | ||
&& studio.custom_email_template.flashCredit.link | ||
) || template; | ||
break; | ||
case MailClient.EmailTypes.FLASH_CREDIT_DEFAULT: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.flashCredit && | ||
studio.custom_email_template.flashCredit.link) || | ||
template; | ||
break; | ||
case MailClient.EmailTypes.FLASH_CREDIT_SECRET: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.flashCredit | ||
&& studio.custom_email_template.flashCredit.link | ||
) || template; | ||
break; | ||
case MailClient.EmailTypes.FLASH_CREDIT_SECRET: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.flashCredit && | ||
studio.custom_email_template.flashCredit.link) || | ||
template; | ||
break; | ||
case MailClient.EmailTypes.FLASH_CREDIT_SECRET_UNLOCKED: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.flashCreditSecretUnlock | ||
&& studio.custom_email_template.flashCreditSecretUnlock.link | ||
) || template; | ||
customEmailText = ( | ||
studio.custom_email_text | ||
&& (this.studio.custom_email_text.find(cet => cet.template === 'flashcredit-unlock') || {}).text | ||
) || ''; | ||
break; | ||
case MailClient.EmailTypes.FLASH_CREDIT_SECRET_UNLOCKED: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.flashCreditSecretUnlock && | ||
studio.custom_email_template.flashCreditSecretUnlock.link) || | ||
template; | ||
customEmailText = | ||
(studio.custom_email_text && | ||
( | ||
this.studio.custom_email_text.find( | ||
(cet) => cet.template === 'flashcredit-unlock' | ||
) || {} | ||
).text) || | ||
''; | ||
break; | ||
case MailClient.EmailTypes.REFER_A_FRIEND: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.referral | ||
&& studio.custom_email_template.referral.link | ||
) || template; | ||
customEmailText = ( | ||
studio.custom_email_text | ||
&& (studio.custom_email_text.find( | ||
cet => cet.template === 'friend-referral') || {}).text | ||
) || ''; | ||
break; | ||
case MailClient.EmailTypes.REFER_A_FRIEND: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.referral && | ||
studio.custom_email_template.referral.link) || | ||
template; | ||
customEmailText = | ||
(studio.custom_email_text && | ||
( | ||
studio.custom_email_text.find( | ||
(cet) => cet.template === 'friend-referral' | ||
) || {} | ||
).text) || | ||
''; | ||
break; | ||
case MailClient.EmailTypes.REFERRAL_COMPLETED: | ||
template = ( | ||
studio.custom_email_template | ||
&& studio.custom_email_template.referralCredits | ||
&& studio.custom_email_template.referralCredits.link | ||
) || template; | ||
customEmailText = ( | ||
studio.custom_email_text | ||
&& (studio.custom_email_text.find( | ||
cet => cet.template === 'referral-reminder') || {}).text | ||
) || ''; | ||
break; | ||
case MailClient.EmailTypes.REFERRAL_COMPLETED: | ||
template = | ||
(studio.custom_email_template && | ||
studio.custom_email_template.referralCredits && | ||
studio.custom_email_template.referralCredits.link) || | ||
template; | ||
customEmailText = | ||
(studio.custom_email_text && | ||
( | ||
studio.custom_email_text.find( | ||
(cet) => cet.template === 'referral-reminder' | ||
) || {} | ||
).text) || | ||
''; | ||
break; | ||
default: | ||
template = studio.custom_email_template.confirmation.link; | ||
} | ||
} | ||
console.log(`studio info from mailclient ------>>>>>\n\n${JSON.stringify(studio)}`); | ||
default: | ||
template = studio.custom_email_template.confirmation.link; | ||
} | ||
} | ||
console.log(`studio info from mailclient ------>>>>>\n\n${JSON.stringify(studio)}`); | ||
return { | ||
studio, | ||
studioName: studio.name, | ||
studioEmail: studio.studio_email || 'info@ondibs.com', | ||
template, | ||
customEmailText, | ||
phone: { | ||
text: getPhoneNumberFromCurrency(studio.currency), | ||
href: getPhoneNumberFromCurrency(studio.currency, { href: true }), | ||
}, | ||
domain: studio.customSendingDomain || 'no-reply@ondibs.com', | ||
purchasePlaceLink: (studio.live && (!studio.liveWidget)) ? | ||
'https://www.ondibs.com/widget/studio/${studio.id}' | ||
: `https://${studio.domain}?dibs_open`, | ||
}; | ||
return { | ||
studio, | ||
studioName: studio.name, | ||
studioEmail: studio.studio_email || 'info@ondibs.com', | ||
template, | ||
customEmailText, | ||
phone: { | ||
text: getPhoneNumberFromCurrency(studio.currency), | ||
href: getPhoneNumberFromCurrency(studio.currency, { href: true }), | ||
}, | ||
domain: studio.customSendingDomain || 'no-reply@ondibs.com', | ||
purchasePlaceLink: | ||
studio.live && !studio.liveWidget | ||
? 'https://www.ondibs.com/widget/studio/${studio.id}' | ||
: `https://${studio.domain}?dibs_open`, | ||
}; | ||
}; | ||
MailClient.prototype.transactions = function transactions(subject, message, options, cb) { | ||
this.sendEmail('transactions@ondibs.com', subject, message, options, cb); | ||
this.sendEmail('transactions@ondibs.com', subject, message, options, cb); | ||
}; | ||
@@ -455,8 +515,12 @@ | ||
*/ | ||
MailClient.prototype.addEmailToTransactionalSuppressionList = function addEmailToTransactionalSupressionList(email, cb) { | ||
this.client.suppressionList.upsert({ | ||
recipient: email, | ||
type: 'transactional', | ||
}, cb); | ||
}; | ||
MailClient.prototype.addEmailToTransactionalSuppressionList = | ||
function addEmailToTransactionalSupressionList(email, cb) { | ||
this.client.suppressionList.upsert( | ||
{ | ||
recipient: email, | ||
type: 'transactional', | ||
}, | ||
cb | ||
); | ||
}; | ||
@@ -468,8 +532,12 @@ /** | ||
*/ | ||
MailClient.prototype.addEmailToNonTransactionalSuppressionList = function addEmailToTransactionalSupressionList(email, cb) { | ||
this.client.suppressionList.upsert({ | ||
recipient: email, | ||
type: 'non_transactional', | ||
}, cb); | ||
}; | ||
MailClient.prototype.addEmailToNonTransactionalSuppressionList = | ||
function addEmailToTransactionalSupressionList(email, cb) { | ||
this.client.suppressionList.upsert( | ||
{ | ||
recipient: email, | ||
type: 'non_transactional', | ||
}, | ||
cb | ||
); | ||
}; | ||
@@ -481,17 +549,19 @@ /** | ||
*/ | ||
MailClient.prototype.removeEmailFromTransactionalSuppressionList = function removeEmailFromTransactionalSupressionList(email, cb) { | ||
if (typeof cb !== 'function') cb = () => {}; | ||
axios.delete(`${this.sparkPostPrefix}/suppression-list/${email}`, { | ||
headers: { | ||
Authorization: this.apiKey, | ||
Accept: 'application/json', | ||
}, | ||
data: { | ||
type: 'transactional', | ||
}, | ||
}).then(response => cb(null, response.data)) | ||
.catch((err => cb(err.response.data))); | ||
}; | ||
MailClient.prototype.removeEmailFromTransactionalSuppressionList = | ||
function removeEmailFromTransactionalSupressionList(email, cb) { | ||
if (typeof cb !== 'function') cb = () => {}; | ||
axios | ||
.delete(`${this.sparkPostPrefix}/suppression-list/${email}`, { | ||
headers: { | ||
Authorization: this.apiKey, | ||
Accept: 'application/json', | ||
}, | ||
data: { | ||
type: 'transactional', | ||
}, | ||
}) | ||
.then((response) => cb(null, response.data)) | ||
.catch((err) => cb(err.response.data)); | ||
}; | ||
/** | ||
@@ -503,15 +573,18 @@ * | ||
*/ | ||
MailClient.prototype.removeEmailFromNonTransactionalSuppressionList = function removeEmailFromNonTransactionalSupressionList(email, cb) { | ||
if (typeof cb !== 'function') cb = () => {}; | ||
axios.delete(`${this.sparkPostPrefix}/suppression-list/${email}`, { | ||
headers: { | ||
Authorization: this.apiKey, | ||
Accept: 'application/json', | ||
}, | ||
data: { | ||
type: 'non_transactional', | ||
}, | ||
}).then(response => cb(null, response.data)) | ||
.catch((err => cb(err.response.data))); | ||
}; | ||
MailClient.prototype.removeEmailFromNonTransactionalSuppressionList = | ||
function removeEmailFromNonTransactionalSupressionList(email, cb) { | ||
if (typeof cb !== 'function') cb = () => {}; | ||
axios | ||
.delete(`${this.sparkPostPrefix}/suppression-list/${email}`, { | ||
headers: { | ||
Authorization: this.apiKey, | ||
Accept: 'application/json', | ||
}, | ||
data: { | ||
type: 'non_transactional', | ||
}, | ||
}) | ||
.then((response) => cb(null, response.data)) | ||
.catch((err) => cb(err.response.data)); | ||
}; | ||
/** | ||
@@ -525,4 +598,4 @@ * | ||
function supressor(context, prefix, list) { | ||
const method = `${prefix}${list}SuppressionList`; | ||
return context[method].bind(context); | ||
const method = `${prefix}${list}SuppressionList`; | ||
return context[method].bind(context); | ||
} | ||
@@ -540,15 +613,17 @@ | ||
MailClient.prototype.suppress = function suppress(type, list, email, cb) { | ||
if (!type.match(/^(add|remove)$/)) throw new Error('Not a valid type of method'); | ||
if (!list.match(/^(Transactional|NonTransactional)$/)) throw new Error('Not a valid type of list'); | ||
const that = this; | ||
return { | ||
add() { | ||
return supressor(that, 'addEmailTo', list)(email, cb); | ||
}, | ||
remove() { | ||
return supressor(that, 'removeEmailFrom', list)(email, cb); | ||
}, | ||
}[type](); | ||
if (!type.match(/^(add|remove)$/)) throw new Error('Not a valid type of method'); | ||
if (!list.match(/^(Transactional|NonTransactional)$/)) | ||
throw new Error('Not a valid type of list'); | ||
const that = this; | ||
return { | ||
add() { | ||
return supressor(that, 'addEmailTo', list)(email, cb); | ||
}, | ||
remove() { | ||
return supressor(that, 'removeEmailFrom', list)(email, cb); | ||
}, | ||
}[type](); | ||
}; | ||
module.exports = MailClient; | ||
{ | ||
"name": "@dibs-tech/mail-client", | ||
"version": "4.2.1", | ||
"version": "4.2.2", | ||
"description": "A mail client implementation, using sparkpost", | ||
@@ -21,3 +21,2 @@ "main": "index.js", | ||
"handlebars": "^4.7.7", | ||
"sparkpost": "^2.1.4", | ||
"validator": "^13.7.0" | ||
@@ -37,2 +36,1 @@ }, | ||
} | ||
25353
5
12
667
6
- Removedsparkpost@^2.1.4
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedlodash@4.17.21(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.15.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsparkpost@2.1.4(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)