Skip to content

Commit

Permalink
#1125 | restore userInfo after fast sync db restore
Browse files Browse the repository at this point in the history
  • Loading branch information
himeshr committed Oct 6, 2023
1 parent e9ee8cf commit 5d31c03
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/openchs-android/src/service/BackupRestoreRealm.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import SubjectTypeService from "./SubjectTypeService";
import IndividualService from "./IndividualService";
import SubjectMigrationService from "./SubjectMigrationService";
import FormMappingService from "./FormMappingService";
import UserInfoService from './UserInfoService';

const REALM_FILE_NAME = "default.realm";
const REALM_FILE_FULL_PATH = `${fs.DocumentDirectoryPath}/${REALM_FILE_NAME}`;
Expand Down Expand Up @@ -85,6 +86,7 @@ export default class BackupRestoreRealmService extends BaseService {
let downloadedFile = `${fs.DocumentDirectoryPath}/${General.randomUUID()}.zip`;
let downloadedUncompressedDir = `${fs.DocumentDirectoryPath}/${General.randomUUID()}`;
const prevSettings = settingsService.getSettings().clone();
const prevUserInfo = UserInfo.fromResource({username: prevSettings.userId, organisationName: 'dummy', name: prevSettings.userId});

General.logInfo("BackupRestoreRealm", `To be downloaded file: ${downloadedFile}, Unzipped directory: ${downloadedUncompressedDir}, Realm file: ${REALM_FILE_FULL_PATH}`);

Expand Down Expand Up @@ -143,6 +145,11 @@ export default class BackupRestoreRealmService extends BaseService {
this._deleteUserInfoAndIdAssignment();
General.logDebug("BackupRestoreRealmService", "Deleted user info and id assignment");
})
.then(() => {
this._restoreUserInfo(prevUserInfo);
General.logDebug("BackupRestoreRealmService", "Restoring prev userInfo to ensure we have user details" +
" immediately after fast sync restore");
})
.then(() => {
this._deleteUserGroups();
General.logDebug("BackupRestoreRealmService", "Deleted user groups");
Expand Down Expand Up @@ -217,6 +224,10 @@ export default class BackupRestoreRealmService extends BaseService {
this.getService(SettingsService).saveOrUpdate(prevSettings);
}

_restoreUserInfo(prevUserInfo) {
this.getService(UserInfoService).saveOrUpdate(prevUserInfo);
}

resetSyncForSubjectType(subjectType, db) {
const formMappingsForSubjectType = this.getService(FormMappingService).getFormMappingsForSubjectType(subjectType).map(_.identity);
_.forEach(formMappingsForSubjectType, (formMapping) => {
Expand Down

0 comments on commit 5d31c03

Please sign in to comment.