Skip to content

Commit

Permalink
1573 Get subscriber subscriptions endpoint (#1574)
Browse files Browse the repository at this point in the history
* Added get subscriptions endpoint.

* Use environment variable from .env

* Fixed issues created when merging.
  • Loading branch information
dhochbaum-dcp authored Nov 12, 2024
1 parent 0ca02e5 commit 3477cce
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
9 changes: 8 additions & 1 deletion server/src/subscriber/subscriber.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Post, Patch, Req, Res, Param } 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";
Expand Down Expand Up @@ -90,6 +90,13 @@ export class SubscriberController {

}

@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;
}

@Patch("/subscribers/:id")
async update(@Req() request: Request, @Param("id") id, @Res() response) {
const email = await this.subscriberService.getUserById(id);
Expand Down
37 changes: 35 additions & 2 deletions server/src/subscriber/subscriber.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ function delay(milliseconds) {
@Injectable()
export class SubscriberService {
sendgridEnvironmentIdVariable = "";
environment = "";
constructor(
private readonly config: ConfigService,
private client: Client,
private mailer: MailService
) {
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`;
this.mailer.setApiKey(this.config.get("SENDGRID_API_KEY"));
}
Expand Down Expand Up @@ -179,6 +181,38 @@ 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 = `${this.sendgridEnvironmentIdVariable} LIKE '${id}'`
const request = {
url: `/v3/marketing/contacts/search`,
method:<HttpMethod> '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."};
}
var subscriptionList = {};
for (const [key, value] of Object.entries(subscriptions[0].body["result"][0]["custom_fields"])) {
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};
} catch(error) {
return {isError: true, ...error};
}
};

/**
* Fetch the user email
* @param {string} id - The user's zap_production_id or zap_staging_id.
* @returns {object}
Expand All @@ -199,7 +233,7 @@ export class SubscriberService {
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 };
Expand Down Expand Up @@ -237,7 +271,6 @@ export class SubscriberService {
}
}


/**
* Validate a list of subscriptions.
* @param {object} subscriptions - The subscriptions to validate.
Expand Down

0 comments on commit 3477cce

Please sign in to comment.