Skip to content

Commit

Permalink
Slight refactor from previous.
Browse files Browse the repository at this point in the history
"contact" is not passed to postMessageSave (even though its passed in cacheable_queries.message).

This queries the DB for the custom fields table which include van id, and uses that to update the VAN records.
  • Loading branch information
engelhartrueben committed Dec 11, 2024
1 parent 7d0bbf4 commit f2e098c
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/extensions/message-handlers/ngpvan-optout/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { hasConfig, getConfig } from "../../../server/api/lib/config";
const Van = require("../../../extensions/action-handlers/ngpvan-action");
import { r } from "../../../server/models";

export const serverAdministratorInstructions = () => {
return {
Expand All @@ -19,49 +20,58 @@ export const serverAdministratorInstructions = () => {
};
}

const dbQuery = async (campaignContactId) => {
return await r
.knex("campaign_contact")
.select("custom_fields")
.where("id", campaignContactId)
}

export const available = organization =>
(hasConfig("NGP_VAN_API_KEY", organization) ||
hasConfig("NGP_VAN_API_KEY_ENCRYPTED", organization)) &&
hasConfig("NGP_VAN_APP_NAME", organization) &&
getConfig("MESSAGE_HANDLERS", organization).indexOf("auto-optout") !== -1;

/* Sends a request to VAN to place an opt out tag to an individual.
/*
* Sends a request to VAN to place an opt out tag to an individual.
*/
export const postMessageSave = async ({
contact,
handlerContext,
organization
organization,
message
}) => {
if (!exports.available(organization)) return {};

let query;
let customField;
let vanId;
let cell;

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

// Checking for vanID in contact
// While Van.postCanvassResponse will check the customFields,
// we don't want to call that function every time if a vanid
// was never provided in the first place. Example: CSV
try {
customField = JSON.parse(contact.custom_fields);
vanId = customField.VanID || customField.vanid;
if (!vanId) return {}
} catch (exception) {
console.log("message-handlers | ngpvan-optout ERROR", exception);
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

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

// https://docs.ngpvan.com/reference/peoplevanidcanvassresponses
const body = {
"canvassContext": {
"inputTypeId": 11, // API input
"phone": {
"dialingPrefix": "1",
"phoneNumber": contact.cell,
"phoneNumber": cell,
"smsOptInStatus": "O" // opt out status
}
},
Expand Down

0 comments on commit f2e098c

Please sign in to comment.