Skip to content

Commit

Permalink
Add action handler for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
johntwyman committed Aug 6, 2024
1 parent 7d40824 commit 3b83594
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/extensions/action-handlers/civicrm-addpendingnote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-disable no-empty-function */
import { r } from "../../server/models";
import { available as loaderAvailable } from "../contact-loaders/civicrm";
import { addPendingNote } from "../contact-loaders/civicrm/util";
import { getCacheLength } from "../contact-loaders/civicrm/getcachelength";
import { getConfig } from "../../server/api/lib/config";
import {
ENVIRONMENTAL_VARIABLES_MANDATORY,
CIVICRM_CONTACT_LOADER,
CIVICRM_ACTION_HANDLER_ADDTAG
} from "../contact-loaders/civicrm/const";

export const name = CIVICRM_ACTION_HANDLER_ADDPENDINGNOTE;

// What the user sees as the option
export const displayName = () => "Add pending note to Contact";

// The Help text for the user after selecting the action
export const instructions = () =>
"What note would you like added to the person's CiviCRM/Rocket record?";

export function serverAdministratorInstructions() {
return {
description: `
This action is for allowing texters to add notes to Rocket/CiviCRM contact records.
`,
setupInstructions: `
1. Add "civicrm-addpendingnote" to the environment variable "ACTION_HANDLERS";
2. Set up Spoke to use the existing civicrm contact loader.
`,
environmentVariables: [...ENVIRONMENTAL_VARIABLES_MANDATORY]
};
}

// eslint-disable-next-line no-unused-vars
export function clientChoiceDataCacheKey(organization, user) {
return `${organization.id}`;
}

// return true, if the action is usable and available for the organizationId
// Sometimes this means certain variables/credentials must be setup
// either in environment variables or organization.features json data
// Besides this returning true, "civicrm-addtag" will also need to be added to
// process.env.ACTION_HANDLERS
export async function available(organizationId) {
const contactLoadersConfig = getConfig("CONTACT_LOADERS").split(",");
if (contactLoadersConfig.indexOf(CIVICRM_CONTACT_LOADER) !== -1) {
const hasLoader = await loaderAvailable(organizationId, 0);
return {
result: hasLoader.result,
expiresSeconds: getCacheLength(CIVICRM_ACTION_HANDLER_ADDPENDINGNOTE)
};
}
return {
result: false,
expiresSeconds: getCacheLength(CIVICRM_ACTION_HANDLER_ADDPENDINGNOTE)
};
}

// What happens when a texter saves the answer that triggers the action
// This is presumably the meat of the action
export async function processAction({
actionObject,
campaignContactId,
contact
}) {
// This is a meta action that updates a variable in the contact record itself.
// Generally, you want to send action data to the outside world, so you
// might want the request library loaded above
const civiContactId = contact.external_id;
await addPendingNote(civiContactId);

await r
.knex("campaign_contact")
.where("campaign_contact.id", campaignContactId)
.update("custom_fields", JSON.stringify(customFields));
}

// What happens when a texter removes an answer that triggers the action
// eslint-disable-next-line no-unused-vars
export async function processDeletedQuestionResponse(options) {}
33 changes: 33 additions & 0 deletions src/extensions/contact-loaders/civicrm/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,39 @@ export async function addContactToTag(contactId, tagId) {
return res;
}

export async function addPendingNote(contactId) {
const config = getCivi();

const res = await fetchfromAPI(
config,
"CustomValue",
{
entity_id: contactId,
entity_table: "civicrm_contact",
custom_247: "Contacted via Spoke text message"
},
"create",
{ method: "post" }
};

const res = await fetchfromAPI(
config,
"Activity",
{
source_contact_id: "spoke cid",
subject: note,
target_id: contactId,
priority_id: "Normal",
status_id: "Completed",
activity_date_time: moment().format('YYYY-MM-DD HH:mm:ss')
},
"create",
{ method: "post" }
};

return res;
}

/**
* @returns {Promise<{ name: string; id: number }[]>}
*/
Expand Down

0 comments on commit 3b83594

Please sign in to comment.