From cc380465f8dc37a975c132c7594a3a8f924ce6e0 Mon Sep 17 00:00:00 2001 From: tonyco97 Date: Mon, 30 Sep 2024 11:08:29 +0200 Subject: [PATCH] operators. Fixed favourite operators migration --- components/layout/Layout.tsx | 8 ++++---- lib/operators.ts | 25 ++++++++++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/components/layout/Layout.tsx b/components/layout/Layout.tsx index c00ddeca..ad281e61 100644 --- a/components/layout/Layout.tsx +++ b/components/layout/Layout.tsx @@ -206,11 +206,11 @@ export const Layout: FC = ({ children }) => { store.dispatch.operators.setErrorMessage('') try { - retrieveUserEndpoints() + const userEndpoints = await retrieveUserEndpoints() retrieveGroups() retrieveExtensions() retrieveAvatars(authStore) - retrieveFavoriteOperators(authStore, operatorsStore?.operators) + retrieveFavoriteOperators(authStore, userEndpoints) } catch (e) { store.dispatch.operators.setErrorMessage('Cannot retrieve operators') store.dispatch.operators.setOperatorsLoaded(true) @@ -227,7 +227,7 @@ export const Layout: FC = ({ children }) => { } // eslint-disable-next-line react-hooks/exhaustive-deps }, [ - operatorsStore.isOperatorsLoaded, + operatorsStore?.isOperatorsLoaded, firstRenderOperators, profile?.macro_permissions?.presence_panel?.value, ]) @@ -488,7 +488,7 @@ export const Layout: FC = ({ children }) => { store.dispatch.operators.updateExten(data[opName]?.number, extensionInformation) }) - + useEventListener('phone-island-conversations', (data) => { const opName = Object.keys(data)[0] const conversations = data[opName].conversations diff --git a/lib/operators.ts b/lib/operators.ts index f696f9fc..f37e555a 100644 --- a/lib/operators.ts +++ b/lib/operators.ts @@ -204,11 +204,13 @@ export const retrieveUserEndpoints = async () => { const endpoints = await getUserEndpointsAll() store.dispatch.operators.setUserEndpoints(endpoints) store.dispatch.operators.setUserEndpointsLoaded(true) + return endpoints } catch (e) { console.error(e) store.dispatch.operators.setErrorMessage('Cannot retrieve user endpoints') store.dispatch.operators.setOperatorsLoaded(true) store.dispatch.operators.setLoading(false) + return null } } @@ -276,13 +278,26 @@ export const retrieveFavoriteOperators = async (authStore: any, operatorObject: const favoriteOperatorsLocalStorage = loadPreference('favoriteOperators', authStore.username) || [] + //check if the favoriteOperatorsLocalStorage is not empty if (favoriteOperatorsLocalStorage.length > 0) { + // Cycle through all the favorite operators in local storage for (const operatorName of favoriteOperatorsLocalStorage) { - const operatorSpeedDial = speedDials.find((speedDial: any) => speedDial.name === operatorName) - const mainExtension = operatorSpeedDial?.speeddial_num - if (mainExtension) { - let operatorRealName = operatorObject[operatorName]?.name - await addOperatorToFavorites(operatorName, mainExtension, operatorRealName) + // Check if the operator already exists in the speed dials + const operatorAlreadyExists = speedDials.some( + (speedDial: any) => speedDial?.name === operatorName, + ) + + // If the operator does not exist in the speed dials, add it + if (!operatorAlreadyExists) { + const operatorSpeedDial = operatorObject[operatorName] + if (operatorSpeedDial) { + const mainExtension = operatorSpeedDial?.endpoints?.mainextension[0]?.id + const operatorRealName = operatorSpeedDial?.name + + if (mainExtension && operatorRealName) { + await addOperatorToFavorites(operatorName, mainExtension, operatorRealName) + } + } } }