jws-backend/src/controllers/01-branch-controller.ts

770 lines
23 KiB
TypeScript
Raw Normal View History

2024-04-09 15:13:44 +07:00
import { Prisma, Status, UserType } from "@prisma/client";
2024-04-02 09:28:36 +07:00
import {
Body,
Controller,
Delete,
Get,
2024-04-03 10:54:46 +07:00
Put,
2024-04-02 09:28:36 +07:00
Path,
Post,
Query,
Request,
Route,
Security,
Tags,
2024-10-22 10:43:20 +07:00
Head,
2024-04-02 09:28:36 +07:00
} from "tsoa";
2024-04-05 11:30:18 +07:00
import prisma from "../db";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import { RequestWithUser } from "../interfaces/user";
2024-10-22 10:43:20 +07:00
import {
deleteFile,
deleteFolder,
fileLocation,
getFile,
getPresigned,
listFile,
setFile,
} from "../utils/minio";
import {
branchRelationPermInclude,
createPermCheck,
createPermCondition,
} from "../services/permission";
2024-09-09 14:49:01 +07:00
import { filterStatus } from "../services/prisma";
import {
connectOrDisconnect,
connectOrNot,
queryOrNot,
whereAddressQuery,
} from "../utils/relation";
import { isUsedError, notFoundError, relationError } from "../utils/error";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
}
2024-09-03 09:47:58 +07:00
const MANAGE_ROLES = ["system", "head_of_admin"];
function globalAllow(user: RequestWithUser["user"]) {
return MANAGE_ROLES.some((v) => user.roles?.includes(v));
}
function globalAllowView(user: RequestWithUser["user"]) {
return MANAGE_ROLES.concat("head_of_accountant", "head_of_sale").some((v) =>
user.roles?.includes(v),
);
}
type BranchCreate = {
status?: Status;
2024-08-07 14:49:02 +07:00
code: string;
taxNo: string;
nameEN: string;
name: string;
2024-09-12 09:48:56 +07:00
permitNo: string;
2024-12-11 09:04:48 +07:00
permitIssueDate?: Date | null;
permitExpireDate?: Date | null;
addressEN: string;
address: string;
2024-09-11 15:06:27 +07:00
soi?: string | null;
soiEN?: string | null;
moo?: string | null;
mooEN?: string | null;
street?: string | null;
streetEN?: string | null;
email: string;
2024-04-19 09:28:17 +07:00
contactName?: string | null;
2024-08-29 11:53:00 +07:00
webUrl?: string | null;
contact?: string | string[] | null;
2024-04-18 13:21:37 +07:00
telephoneNo: string;
2024-04-19 09:28:17 +07:00
lineId?: string | null;
longitude: string;
latitude: string;
2024-09-03 16:54:52 +07:00
virtual?: boolean;
2024-09-09 14:58:48 +07:00
selectedImage?: string;
2024-09-24 09:56:05 +07:00
remark?: string;
bank?: {
bankName: string;
2024-08-02 17:30:29 +07:00
bankBranch: string;
accountName: string;
accountNumber: string;
2024-08-02 17:30:29 +07:00
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;
2024-09-12 09:48:56 +07:00
permitNo?: string;
2024-12-11 09:04:48 +07:00
permitIssueDate?: Date | null;
permitExpireDate?: Date | null;
addressEN?: string;
address?: string;
2024-09-11 15:06:27 +07:00
soi?: string | null;
soiEN?: string | null;
moo?: string | null;
mooEN?: string | null;
street?: string | null;
streetEN?: string | null;
email?: string;
telephoneNo?: string;
2024-04-17 13:42:11 +07:00
contactName?: string;
2024-08-29 11:53:00 +07:00
webUrl?: string | null;
contact?: string | string[] | null;
lineId?: string;
longitude?: string;
latitude?: string;
2024-09-03 16:54:52 +07:00
virtual?: boolean;
2024-09-09 14:58:48 +07:00
selectedImage?: string;
2024-09-24 09:56:05 +07:00
remark?: string;
subDistrictId?: string | null;
districtId?: string | null;
provinceId?: string | null;
headOfficeId?: string | null;
bank?: {
2024-09-05 11:07:10 +07:00
id?: string;
bankName: string;
2024-08-02 17:30:29 +07:00
bankBranch: string;
accountName: string;
accountNumber: string;
2024-08-02 17:30:29 +07:00
accountType: string;
currentlyUse: boolean;
}[];
};
const permissionCond = createPermCondition(globalAllowView);
const permissionCheck = createPermCheck(globalAllow);
2024-09-06 16:48:52 +07:00
2024-06-06 09:42:02 +07:00
@Route("api/v1/branch")
2024-04-02 09:28:36 +07:00
@Tags("Branch")
export class BranchController extends Controller {
2024-04-10 17:27:00 +07:00
@Get("stats")
2024-07-02 13:46:22 +07:00
@Security("keycloak")
2024-09-06 11:56:15 +07:00
async getStats(@Request() req: RequestWithUser, @Query() headOfficeId?: string) {
2024-09-05 15:05:38 +07:00
const where = {
AND: {
2024-12-06 15:44:52 +07:00
OR: permissionCond(req.user, { alwaysIncludeHead: true }),
},
2024-09-05 15:05:38 +07:00
};
const [hq, br, virtual] = await prisma.$transaction([
prisma.branch.count({
where: {
2024-09-06 11:56:15 +07:00
id: headOfficeId ? headOfficeId : undefined,
2024-09-05 15:05:38 +07:00
headOfficeId: null,
...where,
},
}),
prisma.branch.count({
where: {
2024-09-06 11:56:15 +07:00
headOfficeId: headOfficeId ? headOfficeId : { not: null },
2024-09-05 15:05:38 +07:00
virtual: false,
...where,
},
}),
prisma.branch.count({
where: {
2024-09-06 11:56:15 +07:00
headOfficeId: headOfficeId ? headOfficeId : { not: null },
2024-09-05 15:05:38 +07:00
virtual: true,
...where,
},
}),
]);
2024-04-10 17:27:00 +07:00
2024-09-05 15:05:38 +07:00
return { hq, br, virtual };
2024-04-10 17:27:00 +07:00
}
2024-04-09 15:13:44 +07:00
@Get("user-stats")
2024-07-02 13:46:22 +07:00
@Security("keycloak")
async getUserStat(@Request() req: RequestWithUser, @Query() userType?: UserType) {
2024-04-03 17:33:19 +07:00
const list = await prisma.branchUser.groupBy({
_count: true,
2024-07-02 13:46:22 +07:00
where: {
2024-09-03 09:47:58 +07:00
userId: !MANAGE_ROLES.some((v) => req.user.roles?.includes(v)) ? req.user.sub : undefined,
2024-07-02 13:46:22 +07:00
user: {
userType,
},
},
2024-04-09 15:13:44 +07:00
by: "branchId",
2024-04-03 17:33:19 +07:00
});
const record = await prisma.branch.findMany({
2024-07-02 13:46:22 +07:00
where: {
2024-09-03 09:47:58 +07:00
user: !MANAGE_ROLES.some((v) => req.user.roles?.includes(v))
2024-07-02 13:46:22 +07:00
? { some: { userId: req.user.sub } }
: undefined,
},
2024-04-03 17:33:19 +07:00
select: {
id: true,
2024-04-23 13:48:08 +07:00
headOfficeId: true,
isHeadOffice: true,
2024-04-03 17:33:19 +07:00
nameEN: true,
name: true,
2024-04-03 17:33:19 +07:00
},
2024-04-23 13:48:08 +07:00
orderBy: [{ isHeadOffice: "desc" }, { createdAt: "asc" }],
2024-04-03 17:33:19 +07:00
});
2024-04-23 13:48:08 +07:00
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) =>
2024-04-09 15:17:16 +07:00
Object.assign(a, {
count: list.find((b) => b.branchId === a.id)?._count ?? 0,
}),
);
2024-04-03 17:33:19 +07:00
}
2024-04-02 09:29:20 +07:00
@Get()
2024-07-02 13:46:22 +07:00
@Security("keycloak")
2024-04-02 09:29:20 +07:00
async getBranch(
2024-09-03 09:24:27 +07:00
@Request() req: RequestWithUser,
@Query() filter?: "head" | "sub",
@Query() headOfficeId?: string,
2024-09-25 14:13:27 +07:00
@Query() includeHead?: boolean, // Include relation
@Query() withHead?: boolean, // List cover head
@Query() tree?: boolean,
2024-09-09 14:49:01 +07:00
@Query() status?: Status,
2024-12-06 16:08:24 +07:00
@Query() activeOnly?: boolean,
2024-04-02 09:29:20 +07:00
@Query() query: string = "",
@Query() page: number = 1,
@Query() pageSize: number = 30,
) {
const where = {
AND: {
...filterStatus(activeOnly ? Status.ACTIVE : status),
AND: activeOnly
? {
OR: [{ headOffice: { status: { not: Status.INACTIVE } } }, { headOffice: null }],
}
: undefined,
headOfficeId: headOfficeId ?? (filter === "head" || tree ? null : undefined),
NOT: { headOfficeId: filter === "sub" && !headOfficeId ? null : undefined },
2024-12-06 16:08:24 +07:00
OR: permissionCond(req.user, { alwaysIncludeHead: withHead, activeOnly }),
},
OR: queryOrNot<Prisma.BranchWhereInput[]>(query, [
2024-10-18 08:46:17 +07:00
{ code: { contains: query, mode: "insensitive" } },
2024-08-21 10:05:27 +07:00
{ nameEN: { contains: query } },
{ name: { contains: query } },
{ email: { contains: query } },
{ telephoneNo: { contains: query } },
2024-09-11 15:06:27 +07:00
...whereAddressQuery(query),
{
branch: {
some: {
OR: [
2024-10-18 08:46:17 +07:00
{ code: { contains: query, mode: "insensitive" } },
2024-09-11 15:06:27 +07:00
{ nameEN: { contains: query } },
{ name: { contains: query } },
{ email: { contains: query } },
{ telephoneNo: { contains: query } },
...whereAddressQuery(query),
],
},
},
},
]),
2024-04-02 09:29:20 +07:00
} satisfies Prisma.BranchWhereInput;
const [result, total] = await prisma.$transaction([
prisma.branch.findMany({
2024-08-23 15:01:05 +07:00
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
2024-04-02 09:29:20 +07:00
include: {
province: true,
district: true,
subDistrict: true,
2024-04-17 13:59:30 +07:00
contact: true,
2024-08-30 11:33:08 +07:00
headOffice: includeHead
? {
include: {
province: true,
district: true,
subDistrict: true,
},
}
: false,
2024-08-23 15:45:46 +07:00
branch: tree
? {
2024-09-11 15:06:27 +07:00
where: {
AND: { OR: permissionCond(req.user) },
2024-09-11 15:06:27 +07:00
OR: [
{ nameEN: { contains: query } },
{ name: { contains: query } },
{ email: { contains: query } },
{ telephoneNo: { contains: query } },
...whereAddressQuery(query),
],
},
2024-08-23 15:45:46 +07:00
include: {
province: true,
district: true,
subDistrict: true,
},
2025-01-29 14:51:03 +07:00
orderBy: { code: "asc" },
2024-08-23 15:45:46 +07:00
}
: false,
2024-08-02 17:30:29 +07:00
bank: true,
2024-07-10 10:16:56 +07:00
_count: {
select: { branch: true },
},
2024-07-01 14:38:07 +07:00
createdBy: true,
updatedBy: true,
2024-04-02 09:29:20 +07:00
},
where,
take: pageSize,
skip: (page - 1) * pageSize,
}),
prisma.branch.count({ where }),
]);
return { result, page, pageSize, total };
}
@Get("{branchId}")
2024-07-02 13:46:22 +07:00
@Security("keycloak")
async getBranchById(
@Request() req: RequestWithUser,
@Path() branchId: string,
@Query() includeSubBranch?: boolean,
@Query() includeContact?: boolean,
) {
2024-04-02 15:32:17 +07:00
const record = await prisma.branch.findFirst({
2024-04-02 09:29:20 +07:00
include: {
province: true,
district: true,
subDistrict: true,
2024-07-01 14:38:07 +07:00
createdBy: true,
updatedBy: true,
branch: includeSubBranch && {
where: { AND: { OR: permissionCond(req.user) } },
include: {
province: true,
district: true,
subDistrict: true,
bank: true,
contact: includeContact,
},
2025-01-29 14:51:03 +07:00
orderBy: { code: "asc" },
},
2024-08-02 17:30:29 +07:00
bank: true,
contact: includeContact,
2024-04-02 09:29:20 +07:00
},
where: { id: branchId },
});
2024-04-02 15:32:17 +07:00
if (!record) throw notFoundError("Branch");
2024-04-02 15:32:17 +07:00
return record;
2024-04-02 09:29:20 +07:00
}
2024-04-02 10:45:37 +07:00
2024-04-02 11:19:12 +07:00
@Post()
2024-09-03 09:24:27 +07:00
@Security("keycloak", MANAGE_ROLES)
2024-04-02 11:19:12 +07:00
async createBranch(@Request() req: RequestWithUser, @Body() body: BranchCreate) {
2024-04-09 11:45:29 +07:00
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 } }),
]);
2024-09-11 15:06:27 +07:00
if (body.provinceId && !province) throw relationError("Province");
if (body.districtId && !district) throw relationError("District");
if (body.subDistrictId && !subDistrict) throw relationError("SubDistrict");
if (body.headOfficeId && !head) throw relationError("HQ");
2024-04-02 11:19:12 +07:00
2024-08-07 14:49:02 +07:00
const { provinceId, districtId, subDistrictId, headOfficeId, bank, contact, code, ...rest } =
body;
2024-04-09 11:45:29 +07:00
2024-08-27 09:56:33 +07:00
if (headOfficeId && head && head.code.slice(0, -5) !== code) {
2024-08-08 10:18:04 +07:00
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Headoffice code not match with branch code",
"codeMismatch",
);
}
2024-04-22 17:05:08 +07:00
const record = await prisma.$transaction(
async (tx) => {
const last = await tx.runningNo.upsert({
where: {
key: `MAIN_BRANCH_${code.toLocaleUpperCase()}`,
2024-04-22 17:05:08 +07:00
},
create: {
key: `MAIN_BRANCH_${code.toLocaleUpperCase()}`,
2024-04-22 17:05:08 +07:00
value: 1,
},
update: { value: { increment: 1 } },
});
2024-04-09 11:45:29 +07:00
2024-09-12 14:31:21 +07:00
const errorBranchExists = new HttpError(
HttpStatus.BAD_REQUEST,
"Branch with same code already exists.",
"sameBranchCodeExists",
);
2024-08-08 10:18:04 +07:00
if (last.value === 1) {
const exist = await tx.branch.findFirst({
2024-08-26 13:15:23 +07:00
where: { code: `${code?.toLocaleUpperCase()}${`${last.value - 1}`.padStart(5, "0")}` },
2024-08-08 10:18:04 +07:00
});
2024-09-12 14:31:21 +07:00
if (exist) throw errorBranchExists;
2024-08-08 15:48:41 +07:00
}
2024-09-12 14:31:21 +07:00
if (last.value !== 1 && !headOfficeId) throw errorBranchExists;
2024-08-08 15:48:41 +07:00
2024-04-22 17:05:08 +07:00
return await tx.branch.create({
include: {
province: true,
district: true,
subDistrict: true,
contact: true,
2024-09-05 14:37:39 +07:00
bank: true,
2024-07-01 14:38:07 +07:00
createdBy: true,
updatedBy: true,
2024-04-22 17:05:08 +07:00
},
data: {
...rest,
2024-06-24 13:14:44 +07:00
statusOrder: +(rest.status === "INACTIVE"),
2024-08-26 13:15:23 +07:00
code: `${code?.toLocaleUpperCase()}${`${last.value - 1}`.padStart(5, "0")}`,
bank: bank ? { createMany: { data: bank } } : undefined,
2024-04-22 17:05:08 +07:00
isHeadOffice: !headOfficeId,
2024-08-20 17:47:06 +07:00
contact: {
create: (typeof contact === "string" ? [contact] : contact)?.map((v) => ({
telephoneNo: v,
})),
},
province: connectOrNot(provinceId),
district: connectOrNot(districtId),
subDistrict: connectOrNot(subDistrictId),
headOffice: connectOrNot(headOfficeId),
2024-07-01 13:24:02 +07:00
createdBy: { connect: { id: req.user.sub } },
updatedBy: { connect: { id: req.user.sub } },
2024-04-22 17:05:08 +07:00
},
});
},
2024-04-23 11:19:27 +07:00
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
2024-04-22 17:05:08 +07:00
);
if (headOfficeId) {
await prisma.branch.updateMany({
where: { id: headOfficeId, status: Status.CREATED },
data: { status: Status.ACTIVE },
});
}
this.setStatus(HttpStatus.CREATED);
return record;
2024-04-02 11:19:12 +07:00
}
2024-04-03 10:54:46 +07:00
@Put("{branchId}")
2024-09-03 09:47:58 +07:00
@Security("keycloak", MANAGE_ROLES.concat("admin", "branch_manager"))
2024-04-02 11:52:55 +07:00
async editBranch(
@Request() req: RequestWithUser,
@Body() body: BranchUpdate,
@Path() branchId: string,
) {
2024-04-10 16:21:56 +07:00
if (body.headOfficeId === branchId)
throw new HttpError(
HttpStatus.BAD_REQUEST,
2024-10-29 16:01:13 +07:00
"Cannot make this as headquarters and branch at the same time.",
2024-06-14 05:44:03 +00:00
"cantMakeHQAndBranchSameTime",
2024-04-10 16:21:56 +07:00
);
2024-04-02 11:52:55 +07:00
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 } }),
]);
2024-09-11 15:06:27 +07:00
if (body.provinceId && !province) throw relationError("Province");
if (body.districtId && !district) throw relationError("District");
if (body.subDistrictId && !subDistrict) throw relationError("SubDistrict");
if (body.headOfficeId && !branch) throw relationError("HQ");
2024-04-02 11:52:55 +07:00
}
const { provinceId, districtId, subDistrictId, headOfficeId, bank, contact, ...rest } = body;
2024-04-02 11:52:55 +07:00
2024-09-06 16:48:52 +07:00
await permissionCheck(req.user, branchId);
2024-07-02 13:46:22 +07:00
2024-09-05 11:07:10 +07:00
return await prisma.$transaction(async (tx) => {
const listDeleted = bank
? await tx.branchBank.findMany({
where: { id: { not: { in: bank.flatMap((v) => (!!v.id ? v.id : [])) } }, branchId },
})
: [];
await Promise.all(
listDeleted.map((v) => deleteFile(fileLocation.branch.bank(v.branchId, v.id))),
2024-09-05 11:07:10 +07:00
);
return await prisma.branch.update({
2024-09-05 14:37:39 +07:00
include: {
province: true,
district: true,
subDistrict: true,
contact: true,
bank: true,
createdBy: true,
updatedBy: true,
},
2024-09-05 11:07:10 +07:00
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
isHeadOffice: headOfficeId !== undefined ? headOfficeId === null : undefined,
bank: bank
? {
2024-09-05 14:17:32 +07:00
deleteMany:
listDeleted.length > 0 ? { id: { in: listDeleted.map((v) => v.id) } } : undefined,
2024-09-05 11:07:10 +07:00
upsert: bank.map((v) => ({
2024-09-05 14:37:39 +07:00
where: { id: v.id || "" },
2024-09-05 14:17:32 +07:00
create: { ...v, id: undefined },
update: v,
2024-09-05 11:07:10 +07:00
})),
}
: undefined,
province: connectOrDisconnect(provinceId),
district: connectOrDisconnect(districtId),
subDistrict: connectOrDisconnect(subDistrictId),
headOffice: connectOrDisconnect(headOfficeId),
2024-09-05 11:07:10 +07:00
contact: contact
? {
deleteMany: {},
create: (typeof contact === "string" ? [contact] : contact)?.map((v) => ({
telephoneNo: v,
})),
}
: undefined,
updatedBy: { connect: { id: req.user.sub } },
2024-04-02 11:52:55 +07:00
},
2024-09-05 11:07:10 +07:00
where: { id: branchId },
});
2024-04-02 11:52:55 +07:00
});
}
2024-04-02 10:45:37 +07:00
@Delete("{branchId}")
2024-09-03 09:24:27 +07:00
@Security("keycloak", MANAGE_ROLES)
2024-07-02 13:46:22 +07:00
async deleteBranch(@Request() req: RequestWithUser, @Path() branchId: string) {
2024-09-06 16:48:52 +07:00
const record = await prisma.branch.findUnique({
include: branchRelationPermInclude(req.user),
where: { id: branchId },
});
2024-04-02 10:45:37 +07:00
if (!record) throw notFoundError("Branch");
2024-09-06 16:48:52 +07:00
await permissionCheck(req.user, record);
2024-07-02 13:46:22 +07:00
if (record.status !== Status.CREATED) throw isUsedError("Branch");
2024-04-02 10:45:37 +07:00
return await prisma.$transaction(async (tx) => {
const data = await tx.branch.delete({
include: {
province: true,
district: true,
subDistrict: true,
contact: true,
bank: true,
createdBy: true,
updatedBy: true,
},
where: { id: branchId },
});
if (record.isHeadOffice) {
await tx.runningNo.delete({
where: {
2024-08-27 09:56:33 +07:00
key: `MAIN_BRANCH_${record.code.slice(0, -5)}`,
},
});
}
await Promise.all([
2024-09-10 10:02:07 +07:00
deleteFolder(fileLocation.branch.img(branchId)),
deleteFile(fileLocation.branch.line(branchId)),
...data.bank.map((v) => deleteFile(fileLocation.branch.bank(branchId, v.id))),
]);
return data;
});
2024-04-02 10:45:37 +07:00
}
}
2024-08-01 09:53:16 +07:00
@Route("api/v1/branch/{branchId}")
@Tags("Branch")
export class BranchFileController extends Controller {
private async checkPermission(user: RequestWithUser["user"], id: string) {
const data = await prisma.branch.findUnique({
include: branchRelationPermInclude(user),
where: { id },
});
if (!data) throw notFoundError("Branch");
await permissionCheck(user, data);
2024-08-01 09:53:16 +07:00
}
@Get("image")
@Security("keycloak")
async listImage(@Request() req: RequestWithUser, @Path() branchId: string) {
await this.checkPermission(req.user, branchId);
return await listFile(fileLocation.branch.img(branchId));
2024-08-01 09:53:16 +07:00
}
@Get("image/{name}")
async getImage(@Request() req: RequestWithUser, @Path() branchId: string, @Path() name: string) {
return req.res?.redirect(await getFile(fileLocation.branch.img(branchId, name)));
}
2024-09-06 13:22:04 +07:00
2024-10-22 10:43:20 +07:00
@Head("image/{name}")
async headImage(@Request() req: RequestWithUser, @Path() branchId: string, @Path() name: string) {
return req.res?.redirect(await getPresigned("head", fileLocation.branch.img(branchId, name)));
}
@Put("image/{name}")
@Security("keycloak")
async putImage(@Request() req: RequestWithUser, @Path() branchId: string, @Path() name: string) {
if (!req.headers["content-type"]?.startsWith("image/")) {
throw new HttpError(HttpStatus.BAD_REQUEST, "Not a valid image.", "notValidImage");
}
await this.checkPermission(req.user, branchId);
return req.res?.redirect(await setFile(fileLocation.branch.img(branchId, name)));
2024-09-06 13:22:04 +07:00
}
@Delete("image/{name}")
@Security("keycloak")
async delImage(@Request() req: RequestWithUser, @Path() branchId: string, @Path() name: string) {
await this.checkPermission(req.user, branchId);
return await deleteFile(fileLocation.branch.img(branchId, name));
2024-09-05 10:45:55 +07:00
}
@Get("bank-qr/{bankId}")
async getBankImage(
2024-09-05 10:45:55 +07:00
@Request() req: RequestWithUser,
@Path() branchId: string,
@Path() bankId: string,
) {
return req.res?.redirect(await getFile(fileLocation.branch.bank(branchId, bankId)));
2024-09-05 10:45:55 +07:00
}
2024-09-06 13:22:04 +07:00
2024-10-22 17:54:46 +07:00
@Head("bank-qr/{bankId}")
async headBankImage(
@Request() req: RequestWithUser,
@Path() branchId: string,
@Path() bankId: string,
) {
return req.res?.redirect(
await getPresigned("head", fileLocation.branch.bank(branchId, bankId)),
);
}
@Put("bank-qr/{bankId}")
@Security("keycloak")
async putBankImage(
2024-09-06 13:22:04 +07:00
@Request() req: RequestWithUser,
@Path() branchId: string,
@Path() bankId: string,
) {
if (!req.headers["content-type"]?.startsWith("image/")) {
throw new HttpError(HttpStatus.BAD_REQUEST, "Not a valid image.", "notValidImage");
2024-09-09 14:02:17 +07:00
}
await this.checkPermission(req.user, branchId);
return req.res?.redirect(await setFile(fileLocation.branch.bank(branchId, bankId)));
2024-09-09 14:02:17 +07:00
}
@Delete("bank-qr/{bankId}")
@Security("keycloak")
async delBankImage(
2024-09-09 14:02:17 +07:00
@Request() req: RequestWithUser,
@Path() branchId: string,
@Path() bankId: string,
2024-09-09 14:02:17 +07:00
) {
await this.checkPermission(req.user, branchId);
return await deleteFile(fileLocation.branch.bank(branchId, bankId));
2024-09-09 14:02:17 +07:00
}
@Get("line-image")
async getLineImage(@Request() req: RequestWithUser, @Path() branchId: string) {
return req.res?.redirect(await getFile(fileLocation.branch.line(branchId)));
}
2024-10-22 17:54:46 +07:00
@Head("line-image")
async headLineImage(@Request() req: RequestWithUser, @Path() branchId: string) {
return req.res?.redirect(await getPresigned("head", fileLocation.branch.line(branchId)));
}
@Put("line-image")
2024-09-09 14:02:17 +07:00
@Security("keycloak")
async putLineImage(@Request() req: RequestWithUser, @Path() branchId: string) {
if (!req.headers["content-type"]?.startsWith("image/")) {
throw new HttpError(HttpStatus.BAD_REQUEST, "Not a valid image.", "notValidImage");
}
await this.checkPermission(req.user, branchId);
return req.res?.redirect(await setFile(fileLocation.branch.line(branchId)));
2024-09-09 14:02:17 +07:00
}
@Delete("line-image")
2024-09-09 16:13:21 +07:00
@Security("keycloak")
async delLineImage(@Request() req: RequestWithUser, @Path() branchId: string) {
await this.checkPermission(req.user, branchId);
return await deleteFile(fileLocation.branch.line(branchId));
2024-09-09 14:02:17 +07:00
}
2024-09-12 13:29:48 +07:00
@Get("attachment")
@Security("keycloak")
async listAttachment(@Request() req: RequestWithUser, @Path() branchId: string) {
await this.checkPermission(req.user, branchId);
return await listFile(fileLocation.branch.attachment(branchId));
}
@Get("attachment/{name}")
@Security("keycloak")
async getAttachment(@Path() branchId: string, @Path() name: string) {
return await getFile(fileLocation.branch.attachment(branchId, name));
}
2024-10-22 17:54:46 +07:00
@Head("attachment/{name}")
@Security("keycloak")
async headAttachment(@Path() branchId: string, @Path() name: string) {
return await getPresigned("head", fileLocation.branch.attachment(branchId, name));
}
2024-09-12 13:29:48 +07:00
@Put("attachment/{name}")
@Security("keycloak")
async putAttachment(
@Request() req: RequestWithUser,
@Path() branchId: string,
@Path() name: string,
) {
await this.checkPermission(req.user, branchId);
return await setFile(fileLocation.branch.attachment(branchId, name));
2024-09-12 13:29:48 +07:00
}
@Delete("attachment/{name}")
@Security("keycloak")
async delAttachment(
@Request() req: RequestWithUser,
@Path() branchId: string,
@Path() name: string,
) {
await this.checkPermission(req.user, branchId);
return await deleteFile(fileLocation.branch.attachment(branchId, name));
}
2024-04-02 09:28:36 +07:00
}