diff --git a/Prototype/server/src/controllers/fileController.ts b/Prototype/server/src/controllers/fileController.ts index 7999145..fb47fa4 100644 --- a/Prototype/server/src/controllers/fileController.ts +++ b/Prototype/server/src/controllers/fileController.ts @@ -1,5 +1,6 @@ import { Controller, + Delete, FormField, Get, Path, @@ -165,4 +166,44 @@ export class FileController extends Controller { return records; } + + @Delete("/{cabinetName}/drawer/{drawerName}/folder/{folderName}/file/{fileName}") + @Tags("File") + @SuccessResponse(HttpStatusCode.OK) + public async deleteFile( + @Path() cabinetName: string, + @Path() drawerName: string, + @Path() folderName: string, + @Path() fileName: string, + ) { + const search = await esClient.search< + EhrFile & { + attachment: Record; + } + >({ + index: "ehr-api-client", + query: { + match: { + pathname: `${cabinetName}/${drawerName}/${folderName}/${fileName}`, + }, + }, + }); + + if (search && search.hits.hits.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "Not found"); + } + + const esResult = await esClient + .delete({ + index: "ehr-api-client", + id: search.hits.hits[0]._id, + }) + .catch((e) => console.error(e)); + + if (!esResult) throw new Error("An error occured, cannot perform this action."); + + await minioClient.removeObject("ehr", `${cabinetName}/${drawerName}/${folderName}/${fileName}`); + + return this.setStatus(HttpStatusCode.NO_CONTENT); + } } diff --git a/Prototype/server/src/routes.ts b/Prototype/server/src/routes.ts index 14d6579..3c9b377 100644 --- a/Prototype/server/src/routes.ts +++ b/Prototype/server/src/routes.ts @@ -317,7 +317,7 @@ export function RegisterRoutes(app: Router) { } }); // WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa - app.get('/cabinet/:cabinetName/drawer/:drawerName/folder/:folderName/file/:fileName', + app.delete('/cabinet/:cabinetName/drawer/:drawerName/folder/:folderName/file/:fileName', ...(fetchMiddlewares(FileController)), ...(fetchMiddlewares(FileController.prototype.deleteFile)), diff --git a/Prototype/server/src/swagger.json b/Prototype/server/src/swagger.json index fa19899..0878d45 100644 --- a/Prototype/server/src/swagger.json +++ b/Prototype/server/src/swagger.json @@ -569,7 +569,7 @@ } }, "/cabinet/{cabinetName}/drawer/{drawerName}/folder/{folderName}/file/{fileName}": { - "get": { + "delete": { "operationId": "DeleteFile", "responses": { "200": {