Skip to content

Commit f2e098c

Browse files
Slight refactor from previous.
"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.
1 parent 7d0bbf4 commit f2e098c

File tree

1 file changed

+27
-17
lines changed
  • src/extensions/message-handlers/ngpvan-optout

1 file changed

+27
-17
lines changed

src/extensions/message-handlers/ngpvan-optout/index.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { hasConfig, getConfig } from "../../../server/api/lib/config";
22
const Van = require("../../../extensions/action-handlers/ngpvan-action");
3+
import { r } from "../../../server/models";
34

45
export const serverAdministratorInstructions = () => {
56
return {
@@ -19,49 +20,58 @@ export const serverAdministratorInstructions = () => {
1920
};
2021
}
2122

23+
const dbQuery = async (campaignContactId) => {
24+
return await r
25+
.knex("campaign_contact")
26+
.select("custom_fields")
27+
.where("id", campaignContactId)
28+
}
29+
2230
export const available = organization =>
2331
(hasConfig("NGP_VAN_API_KEY", organization) ||
2432
hasConfig("NGP_VAN_API_KEY_ENCRYPTED", organization)) &&
2533
hasConfig("NGP_VAN_APP_NAME", organization) &&
2634
getConfig("MESSAGE_HANDLERS", organization).indexOf("auto-optout") !== -1;
2735

28-
/* Sends a request to VAN to place an opt out tag to an individual.
36+
/*
37+
* Sends a request to VAN to place an opt out tag to an individual.
2938
*/
3039
export const postMessageSave = async ({
31-
contact,
3240
handlerContext,
33-
organization
41+
organization,
42+
message
3443
}) => {
3544
if (!exports.available(organization)) return {};
3645

46+
let query;
3747
let customField;
3848
let vanId;
49+
let cell;
3950

51+
// If no message or optOut, return
4052
if (
41-
!contact ||
53+
!message ||
4254
!handlerContext.autoOptOutReason
4355
) return {};
4456

45-
// Checking for vanID in contact
46-
// While Van.postCanvassResponse will check the customFields,
47-
// we don't want to call that function every time if a vanid
48-
// was never provided in the first place. Example: CSV
49-
try {
50-
customField = JSON.parse(contact.custom_fields);
51-
vanId = customField.VanID || customField.vanid;
52-
if (!vanId) return {}
53-
} catch (exception) {
54-
console.log("message-handlers | ngpvan-optout ERROR", exception);
55-
return {};
56-
}
57+
// Grabs van id and phone number
58+
// While there may be multiple phone numbers,
59+
// we want to use the # we originally texted
60+
query = await dbQuery(message.campaign_campaign_id);
61+
customField = JSON.parse(query[0]["custom_fields"] || "{}");
62+
vanId = customField["VanID"] || customField["vanid"];
63+
cell = message["contact_number"] || ""; // Phone number
64+
65+
// if no van id or cell #, return
66+
if (!vanId || !cell) return {};
5767

5868
// https://docs.ngpvan.com/reference/peoplevanidcanvassresponses
5969
const body = {
6070
"canvassContext": {
6171
"inputTypeId": 11, // API input
6272
"phone": {
6373
"dialingPrefix": "1",
64-
"phoneNumber": contact.cell,
74+
"phoneNumber": cell,
6575
"smsOptInStatus": "O" // opt out status
6676
}
6777
},

0 commit comments

Comments
 (0)