diff --git a/backend/routes/api/patients.js b/backend/routes/api/patients.js index 886dc695..b5cb3d38 100644 --- a/backend/routes/api/patients.js +++ b/backend/routes/api/patients.js @@ -169,6 +169,28 @@ router.delete( }), ); +const modifyFileName = async (filename, data) => { + var updatedFileName = filename; + var file = ''; + var suffix = ''; + if (filename.includes('.')) { + file = filename.split('.')[0]; + suffix = '.' + filename.split('.')[1]; + } else { + file = filename; + } + if (data.filter((e) => e.filename === filename).length > 0) { + var numPrev = 1; + while ( + data.filter((e) => e.filename === file + '_' + numPrev + suffix) + .length > 0 + ) + numPrev += 1; + updatedFileName = file + '_' + numPrev + suffix; + } + return updatedFileName; +}; + // POST: upload individual files router.post( '/:id/files/:stepKey/:fieldKey/:fileName', @@ -211,8 +233,12 @@ router.post( if (err) { res.json(err); } else { + const modifiedFileName = await modifyFileName( + fileName, + stepData[fieldKey], + ); stepData[fieldKey].push({ - filename: fileName, + filename: modifiedFileName, uploadedBy: req.user.Username, uploadDate: Date.now(), }); @@ -232,13 +258,14 @@ router.post( success: true, message: 'File successfully uploaded', data: { - name: fileName, + name: modifiedFileName, uploadedBy: req.user.Username, uploadDate: Date.now(), mimetype: file.mimetype, size: file.size, }, }); + /*console.log(res.data)*/ } }, );