From 1f4181d776ca5b5ea4d7c1d00c9f9529253403ca Mon Sep 17 00:00:00 2001 From: Jamie Wood Date: Mon, 24 Jun 2024 00:19:06 +0100 Subject: [PATCH] feat(mailchimp): integrate changes from PR #65 by vpetersson Co-authored-by: Viktor Petersson --- src/routes/authorise.ts | 5 +++- src/utils/config.ts | 1 + src/utils/logAuthDrivers.ts | 54 +++++++++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/routes/authorise.ts b/src/routes/authorise.ts index b7f1371..f2f3271 100644 --- a/src/routes/authorise.ts +++ b/src/routes/authorise.ts @@ -1,6 +1,6 @@ import express, { Request, Response } from 'express'; import { logger } from '../utils/logger'; -import { webhook, googleSheets } from '../utils/logAuthDrivers'; +import { webhook, googleSheets, mailchimp } from '../utils/logAuthDrivers'; import { config, UnifiControllerType } from '../utils/config'; import { createAxiosInstance } from '../utils/axios'; @@ -56,6 +56,9 @@ const logAuth = async (formData: any): Promise => { case 'googlesheets': await googleSheets(formData); break; + case 'mailchimp': + await mailchimp(formData); + break; default: break; } diff --git a/src/utils/config.ts b/src/utils/config.ts index dfef14d..d856594 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -11,6 +11,7 @@ export enum LogAuthDriver { None = 'none', Webhook = 'webhook', Googlesheets = 'googlesheets', + Mailchimp = 'mailchimp', } export enum UnifiControllerType { diff --git a/src/utils/logAuthDrivers.ts b/src/utils/logAuthDrivers.ts index 46098fc..2da97a0 100644 --- a/src/utils/logAuthDrivers.ts +++ b/src/utils/logAuthDrivers.ts @@ -1,4 +1,5 @@ import axios from 'axios'; +import crypto from 'crypto'; import { logger } from './logger'; import { GoogleSpreadsheet } from 'google-spreadsheet'; import { JWT as GoogleAuthJWT, CredentialBody } from 'google-auth-library'; @@ -26,15 +27,17 @@ export const webhook = async (formData: FormData): Promise => { export const googleSheets = async (formData: FormData): Promise => { // Post formData to Google Sheets try { - // Decode the credentials from base64 + //Ensure LOG_AUTH_GOOGLE_CREDENTIALS is defined if (!process.env.LOG_AUTH_GOOGLE_CREDENTIALS) { throw new Error('LOG_AUTH_GOOGLE_CREDENTIALS is not set'); } - // Ensure LOG_AUTH_GOOGLE_SHEET_ID is defined and not empty + + // Ensure LOG_AUTH_GOOGLE_SHEET_ID is defined const sheetId = process.env.LOG_AUTH_GOOGLE_SHEET_ID; if (!sheetId) { throw new Error('LOG_AUTH_GOOGLE_SHEET_ID is not set'); } + const credentialsBase64String = process.env.LOG_AUTH_GOOGLE_CREDENTIALS; if (!credentialsBase64String) { throw new Error('LOG_AUTH_GOOGLE_CREDENTIALS is not set'); @@ -69,3 +72,50 @@ export const googleSheets = async (formData: FormData): Promise => { logger.error('Google Sheets Failed'); } }; + +export const mailchimp = async (formData: FormData): Promise => { + //Ensure LOG_AUTH_MAILCHIMP_SERVER_PREFIX is defined + if (!process.env.LOG_AUTH_MAILCHIMP_SERVER_PREFIX) { + throw new Error('LOG_AUTH_MAILCHIMP_SERVER_PREFIX is not set'); + } + + //Ensure LOG_AUTH_MAILCHIMP_API_KEY is defined + if (!process.env.LOG_AUTH_MAILCHIMP_API_KEY) { + throw new Error('LOG_AUTH_MAILCHIMP_API_KEY is not set'); + } + + //Ensure LOG_AUTH_MAILCHIMP_LIST_ID is defined + if (!process.env.LOG_AUTH_MAILCHIMP_LIST_ID) { + throw new Error('LOG_AUTH_MAILCHIMP_LIST_ID is not set'); + } + + // Ensure formData.email is defined + if (!formData.email) { + throw new Error('Form has no property set - email'); + } + try { + const userData = { + email_address: formData.email, + status: 'subscribed', + }; + const subscriberHash = crypto + .createHash('md5') + .update(userData.email_address.toLowerCase()) + .digest('hex'); + + const response = await axios({ + method: 'post', + url: `https://${process.env.LOG_AUTH_MAILCHIMP_SERVER_PREFIX}.api.mailchimp.com/3.0/lists/${process.env.LOG_AUTH_MAILCHIMP_LIST_ID}/members/${subscriberHash}`, + headers: { + 'Content-Type': 'application/json', + Authorization: `apikey ${process.env.LOG_AUTH_MAILCHIMP_API_KEY}`, + }, + data: JSON.stringify(userData), + }); + + logger.info(`User added successfully: ${response.data}`); + } catch (err) { + logger.error(err); + logger.error('Mailchimp Failed'); + } +};