From 68654a4cfea535b42250ad2ec2e1ae5f0cfae657 Mon Sep 17 00:00:00 2001 From: David Hochbaum Date: Mon, 4 Nov 2024 11:51:41 -0500 Subject: [PATCH 1/3] Added get subscriptions endpoint. --- .../src/subscriber/subscriber.controller.ts | 9 ++++- server/src/subscriber/subscriber.service.ts | 33 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/server/src/subscriber/subscriber.controller.ts b/server/src/subscriber/subscriber.controller.ts index bdfa3e05..7aefba7f 100644 --- a/server/src/subscriber/subscriber.controller.ts +++ b/server/src/subscriber/subscriber.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Post, Req, Res } from "@nestjs/common"; +import { Controller, Get, Param, Post, Req, Res } from "@nestjs/common"; import { ConfigService } from "../config/config.service"; import { SubscriberService } from "./subscriber.service"; import { Request } from "express"; @@ -82,4 +82,11 @@ export class SubscriberController { return; } + + @Get("/subscribers/:id") + async subscriptions(@Param() params, @Res() response) { + const subscriptions = await this.subscriberService.getSubscriptions(params.id); + response.status(subscriptions.code).send(subscriptions.isError ? subscriptions : subscriptions["subscription_list"]) + return; + } } \ No newline at end of file diff --git a/server/src/subscriber/subscriber.service.ts b/server/src/subscriber/subscriber.service.ts index 5ab0584f..29422471 100644 --- a/server/src/subscriber/subscriber.service.ts +++ b/server/src/subscriber/subscriber.service.ts @@ -145,6 +145,39 @@ export class SubscriberService { } } + /** + * Fetch the user's list of subscriptions. + * @param {string} id - The user's zap_production_id or zap_staging_id. + * @returns {object} + */ + async getSubscriptions(id: string) { + const query = `zap_production_id LIKE '${id}' OR zap_staging_id LIKE '${id}'` + const request = { + url: `/v3/marketing/contacts/search`, + method: 'POST', + body: { query } + } + + // https://www.twilio.com/docs/sendgrid/api-reference/contacts/search-contacts + // https://www.twilio.com/docs/sendgrid/for-developers/sending-email/segmentation-query-language + try { + const subscriptions = await this.client.request(request); + if(subscriptions[0].body["contact_count"] === 0) { + return {isError: true, code: 404, message: "No users found."}; + } + const environment = (subscriptions[0].body["result"][0]["custom_fields"]["zap_production_id"] === id) ? "production" : "staging"; + var subscriptionList = {}; + for (const [key, value] of Object.entries(subscriptions[0].body["result"][0]["custom_fields"])) { + if(key.startsWith(`zap_${environment}_`) && validCustomFieldNames.includes(key.replace(`zap_${environment}_`, "") as CustomFieldName)) { + subscriptionList[key.replace(`zap_${environment}_`, "")] = value; + } + } + return {isError: false, code: subscriptions[0].statusCode, "subscription_list": subscriptionList}; + } catch(error) { + return {isError: true, ...error}; + } + }; + /** * Validate a list of subscriptions. * @param {object} subscriptions - The subscriptions to validate. From 7ba3bbd278f139a19a08497196fd849b76d0b5dd Mon Sep 17 00:00:00 2001 From: David Hochbaum Date: Wed, 6 Nov 2024 10:57:06 -0500 Subject: [PATCH 2/3] Use environment variable from .env --- server/src/subscriber/subscriber.service.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/server/src/subscriber/subscriber.service.ts b/server/src/subscriber/subscriber.service.ts index 29422471..045cc92f 100644 --- a/server/src/subscriber/subscriber.service.ts +++ b/server/src/subscriber/subscriber.service.ts @@ -24,11 +24,13 @@ function delay(milliseconds){ @Injectable() export class SubscriberService { sendgridEnvironmentIdVariable = ""; + environment = ""; constructor( private readonly config: ConfigService, private client: Client ) { this.client.setApiKey(this.config.get("SENDGRID_API_KEY")); + this.environment = this.config.get("SENDGRID_ENVIRONMENT"); this.sendgridEnvironmentIdVariable = `zap_${this.config.get("SENDGRID_ENVIRONMENT")}_id`; } @@ -151,7 +153,7 @@ export class SubscriberService { * @returns {object} */ async getSubscriptions(id: string) { - const query = `zap_production_id LIKE '${id}' OR zap_staging_id LIKE '${id}'` + const query = `${this.sendgridEnvironmentIdVariable} LIKE '${id}'` const request = { url: `/v3/marketing/contacts/search`, method: 'POST', @@ -165,11 +167,10 @@ export class SubscriberService { if(subscriptions[0].body["contact_count"] === 0) { return {isError: true, code: 404, message: "No users found."}; } - const environment = (subscriptions[0].body["result"][0]["custom_fields"]["zap_production_id"] === id) ? "production" : "staging"; var subscriptionList = {}; for (const [key, value] of Object.entries(subscriptions[0].body["result"][0]["custom_fields"])) { - if(key.startsWith(`zap_${environment}_`) && validCustomFieldNames.includes(key.replace(`zap_${environment}_`, "") as CustomFieldName)) { - subscriptionList[key.replace(`zap_${environment}_`, "")] = value; + if(key.startsWith(`zap_${this.environment}_`) && validCustomFieldNames.includes(key.replace(`zap_${this.environment}_`, "") as CustomFieldName)) { + subscriptionList[key.replace(`zap_${this.environment}_`, "")] = value; } } return {isError: false, code: subscriptions[0].statusCode, "subscription_list": subscriptionList}; From 06a963557f9a939a7207db4956d292387de03bef Mon Sep 17 00:00:00 2001 From: David Hochbaum Date: Tue, 12 Nov 2024 13:17:49 -0500 Subject: [PATCH 3/3] Fixed issues created when merging. --- server/src/subscriber/subscriber.controller.ts | 2 +- server/src/subscriber/subscriber.service.ts | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/server/src/subscriber/subscriber.controller.ts b/server/src/subscriber/subscriber.controller.ts index 26f4ea86..d11e0e6d 100644 --- a/server/src/subscriber/subscriber.controller.ts +++ b/server/src/subscriber/subscriber.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Param, Post, Req, Res } from "@nestjs/common"; +import { Controller, Get, Param, Patch, Post, Req, Res } from "@nestjs/common"; import { ConfigService } from "../config/config.service"; import { SubscriberService } from "./subscriber.service"; import { Request } from "express"; diff --git a/server/src/subscriber/subscriber.service.ts b/server/src/subscriber/subscriber.service.ts index f35654c0..ecf1012b 100644 --- a/server/src/subscriber/subscriber.service.ts +++ b/server/src/subscriber/subscriber.service.ts @@ -181,7 +181,6 @@ export class SubscriberService { } /** - * Fetch the user's list of subscriptions. * @param {string} id - The user's zap_production_id or zap_staging_id. * @returns {object} @@ -223,12 +222,18 @@ export class SubscriberService { const request = { url: `/v3/marketing/contacts/search`, method: 'POST', + body: { query } + } + + // https://www.twilio.com/docs/sendgrid/api-reference/contacts/search-contacts + // https://www.twilio.com/docs/sendgrid/for-developers/sending-email/segmentation-query-language + try { const users = await this.client.request(request); if (users[0].body["contact_count"] === 0) { return { isError: true, code: 404, message: "No users found." }; } const email = users[0].body["result"][0].email; - + return { isError: false, code: users[0].statusCode, "email": email }; } catch (error) { return { isError: true, ...error };