From 35600b9d7ee1167e7d3e11388026818b5cb1437e Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Thu, 29 Aug 2024 09:42:33 +0700 Subject: [PATCH] feat: map image --- src/controllers/branch-controller.ts | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/controllers/branch-controller.ts b/src/controllers/branch-controller.ts index 6de8699..916d437 100644 --- a/src/controllers/branch-controller.ts +++ b/src/controllers/branch-controller.ts @@ -97,6 +97,10 @@ function branchImageLoc(id: string) { return `branch/branch-img-${id}`; } +function mapImageLoc(id: string) { + return `branch/map-img-${id}`; +} + @Route("api/v1/branch") @Tags("Branch") export class BranchController extends Controller { @@ -669,4 +673,45 @@ export class BranchController extends Controller { await minio.presignedPutObject(MINIO_BUCKET, branchImageLoc(record.id), 12 * 60 * 60), ); } + + @Get("{branchId}/map-image") + async getMapImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { + const url = await presignedGetObjectIfExist(MINIO_BUCKET, mapImageLoc(branchId), 60 * 60); + + if (!url) { + throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound"); + } + + return req.res?.redirect(url); + } + + @Put("{branchId}/map-image") + @Security("keycloak", ["system", "head_of_admin", "admin", "branch_admin", "branch_manager"]) + async setMapImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { + const record = await prisma.branch.findUnique({ + include: { + user: { where: { userId: req.user.sub } }, + }, + where: { id: branchId }, + }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound"); + } + + if ( + !["system", "head_of_admin", "admin"].some((v) => req.user.roles?.includes(v)) && + !record?.user.find((v) => v.userId === req.user.sub) + ) { + throw new HttpError( + HttpStatus.FORBIDDEN, + "You do not have permission to perform this action.", + "noPermission", + ); + } + + return req.res?.redirect( + await minio.presignedPutObject(MINIO_BUCKET, mapImageLoc(record.id), 12 * 60 * 60), + ); + } }