-
-
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.
- Loading branch information
1 parent
0a9db49
commit 956c092
Showing
6 changed files
with
652 additions
and
484 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,59 @@ | ||
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, | ||
}; | ||
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.ids || args.id || '') | ||
.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, | ||
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 -- --ids=<id1>,<id2> | ||
${bold('OPTIONS')} | ||
--ids=<id1>,<id2> | ||
A comma delimited list of ids of contacts to be deleted. | ||
--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,42 @@ | ||
const DataSource = require('./hierarchy-data-source'); | ||
const JsDocs = require('./jsdocFolder'); | ||
const { trace, info } = require('../log'); | ||
|
||
const prettyPrintDocument = doc => `'${doc.name}' (${doc._id})`; | ||
async function deleteHierarchy(db, options, sourceIds) { | ||
console.log(db, options, sourceIds); | ||
const sourceDocs = await DataSource.getContactsByIds(db, sourceIds); | ||
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) { | ||
JsDocs.deleteDoc(options, descendant); | ||
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 + reportBatch.length; | ||
} | ||
|
||
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
Oops, something went wrong.