feat: add endpoint institution

This commit is contained in:
Methapon Metanipat 2024-11-05 10:43:50 +07:00
parent 10e64b13a8
commit 7135258b84
2 changed files with 109 additions and 0 deletions

View file

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

View file

@ -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" },