109 lines
2.4 KiB
TypeScript
109 lines
2.4 KiB
TypeScript
|
|
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;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|