webtasks/webtask-mailchimp.js

116 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-10-20 03:19:36 +02:00
const express = require('express')
const Webtask = require('webtask-tools')
const cors = require('cors')
const crypto = require('crypto')
const bodyParser = require('body-parser')
const request = require('request')
const server = express()
server.listen(4430)
server.use(bodyParser.json())
//
// Allow requests from these domains only
//
const corsOptions = {
origin: ['https://oceanprotocol.com', /\.oceanprotocol\.com$/],
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
server.use(cors(corsOptions))
const baseUrl = 'https://us16.api.mailchimp.com/3.0'
const listId = '3c6eed8b71'
2019-08-21 11:46:02 +02:00
const md5 = data =>
crypto
.createHash('md5')
.update(data)
.digest('hex')
2018-10-20 03:19:36 +02:00
server.post('/newsletter/:email', (req, res) => {
const { email } = req.params
const { MAILCHIMP_API_KEY } = req.webtaskContext.secrets
const emailDecoded = decodeURIComponent(email)
const subscriberHash = md5(emailDecoded)
const baseOptions = {
url: `${baseUrl}/lists/${listId}/members/${subscriberHash}`,
2019-08-19 13:03:48 +02:00
auth: {
user: 'oceanprotocol',
pass: MAILCHIMP_API_KEY
2018-10-20 03:19:36 +02:00
}
}
const optionsCreate = {
...baseOptions,
json: {
2019-08-19 13:03:48 +02:00
email_address: emailDecoded,
status: 'pending', // double opt-in
merge_fields: {
2018-10-20 03:19:36 +02:00
// our GDPR fallback
2019-08-19 13:03:48 +02:00
GDPR: 'yes'
2018-10-20 03:19:36 +02:00
}
}
}
2019-08-21 11:46:02 +02:00
const optionsMarketing = marketingPermissionId => ({
...baseOptions,
json: {
marketing_permissions: [
{
2019-08-19 13:03:48 +02:00
marketing_permission_id: marketingPermissionId,
text: 'Email',
enabled: true
2019-08-21 11:46:02 +02:00
}
]
2018-10-20 03:19:36 +02:00
}
2019-08-21 11:46:02 +02:00
})
2018-10-20 03:19:36 +02:00
const addMarketingPermissions = (data, cb) => {
2019-08-21 11:46:02 +02:00
const marketingPermissionId =
data.marketing_permissions[0].marketing_permission_id
2018-10-20 03:19:36 +02:00
2019-08-21 11:46:02 +02:00
request.patch(
optionsMarketing(marketingPermissionId),
(error, response, body) => {
if (error) res.send(error)
2018-10-20 03:19:36 +02:00
2019-08-21 11:46:02 +02:00
return cb(body)
}
)
2018-10-20 03:19:36 +02:00
}
// Check if user exists first
request.get(baseOptions, (error, response, body) => {
if (error) res.send(error)
2018-10-30 12:52:37 +01:00
const data = JSON.parse(body)
2018-10-20 03:19:36 +02:00
// Member exists and is subscribed
2018-10-30 12:52:37 +01:00
if (data.status === 'subscribed') {
2018-10-20 03:19:36 +02:00
// Patch in native GDPR permissions
2018-10-30 12:52:37 +01:00
addMarketingPermissions(data, () => {
2018-10-20 03:19:36 +02:00
res.send('{ "status": "exists" }')
})
} else {
// Create user
request.put(optionsCreate, (error2, response, body2) => {
if (error2) res.send(error2)
2018-12-11 12:33:33 +01:00
if (Number.isInteger(body2.status)) {
2018-10-20 03:19:36 +02:00
res.send(body2)
}
// Patch in native GDPR permissions
2018-12-11 12:33:33 +01:00
addMarketingPermissions(body2, () => {
2018-10-20 03:19:36 +02:00
res.send('{ "status": "created" }')
})
})
}
})
})
module.exports = Webtask.fromExpress(server)