refactor: organize file
This commit is contained in:
parent
e141ea330a
commit
2af4e750b0
19 changed files with 0 additions and 0 deletions
762
src/controllers/01-branch-controller.ts
Normal file
762
src/controllers/01-branch-controller.ts
Normal file
|
|
@ -0,0 +1,762 @@
|
|||
import { Prisma, Status, UserType } from "@prisma/client";
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Put,
|
||||
Path,
|
||||
Post,
|
||||
Query,
|
||||
Request,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
} from "tsoa";
|
||||
|
||||
import prisma from "../db";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import { RequestWithUser } from "../interfaces/user";
|
||||
import minio, { presignedGetObjectIfExist } from "../services/minio";
|
||||
import { isSystem } from "../utils/keycloak";
|
||||
|
||||
if (!process.env.MINIO_BUCKET) {
|
||||
throw Error("Require MinIO bucket.");
|
||||
}
|
||||
|
||||
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
||||
const MANAGE_ROLES = ["system", "head_of_admin"];
|
||||
|
||||
function globalAllow(user: RequestWithUser["user"]) {
|
||||
return MANAGE_ROLES.some((v) => user.roles?.includes(v));
|
||||
}
|
||||
|
||||
type BranchCreate = {
|
||||
status?: Status;
|
||||
code: string;
|
||||
taxNo: string;
|
||||
nameEN: string;
|
||||
name: string;
|
||||
addressEN: string;
|
||||
address: string;
|
||||
zipCode: string;
|
||||
email: string;
|
||||
contactName?: string | null;
|
||||
webUrl?: string | null;
|
||||
contact?: string | string[] | null;
|
||||
telephoneNo: string;
|
||||
lineId?: string | null;
|
||||
longitude: string;
|
||||
latitude: string;
|
||||
virtual?: boolean;
|
||||
|
||||
bank?: {
|
||||
bankName: string;
|
||||
bankBranch: string;
|
||||
accountName: string;
|
||||
accountNumber: string;
|
||||
accountType: string;
|
||||
currentlyUse: boolean;
|
||||
}[];
|
||||
|
||||
subDistrictId?: string | null;
|
||||
districtId?: string | null;
|
||||
provinceId?: string | null;
|
||||
headOfficeId?: string | null;
|
||||
};
|
||||
|
||||
type BranchUpdate = {
|
||||
status?: "ACTIVE" | "INACTIVE";
|
||||
taxNo?: string;
|
||||
nameEN?: string;
|
||||
name?: string;
|
||||
addressEN?: string;
|
||||
address?: string;
|
||||
zipCode?: string;
|
||||
email?: string;
|
||||
telephoneNo?: string;
|
||||
contactName?: string;
|
||||
webUrl?: string | null;
|
||||
contact?: string | string[] | null;
|
||||
lineId?: string;
|
||||
longitude?: string;
|
||||
latitude?: string;
|
||||
virtual?: boolean;
|
||||
|
||||
subDistrictId?: string | null;
|
||||
districtId?: string | null;
|
||||
provinceId?: string | null;
|
||||
headOfficeId?: string | null;
|
||||
|
||||
bank?: {
|
||||
bankName: string;
|
||||
bankBranch: string;
|
||||
accountName: string;
|
||||
accountNumber: string;
|
||||
accountType: string;
|
||||
currentlyUse: boolean;
|
||||
}[];
|
||||
};
|
||||
|
||||
function lineImageLoc(id: string) {
|
||||
return `branch/line-qr-${id}`;
|
||||
}
|
||||
|
||||
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 {
|
||||
@Get("stats")
|
||||
@Security("keycloak")
|
||||
async getStats(@Request() req: RequestWithUser) {
|
||||
const list = await prisma.branch.groupBy({
|
||||
_count: true,
|
||||
by: "isHeadOffice",
|
||||
where: {
|
||||
AND: isSystem(req.user)
|
||||
? undefined
|
||||
: [
|
||||
{
|
||||
user: { some: { userId: req.user.sub } },
|
||||
},
|
||||
{
|
||||
headOffice: globalAllow(req.user)
|
||||
? { user: { some: { userId: req.user.sub } } }
|
||||
: undefined,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
return list.reduce<Record<"hq" | "br", number>>(
|
||||
(a, c) => {
|
||||
a[c.isHeadOffice ? "hq" : "br"] = c._count;
|
||||
return a;
|
||||
},
|
||||
{ hq: 0, br: 0 },
|
||||
);
|
||||
}
|
||||
|
||||
@Get("user-stats")
|
||||
@Security("keycloak")
|
||||
async getUserStat(@Request() req: RequestWithUser, @Query() userType?: UserType) {
|
||||
const list = await prisma.branchUser.groupBy({
|
||||
_count: true,
|
||||
where: {
|
||||
userId: !MANAGE_ROLES.some((v) => req.user.roles?.includes(v)) ? req.user.sub : undefined,
|
||||
user: {
|
||||
userType,
|
||||
},
|
||||
},
|
||||
by: "branchId",
|
||||
});
|
||||
|
||||
const record = await prisma.branch.findMany({
|
||||
where: {
|
||||
user: !MANAGE_ROLES.some((v) => req.user.roles?.includes(v))
|
||||
? { some: { userId: req.user.sub } }
|
||||
: undefined,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
headOfficeId: true,
|
||||
isHeadOffice: true,
|
||||
nameEN: true,
|
||||
name: true,
|
||||
},
|
||||
orderBy: [{ isHeadOffice: "desc" }, { createdAt: "asc" }],
|
||||
});
|
||||
|
||||
const sort = record.reduce<(typeof record)[]>((acc, curr) => {
|
||||
for (const i of acc) {
|
||||
if (i[0].id === curr.headOfficeId) {
|
||||
i.push(curr);
|
||||
return acc;
|
||||
}
|
||||
}
|
||||
acc.push([curr]);
|
||||
return acc;
|
||||
}, []);
|
||||
|
||||
return sort.flat().map((a) =>
|
||||
Object.assign(a, {
|
||||
count: list.find((b) => b.branchId === a.id)?._count ?? 0,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@Security("keycloak")
|
||||
async getBranch(
|
||||
@Request() req: RequestWithUser,
|
||||
@Query() zipCode?: string,
|
||||
@Query() filter?: "head" | "sub",
|
||||
@Query() headOfficeId?: string,
|
||||
@Query() includeHead?: boolean,
|
||||
@Query() tree?: boolean,
|
||||
@Query() query: string = "",
|
||||
@Query() page: number = 1,
|
||||
@Query() pageSize: number = 30,
|
||||
) {
|
||||
const where = {
|
||||
AND: {
|
||||
zipCode,
|
||||
headOfficeId: headOfficeId ?? (filter === "head" || tree ? null : undefined),
|
||||
NOT: { headOfficeId: filter === "sub" && !headOfficeId ? null : undefined },
|
||||
OR: isSystem(req.user)
|
||||
? undefined
|
||||
: [
|
||||
{
|
||||
user: { some: { userId: req.user.sub } },
|
||||
},
|
||||
{
|
||||
headOffice: globalAllow(req.user)
|
||||
? { user: { some: { userId: req.user.sub } } }
|
||||
: undefined,
|
||||
},
|
||||
],
|
||||
},
|
||||
OR: [
|
||||
{ nameEN: { contains: query } },
|
||||
{ name: { contains: query } },
|
||||
{ email: { contains: query } },
|
||||
{ telephoneNo: { contains: query } },
|
||||
],
|
||||
} satisfies Prisma.BranchWhereInput;
|
||||
|
||||
const [result, total] = await prisma.$transaction([
|
||||
prisma.branch.findMany({
|
||||
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
|
||||
include: {
|
||||
province: true,
|
||||
district: true,
|
||||
subDistrict: true,
|
||||
contact: true,
|
||||
headOffice: includeHead
|
||||
? {
|
||||
include: {
|
||||
province: true,
|
||||
district: true,
|
||||
subDistrict: true,
|
||||
},
|
||||
}
|
||||
: false,
|
||||
branch: tree
|
||||
? {
|
||||
include: {
|
||||
province: true,
|
||||
district: true,
|
||||
subDistrict: true,
|
||||
},
|
||||
}
|
||||
: false,
|
||||
bank: true,
|
||||
_count: {
|
||||
select: { branch: true },
|
||||
},
|
||||
createdBy: true,
|
||||
updatedBy: true,
|
||||
},
|
||||
where,
|
||||
take: pageSize,
|
||||
skip: (page - 1) * pageSize,
|
||||
}),
|
||||
prisma.branch.count({ where }),
|
||||
]);
|
||||
|
||||
return { result, page, pageSize, total };
|
||||
}
|
||||
|
||||
@Get("{branchId}")
|
||||
@Security("keycloak")
|
||||
async getBranchById(
|
||||
@Path() branchId: string,
|
||||
@Query() includeSubBranch?: boolean,
|
||||
@Query() includeContact?: boolean,
|
||||
) {
|
||||
const record = await prisma.branch.findFirst({
|
||||
include: {
|
||||
province: true,
|
||||
district: true,
|
||||
subDistrict: true,
|
||||
createdBy: true,
|
||||
updatedBy: true,
|
||||
branch: includeSubBranch && {
|
||||
include: {
|
||||
province: true,
|
||||
district: true,
|
||||
subDistrict: true,
|
||||
},
|
||||
},
|
||||
bank: true,
|
||||
contact: includeContact,
|
||||
},
|
||||
where: { id: branchId },
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
|
||||
}
|
||||
|
||||
return Object.assign(record, {
|
||||
imageUrl: await minio.presignedGetObject(MINIO_BUCKET, branchImageLoc(record.id)),
|
||||
qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, lineImageLoc(record.id)),
|
||||
});
|
||||
}
|
||||
|
||||
@Post()
|
||||
@Security("keycloak", MANAGE_ROLES)
|
||||
async createBranch(@Request() req: RequestWithUser, @Body() body: BranchCreate) {
|
||||
const [province, district, subDistrict, head] = await prisma.$transaction([
|
||||
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
||||
prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
|
||||
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
|
||||
prisma.branch.findFirst({ where: { id: body.headOfficeId || undefined } }),
|
||||
]);
|
||||
if (body.provinceId && !province)
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Province cannot be found.",
|
||||
"relationProvinceNotFound",
|
||||
);
|
||||
if (body.districtId && !district)
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"District cannot be found.",
|
||||
"relationDistrictNotFound",
|
||||
);
|
||||
if (body.subDistrictId && !subDistrict)
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Sub-district cannot be found.",
|
||||
"relationSubDistrictNotFound",
|
||||
);
|
||||
if (body.headOfficeId && !head)
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Headquaters cannot be found.",
|
||||
"relationHQNotFound",
|
||||
);
|
||||
|
||||
const { provinceId, districtId, subDistrictId, headOfficeId, bank, contact, code, ...rest } =
|
||||
body;
|
||||
|
||||
if (headOfficeId && head && head.code.slice(0, -5) !== code) {
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Headoffice code not match with branch code",
|
||||
"codeMismatch",
|
||||
);
|
||||
}
|
||||
|
||||
const record = await prisma.$transaction(
|
||||
async (tx) => {
|
||||
const last = await tx.runningNo.upsert({
|
||||
where: {
|
||||
key: `MAIN_BRANCH_${code.toLocaleUpperCase()}`,
|
||||
},
|
||||
create: {
|
||||
key: `MAIN_BRANCH_${code.toLocaleUpperCase()}`,
|
||||
value: 1,
|
||||
},
|
||||
update: { value: { increment: 1 } },
|
||||
});
|
||||
|
||||
if (last.value === 1) {
|
||||
const exist = await tx.branch.findFirst({
|
||||
where: { code: `${code?.toLocaleUpperCase()}${`${last.value - 1}`.padStart(5, "0")}` },
|
||||
});
|
||||
if (exist)
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Branch with same code already exists.",
|
||||
"sameBranchCodeExists",
|
||||
);
|
||||
}
|
||||
|
||||
if (last.value !== 1 && !headOfficeId) {
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Branch with same code already exists.",
|
||||
"sameBranchCodeExists",
|
||||
);
|
||||
}
|
||||
|
||||
return await tx.branch.create({
|
||||
include: {
|
||||
province: true,
|
||||
district: true,
|
||||
subDistrict: true,
|
||||
createdBy: true,
|
||||
updatedBy: true,
|
||||
},
|
||||
data: {
|
||||
...rest,
|
||||
statusOrder: +(rest.status === "INACTIVE"),
|
||||
code: `${code?.toLocaleUpperCase()}${`${last.value - 1}`.padStart(5, "0")}`,
|
||||
bank: bank ? { createMany: { data: bank } } : undefined,
|
||||
isHeadOffice: !headOfficeId,
|
||||
contact: {
|
||||
create: (typeof contact === "string" ? [contact] : contact)?.map((v) => ({
|
||||
telephoneNo: v,
|
||||
})),
|
||||
},
|
||||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
||||
district: { connect: districtId ? { id: districtId } : undefined },
|
||||
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
|
||||
headOffice: { connect: headOfficeId ? { id: headOfficeId } : undefined },
|
||||
createdBy: { connect: { id: req.user.sub } },
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
});
|
||||
},
|
||||
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
|
||||
);
|
||||
|
||||
if (headOfficeId) {
|
||||
await prisma.branch.updateMany({
|
||||
where: { id: headOfficeId, status: Status.CREATED },
|
||||
data: { status: Status.ACTIVE },
|
||||
});
|
||||
}
|
||||
|
||||
this.setStatus(HttpStatus.CREATED);
|
||||
|
||||
return Object.assign(record, {
|
||||
contact: await prisma.branchContact.findMany({ where: { branchId: record.id } }),
|
||||
imageUrl: await minio.presignedGetObject(MINIO_BUCKET, branchImageLoc(record.id)),
|
||||
imageUploadUrl: await minio.presignedPutObject(
|
||||
MINIO_BUCKET,
|
||||
branchImageLoc(record.id),
|
||||
12 * 60 * 60,
|
||||
),
|
||||
qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, lineImageLoc(record.id)),
|
||||
qrCodeImageUploadUrl: await minio.presignedPutObject(
|
||||
MINIO_BUCKET,
|
||||
lineImageLoc(record.id),
|
||||
12 * 60 * 60,
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
@Put("{branchId}")
|
||||
@Security("keycloak", MANAGE_ROLES.concat("admin", "branch_manager"))
|
||||
async editBranch(
|
||||
@Request() req: RequestWithUser,
|
||||
@Body() body: BranchUpdate,
|
||||
@Path() branchId: string,
|
||||
) {
|
||||
if (body.headOfficeId === branchId)
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Cannot make this as headquaters and branch at the same time.",
|
||||
"cantMakeHQAndBranchSameTime",
|
||||
);
|
||||
|
||||
if (body.subDistrictId || body.districtId || body.provinceId || body.headOfficeId) {
|
||||
const [province, district, subDistrict, branch] = await prisma.$transaction([
|
||||
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
||||
prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
|
||||
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
|
||||
prisma.branch.findFirst({ where: { id: body.headOfficeId || undefined } }),
|
||||
]);
|
||||
if (body.provinceId && !province)
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Province cannot be found.",
|
||||
"relationProvinceNotFound",
|
||||
);
|
||||
if (body.districtId && !district)
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"District cannot be found.",
|
||||
"relationDistrictNotFound",
|
||||
);
|
||||
if (body.subDistrictId && !subDistrict)
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Sub-district cannot be found.",
|
||||
"relationSubDistrictNotFound",
|
||||
);
|
||||
if (body.headOfficeId && !branch)
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Headquaters cannot be found.",
|
||||
"relationHQNotFound",
|
||||
);
|
||||
}
|
||||
|
||||
const { provinceId, districtId, subDistrictId, headOfficeId, bank, contact, ...rest } = body;
|
||||
|
||||
const branch = await prisma.branch.findUnique({
|
||||
include: {
|
||||
user: { where: { userId: req.user.sub } },
|
||||
},
|
||||
where: { id: branchId },
|
||||
});
|
||||
|
||||
if (!branch) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
|
||||
}
|
||||
|
||||
if (
|
||||
!MANAGE_ROLES.some((v) => req.user.roles?.includes(v)) &&
|
||||
!branch?.user.find((v) => v.userId === req.user.sub)
|
||||
) {
|
||||
throw new HttpError(
|
||||
HttpStatus.FORBIDDEN,
|
||||
"You do not have permission to perform this action.",
|
||||
"noPermission",
|
||||
);
|
||||
}
|
||||
|
||||
const record = await prisma.branch.update({
|
||||
include: { province: true, district: true, subDistrict: true },
|
||||
data: {
|
||||
...rest,
|
||||
statusOrder: +(rest.status === "INACTIVE"),
|
||||
isHeadOffice: headOfficeId !== undefined ? headOfficeId === null : undefined,
|
||||
bank: bank ? { deleteMany: {}, createMany: { data: bank } } : undefined,
|
||||
province: {
|
||||
connect: provinceId ? { id: provinceId } : undefined,
|
||||
disconnect: provinceId === null || undefined,
|
||||
},
|
||||
district: {
|
||||
connect: districtId ? { id: districtId } : undefined,
|
||||
disconnect: districtId === null || undefined,
|
||||
},
|
||||
subDistrict: {
|
||||
connect: subDistrictId ? { id: subDistrictId } : undefined,
|
||||
disconnect: subDistrictId === null || undefined,
|
||||
},
|
||||
headOffice: {
|
||||
connect: headOfficeId ? { id: headOfficeId } : undefined,
|
||||
disconnect: headOfficeId === null || undefined,
|
||||
},
|
||||
contact: contact
|
||||
? {
|
||||
deleteMany: {},
|
||||
create: (typeof contact === "string" ? [contact] : contact)?.map((v) => ({
|
||||
telephoneNo: v,
|
||||
})),
|
||||
}
|
||||
: undefined,
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
where: { id: branchId },
|
||||
});
|
||||
|
||||
return Object.assign(record, {
|
||||
imageUrl: await minio.presignedGetObject(MINIO_BUCKET, branchImageLoc(record.id)),
|
||||
imageUploadUrl: await minio.presignedPutObject(
|
||||
MINIO_BUCKET,
|
||||
branchImageLoc(record.id),
|
||||
12 * 60 * 60,
|
||||
),
|
||||
qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, lineImageLoc(record.id)),
|
||||
qrCodeImageUploadUrl: await minio.presignedPutObject(
|
||||
MINIO_BUCKET,
|
||||
lineImageLoc(record.id),
|
||||
12 * 60 * 60,
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
@Delete("{branchId}")
|
||||
@Security("keycloak", MANAGE_ROLES)
|
||||
async deleteBranch(@Request() req: RequestWithUser, @Path() branchId: string) {
|
||||
const record = await prisma.branch.findFirst({
|
||||
include: {
|
||||
province: true,
|
||||
district: true,
|
||||
subDistrict: true,
|
||||
createdBy: true,
|
||||
updatedBy: true,
|
||||
user: { where: { userId: req.user.sub } },
|
||||
},
|
||||
where: { id: branchId },
|
||||
});
|
||||
|
||||
if (!MANAGE_ROLES.some((v) => req.user.roles?.includes(v))) {
|
||||
if (
|
||||
record?.createdByUserId !== req.user.sub &&
|
||||
!record?.user.find((v) => v.userId === req.user.sub)
|
||||
) {
|
||||
throw new HttpError(
|
||||
HttpStatus.FORBIDDEN,
|
||||
"You do not have permission to perform this action.",
|
||||
"noPermission",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
|
||||
}
|
||||
|
||||
if (record.status !== Status.CREATED) {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, "Branch is in used.", "branchInUsed");
|
||||
}
|
||||
|
||||
return await prisma.$transaction(async (tx) => {
|
||||
const data = await tx.branch.delete({
|
||||
include: {
|
||||
province: true,
|
||||
district: true,
|
||||
subDistrict: true,
|
||||
createdBy: true,
|
||||
updatedBy: true,
|
||||
},
|
||||
where: { id: branchId },
|
||||
});
|
||||
|
||||
if (record.isHeadOffice) {
|
||||
await tx.runningNo.delete({
|
||||
where: {
|
||||
key: `MAIN_BRANCH_${record.code.slice(0, -5)}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
await minio.removeObject(MINIO_BUCKET, lineImageLoc(branchId), {
|
||||
forceDelete: true,
|
||||
});
|
||||
await minio.removeObject(MINIO_BUCKET, branchImageLoc(branchId), {
|
||||
forceDelete: true,
|
||||
});
|
||||
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
@Get("{branchId}/line-image")
|
||||
async getLineImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
|
||||
const url = await presignedGetObjectIfExist(MINIO_BUCKET, lineImageLoc(branchId), 60 * 60);
|
||||
|
||||
if (!url) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
|
||||
}
|
||||
|
||||
return req.res?.redirect(url);
|
||||
}
|
||||
|
||||
@Put("{branchId}/line-image")
|
||||
@Security("keycloak", MANAGE_ROLES.concat("admin", "branch_manager"))
|
||||
async setLineImageByBranchId(@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 (
|
||||
!MANAGE_ROLES.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, lineImageLoc(record.id), 12 * 60 * 60),
|
||||
);
|
||||
}
|
||||
|
||||
@Get("{branchId}/branch-image")
|
||||
async getBranchImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
|
||||
const url = await presignedGetObjectIfExist(MINIO_BUCKET, branchImageLoc(branchId), 60 * 60);
|
||||
|
||||
if (!url) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
|
||||
}
|
||||
|
||||
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) {
|
||||
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 (
|
||||
!MANAGE_ROLES.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, 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", MANAGE_ROLES.concat("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 (
|
||||
!MANAGE_ROLES.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),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue