-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(website): migrate to sendgrid for newsletter (#819)
- Loading branch information
1 parent
5d100de
commit 77ac331
Showing
16 changed files
with
249 additions
and
126 deletions.
There are no files selected for viewing
3 changes: 1 addition & 2 deletions
3
functions/src/webhooks/admin/payment-process/tasks/PaymentTask.ts
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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { Client } from '@sendgrid/client'; | ||
import { SendgridContactType } from '@socialincome/shared/src/sendgrid/types'; | ||
import { CountryCode } from '../types/country'; | ||
import { Suppression } from './types'; | ||
|
||
export const NEWSLETTER_LIST_ID = '2896ee4d-d1e0-4a4a-8565-7e592c377e36'; | ||
export const NEWSLETTER_SUPPRESSION_LIST_ID = 45634; | ||
|
||
export type NewsletterSubscriptionData = { | ||
firstname?: string; | ||
lastname?: string; | ||
email: string; | ||
language: 'de' | 'en'; | ||
country?: CountryCode; | ||
status?: 'subscribed' | 'unsubscribed'; | ||
isContributor?: boolean; | ||
}; | ||
|
||
export type SendgridSubscriptionClientProps = { | ||
apiKey: string; | ||
listId: string; | ||
suppressionListId: number; | ||
}; | ||
|
||
export class SendgridSubscriptionClient extends Client { | ||
listId: string; | ||
suppressionListId: number; // unsubscribe group id | ||
|
||
constructor(sendgridClientProps: SendgridSubscriptionClientProps) { | ||
super(); | ||
this.setApiKey(sendgridClientProps.apiKey); | ||
this.listId = sendgridClientProps.listId; | ||
this.suppressionListId = sendgridClientProps.suppressionListId; | ||
} | ||
|
||
getContact = async (email: string): Promise<SendgridContactType | null> => { | ||
try { | ||
const [, body] = await this.request({ | ||
method: 'POST', | ||
url: '/v3/marketing/contacts/search/emails', | ||
body: { emails: [email] }, | ||
}); | ||
const contact = body.result[email].contact as SendgridContactType; | ||
const isSuppressed = await this.isSuppressed(email); | ||
return { ...contact, status: isSuppressed ? 'unsubscribed' : 'subscribed' } as SendgridContactType; | ||
} catch (e: any) { | ||
if (e.code === 404) return null; | ||
throw e; | ||
} | ||
}; | ||
|
||
upsertSubscription = async (data: NewsletterSubscriptionData) => { | ||
const contact = await this.getContact(data.email); | ||
if (!contact) { | ||
await this.addContact(data); | ||
} | ||
|
||
if (data.status === 'subscribed') { | ||
await this.removeSuppression(data.email); | ||
} else { | ||
await this.addSuppression(data.email); | ||
} | ||
}; | ||
|
||
isSuppressed = async (email: string): Promise<boolean> => { | ||
const [_, body] = await this.request({ url: `/v3/asm/suppressions/${email}`, method: 'GET' }); | ||
return body.suppressions.some( | ||
(suppression: Suppression) => suppression.id === this.suppressionListId && suppression.suppressed, | ||
); | ||
}; | ||
|
||
removeSuppression = async (email: string) => { | ||
await this.request({ url: `/v3/asm/groups/${this.suppressionListId}/suppressions/${email}`, method: 'DELETE' }); | ||
}; | ||
|
||
/** | ||
* Add an email to the unsubscribe list. | ||
*/ | ||
addSuppression = async (email: string) => { | ||
await this.request({ | ||
url: `/v3/asm/groups/${this.suppressionListId}/suppressions`, | ||
method: 'POST', | ||
body: { recipient_emails: [email] }, | ||
}); | ||
}; | ||
|
||
/** | ||
* Add a contact to the global contacts list. | ||
*/ | ||
addContact = async (data: NewsletterSubscriptionData) => { | ||
await this.request({ | ||
url: `/v3/marketing/contacts`, | ||
method: 'PUT', | ||
body: { | ||
list_ids: [this.listId], | ||
contacts: [ | ||
{ | ||
email: data.email, | ||
first_name: data.firstname, | ||
last_name: data.lastname, | ||
country: data.country, | ||
custom_fields: { language: data.language, is_contributor: data.isContributor }, | ||
}, | ||
], | ||
}, | ||
}); | ||
}; | ||
} |
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,35 @@ | ||
export type Suppression = { | ||
name: string; | ||
id: number; | ||
description: string; | ||
is_default: boolean; | ||
suppressed: boolean; | ||
}; | ||
|
||
export type SendgridContactType = { | ||
address_line_1?: string; | ||
address_line_2?: string; | ||
alternate_emails: string[]; | ||
city?: string; | ||
country?: string; | ||
email: string; | ||
first_name?: string; | ||
id?: string; | ||
last_name?: string; | ||
list_ids: string[]; | ||
postal_code?: string; | ||
state_province_region?: string; | ||
phone_number?: string; | ||
whatsapp?: string; | ||
line?: string; | ||
facebook?: string; | ||
unique_name?: string; | ||
_metadata: any[]; | ||
custom_fields: { | ||
language: string; | ||
is_contributor: string; | ||
}; | ||
created_at: string; | ||
updated_at: string; | ||
status?: 'subscribed' | 'unsubscribed'; | ||
}; |
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
This file was deleted.
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
Oops, something went wrong.