Skip to content

Commit

Permalink
Move flag onto the functions not upload-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kennsippell committed Dec 9, 2024
1 parent 9685122 commit dcff03e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/fn/delete-contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
1 change: 1 addition & 0 deletions src/fn/merge-contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
7 changes: 3 additions & 4 deletions src/fn/upload-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib/hierarchy-operations/delete-hierarchy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib/hierarchy-operations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})`;
Expand Down
1 change: 1 addition & 0 deletions test/fn/merge-contacts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '/',
});
Expand Down
9 changes: 7 additions & 2 deletions test/lib/hierarchy-operations/hierarchy-operations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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 = [
Expand All @@ -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, {
Expand Down

0 comments on commit dcff03e

Please sign in to comment.