Skip to content

Commit

Permalink
refactor away from using the van action-handler post request.
Browse files Browse the repository at this point in the history
Same concept before, but building this POST request from scratch as the contact object is not always passed to postMessageSave.
  • Loading branch information
engelhartrueben committed Dec 13, 2024
1 parent f2e098c commit df04c67
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions src/extensions/message-handlers/ngpvan-optout/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { hasConfig, getConfig } from "../../../server/api/lib/config";
const Van = require("../../../extensions/action-handlers/ngpvan-action");
import { r } from "../../../server/models";
import httpRequest from "../../../server/lib/http-request";
import {
getCountryCode,
getDashedPhoneNumberDisplay
} from "../../../lib/phone-format";
import Van from "../../contact-loaders/ngpvan/util";

export const serverAdministratorInstructions = () => {
return {
Expand All @@ -16,6 +21,7 @@ export const serverAdministratorInstructions = () => {
Additionally, "ngpvan-optout" must be added to the message handler
environment variable.
`,
// Does this include NGP_VAN env variables and what not?
environmentVariables: []
};
}
Expand All @@ -41,53 +47,72 @@ export const postMessageSave = async ({
organization,
message
}) => {
// Redundent, but other message-handlers check this first as well
if (!exports.available(organization)) return {};

let query;
let query; // store custom_fields of the contact
let customField;
let vanId;
let cell;
let vanId; // vanid of contact
let cell; // phone number that sent opt out message
let phoneCountry; // The coutnry code
let url; // url of VAN api

// If no message or optOut, return
if (
!message ||
!handlerContext.autoOptOutReason
) return {};

// Grabs van id and phone number
// While there may be multiple phone numbers,
// we want to use the # we originally texted
query = await dbQuery(message.campaign_campaign_id);
customField = JSON.parse(query[0]["custom_fields"] || "{}");
vanId = customField["VanID"] || customField["vanid"];
cell = message["contact_number"] || ""; // Phone number

try {
query = await dbQuery(message.campaign_contact_id);
customField = JSON.parse(query[0]["custom_fields"] || "{}");

vanId = customField["VanID"] || customField["vanid"];
cell = message["contact_number"] || "";
} catch (exception) {
console.error(
`postMessageSave.ngpvan-optout ERROR finding contact or ` +
`parsing custom fields for contact ${message.campaign_contact_id}`
)
}

// if no van id or cell #, return
if (!vanId || !cell) return {};

phoneCountry = process.env.PHONE_NUMBER_COUNTRY || "US";
cell = getDashedPhoneNumberDisplay(cell, phoneCountry);

url = Van.makeUrl(`v4/people/${vanId}/canvassResponses`, organization);

// https://docs.ngpvan.com/reference/peoplevanidcanvassresponses
const body = {
"canvassContext": {
"inputTypeId": 11, // API input
"phone": {
"dialingPrefix": "1",
"dialingPrefix": getCountryCode(cell, phoneCountry).toString(),
"phoneNumber": cell,
"smsOptInStatus": "O" // opt out status
}
},
"resultCodeId": 130 // Do Not Text result code
// Do Not Text result code
// Unsure if this is specfic to each VAN committe ?
"resultCodeId": 130
};

return Van.postCanvassResponse(contact, organization, body)
.then(() => ({}))
.catch(caughtError => {
// eslint-disable-next-line no-console
console.log(
"Encountered exception in ngpvan-optout.postMessageSave",
caughtError
)
return {};
})
return httpRequest(url, {
method: "POST",
retries: 1,
timeout: Van.getVanTimeout(organization),
headers: {
Authorization: await Van.getAuth(organization),
"accept": "text/plain",
"Content-Type": "application/json"
},
body: JSON.stringify(body),
validStatuses: [204],
compress: false
})
}


0 comments on commit df04c67

Please sign in to comment.