fleet/website/api/controllers/deliver-contact-form-message.js
Eric 31f8c7e548
Website: Create new Salesforce helper, update contact form actions and signup action. (#19082)
Changes:
- Added a new helper:
`salesforce/update-or-create-contact-and-account-and-create-lead`, a
wrapper for the `update-or-create-contact-and-account` and `create-lead`
helpers that is intended to be run in the background.
- Updated the `signup`, `deliver-contact-form-message`, and
`deliver-talk-to-us-form-submission` actions to use the new helper
2024-05-16 15:38:09 -05:00

89 lines
2.7 KiB
JavaScript
Vendored

module.exports = {
friendlyName: 'Deliver contact form message',
description: 'Deliver a contact form message to the appropriate internal channel(s).',
inputs: {
emailAddress: {
required: true,
type: 'string',
description: 'A return email address where we can respond.',
example: '[email protected]'
},
firstName: {
required: true,
type: 'string',
description: 'The first name of the human sending this message.',
example: 'Emma'
},
lastName: {
required: true,
type: 'string',
description: 'The last name of the human sending this message.',
example: 'Watson'
},
message: {
type: 'string',
required: true,
description: 'The custom message, in plain text.'
}
},
exits: {
invalidEmailDomain: {
description: 'This email address is on a denylist of domains and was not delivered.',
responseType: 'badRequest'
},
success: {
description: 'The message was sent successfully.'
}
},
fn: async function({emailAddress, firstName, lastName, message}) {
if (!sails.config.custom.slackWebhookUrlForContactForm) {
throw new Error(
'Message not delivered: slackWebhookUrlForContactForm needs to be configured in sails.config.custom. Here\'s the undelivered message: ' +
`Name: ${firstName + ' ' + lastName}, Email: ${emailAddress}, Message: ${message ? message : 'No message.'}`
);
}
let emailDomain = emailAddress.split('@')[1];
if(_.includes(sails.config.custom.bannedEmailDomainsForWebsiteSubmissions, emailDomain.toLowerCase())){
throw 'invalidEmailDomain';
}
await sails.helpers.http.post(sails.config.custom.slackWebhookUrlForContactForm, {
text: `New contact form message: (Remember: we have to email back; can't just reply to this thread.) cc @sales `+
`Name: ${firstName + ' ' + lastName}, Email: ${emailAddress}, Message: ${message ? message : 'No message.'}`
});
// Use timers.setImmediate() to update/create CRM records in the background.
require('timers').setImmediate(async ()=>{
await sails.helpers.salesforce.updateOrCreateContactAndAccountAndCreateLead.with({
emailAddress: emailAddress,
firstName: firstName,
lastName: lastName,
leadSource: 'Website - Contact forms',
leadDescription: `Sent a contact form message: ${message}`,
}).tolerate((err)=>{
sails.log.warn(`Background task failed: When a user submitted a contact form message, a lead/contact could not be updated in the CRM for this email address: ${emailAddress}. Error:`, err.raw);
});
});//_∏_ (Meanwhile...)
}
};