Skip to content

Commit

Permalink
clear up faulty logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruby Engelhart committed Dec 6, 2024
1 parent 8b38d4f commit cd4a8ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
16 changes: 11 additions & 5 deletions src/extensions/message-handlers/ngpvan-optout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,30 @@ export const available = organization =>
/* Sends a request to VAN to place an opt out tag to an individual.
*/
export const postMessageSave = async ({
message,
_, // message
contact,
handlerContext,
organization
}) => {
if (!exports.available(organization)) return {};

// customFields is a JSON object
let customField;
let vanId;

// 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 {
const c = JSON.stringify(contact.customFields);
if (c?.VanID === undefined) return {}
customField = JSON.parse(contact.customFields);
vanId = customField.VanID || customField.vanid;
if (!vanId) return {}
} catch (exception) {
console.log("message-handlers | ngpvan-optout ERROR", exception);
return {};
}

if (
message.is_from_contact ||
!contact ||
!handlerContext.autoOptOutReason
) return {};
Expand Down
18 changes: 9 additions & 9 deletions src/server/api/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1348,8 +1348,9 @@ const rootMutations = {
// Checking that contact contains a vanId
// If not, return and skip next steps
try {
const c = JSON.stringify(contact.customFiels);
if (c?.VanID === undefined) return newContact;
const c = JSON.parse(contact.customFields);
const vanId = c.VanId || c.vanid
if (!vanId) return newContact;
} catch (exception) {
console.log(exception);
return newContact;
Expand All @@ -1359,25 +1360,24 @@ const rootMutations = {
`createOptOut VAN ${contact.cell}`
);

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

const body = {
"canvassContext": {
"inputTypeId": 11,
"inputTypeId": 11, // API Input
"phone": {
"dialingPrefix": "1",
"phoneNumber": contact.cell,
"smsOptInStatus": "O"
"phoneNumber": cell,
"smsOptInStatus": "O" // opt out status
}
},
"resultCodeId": 205
};

try {
// I am assuming here that contact has vanID.
// will need to test
await Van.postCanvassResponse(contact, organization, body);
console.log(`canvasOptOut VAN success ${contact.cell}`)
} catch (e) {
console.log(`Error opting out ${contact.cell}: ${e}`);
console.log(`Error manually opting out ${contact.cell}: ${e}`);
} finally {
return newContact;
}
Expand Down

0 comments on commit cd4a8ac

Please sign in to comment.