Skip to content

Commit

Permalink
feature(general): mailchimp api integration (#747)
Browse files Browse the repository at this point in the history
* Initial Commit for Mailchimp API

- Adding in shared the Event Handler --> can be used by all apps
- Adding a specific next.js api for Mailchimp inside the Website

* Prettified Code!

* various small updates

* Add updates page to add files to Mailchimp

Add /updates page to add a user to Mailchimp

* Update for /updates page

* Prettified Code!

* wip

* cleanup

* wip

* wip

---------

Co-authored-by: brennerthomas <[email protected]>
Co-authored-by: Michael Kündig <[email protected]>
  • Loading branch information
3 people authored Mar 12, 2024
1 parent 09689bb commit c7335f9
Show file tree
Hide file tree
Showing 18 changed files with 664 additions and 10 deletions.
168 changes: 166 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion shared/locales/de/website-me.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
"country": "Land",
"language": "Kommunikationssprache",
"submit-button": "Kontaktangaben aktualisieren",
"user-updated-toast": "Deine Kontaktangaben wurden aktualisiert"
"user-updated-toast": "Deine Kontaktangaben wurden aktualisiert",
"newsletter-switch": "Ich möchte den Newsletter erhalten"
},
"security": {
"reset-password": {
Expand Down
22 changes: 22 additions & 0 deletions shared/locales/de/website-newsletter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"metadata": {
"title": "Datenschutzrichtlinie | Social Income"
},
"updates": {
"alert-title": "Social Income Newsletter",
"alert-description": "Melde dich an, um bei allen Thema rund um Social Income auf dem neusten Stand zu sein.",
"firstname": "Vorname",
"lastname": "Nachname",
"gender": "Geschlecht",
"email": "E-Mail",
"street": "Strasse",
"street-number": "Nummer",
"city": "Ort",
"zip": "PLZ",
"country": "Land",
"language": "Kommunikationssprache",
"submit-button": "Beim Newsletter anmelden",
"newsletter-updated-toast": "Du wurdest zum Newsletter hinzugefügt",
"newsletter-error-toast": "Du konntest leider nicht zum Newsletter hinzugefügt werden"
}
}
3 changes: 2 additions & 1 deletion shared/locales/en/website-me.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
"country": "Country",
"language": "Communication language",
"submit-button": "Update Personal Info",
"user-updated-toast": "Your personal info has been updated"
"user-updated-toast": "Your personal info has been updated",
"newsletter-switch": "I would like to receive the newsletter"
},
"security": {
"reset-password": {
Expand Down
22 changes: 22 additions & 0 deletions shared/locales/en/website-newsletter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"metadata": {
"title": "Datenschutzrichtlinie | Social Income"
},
"updates": {
"alert-title": "Social Income Newsletter",
"alert-description": "Sign up to stay up to date on all things of Social Income.",
"firstname": "First Name",
"lastname": "Last Name",
"gender": "Gender",
"email": "Email",
"street": "Street",
"street-number": "Number",
"city": "City",
"zip": "Zip",
"country": "Country",
"language": "Communication language",
"submit-button": "Subscribe to newsletter",
"newsletter-updated-toast": "You have been added to the newsletter",
"newsletter-error-toast": "We weren't able to add you to our newsletter"
}
}
2 changes: 2 additions & 0 deletions shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.10",
"@types/luxon": "^3.3.5",
"@types/mailchimp__mailchimp_marketing": "^3.0.17",
"@types/mjml": "^4.7.4",
"@types/nodemailer": "^6.4.14",
"firebase-functions-test": "^3.1.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.1"
},
"dependencies": {
"@mailchimp/mailchimp_marketing": "^3.0.80",
"axios": "^1.6.2",
"firebase-admin": "^11.11.0",
"handlebars": "^4.7.8",
Expand Down
65 changes: 65 additions & 0 deletions shared/src/mailchimp/MailchimpAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import mailchimp, { Status } from '@mailchimp/mailchimp_marketing';
import { CountryCode } from '../types/country';

export type MailchimpSubscriptionData = {
email: string;
status: Status;
language?: 'de' | 'en';
firstname?: string;
lastname?: string;
country?: CountryCode;
};

export class MailchimpAPI {
constructor(apiKey: string, server: string) {
mailchimp.setConfig({
apiKey: apiKey,
server: server,
});
}

getSubscriber = async (email: string, listId: string) => {
try {
return await mailchimp.lists.getListMember(listId, this.md5(email.toLowerCase()));
} catch (error) {
return null;
}
};

upsertSubscription = async (data: MailchimpSubscriptionData, listId: string) => {
const subscriber = await this.getSubscriber(data.email, listId);
if (subscriber === null) {
return this.createSubscription(data, listId);
} else {
await mailchimp.lists.updateListMember(listId, this.md5(data.email), {
email_address: data.email,
status: data.status,
merge_fields: {
FNAME: data.firstname ?? '',
LNAME: data.lastname ?? '',
COUNTRY: data.country ?? '',
LANGUAGE: data.language === 'de' ? 'German' : 'English',
},
});
}
};

createSubscription = async (data: MailchimpSubscriptionData, listId: string) => {
await mailchimp.lists.addListMember(listId, {
email_address: data.email,
status: data.status,
merge_fields: {
FNAME: data.firstname ?? '',
LNAME: data.lastname ?? '',
COUNTRY: data.country ?? '',
LANGUAGE: data.language === 'de' ? 'German' : 'English',
},
});
};

// Helper function to calculate the MD5 hash
md5 = (str: string): string => {
const crypto = require('crypto');
return crypto.createHash('md5').update(str).digest('hex');
};
}
Loading

0 comments on commit c7335f9

Please sign in to comment.