Skip to content

Commit

Permalink
Merge pull request #673 from techmatters/CHI-2911-support-channeltype
Browse files Browse the repository at this point in the history
Add ConversationTrigger for channels using Conversations
  • Loading branch information
mythilytm authored Aug 23, 2024
2 parents 3eb0ea8 + b4d4e52 commit 2707116
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions functions/getProfileFlagsForIdentifier.protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,20 @@ const isVoiceTrigger = (obj: any): obj is VoiceTrigger =>
obj && obj.call && typeof obj.call === 'object';

export type Event = {
trigger: ChatTrigger | VoiceTrigger;
trigger: ChatTrigger | VoiceTrigger | ConversationTrigger;
request: { cookies: {}; headers: {} };
channelType?: string;
};

type ConversationTrigger = {
conversation: {
Author: string;
};
};

const isConversationTrigger = (obj: any): obj is ConversationTrigger =>
typeof obj?.conversation === 'object';

type EnvVars = {
TWILIO_WORKSPACE_SID: string;
HRM_STATIC_KEY: string;
Expand All @@ -85,7 +95,7 @@ const channelTransformations: { [k: string]: TransformIdentifierFunction[] } = {
web: [],
};

export const getIdentifier = (trigger: Event['trigger']): string => {
export const getIdentifier = (trigger: Event['trigger'], channelType?: string): string => {
if (isVoiceTrigger(trigger)) {
return channelTransformations.voice.reduce((accum, f) => f(accum), trigger.call.From);
}
Expand All @@ -103,6 +113,13 @@ export const getIdentifier = (trigger: Event['trigger']): string => {
);
}

if (isConversationTrigger(trigger) && channelType) {
return channelTransformations[channelType].reduce(
(accum, f) => f(accum),
trigger.conversation.Author,
);
}

throw new Error('Trigger is none VoiceTrigger nor ChatTrigger');
};

Expand All @@ -120,9 +137,9 @@ export const handler: ServerlessFunctionSignature<EnvVars, Event> = async (
// eslint-disable-next-line @typescript-eslint/naming-convention
const { hrm_base_url, hrm_api_version } = serviceConfig.attributes;
const hrmBaseUrl = `${hrm_base_url}/${hrm_api_version}/accounts/${serviceConfig.accountSid}`;
const { trigger } = event;
const { trigger, channelType } = event;

const identifier = getIdentifier(trigger);
const identifier = getIdentifier(trigger, channelType);
const res = await axios.request({
url: `${hrmBaseUrl}/profiles/identifier/${identifier}/flags`,
method: 'get',
Expand Down

0 comments on commit 2707116

Please sign in to comment.