-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored Sendgrid API calls as a service
- Loading branch information
1 parent
9c62de9
commit fd060c1
Showing
4 changed files
with
96 additions
and
102 deletions.
There are no files selected for viewing
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 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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { SubscriberController } from "./subscriber.controller"; | ||
import { SubscriberService } from "./subscriber.service"; | ||
import { ConfigModule } from "../config/config.module"; | ||
import { Client } from "@sendgrid/client"; | ||
|
||
@Module({ | ||
imports: [ConfigModule], | ||
providers: [], | ||
exports: [], | ||
providers: [SubscriberService, Client], | ||
exports: [SubscriberService], | ||
controllers: [SubscriberController] | ||
}) | ||
export class SubscriberModule {} |
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,81 @@ | ||
import { Injectable, Res } from "@nestjs/common"; | ||
import { ConfigService } from "../config/config.service"; | ||
import { Client } from "@sendgrid/client"; | ||
|
||
type HttpMethod = 'get'|'GET'|'post'|'POST'|'put'|'PUT'|'patch'|'PATCH'|'delete'|'DELETE'; | ||
|
||
function delay(milliseconds){ | ||
return new Promise(resolve => { | ||
setTimeout(resolve, milliseconds); | ||
}); | ||
} | ||
|
||
@Injectable() | ||
export class SubscriberService { | ||
constructor( | ||
private readonly config: ConfigService, | ||
private client: Client | ||
) { | ||
this.client.setApiKey(this.config.get("SENDGRID_API_KEY")); | ||
} | ||
|
||
async findByEmail(email: string) { | ||
const searchRequest = { | ||
url: "/v3/marketing/contacts/search/emails", | ||
method:<HttpMethod> 'POST', | ||
body: { | ||
"emails": [email] | ||
} | ||
} | ||
|
||
// https://www.twilio.com/docs/sendgrid/api-reference/contacts/get-contacts-by-emails | ||
try { | ||
const user = await this.client.request(searchRequest); | ||
return {isError: false, code: user[0].statusCode, ...user}; | ||
} catch(error) { | ||
return {isError: true, ...error}; | ||
} | ||
} | ||
|
||
async create(email: string, list: string, @Res() response) { | ||
const addRequest = { | ||
url: "/v3/marketing/contacts", | ||
method:<HttpMethod> 'PUT', | ||
body: { | ||
"list_ids": [list], | ||
"contacts": [{"email": email}] | ||
} | ||
} | ||
|
||
// If successful, this will add the request to the queue and return a 202 | ||
// https://www.twilio.com/docs/sendgrid/api-reference/contacts/add-or-update-a-contact | ||
try { | ||
const result = await this.client.request(addRequest); | ||
return {isError: false, result: result}; | ||
} catch(error) { | ||
return {isError: true, ...error}; | ||
} | ||
} | ||
|
||
async checkCreate(email: string, @Res() response, counter: number = 0, checksBeforeFail: number, pauseBetweenChecks: number, list: string) { | ||
if(counter >= checksBeforeFail) { | ||
return { isError: true, code: 408 } | ||
} | ||
|
||
await delay(pauseBetweenChecks); | ||
|
||
const existingUser = await this.findByEmail(email); | ||
|
||
if(![200, 404].includes(existingUser.code)) { | ||
console.error(existingUser.code, existingUser.message); | ||
return { isError: true, code: existingUser.code, message: existingUser.message } | ||
} | ||
|
||
if(existingUser[0] && existingUser[0].body.result[email].contact["list_ids"].includes(list)) { | ||
// Success! | ||
return { isError: false, code: 200, ...existingUser }; | ||
} | ||
|
||
return await this.checkCreate(email, response, counter + 1, checksBeforeFail, pauseBetweenChecks, list); | ||
} | ||
} |