Skip to content

Commit df04c67

Browse files
refactor away from using the van action-handler post request.
Same concept before, but building this POST request from scratch as the contact object is not always passed to postMessageSave.
1 parent f2e098c commit df04c67

File tree

1 file changed

+48
-23
lines changed
  • src/extensions/message-handlers/ngpvan-optout

1 file changed

+48
-23
lines changed

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

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { hasConfig, getConfig } from "../../../server/api/lib/config";
2-
const Van = require("../../../extensions/action-handlers/ngpvan-action");
32
import { r } from "../../../server/models";
3+
import httpRequest from "../../../server/lib/http-request";
4+
import {
5+
getCountryCode,
6+
getDashedPhoneNumberDisplay
7+
} from "../../../lib/phone-format";
8+
import Van from "../../contact-loaders/ngpvan/util";
49

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

46-
let query;
53+
let query; // store custom_fields of the contact
4754
let customField;
48-
let vanId;
49-
let cell;
55+
let vanId; // vanid of contact
56+
let cell; // phone number that sent opt out message
57+
let phoneCountry; // The coutnry code
58+
let url; // url of VAN api
5059

5160
// If no message or optOut, return
5261
if (
5362
!message ||
5463
!handlerContext.autoOptOutReason
5564
) return {};
5665

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
6466

67+
try {
68+
query = await dbQuery(message.campaign_contact_id);
69+
customField = JSON.parse(query[0]["custom_fields"] || "{}");
70+
71+
vanId = customField["VanID"] || customField["vanid"];
72+
cell = message["contact_number"] || "";
73+
} catch (exception) {
74+
console.error(
75+
`postMessageSave.ngpvan-optout ERROR finding contact or ` +
76+
`parsing custom fields for contact ${message.campaign_contact_id}`
77+
)
78+
}
79+
6580
// if no van id or cell #, return
6681
if (!vanId || !cell) return {};
6782

83+
phoneCountry = process.env.PHONE_NUMBER_COUNTRY || "US";
84+
cell = getDashedPhoneNumberDisplay(cell, phoneCountry);
85+
86+
url = Van.makeUrl(`v4/people/${vanId}/canvassResponses`, organization);
87+
6888
// https://docs.ngpvan.com/reference/peoplevanidcanvassresponses
6989
const body = {
7090
"canvassContext": {
7191
"inputTypeId": 11, // API input
7292
"phone": {
73-
"dialingPrefix": "1",
93+
"dialingPrefix": getCountryCode(cell, phoneCountry).toString(),
7494
"phoneNumber": cell,
7595
"smsOptInStatus": "O" // opt out status
7696
}
7797
},
78-
"resultCodeId": 130 // Do Not Text result code
98+
// Do Not Text result code
99+
// Unsure if this is specfic to each VAN committe ?
100+
"resultCodeId": 130
79101
};
80102

81-
return Van.postCanvassResponse(contact, organization, body)
82-
.then(() => ({}))
83-
.catch(caughtError => {
84-
// eslint-disable-next-line no-console
85-
console.log(
86-
"Encountered exception in ngpvan-optout.postMessageSave",
87-
caughtError
88-
)
89-
return {};
90-
})
103+
return httpRequest(url, {
104+
method: "POST",
105+
retries: 1,
106+
timeout: Van.getVanTimeout(organization),
107+
headers: {
108+
Authorization: await Van.getAuth(organization),
109+
"accept": "text/plain",
110+
"Content-Type": "application/json"
111+
},
112+
body: JSON.stringify(body),
113+
validStatuses: [204],
114+
compress: false
115+
})
91116
}
92117

93118

0 commit comments

Comments
 (0)