-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(general): mailchimp api integration (#747)
* 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
1 parent
09689bb
commit c7335f9
Showing
18 changed files
with
664 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; | ||
} |
Oops, something went wrong.