From 0ce22129d1ccd354e4f22105e8f5eec0b718e7fa Mon Sep 17 00:00:00 2001 From: Vinod Deolal Date: Mon, 23 Aug 2021 13:06:31 +0530 Subject: [PATCH] #497 | reset my groups after restoring the backup db this ensures that mygroups are synced according to the user and data is delted/synced according the groups synced --- .../src/service/BackupRestoreRealm.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/openchs-android/src/service/BackupRestoreRealm.js b/packages/openchs-android/src/service/BackupRestoreRealm.js index a1a417a9e..4d35a59d9 100644 --- a/packages/openchs-android/src/service/BackupRestoreRealm.js +++ b/packages/openchs-android/src/service/BackupRestoreRealm.js @@ -1,4 +1,4 @@ -import {EntityMetaData, EntitySyncStatus, IdentifierAssignment, UserInfo, Concept} from 'avni-models'; +import {EntityMetaData, EntitySyncStatus, IdentifierAssignment, UserInfo, Concept, MyGroups} from 'avni-models'; import FileSystem from "../model/FileSystem"; import General from "../utility/General"; import fs from 'react-native-fs'; @@ -128,6 +128,7 @@ export default class BackupRestoreRealmService extends BaseService { cb(97, "restoringDb"); }) .then(() => this._deleteUserInfoAndIdAssignment()) + .then(() => this._deleteUserGroups()) .then(() => { General.logDebug("BackupRestoreRealmService", "Personalisation of database complete"); cb(100, "restoreComplete"); @@ -168,6 +169,20 @@ export default class BackupRestoreRealmService extends BaseService { }) }); } + + _deleteUserGroups() { + const db = this.db; + const myGroups = db.objects(EntitySyncStatus.schema.name) + .filtered(`entityName = 'MyGroups'`) + .map(u => _.assign({}, u)); + this.db.write(() => { + db.delete(db.objects(MyGroups.schema.name)); + myGroups.forEach(({uuid, entityName, entityTypeUuid}) => { + const updatedEntity = EntitySyncStatus.create(entityName, EntitySyncStatus.REALLY_OLD_DATE, uuid, entityTypeUuid); + db.create(EntitySyncStatus.schema.name, updatedEntity, true); + }) + }); + } } export const removeBackupFile = async (backupFilePath) => {