|
|
@@ -46,6 +46,8 @@ function makeMailchimpProvider(listName, listId) {
|
|
|
subscribe,
|
|
|
unsubscribe,
|
|
|
changeEmail,
|
|
|
+ tag,
|
|
|
+ removeTag,
|
|
|
}
|
|
|
|
|
|
async function subscribed(user) {
|
|
|
@@ -85,6 +87,38 @@ function makeMailchimpProvider(listName, listId) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ async function tag(user, tag) {
|
|
|
+ try {
|
|
|
+ const path = getMemberTagsPath(user.email)
|
|
|
+ await mailchimp.post(path, {
|
|
|
+ tags: [{ name: tag, status: 'active' }],
|
|
|
+ })
|
|
|
+ logger.debug({ user, listName }, `finished adding ${tag} to user`)
|
|
|
+ } catch (err) {
|
|
|
+ throw OError.tag(err, `error adding ${tag} to user`, {
|
|
|
+ userId: user._id,
|
|
|
+ listName,
|
|
|
+ tag,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async function removeTag(user, tag) {
|
|
|
+ try {
|
|
|
+ const path = getMemberTagsPath(user.email)
|
|
|
+ await mailchimp.post(path, {
|
|
|
+ tags: [{ name: tag, status: 'inactive' }],
|
|
|
+ })
|
|
|
+ logger.debug({ user, listName }, `finished removing ${tag} from user`)
|
|
|
+ } catch (err) {
|
|
|
+ throw OError.tag(err, `error removing ${tag} from user`, {
|
|
|
+ userId: user._id,
|
|
|
+ listName,
|
|
|
+ tag,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async function unsubscribe(user, options = {}) {
|
|
|
try {
|
|
|
const path = getSubscriberPath(user.email)
|
|
|
@@ -208,6 +242,11 @@ function makeMailchimpProvider(listName, listId) {
|
|
|
return `/lists/${MAILCHIMP_LIST_ID}/members/${emailHash}`
|
|
|
}
|
|
|
|
|
|
+ function getMemberTagsPath(email) {
|
|
|
+ const emailHash = hashEmail(email)
|
|
|
+ return `/lists/${MAILCHIMP_LIST_ID}/members/${emailHash}/tags`
|
|
|
+ }
|
|
|
+
|
|
|
function hashEmail(email) {
|
|
|
return crypto.createHash('md5').update(email.toLowerCase()).digest('hex')
|
|
|
}
|