From a33208784e06088efa7ce4ff2a0d112c20a8a760 Mon Sep 17 00:00:00 2001 From: Robert Bo Davis Date: Fri, 8 Sep 2023 11:19:02 -0400 Subject: [PATCH] CHI-1987 refactor: get rid of funky unneeded return type (#1640) * Added generateExternalMediaUrl fn * refactor: abstract away recording path * refactor: only load metadata to check for error * chore: remove console log * refactor: get rid of funky unneeded return type * chore: lint * chore: fix lint warnings --------- Co-authored-by: mythilytm --- .../src/___tests__/utils/setUpActions.test.ts | 5 ++- .../src/services/ContactService.ts | 30 ++++++------- .../src/services/getExternalRecordingInfo.ts | 43 ++++++++++--------- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/plugin-hrm-form/src/___tests__/utils/setUpActions.test.ts b/plugin-hrm-form/src/___tests__/utils/setUpActions.test.ts index e7958c9ad7..634351c14c 100644 --- a/plugin-hrm-form/src/___tests__/utils/setUpActions.test.ts +++ b/plugin-hrm-form/src/___tests__/utils/setUpActions.test.ts @@ -57,7 +57,10 @@ describe('afterCompleteTask', () => { describe('excludeDeactivateConversationOrchestration', () => { test('backend_handled_chat_janitor === false and enable_post_survey === false should not change ChatOrchestrator', async () => { const setOrchestrationsSpy = jest.spyOn(ChatOrchestrator, 'setOrchestrations'); - excludeDeactivateConversationOrchestration({ enable_post_survey: false, backend_handled_chat_janitor: false }); + excludeDeactivateConversationOrchestration({ + enable_post_survey: false, + backend_handled_chat_janitor: false, + }); expect(setOrchestrationsSpy).not.toHaveBeenCalled(); }); diff --git a/plugin-hrm-form/src/services/ContactService.ts b/plugin-hrm-form/src/services/ContactService.ts index 6c8fd5f82c..73df34a45b 100644 --- a/plugin-hrm-form/src/services/ContactService.ts +++ b/plugin-hrm-form/src/services/ContactService.ts @@ -46,10 +46,9 @@ import { getNumberFromTask } from '../utils'; import { TaskEntry } from '../states/contacts/types'; import { ExternalRecordingInfoSuccess, - ExternalRecordingUnneeded, getExternalRecordingInfo, isFailureExternalRecordingInfo, - isSuccessfulExternalRecordingInfo, + shouldGetExternalRecordingInfo, } from './getExternalRecordingInfo'; import { generateUrl } from './fetchApi'; @@ -210,7 +209,7 @@ type HandleTwilioTaskResponse = { channelSid?: string; serviceSid?: string; conversationMedia: ConversationMedia[]; - externalRecordingInfo?: ExternalRecordingInfoSuccess | ExternalRecordingUnneeded; + externalRecordingInfo?: ExternalRecordingInfoSuccess; }; export const handleTwilioTask = async (task): Promise => { @@ -242,24 +241,23 @@ export const handleTwilioTask = async (task): Promise }); } + if (!shouldGetExternalRecordingInfo(task)) return returnData; + const externalRecordingInfo = await getExternalRecordingInfo(task); if (isFailureExternalRecordingInfo(externalRecordingInfo)) { throw new Error(`Error getting external recording info: ${externalRecordingInfo.error}`); } returnData.externalRecordingInfo = externalRecordingInfo; - - if (isSuccessfulExternalRecordingInfo(externalRecordingInfo)) { - const { bucket, key } = externalRecordingInfo; - returnData.conversationMedia.push({ - store: 'S3', - type: 'recording', - location: { - bucket, - key, - }, - }); - } + const { bucket, key } = externalRecordingInfo; + returnData.conversationMedia.push({ + store: 'S3', + type: 'recording', + location: { + bucket, + key, + }, + }); return returnData; }; @@ -269,7 +267,7 @@ type NewHrmServiceContact = Omit { - return r && r.status === 'unneeded'; -}; - export const isFailureExternalRecordingInfo = (r: any): r is ExternalRecordingInfoFailure => { return r && r.status === 'failure'; }; -export type ExternalRecordingInfo = - | ExternalRecordingInfoSuccess - | ExternalRecordingUnneeded - | ExternalRecordingInfoFailure; +export type ExternalRecordingInfo = ExternalRecordingInfoSuccess | ExternalRecordingInfoFailure; -const unneededRecordingInfo: ExternalRecordingUnneeded = { - status: 'unneeded', -}; - -export const getExternalRecordingInfo = async (task: CustomITask): Promise => { - if (isOfflineContactTask(task)) return unneededRecordingInfo; +/* eslint-disable sonarjs/prefer-single-boolean-return */ +export const shouldGetExternalRecordingInfo = (task: CustomITask): task is InMyBehalfITask => { + if (isOfflineContactTask(task)) return false; const { channelType } = task; - if (!isVoiceChannel(channelType)) return unneededRecordingInfo; + if (!isVoiceChannel(channelType)) return false; const { externalRecordingsEnabled } = getHrmConfig(); - if (!externalRecordingsEnabled) return unneededRecordingInfo; + if (!externalRecordingsEnabled) return false; + + return true; +}; +/* eslint-enable sonarjs/prefer-single-boolean-return */ + +export const getExternalRecordingInfo = async (task: CustomITask): Promise => { + if (!shouldGetExternalRecordingInfo(task)) { + return { + status: 'failure', + name: 'InvalidTask', + error: 'Invalid task', + }; + } // The call id related to the worker is always the one with the recording, as far as I can tell (rbd) const callSid = task.attributes.conference?.participants?.worker; if (!callSid) { return { status: 'failure', + name: 'NoCallSid', error: 'Could not find call sid', }; }