Skip to content

Commit

Permalink
Fixing how the cli script runs
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Sep 11, 2024
1 parent 547c406 commit 0ac8136
Show file tree
Hide file tree
Showing 5 changed files with 662 additions and 511 deletions.
10 changes: 3 additions & 7 deletions lib/bin/purge.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
// (that has already been marked deleted).

const { run } = require('../task/task');
const { purgeForms, purgeSubmissions, purgeUnattachedBlobs } = require('../task/purge');
const { purgeTask } = require('../task/purge');

const { program } = require('commander');
program.option('-f, --force', 'Force any soft-deleted form to be purged right away.');
program.option('-m, --mode <value>', 'Mode of purging. Can be "forms", "submissions", or "all". Default is "all".', 'all');
program.option('-i, --formId <integer>', 'Purge a specific form based on its id.', parseInt);
program.option('-p, --projectId <integer>', 'Restrict purging to a specific project.', parseInt);
program.option('-x, --xmlFormId <value>', 'Restrict purging to specific form based on xmlFormId (must be used with projectId).');
Expand All @@ -30,9 +31,4 @@ program.parse();

const options = program.opts();

const purgeTasks = options.instanceId
? purgeSubmissions(options.force, options.projectId, options.xmlFormId, options.submissionId)
: purgeForms(options.force, options.formId, options.projectId, options.xmlFormId);

run(purgeTasks
.then((message) => purgeUnattachedBlobs().then(() => message)));
run(purgeTask(options));
15 changes: 12 additions & 3 deletions lib/model/query/submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ const DAY_RANGE = config.has('default.taskSchedule.purge')
? config.get('default.taskSchedule.purge')
: 30; // Default is 30 days


// Submission purging and the trash filter can target a specific submission
// or all deleted submissions, but can't be used to filter-purge by project or form.
const _trashedFilter = (force, projectId, xmlFormId, instanceId) => {
const idFilter = ((instanceId != null) && (projectId != null) && (xmlFormId != null)
? sql`and submissions."instanceId" = ${instanceId}
Expand All @@ -411,20 +414,25 @@ const _trashedFilter = (force, projectId, xmlFormId, instanceId) => {
: sql`submissions."deletedAt" < current_date - cast(${DAY_RANGE} as int) ${idFilter}`);
};

const purge = (force = false, projectId = null, xmlFormId = null, instanceId = null) => ({ oneFirst }) =>
oneFirst(sql`
const purge = (force = false, projectId = null, xmlFormId = null, instanceId = null) => ({ oneFirst }) => {
if ((instanceId != null || projectId != null || xmlFormId != null) && !(instanceId != null && projectId != null && xmlFormId != null)) {
throw Problem.internal.unknown({ error: 'Must specify either all or none of projectId, xmlFormId, and instanceId' });
}
return oneFirst(sql`
with redacted_audits as (
update audits set notes = ''
from submissions
join forms on submissions."formId" = forms.id
where (audits.details->>'submissionId')::int = submissions.id
and ${_trashedFilter(force, projectId, xmlFormId, instanceId)}
), deleted_client_audits as (
delete from client_audits
using submission_attachments, submission_defs, submissions
using submission_attachments, submission_defs, submissions, forms
where client_audits."blobId" = submission_attachments."blobId"
and submission_attachments."submissionDefId" = submission_defs.id
and submission_attachments."isClientAudit" = true
and submission_defs."submissionId" = submissions.id
and submissions."formId" = forms.id
and ${_trashedFilter(force, projectId, xmlFormId, instanceId)}
), purge_audits as (
insert into audits ("action", "loggedAt", "processed")
Expand All @@ -437,6 +445,7 @@ with redacted_audits as (
returning submissions.*
)
select count(*) from deleted_submissions`);
};

module.exports = {
createNew, createVersion,
Expand Down
30 changes: 21 additions & 9 deletions lib/task/purge.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@

const { task } = require('./task');

const purgeForms = task.withContainer(({ Forms }) => (force = false, formId = null, projectId = null, xmlFormId = null) =>
Forms.purge(force, formId, projectId, xmlFormId)
.then((count) => `Forms purged: ${count}`));
const purgeTask = task.withContainer((container) => (options = {}) => container.transacting(async ({ Blobs, Forms, Submissions }) => {
let message;

const purgeSubmissions = task.withContainer(({ Submissions }) => async (force = false, projectId = null, xmlFormId = null, submissionId = null) => {
const count = await Submissions.purge(force, projectId, xmlFormId, submissionId);
return `Submissions purged: ${count}`;
});
try {
if (options.mode === 'submissions' || options.instanceId) {
const count = await Submissions.purge(options.force, options.projectId, options.xmlFormId, options.instanceId);
message = `Submissions purged: ${count}`;
} else if (options.mode === 'forms' || (options.formId || options.xmlFormId)) {
const count = await Forms.purge(options.force, options.formId, options.projectId, options.xmlFormId);
message = `Forms purged: ${count}`;
} else {
const formCount = await Forms.purge(options.force, options.formId, options.projectId, options.xmlFormId);
const submissionCount = await Submissions.purge(options.force, options.projectId, options.xmlFormId, options.instanceId);
message = `Forms purged: ${formCount}, Submissions purged: ${submissionCount}`;
}
} catch (error) {
return error.problemDetails.error;
}

const purgeUnattachedBlobs = task.withContainer(({ Blobs }) => () => Blobs.purgeUnattached());
await Blobs.purgeUnattached();
return message;
}));

module.exports = { purgeForms, purgeSubmissions, purgeUnattachedBlobs };
module.exports = { purgeTask };
Loading

0 comments on commit 0ac8136

Please sign in to comment.