diff --git a/src/fn/delete-contacts.js b/src/fn/delete-contacts.js index bcc48f20..87c7a1f2 100644 --- a/src/fn/delete-contacts.js +++ b/src/fn/delete-contacts.js @@ -35,6 +35,7 @@ const parseExtraArgs = (projectDir, extraArgs = []) => { return { sourceIds, + disableUsers: !!args['disable-users'], docDirectoryPath: path.resolve(projectDir, args.docDirectoryPath || 'json_docs'), force: !!args.force, }; diff --git a/src/fn/merge-contacts.js b/src/fn/merge-contacts.js index 462b3693..41bae589 100644 --- a/src/fn/merge-contacts.js +++ b/src/fn/merge-contacts.js @@ -41,6 +41,7 @@ const parseExtraArgs = (projectDir, extraArgs = []) => { return { destinationId: args.destination, sourceIds, + disableUsers: !!args['disable-users'], docDirectoryPath: path.resolve(projectDir, args.docDirectoryPath || 'json_docs'), force: !!args.force, }; diff --git a/src/fn/upload-docs.js b/src/fn/upload-docs.js index 5ff50f2e..2db6e1b3 100644 --- a/src/fn/upload-docs.js +++ b/src/fn/upload-docs.js @@ -37,10 +37,8 @@ async function execute() { warnAndPrompt(`This operation will permanently write ${totalCount} docs. Are you sure you want to continue?`); - if (args['disable-users']) { - const deletedDocIds = analysis.map(result => result.delete).filter(Boolean); - await handleUsersAtDeletedFacilities(deletedDocIds); - } + const deletedDocIds = analysis.map(result => result.delete).filter(Boolean); + await handleUsersAtDeletedFacilities(deletedDocIds); const results = { ok:[], failed:{} }; const progress = log.level > log.LEVEL_ERROR ? progressBar.init(totalCount, '{{n}}/{{N}} docs ', ' {{%}} {{m}}:{{s}}') : null; @@ -128,6 +126,7 @@ async function handleUsersAtDeletedFacilities(deletedDocIds) { const affectedUsers = await getAffectedUsers(deletedDocIds); const usernames = affectedUsers.map(userDoc => userDoc.username).join(', '); if (affectedUsers.length === 0) { + trace('No deleted places with potential users found.'); return; } diff --git a/src/lib/hierarchy-operations/delete-hierarchy.js b/src/lib/hierarchy-operations/delete-hierarchy.js index 8ff88212..ec3cff7a 100644 --- a/src/lib/hierarchy-operations/delete-hierarchy.js +++ b/src/lib/hierarchy-operations/delete-hierarchy.js @@ -14,7 +14,8 @@ async function deleteHierarchy(db, options, sourceIds) { let affectedReportCount = 0; for (const descendant of descendantsAndSelf) { - JsDocs.deleteDoc(options, descendant, constraints.isPlace(descendant)); + const toDeleteUsers = options.disableUsers && constraints.isPlace(descendant); + JsDocs.deleteDoc(options, descendant, toDeleteUsers); affectedReportCount += await deleteReportsForContact(db, options, descendant); } diff --git a/src/lib/hierarchy-operations/index.js b/src/lib/hierarchy-operations/index.js index a3e4f889..21aacf8f 100644 --- a/src/lib/hierarchy-operations/index.js +++ b/src/lib/hierarchy-operations/index.js @@ -28,7 +28,8 @@ async function moveHierarchy(db, options, sourceIds, destinationId) { }; if (options.merge) { - JsDocs.deleteDoc(options, sourceDoc, constraints.isPlace(sourceDoc)); + const toDeleteUsers = options.disableUsers && constraints.isPlace(sourceDoc); + JsDocs.deleteDoc(options, sourceDoc, toDeleteUsers); } const prettyPrintDocument = doc => `'${doc.name}' (${doc._id})`; diff --git a/test/fn/merge-contacts.spec.js b/test/fn/merge-contacts.spec.js index fbb8ec6f..a1b0d994 100644 --- a/test/fn/merge-contacts.spec.js +++ b/test/fn/merge-contacts.spec.js @@ -18,6 +18,7 @@ describe('merge-contacts', () => { expect(parseExtraArgs(__dirname, args)).to.deep.eq({ sourceIds: ['food', 'is', 'tasty'], destinationId: 'bar', + disableUsers: false, force: true, docDirectoryPath: '/', }); diff --git a/test/lib/hierarchy-operations/hierarchy-operations.spec.js b/test/lib/hierarchy-operations/hierarchy-operations.spec.js index a437ebf5..1c826804 100644 --- a/test/lib/hierarchy-operations/hierarchy-operations.spec.js +++ b/test/lib/hierarchy-operations/hierarchy-operations.spec.js @@ -664,7 +664,7 @@ describe('hierarchy-operations', () => { }); // action - await HierarchyOperations(pouchDb).merge(['district_2'], 'district_1'); + await HierarchyOperations(pouchDb, { disableUsers: true }).merge(['district_2'], 'district_1'); // assert expectWrittenDocs([ @@ -797,7 +797,7 @@ describe('hierarchy-operations', () => { }); // action - await HierarchyOperations(pouchDb).delete(['district_2']); + await HierarchyOperations(pouchDb, { disableUsers: true }).delete(['district_2']); // assert const deletedPlaces = [ @@ -818,6 +818,11 @@ describe('hierarchy-operations', () => { deletedNonPeople.forEach(id => expectDeleted(id, false)); }); + it('users at are not disabled when disableUsers: false', async () => { + await HierarchyOperations(pouchDb, { disableUsers: false }).delete(['district_2']); + expectDeleted('district_2', false); + }); + it('reports created by deleted contacts are not deleted', async () => { // setup await mockReport(pouchDb, {