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
3 changes: 2 additions & 1 deletion 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 Expand Up @@ -90,7 +91,7 @@
"@types/inflected": "^2.1.3",
"@types/jest": "^29.5.12",
"@types/jsonwebtoken": "^8.5.1",
"@types/node": "11.13.4",
"@types/node": "^22.7.5",
"@types/request": "^2.48.12",
"@types/superagent-proxy": "3.0.4",
"@types/supertest": "^6.0.2",
Expand Down
29 changes: 29 additions & 0 deletions server/src/_utils/validate-email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 { SubscriberModule } from "./subscriber/subscriber.module";

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

@Controller()
export class SubscriberController {
apiKey = "";
list = "";

constructor(
private readonly config: ConfigService,
private readonly subscriberService: SubscriberService
) {
this.apiKey = this.config.get("SENDGRID_API_KEY");
this.list = this.config.get("SENDGRID_LIST");
}

@Post("/subscribers")
async subscribe(@Req() request: Request, @Res() response) {
if (!validateEmail(request.body.email)) {
response.status(400).send({
error: "Invalid email address."
})
return;
}

const existingUser = await this.subscriberService.findByEmail(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.subscriberService.create(request.body.email, this.list, response)

if(addToQueue.isError) {
response.status(addToQueue.code).send({errors: addToQueue.response.body.errors})
return;
}

response.status(200).send({
status: "success",
})
return;

}
}
13 changes: 13 additions & 0 deletions server/src/subscriber/subscriber.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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: [SubscriberService, Client],
exports: [SubscriberService],
controllers: [SubscriberController]
})
export class SubscriberModule {}
61 changes: 61 additions & 0 deletions server/src/subscriber/subscriber.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Injectable, Res } from "@nestjs/common";
import { ConfigService } from "../config/config.service";
import { Client } from "@sendgrid/client";
import crypto from 'crypto';


type HttpMethod = 'get'|'GET'|'post'|'POST'|'put'|'PUT'|'patch'|'PATCH'|'delete'|'DELETE';
dhochbaum-dcp marked this conversation as resolved.
Show resolved Hide resolved

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, "anonymous_id": crypto.randomUUID()}]
}
}

// 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};
}
}
}
49 changes: 45 additions & 4 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 @@ -2332,10 +2347,12 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.37.tgz#a3dd8da4eb84a996c36e331df98d82abd76b516e"
integrity sha512-XYmBiy+ohOR4Lh5jE379fV2IU+6Jn4g5qASinhitfyO71b/sCo6MKsMLF5tc7Zf2CE8hViVQyYSobJNke8OvUw==

"@types/[email protected]":
version "11.13.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.13.4.tgz#f83ec3c3e05b174b7241fadeb6688267fe5b22ca"
integrity sha512-+rabAZZ3Yn7tF/XPGHupKIL5EcAbrLxnTr/hgQICxbeuAfWtT0UZSfULE+ndusckBItcv4o6ZeOJplQikVcLvQ==
"@types/node@^22.7.5":
version "22.7.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.7.5.tgz#cfde981727a7ab3611a481510b473ae54442b92b"
integrity sha512-jML7s2NAzMWc//QSJ1a3prpk78cOPchGvXJsC3C6R6PSMoooztvRVQEz89gmBTBY1SPMaqo5teB4uNHPdetShQ==
dependencies:
undici-types "~6.19.2"

"@types/node@^8.0.47":
version "8.10.66"
Expand Down Expand Up @@ -2837,6 +2854,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 +4206,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 +6514,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 Expand Up @@ -7705,6 +7741,11 @@ typescript@^4.2.3:
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.12.1.tgz#7bb8cc9b3d397e201cf8553336d262544ead829e"
integrity sha512-hEQt0+ZLDVUMhebKxL4x1BTtDY7bavVofhZ9KZ4aI26X9SRaE+Y3m83XUL1UP2jn8ynjndwCCpEHdUG+9pP1Tw==

undici-types@~6.19.2:
version "6.19.8"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.19.8.tgz#35111c9d1437ab83a7cdc0abae2f26d88eda0a02"
integrity sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/universal-url/-/universal-url-2.0.0.tgz#35e7fc2c3374804905cee67ea289ed3a47669809"
Expand Down
Loading