Skip to content

Commit

Permalink
Removed polling, added anonymous_id
Browse files Browse the repository at this point in the history
  • Loading branch information
dhochbaum-dcp committed Oct 16, 2024
1 parent 16cff39 commit 55b82ff
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 44 deletions.
21 changes: 0 additions & 21 deletions server/src/subscriber/subscriber.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import { SubscriberService } from "./subscriber.service";
import { Request } from "express";
import validateEmail from "../_utils/validate-email";

const PAUSE_BETWEEN_CHECKS = 3000;
const CHECKS_BEFORE_FAIL = 10;

@Controller()
export class SubscriberController {
apiKey = "";
Expand Down Expand Up @@ -53,26 +50,8 @@ export class SubscriberController {
return;
}

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

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;

Expand Down
26 changes: 3 additions & 23 deletions server/src/subscriber/subscriber.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Injectable, Res } from "@nestjs/common";
import { ConfigService } from "../config/config.service";
import { Client } from "@sendgrid/client";
const crypto = require('crypto');


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

Expand Down Expand Up @@ -43,7 +45,7 @@ export class SubscriberService {
method:<HttpMethod> 'PUT',
body: {
"list_ids": [list],
"contacts": [{"email": email}]
"contacts": [{"email": email, "anonymous_id": crypto.randomUUID()}]
}
}

Expand All @@ -56,26 +58,4 @@ export class SubscriberService {
return {isError: true, ...error};
}
}

async checkCreate(email: string, @Res() response, counter: number = 0, checksBeforeFail: number, pauseBetweenChecks: number, list: string) {
if(counter >= checksBeforeFail) {
return { isError: true, code: 408 }
}

await delay(pauseBetweenChecks);

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

return await this.checkCreate(email, response, counter + 1, checksBeforeFail, pauseBetweenChecks, list);
}
}

0 comments on commit 55b82ff

Please sign in to comment.