2023-11-17 16:43:07 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Request,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
SuccessResponse,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
2023-11-17 14:43:23 +07:00
|
|
|
import * as Minio from "minio";
|
|
|
|
|
import minioClient from "../storage";
|
|
|
|
|
|
|
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
2023-11-23 10:34:26 +07:00
|
|
|
import { listFolder, listItem, pathExist, replaceIllegalChars } from "../utils/minio";
|
|
|
|
|
import esClient from "../elasticsearch";
|
|
|
|
|
import { EhrFile, EhrFolder } from "../interfaces/ehr-fs";
|
2023-11-17 14:43:23 +07:00
|
|
|
|
2023-11-21 09:24:46 +07:00
|
|
|
@Route("/cabinet/{cabinetName}/drawer")
|
2023-11-17 14:43:23 +07:00
|
|
|
export class DrawerController extends Controller {
|
2023-11-21 09:24:46 +07:00
|
|
|
@Get("/")
|
2023-11-17 14:43:23 +07:00
|
|
|
@Tags("Drawer")
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK)
|
2023-11-23 10:34:26 +07:00
|
|
|
public async listDrawer(@Path() cabinetName: string): Promise<EhrFolder[]> {
|
|
|
|
|
const fullpath = [cabinetName, ""].join("/");
|
|
|
|
|
|
|
|
|
|
if (!(await pathExist(fullpath))) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "Provided path does not exist.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return listFolder(fullpath);
|
2023-11-17 14:43:23 +07:00
|
|
|
}
|
|
|
|
|
|
2023-11-21 09:24:46 +07:00
|
|
|
@Post("/")
|
2023-11-17 14:43:23 +07:00
|
|
|
@Tags("Drawer")
|
2023-11-17 16:43:07 +07:00
|
|
|
@Security("bearerAuth")
|
2023-11-17 14:43:23 +07:00
|
|
|
@SuccessResponse(HttpStatusCode.CREATED)
|
2023-11-17 16:43:07 +07:00
|
|
|
public async createDrawer(
|
|
|
|
|
@Request() request: { user: { preferred_username: string } },
|
|
|
|
|
@Path() cabinetName: string,
|
|
|
|
|
@Body() body: { name: string },
|
|
|
|
|
) {
|
2023-11-17 14:43:23 +07:00
|
|
|
if (!(await pathExist(`${cabinetName}/`))) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.PRECONDITION_FAILED, "Cabinet cannot be found.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const uploaded = await minioClient
|
2023-11-21 10:01:57 +07:00
|
|
|
.putObject("ehr", `${cabinetName}/${replaceIllegalChars(body.name)}/.keep`, "", 0, {
|
2023-11-17 14:43:23 +07:00
|
|
|
createdAt: new Date().toISOString(),
|
2023-11-17 16:43:07 +07:00
|
|
|
createdBy: request.user.preferred_username,
|
2023-11-17 14:43:23 +07:00
|
|
|
})
|
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
|
|
|
|
|
|
if (!uploaded) {
|
|
|
|
|
throw new Error("Object storage error occured.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.setStatus(HttpStatusCode.CREATED);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 09:24:46 +07:00
|
|
|
@Put("/{drawerName}")
|
2023-11-17 14:43:23 +07:00
|
|
|
@Tags("Drawer")
|
2023-11-17 16:43:07 +07:00
|
|
|
@Security("bearerAuth")
|
2023-11-17 14:43:23 +07:00
|
|
|
@SuccessResponse(HttpStatusCode.NO_CONTENT)
|
|
|
|
|
public async editDrawer(
|
|
|
|
|
@Path() cabinetName: string,
|
|
|
|
|
@Path() drawerName: string,
|
|
|
|
|
@Body() body: { name: string },
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const fullpath = `${cabinetName}/${drawerName}/`;
|
|
|
|
|
|
2023-11-23 10:34:26 +07:00
|
|
|
const list = await listItem(fullpath, true);
|
2023-11-17 14:43:23 +07:00
|
|
|
|
2023-11-23 10:34:26 +07:00
|
|
|
const cond = new Minio.CopyConditions();
|
2023-11-17 14:43:23 +07:00
|
|
|
|
2023-11-23 10:34:26 +07:00
|
|
|
await Promise.all(
|
|
|
|
|
list.map(async (current) => {
|
|
|
|
|
if (!current.name) return;
|
2023-11-17 14:43:23 +07:00
|
|
|
|
2023-11-23 10:34:26 +07:00
|
|
|
const destination = `${cabinetName}/${replaceIllegalChars(body.name)}/${current.name.slice(
|
2023-11-21 10:01:57 +07:00
|
|
|
fullpath.length,
|
|
|
|
|
)}`;
|
2023-11-23 10:34:26 +07:00
|
|
|
const source = `/ehr/${current.name}`;
|
|
|
|
|
|
|
|
|
|
return await minioClient
|
|
|
|
|
.copyObject("ehr", destination, source, cond)
|
|
|
|
|
.then(async () => {
|
|
|
|
|
if (!current.name) return;
|
|
|
|
|
|
|
|
|
|
await minioClient.removeObject("ehr", current.name);
|
|
|
|
|
|
|
|
|
|
if (current.name.includes(".keep")) return;
|
|
|
|
|
|
|
|
|
|
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
|
|
|
|
index: process.env.ELASTICSEARCH_INDEX ?? "ehr-index",
|
|
|
|
|
query: {
|
|
|
|
|
match: {
|
|
|
|
|
pathname: current.name,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (search && search.hits.hits.length === 0) {
|
|
|
|
|
throw new Error("Data cannot be found in database.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data = search.hits.hits[0];
|
|
|
|
|
|
|
|
|
|
await esClient.update({
|
|
|
|
|
index: process.env.ELASTICSEARCH_INDEX ?? "ehr-index",
|
|
|
|
|
id: data._id,
|
|
|
|
|
doc: { pathname: destination },
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
console.error(e);
|
|
|
|
|
throw new Error("Failed to move.");
|
|
|
|
|
});
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return this.setStatus(HttpStatusCode.NO_CONTENT);
|
2023-11-17 14:43:23 +07:00
|
|
|
}
|
|
|
|
|
|
2023-11-21 09:24:46 +07:00
|
|
|
@Delete("/{drawerName}")
|
2023-11-17 14:43:23 +07:00
|
|
|
@Tags("Drawer")
|
2023-11-17 16:43:07 +07:00
|
|
|
@Security("bearerAuth")
|
2023-11-17 14:43:23 +07:00
|
|
|
@SuccessResponse(HttpStatusCode.NO_CONTENT)
|
|
|
|
|
public async deleteDrawer(@Path() cabinetName: string, @Path() drawerName: string) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const objects: string[] = [];
|
|
|
|
|
const stream = minioClient.listObjectsV2("ehr", `${cabinetName}/${drawerName}/`, true);
|
|
|
|
|
|
|
|
|
|
stream.on("data", (v) => {
|
|
|
|
|
if (!(v && v.name)) return;
|
|
|
|
|
|
|
|
|
|
objects.push(v.name);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
stream.on("close", () => minioClient.removeObjects("ehr", objects));
|
|
|
|
|
stream.on("error", () => reject(new Error("Object storage error occured.")));
|
|
|
|
|
|
|
|
|
|
resolve(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|