Skip to content

Commit

Permalink
Replicate form deletes to offline users
Browse files Browse the repository at this point in the history
Fixes bug where deletion of forms would not be replicated to offline users.

#5078
  • Loading branch information
dianabarsan authored and garethbowen committed Dec 11, 2018
1 parent 39c2d9c commit 7c6eebf
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 4 deletions.
8 changes: 4 additions & 4 deletions ddocs/medic/views/docs_by_replication_key/map.js
@@ -1,4 +1,8 @@
function (doc) {
if (doc.type === 'tombstone' && doc.tombstone) {
doc = doc.tombstone;
}

if (doc._id === 'resources' ||
doc._id === 'branding' ||
doc._id === 'appcache' ||
Expand All @@ -9,10 +13,6 @@ function (doc) {
return emit('_all', {});
}

if (doc.type === 'tombstone' && doc.tombstone) {
doc = doc.tombstone;
}

var getSubject = function() {
if (doc.form) {
// report
Expand Down
101 changes: 101 additions & 0 deletions tests/e2e/docs-by-replication-key-view.js
Expand Up @@ -84,6 +84,53 @@ describe('view docs_by_replication_key', () => {
_id: 'testuser'
},
errors: [ { code: 'invalid_patient_id' } ]
},
{
_id: 'form:some_deleted_form____tombstone',
type: 'tombstone',
tombstone: {
_id: 'form:some_deleted_form',
reported_date: 1,
type: 'form',
_deleted: true
},
},
{
_id: 'report_about_patient_deleted____tombstone',
type: 'tombstone',
tombstone: {
_id: 'report_about_patient_deleted',
reported_date: 1,
form: 'V',
type: 'data_record',
patient_id: 'testpatient',
_deleted: true
},
},
{
_id: 'report_about_patient_2_deleted____tombstone',
type: 'tombstone',
tombstone: {
_id: 'report_about_patient_2_deleted',
reported_date: 1,
form: 'V',
type: 'data_record',
fields: { patient_id: 'testpatient' },
_deleted: true
},
},
{
_id: 'report_with_contact_deleted____tombstone',
type: 'tombstone',
tombstone: {
_id: 'report_with_contact_deleted',
reported_date: 1,
form: 'V',
type: 'data_record',
contact: {
_id: 'testuser'
}
},
}
];

Expand Down Expand Up @@ -118,6 +165,37 @@ describe('view docs_by_replication_key', () => {
reported_date: 1,
type: 'data_record',
contact: 'not_the_testuser'
},
{
_id: 'fakedoctype_deleted____tombstone',
type: 'tombstone',
tombstone: {
_id: 'fakedoctype_deleted',
reported_date: 1,
type: 'fakedoctype',
_deleted: true
}
},
{
_id: 'not_the_testuser_deleted____tombstone',
type: 'tombstone',
tombstone: {
_id: 'not_the_testuser_deleted',
reported_date: 1,
type: 'person',
_deleted: true
},
},
{
_id: 'test_data_record_wrong_user_deleted____tombstone',
type: 'tombstone',
tombstone: {
_id: 'test_data_record_wrong_user_deleted',
reported_date: 1,
type: 'data_record',
contact: 'not_the_testuser',
_deleted: true
},
}
];

Expand Down Expand Up @@ -151,6 +229,17 @@ describe('view docs_by_replication_key', () => {
_id: 'test_kujua_message_incoming_no_contact',
reported_date: 1,
type: 'data_record'
},
{
_id: 'test_kujua_message_no_tasks_deleted____tombstone',
type: 'tombstone',
tombstone: {
_id: 'test_kujua_message_no_tasks_deleted',
reported_date: 1,
type: 'data_record',
kujua_message: true,
_deleted: true
}
}
];

Expand Down Expand Up @@ -217,13 +306,19 @@ describe('view docs_by_replication_key', () => {
expect(docByPlaceIds).toContain('form:doc_by_place_test_form');
});

it('should always return form deletes', () => {
expect(docByPlaceIds).toContain('form:some_deleted_form____tombstone');
});

describe('Documents associated with the person id', () => {
it('Should return clinics if a recursive parent is the user', () => {
expect(docByPlaceIds).toContain('report_about_patient');
expect(docByPlaceIds).toContain('report_about_patient_deleted____tombstone');
});

it('Should return district_hospitals if the recursive parent is the user', () => {
expect(docByPlaceIds).toContain('report_about_patient_2');
expect(docByPlaceIds).toContain('report_about_patient_2_deleted____tombstone');
});

it('Should return health_centers if the recursive parent is the user', () => {
Expand All @@ -238,6 +333,9 @@ describe('view docs_by_replication_key', () => {
it('Should check the contact of data records', () => {
expect(docByPlaceIds).toContain('report_with_contact');
expect(docByPlaceIds).not.toContain('test_data_record_wrong_user');

expect(docByPlaceIds).toContain('report_with_contact_deleted____tombstone');
expect(docByPlaceIds).not.toContain('test_data_record_wrong_user_deleted____tombstone');
});

it('Falls back to contact id when unknown patient', () => {
Expand All @@ -253,6 +351,9 @@ describe('view docs_by_replication_key', () => {
it('Should pass when no tasks', () => {
expect(docByPlaceIds_unassigned).toContain('test_kujua_message_no_tasks');
expect(docByPlaceIds).not.toContain('test_kujua_message_no_tasks');

expect(docByPlaceIds_unassigned).toContain('test_kujua_message_no_tasks_deleted____tombstone');
expect(docByPlaceIds).not.toContain('test_kujua_message_no_tasks_deleted____tombstone');
});

it('Should pass when empty tasks', () => {
Expand Down

0 comments on commit 7c6eebf

Please sign in to comment.