Skip to content

Commit 69074ec

Browse files
committed
Incremental file name logic
1 parent 2046791 commit 69074ec

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

backend/routes/api/patients.js

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ const _ = require('lodash');
77
const { errorWrap } = require('../../utils');
88
const { models } = require('../../models');
99
const {
10-
uploadFile, downloadFile, deleteFile, deleteFolder,
10+
uploadFile,
11+
downloadFile,
12+
deleteFile,
13+
deleteFolder,
1114
} = require('../../utils/aws/awsS3Helpers');
1215
const { removeRequestAttributes } = require('../../middleware/requests');
1316
const {
1417
STEP_IMMUTABLE_ATTRIBUTES,
1518
PATIENT_IMMUTABLE_ATTRIBUTES,
1619
} = require('../../utils/constants');
17-
const { sendResponse, getDataFromModelWithPaginationAndSearch } = require('../../utils/response');
20+
const {
21+
sendResponse,
22+
getDataFromModelWithPaginationAndSearch,
23+
} = require('../../utils/response');
1824
const { getReadableSteps } = require('../../utils/stepUtils');
1925
const { getStepBaseSchemaKeys } = require('../../utils/initDb');
2026
const {
@@ -29,7 +35,10 @@ const {
2935
router.get(
3036
'/',
3137
errorWrap(async (req, res) => {
32-
const patientData = await getDataFromModelWithPaginationAndSearch(req, models.Patient);
38+
const patientData = await getDataFromModelWithPaginationAndSearch(
39+
req,
40+
models.Patient,
41+
);
3342
await sendResponse(res, 200, '', patientData);
3443
}),
3544
);
@@ -231,9 +240,7 @@ router.delete(
231240
await patient.save();
232241

233242
// Remove this file from AWS as well
234-
await deleteFile(
235-
`${id}/${stepKey}/${fieldKey}/${fileName}`,
236-
);
243+
await deleteFile(`${id}/${stepKey}/${fieldKey}/${fileName}`);
237244

238245
return sendResponse(res, 200, 'File deleted');
239246
}),
@@ -277,14 +284,16 @@ router.post(
277284

278285
// Upload the file to the S3
279286
const file = req.files.uploadedFile;
280-
await uploadFile(
281-
file.data,
282-
`${id}/${stepKey}/${fieldKey}/${fileName}`,
287+
const modifiedFileName = await modifyFileName(
288+
fileName,
289+
stepData[fieldKey],
283290
);
284291

292+
await uploadFile(file.data, `${id}/${stepKey}/${fieldKey}/${fileName}`);
293+
285294
// Record this file in the DB
286295
stepData[fieldKey].push({
287-
filename: fileName,
296+
filename: modifiedFileName,
288297
uploadedBy: req.user.name,
289298
uploadDate: Date.now(),
290299
});
@@ -302,7 +311,7 @@ router.post(
302311

303312
// Send the response
304313
const respData = {
305-
name: fileName,
314+
name: modifiedFileName,
306315
uploadedBy: req.user.name,
307316
uploadDate: Date.now(),
308317
mimetype: file.mimetype,
@@ -374,7 +383,9 @@ router.delete(
374383
if (!patient) return sendResponse(res, 404, `Patient "${id}" not found`);
375384

376385
// Deletes the patient from the Patient Collection
377-
await models.Patient.findOneAndDelete({ _id: mongoose.Types.ObjectId(id) });
386+
await models.Patient.findOneAndDelete({
387+
_id: mongoose.Types.ObjectId(id),
388+
});
378389

379390
const allStepKeys = await models.Step.find({}, 'key');
380391

@@ -388,7 +399,9 @@ router.delete(
388399
await Model.findOneAndDelete({ patientId: id });
389400
} catch (error) {
390401
// eslint-disable-next-line no-console
391-
console.error(`DELETE /patients/:id - step ${stepKey} not found`);
402+
console.error(
403+
`DELETE /patients/:id - step ${stepKey} not found`,
404+
);
392405
return false;
393406
}
394407
return true;
@@ -420,4 +433,29 @@ const updatePatientStepData = async (patientId, StepModel, data) => {
420433
return patientStepData.save();
421434
};
422435

436+
const modifyFileName = async (filename, data) => {
437+
let updatedFileName = filename;
438+
let file = '';
439+
let suffix = '';
440+
if (filename.includes('.')) {
441+
const indexOfLastPeriod = filename.lastIndexOf('.');
442+
file = filename.substring(0, indexOfLastPeriod);
443+
suffix = `.${filename.substring(indexOfLastPeriod)}`;
444+
} else {
445+
file = filename;
446+
}
447+
if (data.filter((e) => e.filename === filename).length > 0) {
448+
// TODO change to some
449+
let inrcrementedNum = 0;
450+
for (
451+
let numPrev = 1;
452+
data.filter((e) => e.filename === `${file}_${numPrev}${suffix}`)
453+
.length > 0;
454+
numPrev++
455+
) inrcrementedNum = numPrev;
456+
updatedFileName = `${file}_${inrcrementedNum}${suffix}`;
457+
}
458+
return updatedFileName;
459+
};
460+
423461
module.exports = router;

0 commit comments

Comments
 (0)