Skip to content

Commit

Permalink
Merge pull request #2074 from opensrp/issue-2072
Browse files Browse the repository at this point in the history
Boresha Afya Sync Improvements
  • Loading branch information
owais-vd authored Jun 28, 2022
2 parents 9f90e26 + a3c1a75 commit 26e3ba0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
4 changes: 2 additions & 2 deletions opensrp-chw/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ android {
applicationId "org.smartregister.chw"
minSdkVersion androidMinSdkVersion
targetSdkVersion androidTargetSdkVersion
versionCode 1
versionName "0.2.0"
versionCode 2
versionName "0.2.1"
multiDexEnabled true
buildConfigField "long", "MAX_SERVER_TIME_DIFFERENCE", "1800000l"
buildConfigField "boolean", "TIME_CHECK", "false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ public boolean hasReferrals() {
public void onVisitEvent(Visit visit) {
if (visit != null) {
Timber.v("Visit Submitted re processing Schedule for event ' %s ' : %s", visit.getVisitType(), visit.getBaseEntityId());
if (CoreLibrary.getInstance().isPeerToPeerProcessing() || SyncStatusBroadcastReceiver.getInstance().isSyncing())
return;

ChwScheduleTaskExecutor.getInstance().execute(visit.getBaseEntityId(), visit.getVisitType(), visit.getDate());

ChildAlertService.updateAlerts(visit.getBaseEntityId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

import androidx.annotation.Nullable;

import org.smartregister.CoreLibrary;
import org.smartregister.chw.application.ChwApplication;
import org.smartregister.chw.core.utils.CoreConstants;
import org.smartregister.chw.dao.ScheduleDao;
import org.smartregister.chw.fp.util.FamilyPlanningConstants;
import org.smartregister.chw.schedulers.ChwScheduleTaskExecutor;
import org.smartregister.receiver.SyncStatusBroadcastReceiver;

import java.util.Date;
import java.util.List;
Expand All @@ -30,8 +32,16 @@ public SchedulesIntentService() {
super("SchedulesIntentService");
}

public boolean isSyncing() {
return CoreLibrary.getInstance().isPeerToPeerProcessing() || SyncStatusBroadcastReceiver.getInstance().isSyncing();
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {

if (isSyncing())
return;

// execute all children schedules
executeChildVisitSchedules();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import android.content.Context;

import org.smartregister.CoreLibrary;
import org.smartregister.chw.application.ChwApplication;
import org.smartregister.chw.core.sync.CoreClientProcessor;
import org.smartregister.chw.core.utils.CoreConstants;
Expand All @@ -12,6 +13,7 @@
import org.smartregister.domain.db.EventClient;
import org.smartregister.domain.jsonmapping.ClientClassification;
import org.smartregister.domain.jsonmapping.Table;
import org.smartregister.receiver.SyncStatusBroadcastReceiver;
import org.smartregister.sync.ClientProcessorForJava;

public class ChwClientProcessor extends CoreClientProcessor {
Expand All @@ -38,7 +40,9 @@ public void processEvents(ClientClassification clientClassification, Table vacci
case CoreConstants.EventType.REMOVE_MEMBER:
ChwApplication.getInstance().getScheduleRepository().deleteSchedulesByEntityID(baseEntityID);
case CoreConstants.EventType.REMOVE_CHILD:
ChwApplication.getInstance().getScheduleRepository().deleteSchedulesByEntityID(baseEntityID);
if (!CoreLibrary.getInstance().isPeerToPeerProcessing() && !SyncStatusBroadcastReceiver.getInstance().isSyncing()) {
ChwApplication.getInstance().getScheduleRepository().deleteSchedulesByEntityID(baseEntityID);
}
break;
default:
break;
Expand All @@ -53,11 +57,16 @@ public void processEvents(ClientClassification clientClassification, Table vacci
case CoreConstants.EventType.CHILD_VISIT_NOT_DONE:
case CoreConstants.EventType.CHILD_REGISTRATION:
case CoreConstants.EventType.UPDATE_CHILD_REGISTRATION:
ChildAlertService.updateAlerts(baseEntityID);
if (!CoreLibrary.getInstance().isPeerToPeerProcessing() && !SyncStatusBroadcastReceiver.getInstance().isSyncing()) {
ChildAlertService.updateAlerts(baseEntityID);
}
default:
break;
}
}
ChwScheduleTaskExecutor.getInstance().execute(event.getBaseEntityId(), event.getEventType(), event.getEventDate().toDate());

if (!CoreLibrary.getInstance().isPeerToPeerProcessing() && !SyncStatusBroadcastReceiver.getInstance().isSyncing()) {
ChwScheduleTaskExecutor.getInstance().execute(event.getBaseEntityId(), event.getEventType(), event.getEventDate().toDate());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package org.smartregister.chw.application;

import static org.mockito.ArgumentMatchers.eq;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.chw.BaseUnitTest;
import org.smartregister.chw.anc.domain.Visit;
import org.smartregister.chw.core.utils.CoreConstants;
import org.smartregister.chw.schedulers.ChwScheduleTaskExecutor;
import org.smartregister.immunization.ImmunizationLibrary;
import org.smartregister.reporting.ReportingLibrary;

import java.util.Date;

public class CoreChwApplicationTest extends BaseUnitTest {

@Test
Expand All @@ -29,4 +36,20 @@ public void testP2PClassifierIsInitialized() {
ReflectionHelpers.setField(application, "flavor", flv);
Assert.assertNotNull(application.getP2PClassifier());
}

@Test
public void testOnVisitEventShouldExecuteChwScheduleTaskExecutor() {
Visit visit = new Visit();
visit.setBaseEntityId("12345-abcde");
visit.setVisitType(CoreConstants.VisitType.DONE.name());
visit.setDate(new Date());

ChwScheduleTaskExecutor chwScheduleTaskExecutor = Mockito.mock(ChwScheduleTaskExecutor.class);
ReflectionHelpers.setField(ChwScheduleTaskExecutor.getInstance(), "scheduleTaskExecutor", chwScheduleTaskExecutor);

ChwApplication chwApplication = new ChwApplication();
chwApplication.onVisitEvent(visit);

Mockito.verify(chwScheduleTaskExecutor, Mockito.times(1)).execute(eq(visit.getBaseEntityId()), eq(visit.getVisitType()), eq(visit.getDate()));
}
}

0 comments on commit 26e3ba0

Please sign in to comment.