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

1544 New subscriber endpoint (part 1) #1545

Merged
merged 9 commits into from
Oct 16, 2024
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@nestjs/testing": "^7.6.15",
"@nestjs/typeorm": "^7.1.5",
"@octokit/rest": "^16.43.1",
"@sendgrid/client": "^8.1.3",
"@turf/bbox": "^6.0.1",
"@turf/buffer": "^5.1.5",
"@turf/helpers": "^6.1.4",
Expand Down
30 changes: 30 additions & 0 deletions server/src/_utils/validate-email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var tester = /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-*\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/;

/**
* Validate an email address.
* @param {string} email - The email address to validate.
* @returns {boolean}
*/
export default function validateEmail(email: string) {

if (!email)
return false;

if(email.length>254)
return false;

var valid = tester.test(email);
if(!valid)
return false;

// Further checking of some things regex can't handle
var parts = email.split("@");
if(parts[0].length>64)
return false;

var domainParts = parts[1].split(".");
if(domainParts.some(function(part) { return part.length>63; }))
return false;

return true;
};
4 changes: 3 additions & 1 deletion server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { AssignmentModule } from "./assignment/assignment.module";
import { DocumentModule } from "./document/document.module";
import { CrmModule } from "./crm/crm.module";
import { ZoningResolutionsModule } from "./zoning-resolutions/zoning-resolutions.module";
import { SubscriptionModule } from "./subscription/subscription.module";

@Module({
imports: [
Expand All @@ -24,7 +25,8 @@ import { ZoningResolutionsModule } from "./zoning-resolutions/zoning-resolutions
DispositionModule,
AssignmentModule,
DocumentModule,
ZoningResolutionsModule
ZoningResolutionsModule,
SubscriptionModule
],
controllers: [AppController]
})
Expand Down
168 changes: 168 additions & 0 deletions server/src/subscription/subscription.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { Controller, Get, Post, Param, Query, Req, Res } from "@nestjs/common";
import { ConfigService } from "../config/config.service";
import { Request } from "express";
import validateEmail from "../_utils/validate-email";
import client from "@sendgrid/client";

type HttpMethod = 'get'|'GET'|'post'|'POST'|'put'|'PUT'|'patch'|'PATCH'|'delete'|'DELETE';

const PAUSE_BETWEEN_CHECKS = 3000;
const CHECKS_BEFORE_FAIL = 10;

function delay(milliseconds){
return new Promise(resolve => {
setTimeout(resolve, milliseconds);
});
}

@Controller()
export class SubscriptionController {
dhochbaum-dcp marked this conversation as resolved.
Show resolved Hide resolved
apiKey = "";
list = "";

constructor(
private readonly config: ConfigService

) {
this.apiKey = this.config.get("SENDGRID_ENVIRONMENT") ==="staging" ? this.config.get("SENDGRID_API_KEY_STAGING") : this.config.get("SENDGRID_API_KEY_PRODUCTION");
dhochbaum-dcp marked this conversation as resolved.
Show resolved Hide resolved
this.list = this.config.get("SENDGRID_ENVIRONMENT") ==="staging" ? this.config.get("SENDGRID_LIST_STAGING") : this.config.get("SENDGRID_LIST_PRODUCTION");
}

@Post("/subscribers")
async subscribe(@Req() request: Request, @Res() response) {
dhochbaum-dcp marked this conversation as resolved.
Show resolved Hide resolved
const searchRequest = {
url: "/v3/marketing/contacts/search/emails",
method:<HttpMethod> 'POST',
body: {
"emails": [request.body.email]
}
}
const addRequest = {
url: "/v3/marketing/contacts",
method:<HttpMethod> 'PUT',
body: {
"list_ids": [this.list],
"contacts": [{"email": request.body.email}]
}
}

if (!validateEmail(request.body.email)) {
response.status(400).send({
error: "Invalid email address."
})
return;
}

client.setApiKey(this.apiKey);
dhochbaum-dcp marked this conversation as resolved.
Show resolved Hide resolved

const existingUser = await this.searchByEmail(request.body.email)
if(![200, 404].includes(existingUser.code)) {
console.error(existingUser.code, existingUser.message);
response.status(existingUser.code).send({ error: existingUser.message })
return;
}

// If the id is already in the list_ids, then they are also already on the list
if(existingUser[0] && existingUser[0].body.result[request.body.email].contact["list_ids"].includes(this.list)) {
response.status(409).send({
status: "error",
error: "A user with that email address already exists, and they are already in the desired list.",
})
return;
}

// If we have reached this point, the user either doesn't exist or isn't signed up for the list
const addToQueue = await this.addSubscriber(request.body.email, response)

if(addToQueue.isError) { return; }

// Now we keep checking to make sure the import was successful
const importConfirmation = await this.checkSuccessfulImport(request.body.email, response, 0)

if(importConfirmation.isError && (importConfirmation.code === 408)) {
response.status(408).send({
status: "error",
error: `Was not able to receive confirmation user information was updated in within ${PAUSE_BETWEEN_CHECKS * CHECKS_BEFORE_FAIL / 1000} seconds`,
})
return;
} else if (importConfirmation.isError) {
response.status(importConfirmation.code).send({
status: "error",
error: importConfirmation.message
})
return;
}

response.status(200).send({
status: "success",
user: importConfirmation[0].body.result[request.body.email]
})
return;

}

async searchByEmail(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 client.request(searchRequest);
return {isError: false, code: user[0].statusCode, ...user};
} catch(error) {
return {isError: true, ...error};
}
}


async addSubscriber(email: string, @Res() response) {
const addRequest = {
url: "/v3/marketing/contacts",
method:<HttpMethod> 'PUT',
body: {
"list_ids": [this.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 client.request(addRequest);
return {isError: false, result: result};
} catch(error) {
response.status(error.code).send({errors: error.response.body.errors})
return {isError: true, ...error};
}
}


async checkSuccessfulImport(email: string, @Res() response, counter = 0) {
if(counter >= CHECKS_BEFORE_FAIL) {
return { isError: true, code: 408 }
}

await delay(PAUSE_BETWEEN_CHECKS);

const existingUser = await this.searchByEmail(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(this.list)) {
// Success!
return { isError: false, code: 200, ...existingUser };
}

return await this.checkSuccessfulImport(email, response, counter + 1);
}

}
11 changes: 11 additions & 0 deletions server/src/subscription/subscription.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from "@nestjs/common";
import { SubscriptionController } from "./subscription.controller";
import { ConfigModule } from "../config/config.module";

@Module({
imports: [ConfigModule],
providers: [],
exports: [],
controllers: [SubscriptionController]
})
export class SubscriptionModule {}
34 changes: 34 additions & 0 deletions server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,21 @@
"@angular-devkit/core" "11.2.6"
"@angular-devkit/schematics" "11.2.6"

"@sendgrid/client@^8.1.3":
version "8.1.3"
resolved "https://registry.yarnpkg.com/@sendgrid/client/-/client-8.1.3.tgz#51fd4a318627c4b615ff98e35609e98486a3bd6f"
integrity sha512-mRwTticRZIdUTsnyzvlK6dMu3jni9ci9J+dW/6fMMFpGRAJdCJlivFVYQvqk8kRS3RnFzS7sf6BSmhLl1ldDhA==
dependencies:
"@sendgrid/helpers" "^8.0.0"
axios "^1.6.8"

"@sendgrid/helpers@^8.0.0":
version "8.0.0"
resolved "https://registry.yarnpkg.com/@sendgrid/helpers/-/helpers-8.0.0.tgz#f74bf9743bacafe4c8573be46166130c604c0fc1"
integrity sha512-Ze7WuW2Xzy5GT5WRx+yEv89fsg/pgy3T1E3FS0QEx0/VvRmigMZ5qyVGhJz4SxomegDkzXv/i0aFPpHKN8qdAA==
dependencies:
deepmerge "^4.2.2"

"@sinclair/typebox@^0.27.8":
version "0.27.8"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.27.8.tgz#6667fac16c436b5434a387a34dedb013198f6e6e"
Expand Down Expand Up @@ -2837,6 +2852,15 @@ [email protected], axios@^0.21.1:
dependencies:
follow-redirects "^1.10.0"

axios@^1.6.8:
version "1.7.7"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.7.tgz#2f554296f9892a72ac8d8e4c5b79c14a91d0a47f"
integrity sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==
dependencies:
follow-redirects "^1.15.6"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

babel-jest@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-29.7.0.tgz#f4369919225b684c56085998ac63dbd05be020d5"
Expand Down Expand Up @@ -4180,6 +4204,11 @@ follow-redirects@^1.10.0:
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.3.tgz#e5598ad50174c1bc4e872301e82ac2cd97f90267"
integrity sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==

follow-redirects@^1.15.6:
version "1.15.9"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.9.tgz#a604fa10e443bf98ca94228d9eebcc2e8a2c8ee1"
integrity sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==

forever-agent@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
Expand Down Expand Up @@ -6483,6 +6512,11 @@ proxy-addr@~2.0.5:
forwarded "~0.1.2"
ipaddr.js "1.9.1"

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

prr@~1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476"
Expand Down
Loading