Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mailchimp support #72

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/routes/authorise.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -56,6 +56,9 @@ const logAuth = async (formData: any): Promise<void> => {
case 'googlesheets':
await googleSheets(formData);
break;
case 'mailchimp':
await mailchimp(formData);
break;
default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum LogAuthDriver {
None = 'none',
Webhook = 'webhook',
Googlesheets = 'googlesheets',
Mailchimp = 'mailchimp',
}

export enum UnifiControllerType {
Expand Down
54 changes: 52 additions & 2 deletions src/utils/logAuthDrivers.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -26,15 +27,17 @@ export const webhook = async (formData: FormData): Promise<void> => {
export const googleSheets = async (formData: FormData): Promise<void> => {
// 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');
Expand Down Expand Up @@ -69,3 +72,50 @@ export const googleSheets = async (formData: FormData): Promise<void> => {
logger.error('Google Sheets Failed');
}
};

export const mailchimp = async (formData: FormData): Promise<void> => {
//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');
}
};