Skip to content

Commit

Permalink
CHI-1987 refactor: get rid of funky unneeded return type (#1640)
Browse files Browse the repository at this point in the history
* 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 <mythily@techmatters.org>
  • Loading branch information
robert-bo-davis and mythilytm authored Sep 8, 2023
1 parent af15c50 commit a332087
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
5 changes: 4 additions & 1 deletion plugin-hrm-form/src/___tests__/utils/setUpActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(<FeatureFlags>{ enable_post_survey: false, backend_handled_chat_janitor: false });
excludeDeactivateConversationOrchestration(<FeatureFlags>{
enable_post_survey: false,
backend_handled_chat_janitor: false,
});

expect(setOrchestrationsSpy).not.toHaveBeenCalled();
});
Expand Down
30 changes: 14 additions & 16 deletions plugin-hrm-form/src/services/ContactService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -210,7 +209,7 @@ type HandleTwilioTaskResponse = {
channelSid?: string;
serviceSid?: string;
conversationMedia: ConversationMedia[];
externalRecordingInfo?: ExternalRecordingInfoSuccess | ExternalRecordingUnneeded;
externalRecordingInfo?: ExternalRecordingInfoSuccess;
};

export const handleTwilioTask = async (task): Promise<HandleTwilioTaskResponse> => {
Expand Down Expand Up @@ -242,24 +241,23 @@ export const handleTwilioTask = async (task): Promise<HandleTwilioTaskResponse>
});
}

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;
};
Expand All @@ -269,7 +267,7 @@ type NewHrmServiceContact = Omit<HrmServiceContact, 'id' | 'updatedAt' | 'update
type SaveContactToHrmResponse = {
response: HrmServiceContact;
request: NewHrmServiceContact;
externalRecordingInfo?: ExternalRecordingInfoSuccess | ExternalRecordingUnneeded;
externalRecordingInfo?: ExternalRecordingInfoSuccess;
};
/**
* Function that saves the form to Contacts table.
Expand Down
43 changes: 22 additions & 21 deletions plugin-hrm-form/src/services/getExternalRecordingInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@

import { getHrmConfig } from '../hrmConfig';
import { isVoiceChannel } from '../states/DomainConstants';
import { CustomITask, isOfflineContactTask } from '../types/types';
import { CustomITask, isOfflineContactTask, InMyBehalfITask } from '../types/types';
import { getExternalRecordingS3Location } from './ServerlessService';

export type ExternalRecordingUnneeded = {
status: 'unneeded';
};

export type ExternalRecordingInfoSuccess = {
status: 'success';
recordingSid: string;
Expand All @@ -32,44 +28,49 @@ export type ExternalRecordingInfoSuccess = {

type ExternalRecordingInfoFailure = {
status: 'failure';
name: string;
error: string;
};

export const isSuccessfulExternalRecordingInfo = (r: any): r is ExternalRecordingInfoSuccess => {
return r && r.status === 'success';
};

const isUnneededExternalRecordingInfo = (r: any): r is ExternalRecordingUnneeded => {
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<ExternalRecordingInfo> => {
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<ExternalRecordingInfo> => {
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',
};
}
Expand Down

0 comments on commit a332087

Please sign in to comment.