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

Feature/79 delete account removes email from sendgrid #177

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions src/modules/email/email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,38 @@ export class EmailService {
return await sgClient.request(request);
}

async getContactId(email: string): Promise<SendgridSearchResult> {
const request = {
json: undefined,
method: 'POST',
url: '/v3/marketing/contacts/search',
body: JSON.stringify({
query: `email = '${email}'`,
}),
};

const response: [ClientResponse, any] = await sgClient.request(request);

if (response[0].statusCode == 200) {
const sendgridResponse: SendgridSearchResult = JSON.parse(
response[0].body,
);
return sendgridResponse;
}

Promise.resolve(null);
}

async deleteContact(contactId: string) {
const request = {
json: undefined,
method: 'DELETE',
url: '/v3/marketing/contacts?ids=' + contactId,
};

const response: [ClientResponse, any] = await sgClient.request(request);
}

private async injectData(name: string, data: Record<string, string>) {
const template = await this.getTemplateContent(name);
const layout = await this.layout;
Expand Down
4 changes: 4 additions & 0 deletions src/modules/email/interfaces/sendgrid/contact.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface SendgridContact {
email: string;
id: string;
}
5 changes: 5 additions & 0 deletions src/modules/email/interfaces/sendgrid/response.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { SendgridSearchResult } from './searchResult.interface';
export interface SendgridResponse {
statusCode: number;
body: SendgridSearchResult;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { SendgridContact } from './contact.interface';

export interface SendgridSearchResult {
contact_count: number;
result: SendgridContact[];
}
10 changes: 9 additions & 1 deletion src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,15 @@ export class UsersController {
reason: params.reason,
},
};
this.emailService.send(emailData);

await this.emailService.send(emailData);
}

// Delete user email from SendGrid
const response = await this.emailService.getContactId(user.email);

if (response != null && response.result.length > 0) {
await this.emailService.deleteContact(response.result[0].id);
}

return {
Expand Down