Skip to content

Commit

Permalink
Individual emails for each segment
Browse files Browse the repository at this point in the history
  • Loading branch information
dhochbaum-dcp committed Dec 11, 2024
1 parent b5b25f7 commit f5f1bf6
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Module } from '@nestjs/common';
import { ListservAuthService } from './auth.service';
import { ListservAuthService } from './listserv-auth.service';
import { ConfigModule } from '../config/config.module';

@Module({
Expand Down
File renamed without changes.
101 changes: 73 additions & 28 deletions server/src/send-update/send-update.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Controller, Param, Post, Req, Res } from "@nestjs/common";
import { ConfigService } from "../config/config.service";
import { SendUpdateService } from "./send-update.service";
import { ProjectService } from "../project/project.service";
import { ListservAuthService } from "../listserv/auth.service";
import { ListservAuthService } from "../listserv/listserv-auth.service";
import { Request } from "express";

@Controller()
Expand Down Expand Up @@ -32,47 +32,92 @@ export class SendUpdateController {

const project = await this.projectService.findOneByName(params.id);
// If no project is found, projectService returns HTTP error automatically, and this function does not continue

// Production names use underscores, staging use dashes
const segmentName = `zap${this.sendgridEnvironment === "production" ? "_production_" : "-staging-"}${project["data"]["attributes"]["dcp-borough"] === "Citywide" ? "CW" : project["data"]["attributes"]["dcp-validatedcommunitydistricts"]}`
const segmentIdResult = await this.sendUpdateService.getSegmentId(segmentName)
var segments = project["data"]["attributes"]["dcp-borough"] === "Citywide" ? [{ name: "CW", envSegment: `zap${this.sendgridEnvironment === "production" ? "_production_" : "-staging-"}CW`, segmentId: "" }] : [];

if(segmentIdResult.isError) {
response.status(500).send(segmentIdResult);
return;
if(project["data"]["attributes"]["dcp-validatedcommunitydistricts"]) {
project["data"]["attributes"]["dcp-validatedcommunitydistricts"].split(",").forEach(element => {
segments.push({ name: element, envSegment: `zap${this.sendgridEnvironment === "production" ? "_production_" : "-staging-"}${element}`, segmentId: "" })
});
}

const emailData = {
"domain": this.sendgridEnvironment === "production" ? "zap.planning.nyc.gov" : "zap-staging.planninglabs.nyc",
"id": params.id,
"name": project["data"]["attributes"]["dcp-projectname"],
"borocd": project["data"]["attributes"]["dcp-borough"] === "Citywide" ? "Citywide" : `${project["data"]["attributes"]["dcp-borough"]} CD ${project["data"]["attributes"]["dcp-validatedcommunitydistricts"].slice(1 )}`,
"status": project["data"]["attributes"]["dcp-publicstatus"],
"date": new Date().toLocaleDateString(),
"additionalpublicinformation": project["data"]["attributes"]["dcp-additionalpublicinformation"],
"dcpIsApplicant": project["data"]["attributes"]["dcp-applicant-customer-value"] === "DCP - Department of City Planning (NYC)"
}

const emailHtml = this.sendUpdateService.createProjectUpdateContent(emailData);

const createUpdate = await this.sendUpdateService.createSingleSendProjectUpdate(params.id, emailHtml, segmentIdResult.segmentId);

if (createUpdate.isError) {
response.status(createUpdate.code).send({
if (!segments.length) {
response.status(400).send({
isError: true,
project,
createUpdate
error: "No associated segments found."
})
return;
}

const sendUpdate = await this.sendUpdateService.scheduleSingleSendProjectUpdate(createUpdate["0"]["body"]["id"]);
for (const segment of segments) {
const segmentIdResult = await this.sendUpdateService.getSegmentId(segment.envSegment);
if(segmentIdResult.isError) {
response.status(500).send(segmentIdResult);
return;
}
segment.segmentId = segmentIdResult.segmentId;
}

var creationUpdates = [];
var sendUpdates = [];

const boros = {
K: 'Brooklyn',
X: 'Bronx',
M: 'Manhattan',
Q: 'Queens',
R: 'Staten Island',
};

for (const segment of segments) {
const emailData = {
"domain": this.sendgridEnvironment === "production" ? "zap.planning.nyc.gov" : "zap-staging.planninglabs.nyc",
"id": params.id,
"name": project["data"]["attributes"]["dcp-projectname"],
"borocd": segment.name === "CW" ? "Citywide" : `${boros[segment.name.slice(0,1)]} CD ${parseInt(segment.name.slice(1), 10)}`,
"status": project["data"]["attributes"]["dcp-publicstatus"],
"date": new Date().toLocaleDateString(),
"additionalpublicinformation": project["data"]["attributes"]["dcp-additionalpublicinformation"],
"dcpIsApplicant": project["data"]["attributes"]["dcp-applicant-customer-value"] === "DCP - Department of City Planning (NYC)",
"spansMoreThanOneCD": (project["data"]["attributes"]["dcp-validatedcommunitydistricts"] && (project["data"]["attributes"]["dcp-validatedcommunitydistricts"].split(",").length > 1))
}

const emailHtml = this.sendUpdateService.createProjectUpdateContent(emailData);

const createUpdate = await this.sendUpdateService.createSingleSendProjectUpdate(params.id, emailHtml, segment.segmentId);

if (createUpdate.isError) {
response.status(createUpdate.code).send({
isError: true,
project,
createUpdate
})
return;
}

const sendUpdate = await this.sendUpdateService.scheduleSingleSendProjectUpdate(createUpdate["0"]["body"]["id"]);

if (sendUpdate.isError) {
response.status(sendUpdate.code).send({
isError: true,
project,
createUpdate,
sendUpdate
})
return;
}

creationUpdates.push(createUpdate);
sendUpdates.push(sendUpdate);
}

response.status(201).send({
isError: false,
project,
createUpdate,
sendUpdate
creationUpdates,
sendUpdates
});
return;
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/send-update/send-update.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { CrmModule } from "../crm/crm.module";
import { DocumentModule } from "../document/document.module";
import { DispositionModule } from "../disposition/disposition.module";
import { GeometryService } from "../project/geometry/geometry.service";
import { ListservAuthModule } from "../listserv/auth.module";
import { ListservAuthService } from "../listserv/auth.service";
import { ListservAuthModule } from "../listserv/listserv-auth.module";
import { ListservAuthService } from "../listserv/listserv-auth.service";

@Module({
imports: [
Expand Down
13 changes: 9 additions & 4 deletions server/src/send-update/send-update.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ export class SendUpdateService {
*/
async createSingleSendProjectUpdate(id: string, emailHtml: string, segmentId: string) {
const data = {
name: "Single Send Test 2024-12-06 v5",
name: "Single Send Test 2024-12-10 v3",
send_to: {
segment_ids: [segmentId]
},
email_config: {
subject: "Single Send Test 2024-12-06 v5",
subject: "Single Send Test 2024-12-10 v3",
html_content: emailHtml,
generate_plain_content: true,
custom_unsubscribe_url: `https://${this.environment === "production" ? "zap.planning.nyc.gov" : "zap-staging.planninglabs.nyc"}/subscribers/{{${this.environment === "production" ? "zap_production_id" : "zap_staging_id"}}}`,
Expand Down Expand Up @@ -115,9 +115,10 @@ export class SendUpdateService {
* @param {string} date
* @param {string} additionalpublicinformation
* @param {string} dcpIsApplicant
* @param {boolean} spansMoreThanOneCD
* @returns {object}
*/
createProjectUpdateContent({domain, id, name, borocd, status, date, additionalpublicinformation, dcpIsApplicant}) {
createProjectUpdateContent({domain, id, name, borocd, status, date, additionalpublicinformation, dcpIsApplicant, spansMoreThanOneCD}) {
var htmlContent = `
<!DOCTYPE html>
<head>
Expand Down Expand Up @@ -175,7 +176,7 @@ export class SendUpdateService {
</p>
<div class="content-body" style="width: 100%;">
<p>
<a href="https://${domain}/projects/${id}" target="_blank" rel="noreferrer" style="color: #AE551D;">${name}</a>
<a href="https://${domain}/projects/${id}" target="_blank" rel="noreferrer" style="color: #AE551D;">${name}</a>${spansMoreThanOneCD ? "*" : ""}
in ${borocd}
`;

Expand All @@ -192,6 +193,10 @@ export class SendUpdateService {
htmlContent += `<p>New York City Department of City Planning is the applicant. <a href="${additionalpublicinformation}" target="_blank" rel="noreferrer" style="color: #3E4451;">Click here to learn more.</a></p> `
}

if (spansMoreThanOneCD) {
htmlContent += `<p style="font-size: 0.85rem;">*Project spans more than one CD.</p>`
}

htmlContent += `
</div>
</div>
Expand Down

0 comments on commit f5f1bf6

Please sign in to comment.