diff --git a/opensrp-chw/build.gradle b/opensrp-chw/build.gradle
index 7f885101af..3f30e854a9 100644
--- a/opensrp-chw/build.gradle
+++ b/opensrp-chw/build.gradle
@@ -378,7 +378,7 @@ dependencies {
implementation('com.google.android.gms:play-services-vision:17.0.2')
- implementation('org.smartregister:opensrp-client-chw-core:2.1.4-SNAPSHOT@aar') {
+ implementation('org.smartregister:opensrp-client-chw-core:2.1.5-SNAPSHOT@aar') {
transitive = true
exclude group: 'com.android.support', module: 'appcompat-v7'
exclude group: 'androidx.legacy', module: 'legacy-support-v4'
@@ -387,6 +387,7 @@ dependencies {
exclude group: 'com.google.guava', module: 'guava'
exclude group: 'com.rengwuxian.materialedittext', module: 'library'
exclude group: 'stax', module: 'stax-api'
+ exclude group: 'com.google.guava', module:'listenablefuture'
}
implementation 'com.mapbox.mapboxsdk:mapbox-sdk-turf:5.1.0'
diff --git a/opensrp-chw/src/main/AndroidManifest.xml b/opensrp-chw/src/main/AndroidManifest.xml
index 52dfd8d690..051c9d9886 100644
--- a/opensrp-chw/src/main/AndroidManifest.xml
+++ b/opensrp-chw/src/main/AndroidManifest.xml
@@ -300,7 +300,9 @@
-
+
diff --git a/opensrp-chw/src/main/java/org/smartregister/chw/interactor/LoginJobSchedulerProvider.java b/opensrp-chw/src/main/java/org/smartregister/chw/interactor/LoginJobSchedulerProvider.java
index 7f5637d6ea..54eea40a9c 100644
--- a/opensrp-chw/src/main/java/org/smartregister/chw/interactor/LoginJobSchedulerProvider.java
+++ b/opensrp-chw/src/main/java/org/smartregister/chw/interactor/LoginJobSchedulerProvider.java
@@ -1,5 +1,9 @@
package org.smartregister.chw.interactor;
+import androidx.work.PeriodicWorkRequest;
+import androidx.work.WorkManager;
+import androidx.work.WorkRequest;
+
import org.smartregister.chw.BuildConfig;
import org.smartregister.chw.application.ChwApplication;
import org.smartregister.chw.contract.LoginJobScheduler;
@@ -11,6 +15,7 @@
import org.smartregister.chw.job.ScheduleJob;
import org.smartregister.immunization.job.VaccineServiceJob;
import org.smartregister.job.DocumentConfigurationServiceJob;
+import org.smartregister.job.DuplicateCleanerWorker;
import org.smartregister.job.ImageUploadServiceJob;
import org.smartregister.job.PlanIntentServiceJob;
import org.smartregister.job.PullUniqueIdsServiceJob;
@@ -53,6 +58,10 @@ public void scheduleJobsPeriodically() {
if (BuildConfig.USE_UNIFIED_REFERRAL_APPROACH)
DocumentConfigurationServiceJob.scheduleJob(DocumentConfigurationServiceJob.TAG,TimeUnit.MINUTES.toMinutes(BuildConfig.DATA_SYNC_DURATION_MINUTES), getFlexValue(BuildConfig.DATA_SYNC_DURATION_MINUTES));
+
+// WorkRequest cleanZeirIdsWorkRequest = new PeriodicWorkRequest.Builder(DuplicateCleanerWorker.class, 15, TimeUnit.MINUTES)
+// .build();
+// WorkManager.getInstance(this.getApplicationContext()).enqueue(cleanZeirIdsWorkRequest);
}
@Override
diff --git a/opensrp-chw/src/main/java/org/smartregister/chw/repository/ZeirIdCleanupRepository.java b/opensrp-chw/src/main/java/org/smartregister/chw/repository/ZeirIdCleanupRepository.java
new file mode 100644
index 0000000000..b7d1a67e97
--- /dev/null
+++ b/opensrp-chw/src/main/java/org/smartregister/chw/repository/ZeirIdCleanupRepository.java
@@ -0,0 +1,63 @@
+package org.smartregister.chw.repository;
+
+import android.database.Cursor;
+
+import org.apache.commons.lang3.StringUtils;
+import org.smartregister.repository.BaseRepository;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import timber.log.Timber;
+
+public class ZeirIdCleanupRepository extends BaseRepository {
+
+ private static final String BASE_ENTITY_ID = "baseEntityId";
+
+ private static final String DUPLICATES_SQL =
+ "WITH duplicates AS ( " +
+ " WITH clients AS ( " +
+ " SELECT baseEntityId, COALESCE(json_extract(json, '$.identifiers.ZEIR_ID'), json_extract(json, '$.identifiers.M_ZEIR_ID')) zeir_id " +
+ " FROM client " +
+ " ) " +
+ " SELECT b.* FROM (SELECT baseEntityId, zeir_id FROM clients GROUP BY zeir_id HAVING count(zeir_id) > 1) a " +
+ " INNER JOIN clients b ON a.zeir_id=b.zeir_id " +
+ " UNION " +
+ " SELECT * FROM clients WHERE zeir_id IS NULL " +
+ ") " +
+ "SELECT baseEntityId, zeir_id, lag(zeir_id) over(order by zeir_id) AS prev_zeir_id FROM duplicates";
+
+ public Map getClientsWithDuplicateZeirIds() {
+ Map duplicates = new HashMap<>();
+
+ Cursor cursor = null;
+ try {
+ cursor = getWritableDatabase().rawQuery(DUPLICATES_SQL, new String[]{});
+
+ while (cursor.moveToNext()) {
+ String baseEntityId = cursor.getString(cursor.getColumnIndex(BASE_ENTITY_ID));
+ String zeirId = cursor.getString(cursor.getColumnIndex("zeir_id"));
+
+ duplicates.put(baseEntityId, zeirId);
+
+ String prevZeirId = null;
+ try {
+ prevZeirId = cursor.getString(cursor.getColumnIndex("prev_zeir_id"));
+ } catch (NullPointerException e) {
+ Timber.e(e, "null prev_zeir_id");
+ }
+
+ if (StringUtils.isNotEmpty(prevZeirId) && (prevZeirId.equals(zeirId))) {
+ duplicates.put(baseEntityId, prevZeirId);
+ }
+ }
+ } catch (Exception e) {
+ Timber.e(e);
+ } finally {
+ if (cursor != null && !cursor.isClosed())
+ cursor.close();
+ }
+
+ return duplicates;
+ }
+}