Skip to content

Commit

Permalink
Add ngpvan message handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lperson committed Jul 9, 2020
1 parent fe883cf commit f706ce5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 37 deletions.
35 changes: 17 additions & 18 deletions src/integrations/action-handlers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getConfig } from "../../server/api/lib/config";
import { r } from "../../server/models";
import { log } from "../../lib";
import _ from "lodash";

export const availabilityCacheKey = (name, organization, userId) =>
`${getConfig("CACHE_PREFIX", organization) || ""}action-avail-${name}-${
Expand All @@ -11,6 +12,8 @@ export const choiceDataCacheKey = (name, organization, suffix) =>
`${getConfig("CACHE_PREFIX", organization) ||
""}action-choices-${name}-${suffix}`;

// TODO: organization is never actually passed to this method so action handlers
// are not actually configurable at the organization level
export function getActionHandlers(organization) {
const enabledActionHandlers = (
getConfig("ACTION_HANDLERS", organization) ||
Expand All @@ -32,6 +35,10 @@ export function getActionHandlers(organization) {
}

const CONFIGURED_ACTION_HANDLERS = getActionHandlers();
const CONFIGURED_TAG_HANDLERS = _.pickBy(
CONFIGURED_ACTION_HANDLERS,
handler => !!handler.onTagUpdate
);

export async function getSetCacheableResult(cacheKey, fallbackFunc) {
if (r.redis && cacheKey) {
Expand Down Expand Up @@ -99,8 +106,8 @@ export async function getActionHandlerAvailability(
try {
return (
await getSetCacheableResult(
availabilityCacheKey(name, organization, user || { id: "" }),
() => fallbackFunction(actionHandler, organization, user || {})
availabilityCacheKey(name, organization, user.id),
() => fallbackFunction(actionHandler, organization, user)
)
).result;
} catch (caughtError) {
Expand All @@ -111,15 +118,19 @@ export async function getActionHandlerAvailability(
}

export function rawActionHandler(name) {
// RARE: You should almost always use getActionHandler() below,
// unless workflow has already tested availability for the org-user
return CONFIGURED_ACTION_HANDLERS[name];
}

export function rawAllActionHandlers() {
return CONFIGURED_ACTION_HANDLERS;
}

// TODO: clean up tag update API. Because tag update handlers are _always_ run if configured
// it would be better to separate tag handler integrations from question response handlers
export function rawAllTagUpdateActionHandlerNames() {
return Object.keys(CONFIGURED_TAG_HANDLERS);
}

export async function getActionHandler(name, organization, user) {
let isAvail;
if (name in CONFIGURED_ACTION_HANDLERS) {
Expand All @@ -142,18 +153,6 @@ export async function getAvailableActionHandlers(organization, user) {
return actionHandlers.filter(x => x);
}

export async function getActionHandlersAvailableForTagUpdate(
organization,
user
) {
const actionHandlers = await Promise.all(
Object.keys(CONFIGURED_ACTION_HANDLERS).map(name =>
getActionHandler(name, organization, user)
)
);
return actionHandlers.filter(x => x && !!x.onTagUpdate);
}

export async function getActionChoiceData(actionHandler, organization, user) {
const cacheKeyFunc =
actionHandler.clientChoiceDataCacheKey || (org => `${org.id}`);
Expand All @@ -165,7 +164,7 @@ export async function getActionChoiceData(actionHandler, organization, user) {
cacheKey = exports.choiceDataCacheKey(
actionHandler.name,
organization,
cacheKeyFunc(organization, user || {})
cacheKeyFunc(organization, user)
);
} catch (caughtException) {
log.error(
Expand All @@ -177,7 +176,7 @@ export async function getActionChoiceData(actionHandler, organization, user) {
try {
returned =
(await exports.getSetCacheableResult(cacheKey, async () =>
clientChoiceDataFunc(organization, user || {})
clientChoiceDataFunc(organization, user)
)) || {};
} catch (caughtException) {
log.error(
Expand Down
30 changes: 11 additions & 19 deletions src/integrations/message-handlers/ngpvan/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { getConfig } from "../../../server/api/lib/config";
import { runningInLambda } from "../../../server/api/lib/utils";
const Van = require("../../../integrations/action-handlers/ngpvan-action");

import {
getActionHandler,
getActionChoiceData
} from "../../../integrations/action-handlers";
import { getActionChoiceData } from "../../../integrations/action-handlers";

export const DEFAULT_NGP_VAN_INITIAL_TEXT_CANVASS_RESULT = "Texted";

Expand All @@ -23,29 +20,30 @@ export const serverAdministratorInstructions = () => {
};
};

export const available = async organization => {
const handler = await getActionHandler("ngpvan-action", organization);
return !!handler;
};
export const available = organization =>
!!getConfig("NGP_VAN_API_KEY", organization) &&
!!getConfig("NGP_VAN_APP_NAME", organization);

// export const preMessageSave = async () => {};

export const postMessageSave = async ({ contact, organization }) => {
if (!available(organization)) {
return {};
}

if (contact.message_status !== "needsMessage") {
return {};
}

const handler = await getActionHandler("ngpvan-action", organization);
const clientChoiceData = await getActionChoiceData(handler, organization);
const clientChoiceData = await getActionChoiceData(Van, organization);
const initialTextResult =
getConfig("NGP_VAN_INITIAL_TEXT_CANVASS_RESULT", organization) ||
DEFAULT_NGP_VAN_INITIAL_TEXT_CANVASS_RESULT;

const texted = clientChoiceData.find(ccd => ccd.name === initialTextResult);
const body = JSON.parse(texted.details);

const promise = handler
.postCanvassResponse(contact, organization, body)
return Van.postCanvassResponse(contact, organization, body)
.then(() => {})
.catch(caughtError => {
// eslint-disable-next-line no-console
Expand All @@ -55,10 +53,4 @@ export const postMessageSave = async ({ contact, organization }) => {
);
return {};
});

if (runningInLambda()) {
return promise;
}

return {};
};

0 comments on commit f706ce5

Please sign in to comment.