Skip to content

Commit

Permalink
#1153 | Refactor code to remove duplication of code across Sync.js an…
Browse files Browse the repository at this point in the history
…d SyncComponent.js for reset after fullSync
  • Loading branch information
himeshr committed Oct 26, 2023
1 parent 55ba8c1 commit 99353db
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 58 deletions.
26 changes: 26 additions & 0 deletions packages/openchs-android/src/service/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import TaskUnAssignmentService from "./task/TaskUnAssignmentService";
import UserSubjectAssignmentService from "./UserSubjectAssignmentService";
import moment from "moment";
import AllSyncableEntityMetaData from "../model/AllSyncableEntityMetaData";
import ProgramConfigService from './ProgramConfigService';
import {IndividualSearchActionNames as IndividualSearchActions} from '../action/individual/IndividualSearchActions';
import {LandingViewActionsNames as LandingViewActions} from '../action/LandingViewActions';

@Service("syncService")
class SyncService extends BaseService {
Expand Down Expand Up @@ -352,6 +355,29 @@ class SyncService extends BaseService {
this.messageService.init();
this.ruleService.init();
}

resetServicesAfterFullSyncCompletion(updatedSyncSource) {
if (updatedSyncSource !== SyncService.syncSources.ONLY_UPLOAD_BACKGROUND_JOB) {
General.logInfo("Sync", "Full Sync completed, performing reset")
setTimeout(() => this.reset(), 1);
this.getService(SettingsService).initLanguages();
General.logInfo("Sync", 'Full Sync completed, reset completed');
}
}

reset() {
this.context.getService(RuleEvaluationService).init();
this.context.getService(ProgramConfigService).init();
this.context.getService(MessageService).init();
this.context.getService(RuleService).init();
this.dispatchAction('RESET');
this.context.getService(PrivilegeService).deleteRevokedEntities();
//To load subjectType after sync
this.dispatchAction(IndividualSearchActions.ON_LOAD);

//To re-render LandingView after sync
this.dispatchAction(LandingViewActions.ON_LOAD, {syncRequired: false});
}
}

export default SyncService;
44 changes: 11 additions & 33 deletions packages/openchs-android/src/task/Sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,17 @@ import _ from "lodash";
import SettingsService from '../service/SettingsService';
import EnvironmentConfig from "../framework/EnvironmentConfig";
import {SyncActionNames as SyncActions} from '../action/SyncActions';
import RuleEvaluationService from '../service/RuleEvaluationService';
import ProgramConfigService from '../service/ProgramConfigService';
import MessageService from '../service/MessageService';
import RuleService from '../service/RuleService';
import PrivilegeService from '../service/PrivilegeService';
import {IndividualSearchActionNames as IndividualSearchActions} from '../action/individual/IndividualSearchActions';
import {LandingViewActions} from '../action/LandingViewActions';
import moment from 'moment';

function dispatchAction(action, params) {
const type = action instanceof Function ? action.Id : action;
if (General.canLog(General.LogLevel.Debug))
General.logDebug('BaseService', `Dispatching action: ${JSON.stringify(type)}`);
return GlobalContext.getInstance().reduxStore.dispatch({type, ...params});
}

class Sync extends BaseTask {
async execute() {
const dispatchAction = (action, params) => {
const type = action instanceof Function ? action.Id : action;
if (General.canLog(General.LogLevel.Debug))
General.logDebug('BaseService', `Dispatching action: ${JSON.stringify(type)}`);
return GlobalContext.getInstance().reduxStore.dispatch({type, ...params});
}

try {
General.logInfo("Sync", "Starting background sync");
Expand Down Expand Up @@ -63,7 +57,7 @@ class Sync extends BaseTask {
},
(message) => {
}, connectionInfo, Date.now(), SyncService.syncSources.ONLY_UPLOAD_BACKGROUND_JOB, null)
.then(this.performPostBackgroundSyncActions(dispatchAction, globalContext))
.then(this.performPostBackgroundSyncActions(globalContext))
.catch((e) => {
ErrorHandler.postScheduledJobError(e);
});
Expand All @@ -81,27 +75,11 @@ class Sync extends BaseTask {
return _.isEmpty(lastSynced) || moment(lastSynced[0].syncEndTime).add(30, 'minutes').isBefore(moment());
}

performPostBackgroundSyncActions(dispatchAction, globalContext) {
performPostBackgroundSyncActions(globalContext) {
return (updatedSyncSource) => {
General.logInfo("Sync", "Sync completed")
General.logInfo("Sync", "Background Sync completed")
dispatchAction(SyncActions.ON_BACKGROUND_SYNC_STATUS_CHANGE, {backgroundSyncInProgress: false});
if (updatedSyncSource === SyncService.syncSources.BACKGROUND_JOB) {
General.logInfo("Background Sync", "Full Background Sync completed, performing reset")
setTimeout(() => {
globalContext.beanRegistry.getService(RuleEvaluationService).init();
globalContext.beanRegistry.getService(ProgramConfigService).init();
globalContext.beanRegistry.getService(MessageService).init();
globalContext.beanRegistry.getService(RuleService).init();
globalContext.beanRegistry.getService(PrivilegeService).deleteRevokedEntities();
//To load subjectType after sync
globalContext.beanRegistry.getService(IndividualSearchActions.ON_LOAD);

//To re-render LandingView after sync
dispatchAction(LandingViewActions.ON_LOAD, {syncRequired: false});
}, 1);
globalContext.beanRegistry.getService(SettingsService).initLanguages();
General.logInfo("Background Sync", 'Background Sync completed, reset completed');
}
globalContext.beanRegistry.getService(SyncService).resetServicesAfterFullSyncCompletion(updatedSyncSource);
};
}
}
Expand Down
26 changes: 1 addition & 25 deletions packages/openchs-android/src/views/SyncComponent.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import AbstractComponent from "../framework/view/AbstractComponent";
import Colors from "./primitives/Colors";
import RuleEvaluationService from "../service/RuleEvaluationService";
import ProgramConfigService from "../service/ProgramConfigService";
import MessageService from "../service/MessageService";
import RuleService from "../service/RuleService";
import {IndividualSearchActionNames as IndividualSearchActions} from "../action/individual/IndividualSearchActions";
import {LandingViewActionsNames as LandingViewActions} from "../action/LandingViewActions";
import {SyncActionNames as SyncActions} from "../action/SyncActions";
import General from "../utility/General";
import {SyncTelemetryActionNames as SyncTelemetryActions} from "../action/SyncTelemetryActions";
Expand All @@ -22,8 +16,6 @@ import EntitySyncStatusService from "../service/EntitySyncStatusService";
import React from "react";
import ProgressBarView from "./ProgressBarView";
import Reducers from "../reducer";
import SettingsService from "../service/SettingsService";
import PrivilegeService from "../service/PrivilegeService";
import AsyncAlert from "./common/AsyncAlert";
import {ScheduleDummySyncJob, ScheduleSyncJob} from "../AvniBackgroundJob";

Expand All @@ -43,20 +35,6 @@ class SyncComponent extends AbstractComponent {
this.dispatchAction(SyncActions.PRE_SYNC);
}

reset() {
this.context.getService(RuleEvaluationService).init();
this.context.getService(ProgramConfigService).init();
this.context.getService(MessageService).init();
this.context.getService(RuleService).init();
this.dispatchAction('RESET');
this.context.getService(PrivilegeService).deleteRevokedEntities();
//To load subjectType after sync
this.dispatchAction(IndividualSearchActions.ON_LOAD);

//To re-render LandingView after sync
this.dispatchAction(LandingViewActions.ON_LOAD, {syncRequired: false});
}

progressBarUpdate(progress, totalNumberOfPagesForCurrentEntity, numberOfPagesProcessedForCurrentEntity) {
this.dispatchAction(SyncActions.ON_UPDATE, {progress, numberOfPagesProcessedForCurrentEntity, totalNumberOfPagesForCurrentEntity})
}
Expand All @@ -68,9 +46,7 @@ class SyncComponent extends AbstractComponent {
_postSync() {
this.setState({syncStarted: false});
this.dispatchAction(SyncActions.POST_SYNC);
setTimeout(() => this.reset(), 1);

this.context.getService(SettingsService).initLanguages();
this.context.getService(SyncService).resetServicesAfterFullSyncCompletion(SyncService.syncSources.BACKGROUND_JOB);
General.logInfo(this.viewName(), 'Sync completed dispatching reset');
}

Expand Down

0 comments on commit 99353db

Please sign in to comment.