Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: FORMS-1138 temp route to move uploads files #1523

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "common-hosted-form-service-app",
"main": "app.js",
"version": "1.5.0",
"private": true,
"license": "Apache-2.0",
Expand Down
9 changes: 9 additions & 0 deletions app/src/forms/file/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},
};
7 changes: 7 additions & 0 deletions app/src/forms/file/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
32 changes: 32 additions & 0 deletions app/src/forms/file/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;