diff --git a/libs/device/models/src/lib/entities/entities-metadata.validator.ts b/libs/device/models/src/lib/entities/entities-metadata.validator.ts index 826bbe4ba7..86aac61b90 100644 --- a/libs/device/models/src/lib/entities/entities-metadata.validator.ts +++ b/libs/device/models/src/lib/entities/entities-metadata.validator.ts @@ -7,6 +7,7 @@ import { z } from "zod" export const entitiesMetadataValidator = z.object({ totalEntities: z.number().nonnegative(), + uniqueKey: z.string().optional(), }) export type EntitiesMetadata = z.infer diff --git a/libs/generic-view/store/src/index.ts b/libs/generic-view/store/src/index.ts index f9a8c48530..91df371b0c 100644 --- a/libs/generic-view/store/src/index.ts +++ b/libs/generic-view/store/src/index.ts @@ -52,6 +52,7 @@ export * from "./lib/entities/get-entities-metadata.action" export * from "./lib/entities/delete-entities-data.action" export * from "./lib/entities/create-entity-data.action" export * from "./lib/entities/update-entity-data.action" +export * from "./lib/entities/refresh-entities-if-metadata-changed.action" export * from "./lib/data-migration/reducer" export * from "./lib/data-migration/actions" diff --git a/libs/generic-view/store/src/lib/action-names.ts b/libs/generic-view/store/src/lib/action-names.ts index 0c559870fc..3e59c504d2 100644 --- a/libs/generic-view/store/src/lib/action-names.ts +++ b/libs/generic-view/store/src/lib/action-names.ts @@ -109,4 +109,5 @@ export enum ActionName { DeleteEntityData = "entities/delete-entity-data", CreateEntityData = "entities/create-entity-data", UpdateEntityData = "entities/update-entity-data", + RefreshEntitiesIfMetadataChanged = "entities/refresh-entities-if-metadata-changed", } diff --git a/libs/generic-view/store/src/lib/backup/restore-backup.action.ts b/libs/generic-view/store/src/lib/backup/restore-backup.action.ts index f1b6d61612..5a08e441f1 100644 --- a/libs/generic-view/store/src/lib/backup/restore-backup.action.ts +++ b/libs/generic-view/store/src/lib/backup/restore-backup.action.ts @@ -21,6 +21,7 @@ import { selectActiveApiDeviceId } from "../selectors/select-active-api-device-i import { setRestoreProcessFileStatus, setRestoreProcessStatus } from "./actions" import { BackupProcessFileStatus, RestoreProcessStatus } from "./backup.types" import { delay } from "shared/utils" +import { refreshEntitiesIfMetadataChanged } from "../entities/refresh-entities-if-metadata-changed.action" export const restoreBackup = createAsyncThunk< undefined, @@ -221,6 +222,14 @@ export const restoreBackup = createAsyncThunk< return rejectWithValue(undefined) } dispatch(setRestoreProcessStatus({ status: RestoreProcessStatus.Done })) + + await dispatch( + refreshEntitiesIfMetadataChanged({ + deviceId: deviceId, + entitiesType: "contacts", + }) + ) + return undefined } ) diff --git a/libs/generic-view/store/src/lib/data-migration/transfer-migration-data.ts b/libs/generic-view/store/src/lib/data-migration/transfer-migration-data.ts index a91b3dd3f3..2a412f1422 100644 --- a/libs/generic-view/store/src/lib/data-migration/transfer-migration-data.ts +++ b/libs/generic-view/store/src/lib/data-migration/transfer-migration-data.ts @@ -26,6 +26,8 @@ import { removeDirectory } from "system-utils/feature" import { DataMigrationStatus } from "./reducer" import { clearMigrationData } from "./clear-migration-data" import { pureToUnifiedMessage } from "./data-migration-mappers/pure-to-unified-message" +import { refreshEntitiesIfMetadataChanged } from "../entities/refresh-entities-if-metadata-changed.action" +import { selectActiveApiDeviceId } from "../selectors" export const transferMigrationData = createAsyncThunk< void, @@ -56,6 +58,12 @@ export const transferMigrationData = createAsyncThunk< return rejectWithValue(undefined) } + const deviceId = selectActiveApiDeviceId(getState()) + + if (!deviceId) { + return handleError("Device not found") + } + const sourceDeviceId = dataMigration.sourceDevice if (!sourceDeviceId) { return handleError("Source device not selected") @@ -173,6 +181,15 @@ export const transferMigrationData = createAsyncThunk< await delay() + if (domainsData.some((obj) => obj.domain === "contacts-v1")) { + await dispatch( + refreshEntitiesIfMetadataChanged({ + deviceId: deviceId, + entitiesType: "contacts", + }) + ) + } + dispatch(setDataMigrationStatus(DataMigrationStatus.Completed)) return diff --git a/libs/generic-view/store/src/lib/entities/refresh-entities-if-metadata-changed.action.ts b/libs/generic-view/store/src/lib/entities/refresh-entities-if-metadata-changed.action.ts new file mode 100644 index 0000000000..b6775541f1 --- /dev/null +++ b/libs/generic-view/store/src/lib/entities/refresh-entities-if-metadata-changed.action.ts @@ -0,0 +1,46 @@ +/** + * Copyright (c) Mudita sp. z o.o. All rights reserved. + * For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md + */ + +import { createAsyncThunk } from "@reduxjs/toolkit" +import { ReduxRootState } from "Core/__deprecated__/renderer/store" +import { ActionName } from "../action-names" +import { getEntitiesMetadataAction } from "./get-entities-metadata.action" +import { selectEntitiesMetadata } from "../selectors" +import { getEntitiesDataAction } from "./get-entities-data.action" +import { EntitiesMetadata } from "Libs/device/models/src" + +export const refreshEntitiesIfMetadataChanged = createAsyncThunk< + void, + { deviceId: string; entitiesType: string }, + { state: ReduxRootState } +>( + ActionName.RefreshEntitiesIfMetadataChanged, + async ( + { deviceId, entitiesType }, + { dispatch, getState, rejectWithValue } + ) => { + if (!deviceId) { + return rejectWithValue(undefined) + } + + const currentMetadata = selectEntitiesMetadata(getState(), { + deviceId, + entitiesType, + }) + + const metadata = await dispatch( + getEntitiesMetadataAction({ deviceId, entitiesType }) + ) + + if ( + currentMetadata?.uniqueKey !== + (metadata.payload as EntitiesMetadata)?.uniqueKey + ) { + dispatch(getEntitiesDataAction({ deviceId, entitiesType })) + } + + return + } +) diff --git a/libs/generic-view/store/src/lib/imports/start-contacts-import-to-device.ts b/libs/generic-view/store/src/lib/imports/start-contacts-import-to-device.ts index 9680ef2264..d729d16935 100644 --- a/libs/generic-view/store/src/lib/imports/start-contacts-import-to-device.ts +++ b/libs/generic-view/store/src/lib/imports/start-contacts-import-to-device.ts @@ -23,6 +23,7 @@ import { sendFile } from "../file-transfer/send-file.action" import { selectActiveApiDeviceId } from "../selectors" import { setImportProcessFileStatus, setImportProcessStatus } from "./actions" import { delay } from "shared/utils" +import { refreshEntitiesIfMetadataChanged } from "../entities/refresh-entities-if-metadata-changed.action" export const startImportToDevice = createAsyncThunk< undefined, @@ -254,6 +255,13 @@ export const startImportToDevice = createAsyncThunk< return rejectWithValue(undefined) } + await dispatch( + refreshEntitiesIfMetadataChanged({ + deviceId: deviceId, + entitiesType: "contacts", + }) + ) + return undefined } )