2023-11-17 16:43:07 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
SuccessResponse,
|
|
|
|
|
Tags,
|
|
|
|
|
Request,
|
|
|
|
|
} from "tsoa";
|
2023-11-17 09:03:31 +07:00
|
|
|
import * as Minio from "minio";
|
|
|
|
|
import minioClient from "../storage";
|
|
|
|
|
|
2023-11-23 10:34:26 +07:00
|
|
|
import { EhrFile, EhrFolder } from "../interfaces/ehr-fs";
|
2023-11-17 09:03:31 +07:00
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
2023-11-23 10:34:26 +07:00
|
|
|
import { listFolder, listItem, replaceIllegalChars } from "../utils/minio";
|
|
|
|
|
import esClient from "../elasticsearch";
|
2023-11-17 09:03:31 +07:00
|
|
|
|
|
|
|
|
@Route("cabinet")
|
|
|
|
|
export class CabinetController extends Controller {
|
|
|
|
|
@Get("/")
|
|
|
|
|
@Tags("Cabinet")
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK)
|
2023-11-23 10:34:26 +07:00
|
|
|
public async listCabinet(): Promise<EhrFolder[]> {
|
|
|
|
|
const list = await listFolder().catch((e) => console.error(`Error List Folder: ${e}`));
|
|
|
|
|
|
|
|
|
|
if (!list) {
|
|
|
|
|
throw new Error("Error listing folder");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
2023-11-17 09:03:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post("/")
|
|
|
|
|
@Tags("Cabinet")
|
2023-11-17 16:43:07 +07:00
|
|
|
@Security("bearerAuth")
|
2023-11-17 09:03:31 +07:00
|
|
|
@SuccessResponse(HttpStatusCode.CREATED)
|
2023-11-17 16:43:07 +07:00
|
|
|
public async createCabinet(
|
|
|
|
|
@Request() request: { user: { preferred_username: string } },
|
|
|
|
|
@Body() body: { name: string },
|
|
|
|
|
) {
|
2023-11-17 09:03:31 +07:00
|
|
|
const uploaded = await minioClient
|
2023-11-21 10:01:57 +07:00
|
|
|
.putObject("ehr", `${replaceIllegalChars(body.name)}/.keep`, "", 0, {
|
2023-11-17 09:03:31 +07:00
|
|
|
createdAt: new Date().toISOString(),
|
2023-11-17 16:43:07 +07:00
|
|
|
createdBy: request.user.preferred_username,
|
2023-11-17 09:03:31 +07:00
|
|
|
})
|
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
|
|
|
|
|
|
if (!uploaded) throw new Error("Object storage error occured.");
|
|
|
|
|
|
|
|
|
|
return this.setStatus(HttpStatusCode.CREATED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Put("/{cabinetName}")
|
|
|
|
|
@Tags("Cabinet")
|
2023-11-17 16:43:07 +07:00
|
|
|
@Security("bearerAuth")
|
2023-11-17 09:03:31 +07:00
|
|
|
@SuccessResponse(HttpStatusCode.NO_CONTENT, "Success")
|
|
|
|
|
public async editCabinet(
|
|
|
|
|
@Path() cabinetName: string,
|
|
|
|
|
@Body() body: { name: string },
|
|
|
|
|
): Promise<void> {
|
2023-11-23 10:34:26 +07:00
|
|
|
const list = await listItem(`${cabinetName}/`, true);
|
2023-11-17 09:03:31 +07:00
|
|
|
|
2023-11-23 10:34:26 +07:00
|
|
|
const cond = new Minio.CopyConditions();
|
|
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
|
list.map(async (current) => {
|
|
|
|
|
if (!current.name) return;
|
2023-11-17 09:03:31 +07:00
|
|
|
|
2023-11-23 10:34:26 +07:00
|
|
|
const destination = `${replaceIllegalChars(body.name)}/${current.name.slice(
|
2023-11-21 10:01:57 +07:00
|
|
|
cabinetName.length + 1,
|
|
|
|
|
)}`;
|
2023-11-23 10:34:26 +07:00
|
|
|
const source = `/ehr/${current.name}`;
|
2023-11-17 09:03:31 +07:00
|
|
|
|
2023-11-23 10:34:26 +07:00
|
|
|
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 09:03:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("/{cabinetName}")
|
|
|
|
|
@Tags("Cabinet")
|
2023-11-17 16:43:07 +07:00
|
|
|
@Security("bearerAuth")
|
2023-11-17 09:03:31 +07:00
|
|
|
@SuccessResponse(HttpStatusCode.NO_CONTENT)
|
|
|
|
|
public async deleteCabinet(@Path() cabinetName: string) {
|
2023-11-24 11:32:04 +07:00
|
|
|
await new Promise<void>((resolve, reject) => {
|
2023-11-17 09:03:31 +07:00
|
|
|
const objects: string[] = [];
|
|
|
|
|
const stream = minioClient.listObjectsV2("ehr", `${cabinetName}/`, true);
|
|
|
|
|
|
|
|
|
|
stream.on("data", (v) => {
|
|
|
|
|
if (!(v && v.name)) return;
|
|
|
|
|
|
|
|
|
|
objects.push(v.name);
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-24 11:32:04 +07:00
|
|
|
stream.on("close", async () => {
|
|
|
|
|
minioClient.removeObjects("ehr", objects);
|
|
|
|
|
resolve();
|
|
|
|
|
});
|
2023-11-17 09:03:31 +07:00
|
|
|
stream.on("error", () => reject(new Error("Object storage error occured.")));
|
2023-11-24 11:32:04 +07:00
|
|
|
});
|
2023-11-17 09:03:31 +07:00
|
|
|
|
2023-11-24 11:32:04 +07:00
|
|
|
const searchResult = await esClient.search({
|
|
|
|
|
index: process.env.ELASTICSEARCH_INDEX ?? "ehr-index",
|
|
|
|
|
query: {
|
|
|
|
|
prefix: { pathname: `${cabinetName}/` },
|
|
|
|
|
},
|
2023-11-17 11:09:47 +07:00
|
|
|
});
|
2023-11-24 11:32:04 +07:00
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
|
searchResult.hits.hits.map(async (v) => {
|
|
|
|
|
return esClient
|
|
|
|
|
.delete({
|
|
|
|
|
index: process.env.ELASTICSEARCH_INDEX ?? "ehr-index",
|
|
|
|
|
id: v._id,
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => console.error(`ElasticSearch Error: ${e}`));
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return this.setStatus(HttpStatusCode.NO_CONTENT);
|
2023-11-17 11:09:47 +07:00
|
|
|
}
|
2023-11-17 09:03:31 +07:00
|
|
|
}
|