From 2f73dd1634ab58dfb4a3a44835d4c94e357187cf Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Tue, 10 Sep 2024 10:02:07 +0700 Subject: [PATCH] feat: deprecate unused endpoints --- src/controllers/01-branch-controller.ts | 66 +------------------------ src/controllers/02-user-controller.ts | 41 ++------------- src/utils/minio.ts | 2 - 3 files changed, 5 insertions(+), 104 deletions(-) diff --git a/src/controllers/01-branch-controller.ts b/src/controllers/01-branch-controller.ts index af8725d..b141239 100644 --- a/src/controllers/01-branch-controller.ts +++ b/src/controllers/01-branch-controller.ts @@ -20,7 +20,7 @@ import HttpStatus from "../interfaces/http-status"; import { RequestWithUser } from "../interfaces/user"; import minio from "../services/minio"; import { isSystem } from "../utils/keycloak"; -import { deleteFile, fileLocation, listFile } from "../utils/minio"; +import { deleteFile, deleteFolder, fileLocation, listFile } from "../utils/minio"; import { createPermCheck } from "../services/permission"; import { filterStatus } from "../services/prisma"; @@ -653,15 +653,10 @@ export class BranchController extends Controller { } await Promise.all([ + deleteFolder(fileLocation.branch.img(branchId)), minio.removeObject(MINIO_BUCKET, fileLocation.branch.line(branchId), { forceDelete: true, }), - minio.removeObject(MINIO_BUCKET, fileLocation.branch.image(branchId), { - forceDelete: true, - }), - minio.removeObject(MINIO_BUCKET, fileLocation.branch.map(branchId), { - forceDelete: true, - }), ...data.bank.map(async (v) => { await minio.removeObject(MINIO_BUCKET, fileLocation.branch.bank(branchId, v.id), { forceDelete: true, @@ -704,63 +699,6 @@ export class BranchController extends Controller { await deleteFile(fileLocation.branch.line(branchId)); } - @Get("{branchId}/branch-image") - async getBranchImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { - const url = await minio.presignedGetObject( - MINIO_BUCKET, - fileLocation.branch.image(branchId), - 60 * 60, - ); - return req.res?.redirect(url); - } - - @Put("{branchId}/branch-image") - @Security("keycloak", MANAGE_ROLES.concat("admin", "branch_manager")) - async setBranchImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { - await permissionCheck(req.user, branchId); - return req.res?.redirect( - await minio.presignedPutObject( - MINIO_BUCKET, - fileLocation.branch.image(branchId), - 12 * 60 * 60, - ), - ); - } - - @Delete("{branchId}/branch-image") - @Security("keycloak", MANAGE_ROLES.concat("admin", "branch_manager")) - async deleteBranchImage(@Request() req: RequestWithUser, @Path() branchId: string) { - await permissionCheck(req.user, branchId); - await deleteFile(fileLocation.branch.image(branchId)); - } - - @Get("{branchId}/map-image") - async getMapImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { - const url = await minio.presignedGetObject( - MINIO_BUCKET, - fileLocation.branch.image(branchId), - 60 * 60, - ); - return req.res?.redirect(url); - } - - @Put("{branchId}/map-image") - @Security("keycloak", MANAGE_ROLES.concat("admin", "branch_manager")) - async setMapImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { - await permissionCheck(req.user, branchId); - - return req.res?.redirect( - await minio.presignedPutObject(MINIO_BUCKET, fileLocation.branch.map(branchId), 12 * 60 * 60), - ); - } - - @Delete("{branchId}/map-image") - @Security("keycloak", MANAGE_ROLES.concat("admin", "branch_manager")) - async deleteMapImage(@Request() req: RequestWithUser, @Path() branchId: string) { - await permissionCheck(req.user, branchId); - await deleteFile(fileLocation.branch.map(branchId)); - } - @Get("{branchId}/bank-qr/{bankId}") async getBankQRByBranchIdAndBankId( @Request() req: RequestWithUser, diff --git a/src/controllers/02-user-controller.ts b/src/controllers/02-user-controller.ts index 595671b..f04b7ce 100644 --- a/src/controllers/02-user-controller.ts +++ b/src/controllers/02-user-controller.ts @@ -15,7 +15,7 @@ import { import { Branch, Prisma, Status, User, UserType } from "@prisma/client"; import prisma from "../db"; -import minio from "../services/minio"; +import minio, { deleteFolder } from "../services/minio"; import { RequestWithUser } from "../interfaces/user"; import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; @@ -229,10 +229,6 @@ async function userBranchCodeGen(user: User, branch: Branch) { ); } -function imageLocation(id: string) { - return `user/profile-img-${id}`; -} - @Route("api/v1/user") @Tags("User") export class UserController extends Controller { @@ -872,24 +868,8 @@ export class UserController extends Controller { throw new HttpError(HttpStatus.FORBIDDEN, "User is in used.", "userInUsed"); } - await minio.removeObject(MINIO_BUCKET, imageLocation(userId), { - forceDelete: true, - }); - - new Promise((resolve, reject) => { - const item: string[] = []; - - const stream = minio.listObjectsV2(MINIO_BUCKET, fileLocation.user.attachment(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, v, { forceDelete: true }); - }); - }); - + await deleteFolder(MINIO_BUCKET, fileLocation.user.profile(userId)); + await deleteFolder(MINIO_BUCKET, fileLocation.user.attachment(userId)); await deleteUser(userId); return await prisma.user.delete({ @@ -903,21 +883,6 @@ export class UserController extends Controller { where: { id: userId }, }); } - - @Get("{userId}/image") - async getUserImageByUserId(@Request() req: RequestWithUser, @Path() userId: string) { - const url = await minio.presignedGetObject(MINIO_BUCKET, imageLocation(userId), 60 * 60); - return req.res?.redirect(url); - } - - @Put("{userId}/image") - @Security("keycloak", ["system", "head_of_admin", "admin", "branch_manager"]) - async setUserImageByUserId(@Request() req: RequestWithUser, @Path() userId: string) { - await permissionCheck(req.user, userId); - return req.res?.redirect( - await minio.presignedPutObject(MINIO_BUCKET, imageLocation(userId), 12 * 60 * 60), - ); - } } @Route("api/v1/user/{userId}/profile-image") diff --git a/src/utils/minio.ts b/src/utils/minio.ts index 9635e21..48f6a3a 100644 --- a/src/utils/minio.ts +++ b/src/utils/minio.ts @@ -41,8 +41,6 @@ export async function deleteFolder(path: string) { export const fileLocation = { branch: { line: (branchId: string) => `branch/line-qr-${branchId}`, - image: (branchId: string) => `branch/branch-img-${branchId}`, - map: (branchId: string) => `branch/map-img-${branchId}`, bank: (branchId: string, bankId: string) => `branch/bank-qr-${branchId}-${bankId}`, img: (branchId: string, name?: string) => `branch/img-${branchId}/${name || ""}`, },