From 0e411ad5d0a64ad5804ea32f939c1dd1f839ff10 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 31 Jul 2024 15:25:05 +0700 Subject: [PATCH] feat(customer): image endpoint get or upload --- src/controllers/customer-controller.ts | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/controllers/customer-controller.ts b/src/controllers/customer-controller.ts index ca10da2..98b229c 100644 --- a/src/controllers/customer-controller.ts +++ b/src/controllers/customer-controller.ts @@ -573,4 +573,33 @@ export class CustomerController extends Controller { return v; }); } + + @Get("{customerId}/image") + async getCustomerImageById(@Request() req: RequestWithUser, @Path() customerId: string) { + const url = await presignedGetObjectIfExist(MINIO_BUCKET, imageLocation(customerId), 60 * 60); + + if (!url) { + throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound"); + } + + return req.res?.redirect(url); + } + + @Put("{customerId}/image") + @Security("keycloak", MANAGE_ROLES) + async setCustomerImageById(@Request() req: RequestWithUser, @Path() customerId: string) { + const record = await prisma.customer.findFirst({ + where: { + id: customerId, + }, + }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "customerNotFound"); + } + + return req.res?.redirect( + await minio.presignedPutObject(MINIO_BUCKET, imageLocation(customerId), 12 * 60 * 60), + ); + } }