Skip to content

Commit

Permalink
Fix map removal event hook
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhand committed May 23, 2024
1 parent 394ec20 commit 231d1d2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion functions/helpers/chatChannelJanitor.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const deactivateConversation = async (

console.log('conversation attributes', ...Object.entries(attributes));

if (conversation.state !== 'inactive') {
if (conversation.state === 'active') {
if (attributes.proxySession) {
await deleteProxySession(context, attributes.proxySession);
}
Expand Down
12 changes: 1 addition & 11 deletions functions/helpers/customChannels/customChannelToFlex.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ const createConversation = async (
url:
onConversationUpdateWebhookUrl ||
`https://${context.DOMAIN_NAME}/webhooks/FlexChannelUpdate`,
filters: ['onConversationUpdated'],
filters: ['onConversationStateUpdated'],
},
});
}
Expand Down Expand Up @@ -488,16 +488,6 @@ export const sendConversationMessageToFlex = async (
syncServiceSid,
uniqueUserName,
});
const twilioClient = context.getTwilioClient();

if (conversationSid) {
// Check if the conversation is active
const conversation = await twilioClient.conversations.conversations(conversationSid).fetch();
if (conversation.state !== 'active') {
await twilioClient.sync.services(syncServiceSid).documents(uniqueUserName).remove();
conversationSid = undefined;
}
}

if (!conversationSid) {
const { conversationSid: newConversationSid, error } = await createConversation(context, {
Expand Down
39 changes: 25 additions & 14 deletions functions/webhooks/FlexChannelUpdate.protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,30 @@ import {
error500,
success,
} from '@tech-matters/serverless-helpers';
import { ConversationState } from 'twilio/lib/rest/conversations/v1/conversation';
import { ConversationSid } from '../helpers/customChannels/customChannelToFlex.private';

type EnvVars = {
CHAT_SERVICE_SID: string;
SYNC_SERVICE_SID: string;
};

export type Body = {
type ConversationStateUpdatedBody = {
EventType: 'onConversationStateUpdated';
StateFrom: ConversationState;
StateTo: ConversationState;
ConversationSid: ConversationSid;
};

type ChannelUpdatedBody = {
Source?: string;
ChannelSid?: string; // Remove once we've fully migrated to conversations
ConversationSid?: string;
Attributes?: string; // channel attributes (e.g. "{\"from\":\"pgian\",\"channel_type\":\"custom\",\"status\":\"INACTIVE\",\"long_lived\":false}")
UniqueName?: string;
FriendlyName?: string;
ClientIdentity?: string; // client firing the channel update
CreatedBy?: string;
EventType?: string;
EventType: 'onChannelUpdated';
InstanceSid?: string;
DateCreated?: string;
DateUpdated?: string;
Expand All @@ -49,6 +57,8 @@ export type Body = {
WebhookSid?: string;
};

export type Body = ChannelUpdatedBody | ConversationStateUpdatedBody;

function timeout(ms: number) {
// eslint-disable-next-line no-promise-executor-return
return new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -108,27 +118,28 @@ export const handler = async (
}
}

if (event.EventType === 'onConversationUpdated') {
const { ConversationSid } = event;
if (event.EventType === 'onConversationStateUpdated') {
const { ConversationSid: conversationSid, StateFrom, StateTo } = event;

if (ConversationSid === undefined) {
if (conversationSid === undefined) {
resolve(error400('ConversationSid'));
return;
}

const conversations = await client.conversations
.services(context.CHAT_SERVICE_SID)
.conversations(ConversationSid)
.fetch();
const conversations = await client.conversations.conversations(conversationSid).fetch();

const { from } = JSON.parse(conversations.attributes);
const { twilioNumber } = JSON.parse(conversations.attributes);

if (conversations.state === 'inactive') {
if (StateTo !== 'active') {
await timeout(1000); // set small timeout just in case some cleanup is still going on

const removed = await cleanupUserChannelMap(context, from);
const removed = await cleanupUserChannelMap(context, twilioNumber);

resolve(success(`INACTIVE channel triggered map removal for ${from}, removed ${removed}`));
resolve(
success(
`State changing from ${StateFrom} to ${StateTo} triggered map removal for ${twilioNumber}, removed ${removed}`,
),
);
return;
}
}
Expand Down

0 comments on commit 231d1d2

Please sign in to comment.