Skip to content

Commit

Permalink
adjust how manually opting out someone works by reusing the ngpvan-op…
Browse files Browse the repository at this point in the history
…tout message handler code
  • Loading branch information
engelhartrueben committed Dec 13, 2024
1 parent c347bb5 commit 5e627ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
8 changes: 5 additions & 3 deletions src/extensions/message-handlers/ngpvan-optout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ export const postMessageSave = async ({
// If no message or optOut, return
if (
!message ||
!message.is_from_contact ||
!handlerContext.autoOptOutReason
!(handlerContext.autoOptOutReason || handlerContext.optOutReason)
) return {};


Expand Down Expand Up @@ -101,7 +100,10 @@ export const postMessageSave = async ({
"resultCodeId": 130
};

console.log(`ngpvan-optout.postMessageSave VAN ID : ${vanId}`);
console.log(
`ngpvan-optout.postMessageSave VAN ID : ${vanId} ` +
`: ${handlerContext.autoOptOutReason || handlerContext.optOutReason}`
);

await httpRequest(url, {
method: "POST",
Expand Down
52 changes: 17 additions & 35 deletions src/server/api/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ import { Tasks } from "../../workers/tasks";
const uuidv4 = require("uuid").v4;
const Van = require("../../extensions/action-handlers/ngpvan-action.js");

import {
available,
postMessageSave as optOutInVan
} from "../../extensions/message-handlers/ngpvan-optout";

// This function determines whether a field was requested
// in a graphql query. Each graphql resolver receives a fourth parameter,
// which contains information about the current request and the execution
Expand Down Expand Up @@ -1343,44 +1348,21 @@ const rootMutations = {
contact
);

if (!await Van.available(organization)) return newContact;

// Checking that contact contains a vanId
// If not, return and skip next steps
try {
const c = JSON.parse(contact.customFields);
const vanId = c.VanId || c.vanid
if (!vanId) return newContact;
} catch (exception) {
console.log(exception);
return newContact;
}

console.log(
`createOptOut VAN ${contact.cell}`
);
if (!available(organization)) return newContact;

const cell = contact.cell.replace(/\D/g,'');

const body = {
"canvassContext": {
"inputTypeId": 11, // API Input
"phone": {
"dialingPrefix": "1",
"phoneNumber": cell,
"smsOptInStatus": "O" // opt out status
}
// Reusing VAN opt out message-handler
await optOutInVan({
handlerContext: {
optOutReason: reason
},
"resultCodeId": 205
};
organization,
message: {
campaign_contact_id: campaignContactId,
contact_number: contact.cell
}
})

try {
await Van.postCanvassResponse(contact, organization, body);
} catch (e) {
console.log(`Error manually opting out ${contact.cell}: ${e}`);
} finally {
return newContact;
}
return newContact;
},
deleteQuestionResponses: async (
_,
Expand Down

0 comments on commit 5e627ce

Please sign in to comment.