Skip to content

Commit

Permalink
chore(mailjet): update mailjet
Browse files Browse the repository at this point in the history
  • Loading branch information
emile-bex committed Nov 17, 2023
1 parent c1a9efa commit f4f7abd
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 350 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"moment": "^2.29.4",
"moment-timezone": "^0.5.43",
"node-fetch": "^2.6.7",
"node-mailjet": "^3.3.9",
"node-mailjet": "^6.0.4",
"passport": "^0.6.0",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
Expand Down Expand Up @@ -103,7 +103,6 @@
"@types/multer": "^1.4.7",
"@types/node": "^16.0.0",
"@types/node-fetch": "2.x",
"@types/node-mailjet": "^3.3.9",
"@types/passport-jwt": "^3.0.6",
"@types/passport-local": "^1.0.34",
"@types/puppeteer": "^5.4.6",
Expand Down
119 changes: 59 additions & 60 deletions src/external-services/mailjet/mailjet.service.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
import { Injectable } from '@nestjs/common';
import { connect, Email } from 'node-mailjet';
import {
Client,
Contact,
ContactProperties,
ContactSubscription,
LibraryResponse,
SendEmailV3_1,
} from 'node-mailjet';

import {
CustomContactParams,
CustomMailParams,
MailjetContactList,
MailjetContactTag,
MailjetContactTagNames,
MailjetCustomContact,
MailjetCustomResponse,
MailjetListActions,
MailjetOptions,
} from './mailjet.types';

import { createMail } from './mailjet.utils';
import SendParams = Email.SendParams;
import SendParamsRecipient = Email.SendParamsRecipient;

@Injectable()
export class MailjetService {
/*
private mailjetTransactional: Email.Client;
private mailjetNewsletter: Email.Client;
*/

/*
constructor() {
this.mailjetTransactional = this.getTransactionalInstance();
this.mailjetNewsletter = this.getNewsletterInstance();
}
*/

getTransactionalInstance() {
return connect(`${process.env.MAILJET_PUB}`, `${process.env.MAILJET_SEC}`);
}

getNewsletterInstance() {
return connect(
`${process.env.MAILJET_NEWSLETTER_PUB}`,
`${process.env.MAILJET_NEWSLETTER_SEC}`
);
private mailjetTransactional: Client;
private mailjetNewsletter: Client;

constructor() {
this.mailjetTransactional = new Client({
apiKey: `${process.env.MAILJET_PUB}`,
apiSecret: `${process.env.MAILJET_SEC}`,
});
this.mailjetNewsletter = new Client({
apiKey: `${process.env.MAILJET_NEWSLETTER_PUB}`,
apiSecret: `${process.env.MAILJET_NEWSLETTER_SEC}`,
});
}

async sendMail(params: CustomMailParams | CustomMailParams[]) {
const mailjetParams: SendParams = { Messages: [] };
const mailjetParams: SendEmailV3_1.Body = { Messages: [] };
if (Array.isArray(params)) {
mailjetParams.Messages = params.map((p) => {
return createMail(p);
Expand All @@ -49,53 +44,52 @@ export class MailjetService {
mailjetParams.Messages = [createMail(params)];
}

return await this.getTransactionalInstance()
.post('send', {
version: 'v3.1',
})
return this.mailjetTransactional
.post('send', MailjetOptions.MAILS)
.request(mailjetParams);
}

async findContact(
email: CustomContactParams['email']
): Promise<MailjetCustomContact> {
const contact: SendParamsRecipient = {
): Promise<Contact.Contact> {
const contact: Partial<Contact.Contact> = {
Email: email.toLowerCase(),
};

const {
body: { Data: data },
} = (await this.getNewsletterInstance()
.get('contact', { version: 'v3' })
.request(contact)) as MailjetCustomResponse;
}: LibraryResponse<Contact.GetContactResponse> = await this.mailjetNewsletter
.get('contact', MailjetOptions.CONTACTS)
.request(contact);

return data[0];
}

async createContact(
email: CustomContactParams['email']
): Promise<MailjetCustomContact> {
const contact: SendParamsRecipient = {
): Promise<Contact.Contact> {
const contact: Contact.PostContactBody = {
Email: email,
};

const {
body: { Data: data },
} = (await this.getNewsletterInstance()
.post('contact', { version: 'v3' })
.request(contact)) as MailjetCustomResponse;
}: LibraryResponse<Contact.PostContactResponse> = await this.mailjetNewsletter
.post('contact', MailjetOptions.CONTACTS)
.request(contact);

return data[0];
}

async updateContactTags(
id: string,
id: number,
{ zone, status }: Pick<CustomContactParams, 'zone' | 'status'>
) {
let dataToUpdate: { Data: MailjetContactTag[] } = {
): Promise<LibraryResponse<ContactProperties.PutContactDataBody>> {
let dataToUpdate: ContactProperties.PutContactDataBody = {
Data: [
{
Name: MailjetContactTagNames.NEWSLETTER,
Value: true,
Value: 'true',
},
],
};
Expand Down Expand Up @@ -123,24 +117,29 @@ export class MailjetService {
};
}

return await this.getNewsletterInstance()
.put('contactdata', { version: 'v3' })
return this.mailjetNewsletter
.put('contactdata', MailjetOptions.CONTACTS)
.id(id)
.request(dataToUpdate);
}

async subscribeToNewsletterList(id: string) {
const dataToUpdate: { ContactsLists: MailjetContactList[] } = {
ContactsLists: [
{
Action: MailjetListActions.FORCE,
ListID: process.env.MAILJET_NEWSLETTER_LIST_ID,
},
],
};
async subscribeToNewsletterList(
id: number
): Promise<
LibraryResponse<ContactSubscription.PostContactManageContactsListsResponse>
> {
const dataToUpdate: ContactSubscription.PostContactManageContactsListsBody =
{
ContactsLists: [
{
Action: MailjetListActions.FORCE,
ListID: parseInt(process.env.MAILJET_NEWSLETTER_LIST_ID),
},
],
};

return await this.getNewsletterInstance()
.post('contact', { version: 'v3' })
return this.mailjetNewsletter
.post('contact', MailjetOptions.CONTACTS)
.id(id)
.action('managecontactslists')
.request(dataToUpdate);
Expand Down
43 changes: 14 additions & 29 deletions src/external-services/mailjet/mailjet.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { BulkContactManagement } from 'node-mailjet';
import { RequestConstructorConfig } from 'node-mailjet/declarations/request/Request';
import { AdminZone } from 'src/utils/types';
import ManageContactsAction = BulkContactManagement.ManageContactsAction;

export const MailjetOptions: { [K in string]: RequestConstructorConfig } = {
MAILS: { version: 'v3.1' },
CONTACTS: { version: 'v3' },
} as const;

export interface CustomMailParams {
toEmail:
Expand Down Expand Up @@ -29,26 +37,16 @@ export const MailjetContactTagNames = {
STATUS: 'profil_linkedout',
} as const;

export type MailjetContactTagName =
typeof MailjetContactTagNames[keyof typeof MailjetContactTagNames];

export interface MailjetContactTag {
Name: MailjetContactTagName;
Value: string | boolean;
}

export const MailjetListActions = {
const MailjetListActionsCustom = {
NO_FORCE: 'addnoforce',
FORCE: 'addforce',
} as const;

export type MailjetListAction =
typeof MailjetListActions[keyof typeof MailjetListActions];

export interface MailjetContactList {
Action: MailjetListAction;
ListID: string;
}
// Hack because error when getting ManageContactsAction enum value
export const MailjetListActions = MailjetListActionsCustom as Record<
keyof typeof MailjetListActionsCustom,
ManageContactsAction
>;

export const MailjetTemplates = {
ACCOUNT_CREATED: 3920498,
Expand Down Expand Up @@ -97,16 +95,3 @@ export const ContactStatuses = {

export type ContactStatus =
typeof ContactStatuses[keyof typeof ContactStatuses];

export interface MailjetError {
statusCode: number;
ErrorMessage: string;
}

export interface MailjetCustomContact {
ID: string;
}

export interface MailjetCustomResponse {
body: { Data: ReadonlyArray<MailjetCustomContact> };
}
5 changes: 2 additions & 3 deletions src/external-services/mailjet/mailjet.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import _ from 'lodash';
import { Email } from 'node-mailjet';
import { SendEmailV3_1 } from 'node-mailjet';
import { CustomMailParams, MailjetTemplates } from './mailjet.types';
import SendParams = Email.SendParams;

const useCampaigns = process.env.MAILJET_CAMPAIGNS_ACTIVATED === 'true';

Expand All @@ -14,7 +13,7 @@ export function createMail({
variables,
templateId,
}: CustomMailParams) {
const recipients: SendParams['Messages'][number] = {
const recipients: SendEmailV3_1.Body['Messages'][number] = {
To: [],
Cc: [],
From: { Email: '' },
Expand Down
2 changes: 0 additions & 2 deletions tests/mocks.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,4 @@ export const MailjetMock: ProviderMock<MailjetService> = {
updateContactTags: jest.fn(),
subscribeToNewsletterList: jest.fn(),
sendContact: jest.fn(),
getNewsletterInstance: jest.fn(),
getTransactionalInstance: jest.fn(),
} as const;
Loading

0 comments on commit f4f7abd

Please sign in to comment.