feat: map image

This commit is contained in:
Methapon Metanipat 2024-08-29 09:42:33 +07:00
parent d11fb007b3
commit 35600b9d7e

View file

@ -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),
);
}
}