Skip to content

Commit

Permalink
fix: FORMS-1138 temp route to move uploads files
Browse files Browse the repository at this point in the history
A bug left uploaded files in the "uploads" directory in the object storage, when they should have been moved to the "submissions" directory. This temporary route will move the files for a given submission ID.
  • Loading branch information
WalterMoar committed Oct 23, 2024
1 parent 75daa29 commit 870f561
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
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;

0 comments on commit 870f561

Please sign in to comment.