diff --git a/app/src/forms/file/controller.js b/app/src/forms/file/controller.js index 517c1e6ea..5390fb257 100644 --- a/app/src/forms/file/controller.js +++ b/app/src/forms/file/controller.js @@ -61,4 +61,13 @@ module.exports = { next(error); } }, + + tempfix: async (req, res, next) => { + try { + await service.tempfix(req.params.submissionId); + res.sendStatus(200); + } catch (error) { + next(error); + } + }, }; diff --git a/app/src/forms/file/routes.js b/app/src/forms/file/routes.js index 95e21c232..f152ded67 100644 --- a/app/src/forms/file/routes.js +++ b/app/src/forms/file/routes.js @@ -24,4 +24,11 @@ routes.delete('/:fileId', currentFileRecord, hasFilePermissions([P.SUBMISSION_UP await controller.delete(req, res, next); }); +// FORMS-1138: Add a temporary route that can be called with a submission ID to +// fix any files that are stuck in the "uploads" directory. This will be removed +// once all the files have been moved into the proper submissions directory. +routes.post('/tempfix/:submissionId', async (req, res, next) => { + await controller.tempfix(req, res, next); +}); + module.exports = routes; diff --git a/app/src/forms/file/service.js b/app/src/forms/file/service.js index 73f6e5af1..77c481203 100644 --- a/app/src/forms/file/service.js +++ b/app/src/forms/file/service.js @@ -112,6 +112,38 @@ const service = { throw err; } }, + + // This is moveSubmissionFiles with the restriction that it only moves files + // in the chefs/prod/uploads directory. + tempfix: async (submissionId) => { + let trx; + try { + trx = await FileStorage.startTransaction(); + + // fetch all the File Storage records for a submission id + // move them to permanent storage + // update their new paths. + const items = await FileStorage.query(trx).where('formSubmissionId', submissionId); + + for (const item of items) { + if (item.path.startsWith('chefs/prod/uploads')) { + // move the files under a sub directory for this submission + const newPath = await storageService.move(item, 'submissions', submissionId); + if (!newPath) { + throw new Error('Error moving files for submission'); + } + await FileStorage.query(trx).patchAndFetchById(item.id, { + storage: PERMANENT_STORAGE, + path: newPath, + }); + } + } + await trx.commit(); + } catch (err) { + if (trx) await trx.rollback(); + throw err; + } + }, }; module.exports = service;