-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This adds new action cht delete-contacts which recursively deletes all contacts and reports under a place. npx cht delete-contacts upload-docs --local -- --contacts=uuid1
- Loading branch information
1 parent
052aaa3
commit 129f38f
Showing
7 changed files
with
717 additions
and
526 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const minimist = require('minimist'); | ||
const path = require('path'); | ||
|
||
const environment = require('../lib/environment'); | ||
const pouch = require('../lib/db'); | ||
const { info } = require('../lib/log'); | ||
|
||
const HierarchyOperations = require('../lib/hierarchy-operations'); | ||
|
||
module.exports = { | ||
requiresInstance: true, | ||
execute: () => { | ||
const args = parseExtraArgs(environment.pathToProject, environment.extraArgs); | ||
const db = pouch(); | ||
const options = { | ||
docDirectoryPath: args.docDirectoryPath, | ||
force: args.force, | ||
disableUsers: args.disableUsers, | ||
}; | ||
return HierarchyOperations(db, options).delete(args.sourceIds); | ||
} | ||
}; | ||
|
||
// Parses extraArgs and asserts if required parameters are not present | ||
const parseExtraArgs = (projectDir, extraArgs = []) => { | ||
const args = minimist(extraArgs, { boolean: true }); | ||
|
||
const sourceIds = (args.contacts || args.contact || '') | ||
.split(',') | ||
.filter(id => id); | ||
|
||
if (sourceIds.length === 0) { | ||
usage(); | ||
throw Error('Action "delete-contacts" is missing required list of contacts to be deleted'); | ||
} | ||
|
||
return { | ||
sourceIds, | ||
disableUsers: !!args['disable-users'], | ||
docDirectoryPath: path.resolve(projectDir, args.docDirectoryPath || 'json_docs'), | ||
force: !!args.force, | ||
}; | ||
}; | ||
|
||
const bold = text => `\x1b[1m${text}\x1b[0m`; | ||
const usage = () => { | ||
info(` | ||
${bold('cht-conf\'s delete-contacts action')} | ||
When combined with 'upload-docs' this action recursively deletes a contact and all of their descendant contacts and data. ${bold('This operation is permanent. It cannot be undone.')} | ||
${bold('USAGE')} | ||
cht --local delete-contacts -- --contacts=<id1>,<id2> | ||
${bold('OPTIONS')} | ||
--contacts=<id1>,<id2> (or --contact=<id1>,<id2>) | ||
A comma delimited list of ids of contacts to be deleted. | ||
--disable-users | ||
When flag is present, users at any deleted place will be permanently disabled. | ||
--docDirectoryPath=<path to stage docs> | ||
Specifies the folder used to store the documents representing the changes in hierarchy. | ||
`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const DataSource = require('./hierarchy-data-source'); | ||
const JsDocs = require('./jsdocFolder'); | ||
const lineageConstraints = require('./lineage-constraints'); | ||
const { trace, info } = require('../log'); | ||
|
||
const prettyPrintDocument = doc => `'${doc.name}' (${doc._id})`; | ||
async function deleteHierarchy(db, options, sourceIds) { | ||
JsDocs.prepareFolder(options); | ||
|
||
const sourceDocs = await DataSource.getContactsByIds(db, sourceIds); | ||
const constraints = await lineageConstraints(db, options); | ||
for (const sourceId of sourceIds) { | ||
const sourceDoc = sourceDocs[sourceId]; | ||
trace(`Deleting descendants and reports under: ${prettyPrintDocument(sourceDoc)}`); | ||
const descendantsAndSelf = await DataSource.getContactWithDescendants(db, sourceId); | ||
|
||
let affectedReportCount = 0; | ||
for (const descendant of descendantsAndSelf) { | ||
const toDeleteUsers = options.disableUsers && constraints.isPlace(descendant); | ||
JsDocs.deleteDoc(options, descendant, toDeleteUsers); | ||
affectedReportCount += await deleteReportsForContact(db, options, descendant); | ||
} | ||
|
||
const affectedContactCount = descendantsAndSelf.length; | ||
|
||
info(`Staged updates to delete ${prettyPrintDocument(sourceDoc)}. ${affectedContactCount.length} contact(s) and ${affectedReportCount} report(s).`); | ||
} | ||
} | ||
|
||
async function deleteReportsForContact(db, options, contact) { | ||
let skip = 0; | ||
let reportBatch; | ||
do { | ||
reportBatch = await DataSource.getReportsForContacts(db, [], contact._id, skip); | ||
|
||
for (const report of reportBatch) { | ||
JsDocs.deleteDoc(options, report); | ||
} | ||
|
||
skip += reportBatch.length; | ||
} while (reportBatch.length >= DataSource.BATCH_SIZE); | ||
|
||
return skip; | ||
} | ||
|
||
module.exports = deleteHierarchy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.