Skip to content

fix: conversation duration set to 0 [CHI-3351] #3027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ const hrmState: Partial<RootState[typeof namespace]> = {
startMillis: 0,
endMillis: 0,
categories: { gridView: false, expanded: {} },
recreated: false,
draft: {
dialogsOpen: {},
resourceReferralList: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const contact = {
resourceReferralList: { resourceReferralIdToAdd: '', lookupStatus: ReferralLookupStatus.NOT_STARTED },
},
helpline: '',
metadata: { categories: { expanded: {}, gridView: false }, endMillis: 0, recreated: false, startMillis: 0 },
metadata: { categories: { expanded: {}, gridView: false }, endMillis: 0, startMillis: 0 },
referrals: [],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ describe('resourceReferralReducer', () => {
metadata: {
categories: { expanded: {}, gridView: false },
endMillis: 0,
recreated: false,
startMillis: 0,
draft: {
resourceReferralList: { resourceReferralIdToAdd: '', lookupStatus: ReferralLookupStatus.NOT_STARTED },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const testStore = (stateChanges: ContactsState) =>
],
});

const createdAtTimestamp = new Date().toISOString();
const baseContact: Contact = {
...VALID_EMPTY_CONTACT,
id: '1337',
Expand All @@ -80,6 +81,7 @@ const baseContact: Contact = {
categories: {},
contactlessTask: { ...VALID_EMPTY_CONTACT.rawJson.contactlessTask, channel: 'web' },
},
createdAt: createdAtTimestamp,
};

const task = <ITask>(<unknown>{ taskSid: 'WT-mock task' });
Expand Down Expand Up @@ -247,10 +249,12 @@ describe('actions', () => {
) as unknown);
const { metadata, savedContact } = getState().existingContacts[baseContact.id];
// Check that the difference in startMillis is still insignificant
expect(Math.abs(metadata.startMillis - newContactMetaData(false).startMillis)).toBeLessThanOrEqual(100);
expect(
Math.abs(metadata.startMillis - newContactMetaData({ createdAt: new Date().toISOString() }).startMillis),
).toBeLessThanOrEqual(100);
expect(metadata).toStrictEqual(
expect.objectContaining({
...newContactMetaData(false),
...newContactMetaData({ createdAt: createdAtTimestamp }),
loadingStatus: LoadingStatus.LOADED,
startMillis: expect.any(Number),
}),
Expand Down
1 change: 0 additions & 1 deletion plugin-hrm-form/src/___tests__/testContacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export const VALID_EMPTY_METADATA: ContactMetadata = {
startMillis: undefined,
endMillis: undefined,
categories: { gridView: false, expanded: {} },
recreated: false,
draft: {
resourceReferralList: { resourceReferralIdToAdd: undefined, lookupStatus: ReferralLookupStatus.NOT_STARTED },
dialogsOpen: {},
Expand Down
6 changes: 3 additions & 3 deletions plugin-hrm-form/src/services/ContactService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
} from './getExternalRecordingInfo';
import { GeneralizedSearchParams, SearchParams } from '../states/search/types';
import { ContactDraftChanges } from '../states/contacts/existingContacts';
import { newContactState } from '../states/contacts/contactState';
import { newContact } from '../states/contacts/contactState';
import { ApiError, FetchOptions } from './fetchApi';
import { TaskSID, WorkerSID } from '../types/twilio';
import { recordEvent } from '../fullStory';
Expand Down Expand Up @@ -253,9 +253,9 @@ const saveContactToHrm = async (
}

if (isNonDataCallType(callType)) {
const newContactWithMetaData = newContactState(currentDefinitionVersion, task)(false);
const blankContact = newContact(currentDefinitionVersion, task);
form = {
...newContactWithMetaData.savedContact.rawJson,
...blankContact.rawJson,
callType,
...(isOfflineContactTask(task) && {
contactlessTask: contact.rawJson.contactlessTask,
Expand Down
10 changes: 7 additions & 3 deletions plugin-hrm-form/src/states/contacts/contactReduxUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ export const contactReduxUpdates = (
): ContactsState => {
const { existingContacts } = state;
const id = typeof contact === 'object' ? contact.id : contact;
const savedContact = getUnsavedContact(existingContacts[id]?.savedContact, updates);
return {
...state,
existingContacts: {
...state.existingContacts,
[id]: {
...existingContacts[id],
draftContact: undefined,
savedContact: getUnsavedContact(existingContacts[id]?.savedContact, updates),
savedContact,
metadata: {
...newContactMetaData(false),
...newContactMetaData({ createdAt: savedContact?.createdAt }),
...existingContacts[id]?.metadata,
loadingStatus: LoadingStatus.LOADING,
},
Expand Down Expand Up @@ -75,7 +76,10 @@ export const loadContactIntoRedux = (
if (reference) {
references.add(reference);
}
const metadata = { ...newContactMetaData(false), ...(newMetadata ?? existingContacts[contact.id]?.metadata) };
const metadata = {
...newContactMetaData({ createdAt: contact?.createdAt }),
...(newMetadata ?? existingContacts[contact.id]?.metadata),
};
const existingContact = existingContacts[contact.id]?.savedContact;
const existingAssociations = {
...(existingContact?.csamReports ? { csamReports: existingContact.csamReports } : {}),
Expand Down
18 changes: 4 additions & 14 deletions plugin-hrm-form/src/states/contacts/contactState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ import { TaskHelper } from '@twilio/flex-ui';

import { ContactMetadata, LlmAssistantStatus, LoadingStatus } from './types';
import { ReferralLookupStatus } from './resourceReferral';
import type { ContactState } from './existingContacts';
import { Contact, ContactRawJson, isOfflineContactTask, OfflineContactTask } from '../../types/types';
import { createContactlessTaskTabDefinition } from '../../components/tabbedForms/ContactlessTaskTabDefinition';
import { getHrmConfig } from '../../hrmConfig';
import { createStateItem, getInitialValue } from '../../components/common/forms/formValues';

export const newContactMetaData = (recreated: boolean): ContactMetadata => {
export const newContactMetaData = ({ createdAt }: { createdAt: string }): ContactMetadata => {
const categoriesMeta = {
gridView: false,
expanded: {},
};

const startMillis = createdAt ? new Date(createdAt).getTime() : null;

return {
draft: {
resourceReferralList: {
Expand All @@ -39,9 +40,8 @@ export const newContactMetaData = (recreated: boolean): ContactMetadata => {
},
dialogsOpen: {},
},
startMillis: recreated ? null : new Date().getTime(),
startMillis,
endMillis: null,
recreated,
categories: categoriesMeta,
loadingStatus: LoadingStatus.LOADED,
llmAssistant: {
Expand Down Expand Up @@ -109,13 +109,3 @@ export const newContact = (definitions: DefinitionVersion, task?: ITask | Offlin
conversationMedia: [],
};
};

// eslint-disable-next-line import/no-unused-modules
export const newContactState = (definitions: DefinitionVersion, task?: ITask | OfflineContactTask) => (
recreated: boolean,
): ContactState => ({
savedContact: newContact(definitions, task),
metadata: newContactMetaData(recreated),
draftContact: {},
references: new Set(),
});
6 changes: 4 additions & 2 deletions plugin-hrm-form/src/states/contacts/existingContacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,14 @@ export const loadContactReducer = (state = initialState, action: LoadContactActi
gridView: false,
},
};

const savedContact = action.replaceExisting || !current.references.size ? c : state[c.id].savedContact;
return [
c.id,
{
metadata: newContactMetaData(true),
metadata: newContactMetaData({ createdAt: savedContact?.createdAt }),
...currentContact,
savedContact: action.replaceExisting || !current.references.size ? c : state[c.id].savedContact,
savedContact,
references: action.reference ? current.references.add(action.reference) : current.references,
draftContact: action.replaceExisting ? undefined : draftContact,
},
Expand Down
12 changes: 6 additions & 6 deletions plugin-hrm-form/src/states/contacts/saveContact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export const createContactAsyncAction = createAsyncAction(
// We assume that any contact we create will be the active contact because that's the only way to create contacts currently
// This assumption may not always be valid.
reference: `${taskSid}-active`,
metadata: newContactMetaData(false),
metadata: newContactMetaData({ createdAt: contact?.createdAt }),
};
},
(contactToCreate: Contact, workerSid: string, { taskSid }: CustomITask) => ({
Expand Down Expand Up @@ -362,7 +362,7 @@ export const saveContactReducer = (initialState: ContactsState) =>
handleAction(
createContactAsyncAction.fulfilled,
(state, { payload: { contact, reference } }): ContactsState => {
return loadContactIntoRedux(state, contact, reference, newContactMetaData(false));
return loadContactIntoRedux(state, contact, reference, newContactMetaData({ createdAt: contact?.createdAt }));
},
),
handleAction(
Expand All @@ -379,7 +379,7 @@ export const saveContactReducer = (initialState: ContactsState) =>
handleAction(
submitContactFormAsyncAction.fulfilled,
(state, { payload }): ContactsState => {
return loadContactIntoRedux(state, payload, undefined, newContactMetaData(false));
return loadContactIntoRedux(state, payload, undefined, newContactMetaData({ createdAt: payload?.createdAt }));
},
),
handleAction(
Expand All @@ -402,7 +402,7 @@ export const saveContactReducer = (initialState: ContactsState) =>
handleAction(
newSubmitAndFinalizeContactFromOutsideTaskContextAsyncAction.fulfilled,
(state, { payload }): ContactsState => {
return loadContactIntoRedux(state, payload, undefined, newContactMetaData(false));
return loadContactIntoRedux(state, payload, undefined, newContactMetaData({ createdAt: payload?.createdAt }));
},
),
handleAction(
Expand All @@ -424,7 +424,7 @@ export const saveContactReducer = (initialState: ContactsState) =>
loadContactFromHrmByIdAsyncAction.fulfilled,
(state, { payload: { contact, reference } }): ContactsState => {
if (!contact) return state;
return loadContactIntoRedux(state, contact, reference, newContactMetaData(true));
return loadContactIntoRedux(state, contact, reference, newContactMetaData({ createdAt: contact?.createdAt }));
},
),
handleAction(
Expand All @@ -440,7 +440,7 @@ export const saveContactReducer = (initialState: ContactsState) =>
newLoadContactFromHrmForTaskAsyncAction.fulfilled,
(state, { payload: { contact, reference } }): ContactsState => {
if (!contact) return state;
return loadContactIntoRedux(state, contact, reference, newContactMetaData(true));
return loadContactIntoRedux(state, contact, reference, newContactMetaData({ createdAt: contact?.createdAt }));
},
),
handleAction(
Expand Down
1 change: 0 additions & 1 deletion plugin-hrm-form/src/states/contacts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export type LlmAssistantStatus = typeof LlmAssistantStatus[keyof typeof LlmAssis
export type ContactMetadata = {
startMillis: number;
endMillis: number;
recreated: boolean;
categories: {
gridView: boolean;
expanded: { [key: string]: boolean };
Expand Down
4 changes: 2 additions & 2 deletions plugin-hrm-form/src/utils/conversationDuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const fillEndMillis = (metadata: ContactMetadata): ContactMetadata => ({
* - endMillis was not set
* @param {{ startMillis: number, endMillis: number, recreated: boolean }} metadata
*/
const getConversationDuration = ({ startMillis, endMillis, recreated }) => {
const validMetrics = !recreated && !isNullOrUndefined(endMillis) && !isNullOrUndefined(startMillis);
const getConversationDuration = ({ startMillis, endMillis }: { startMillis: number; endMillis: number }) => {
const validMetrics = !isNullOrUndefined(endMillis) && !isNullOrUndefined(startMillis);

if (!validMetrics) return null;

Expand Down
Loading