62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
const { omnisendApiKey } = require("../config.js")
|
|
const { statusIsValid } = require("./utils.js")
|
|
/**
|
|
*
|
|
* @param {string} email
|
|
*/
|
|
function createNewsletterSubscriber(email) {
|
|
const res = context.http.fetch("https://api.omnisend.com/v5/contacts", {
|
|
method: "POST",
|
|
headers: {
|
|
"X-API-KEY": omnisendApiKey,
|
|
accept: "application/json",
|
|
"content-type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
createdAt: new Date().toISOString(),
|
|
identifiers: [
|
|
{
|
|
channels: {
|
|
email: {
|
|
status: "subscribed",
|
|
statusDate: new Date().toISOString(),
|
|
},
|
|
},
|
|
consent: {
|
|
createdAt: new Date().toISOString(),
|
|
source: "headless-frontend",
|
|
},
|
|
id: email,
|
|
type: "email",
|
|
},
|
|
],
|
|
}),
|
|
})
|
|
if (!statusIsValid(res.status)) {
|
|
throw {
|
|
status: res.status,
|
|
error: res.statusText,
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {string} email
|
|
* @returns {boolean}
|
|
*/
|
|
function checkIfNewsletterSubscriber(email) {
|
|
const res = context.http.fetch("https://api.omnisend.com/v5/contacts", {
|
|
headers: {
|
|
"X-API-KEY": omnisendApiKey,
|
|
accept: "application/json",
|
|
},
|
|
})
|
|
const resJson = res.body.json()
|
|
return resJson.contacts.some((contact) => contact.email === email)
|
|
}
|
|
|
|
module.exports = {
|
|
createNewsletterSubscriber,
|
|
checkIfNewsletterSubscriber,
|
|
}
|