From 7135258b84819449b339f89bcda2b31f2c74d2d5 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Tue, 5 Nov 2024 10:43:50 +0700 Subject: [PATCH] feat: add endpoint institution --- src/controllers/04-institution-controller.ts | 108 +++++++++++++++++++ tsoa.json | 1 + 2 files changed, 109 insertions(+) create mode 100644 src/controllers/04-institution-controller.ts diff --git a/src/controllers/04-institution-controller.ts b/src/controllers/04-institution-controller.ts new file mode 100644 index 0000000..0542469 --- /dev/null +++ b/src/controllers/04-institution-controller.ts @@ -0,0 +1,108 @@ +import { Prisma } from "@prisma/client"; +import { + Body, + Controller, + Delete, + Get, + OperationId, + Path, + Post, + Put, + Query, + Route, + Security, + Tags, +} from "tsoa"; +import { RequestWithUser } from "../interfaces/user"; +import prisma from "../db"; +import { notFoundError } from "../utils/error"; + +const MANAGE_ROLES = ["system", "head_of_admin", "admin"]; + +function globalAllow(user: RequestWithUser["user"]) { + const allowList = ["system", "head_of_admin"]; + return allowList.some((v) => user.roles?.includes(v)); +} + +type InstitutionPayload = { + name: string; + + addressEN: string; + address: string; + soi?: string | null; + soiEN?: string | null; + moo?: string | null; + mooEN?: string | null; + street?: string | null; + streetEN?: string | null; + + subDistrictId: string; + districtId: string; + provinceId: string; +}; + +@Route("api/v1/institution") +@Tags("Institution") +export class InstitutionController extends Controller { + @Get() + @Security("keycloak") + async getInstitutionList( + @Query() query: string = "", + @Query() page: number = 1, + @Query() pageSize: number = 30, + ) { + const where = { + name: { contains: query }, + } satisfies Prisma.InstitutionWhereInput; + + const [result, total] = await prisma.$transaction([ + prisma.institution.findMany({ + where, + take: pageSize, + skip: (page - 1) * pageSize, + }), + prisma.institution.count({ where }), + ]); + + return { result, page, pageSize, total }; + } + + @Get("{institutionId}") + @Security("keycloak") + async getInstitution(@Path() institutionId: string) { + return await prisma.institution.findFirst({ + where: { id: institutionId }, + }); + } + + @Post() + @Security("keycloak") + async createInstitution(@Body() body: InstitutionPayload) { + return await prisma.institution.create({ + data: body, + }); + } + + @Put("{institutionId}") + @Security("keycloak") + async updateInstitution(@Path() institutionId: string, @Body() body: InstitutionPayload) { + return await prisma.institution.update({ + where: { id: institutionId }, + data: body, + }); + } + + @Delete("{institutionId}") + @Security("keycloak") + async deleteInstitution(@Path() institutionId: string) { + return await prisma.$transaction(async (tx) => { + const ret = await tx.institution.deleteMany({ + where: { id: institutionId }, + }); + + if (ret.count <= 0) throw notFoundError("Institution"); + + return ret; + }); + } +} diff --git a/tsoa.json b/tsoa.json index 9b46a33..4ea98ff 100644 --- a/tsoa.json +++ b/tsoa.json @@ -38,6 +38,7 @@ { "name": "Employee In Country Notice" }, { "name": "Employee Work" }, { "name": "Employee Other Info" }, + { "name": "Institution" }, { "name": "Workflow" }, { "name": "Product Group" }, { "name": "Product" },