From d44c2599996accb77199ead8ad62524a0e6a98fe Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 10 Apr 2024 10:50:45 +0700 Subject: [PATCH] feat: add attachment to user controller --- src/controllers/user-controller.ts | 97 ++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/src/controllers/user-controller.ts b/src/controllers/user-controller.ts index 2fefb3a..5816ba2 100644 --- a/src/controllers/user-controller.ts +++ b/src/controllers/user-controller.ts @@ -379,6 +379,22 @@ export class UserController extends Controller { forceDelete: true, }); + new Promise((resolve, reject) => { + const item: string[] = []; + + const stream = minio.listObjectsV2(MINIO_BUCKET, `${attachmentLocation(userId)}/`); + + stream.on("data", (v) => v && v.name && item.push(v.name)); + stream.on("end", () => resolve(item)); + stream.on("error", () => reject(new Error("MinIO error."))); + }).then((list) => { + list.map(async (v) => { + await minio.removeObject(MINIO_BUCKET, `${attachmentLocation(userId)}/${v}`, { + forceDelete: true, + }); + }); + }); + return await prisma.user.delete({ include: { province: true, @@ -389,3 +405,84 @@ export class UserController extends Controller { }); } } + +function attachmentLocation(uid: string) { + return `user-attachment/${uid}`; +} + +@Route("api/user/{userId}/attachment") +@Tags("User") +@Security("keycloak") +export class UserAttachmentController extends Controller { + @Get() + async listAttachment(@Path() userId: string) { + const record = await prisma.user.findFirst({ + include: { + province: true, + district: true, + subDistrict: true, + }, + where: { id: userId }, + }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "data_not_found"); + } + + const list = await new Promise((resolve, reject) => { + const item: string[] = []; + + const stream = minio.listObjectsV2(MINIO_BUCKET, `${attachmentLocation(userId)}/`); + + stream.on("data", (v) => v && v.name && item.push(v.name)); + stream.on("end", () => resolve(item)); + stream.on("error", () => reject(new Error("MinIO error."))); + }); + + return await Promise.all( + list.map(async (v) => ({ + name: v.split("/").at(-1) as string, + url: await minio.presignedGetObject(MINIO_BUCKET, v, 12 * 60 * 60), + })), + ); + } + + @Post() + async addAttachment(@Path() userId: string, @Body() payload: { file: string[] }) { + const record = await prisma.user.findFirst({ + include: { + province: true, + district: true, + subDistrict: true, + }, + where: { id: userId }, + }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "data_not_found"); + } + + return await Promise.all( + payload.file.map(async (v) => ({ + name: v, + url: await minio.presignedGetObject(MINIO_BUCKET, `${attachmentLocation(userId)}/${v}`), + uploadUrl: await minio.presignedPutObject( + MINIO_BUCKET, + `${attachmentLocation(userId)}/${v}`, + 12 * 60 * 60, + ), + })), + ); + } + + @Delete() + async deleteAttachment(@Path() userId: string, @Body() payload: { file: string[] }) { + await Promise.all( + payload.file.map(async (v) => { + await minio.removeObject(MINIO_BUCKET, `${attachmentLocation(userId)}/${v}`, { + forceDelete: true, + }); + }), + ); + } +}