jws-backend/src/controllers/04-institution-controller.ts

131 lines
3 KiB
TypeScript
Raw Normal View History

2024-11-05 10:43:50 +07:00
import { Prisma } from "@prisma/client";
import {
Body,
Controller,
Delete,
Get,
OperationId,
Path,
Post,
Put,
Query,
Route,
Security,
Tags,
} from "tsoa";
import prisma from "../db";
import { notFoundError } from "../utils/error";
2024-11-07 14:18:38 +07:00
import { queryOrNot } from "../utils/relation";
2024-11-05 10:43:50 +07:00
type InstitutionPayload = {
name: string;
2024-11-07 16:59:26 +07:00
nameEN: string;
2024-11-05 10:43:50 +07:00
code: string;
2024-11-05 10:43:50 +07:00
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")
2024-11-07 14:20:52 +07:00
@OperationId("getInstitutionList")
2024-11-05 10:43:50 +07:00
async getInstitutionList(
@Query() query: string = "",
@Query() page: number = 1,
@Query() pageSize: number = 30,
2024-11-07 14:18:38 +07:00
@Query() group?: string,
2024-11-05 10:43:50 +07:00
) {
const where = {
2024-11-07 14:18:38 +07:00
group,
OR: queryOrNot<Prisma.InstitutionWhereInput[]>(query, [{ name: { contains: query } }]),
2024-11-05 10:43:50 +07:00
} 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")
2024-11-07 14:20:52 +07:00
@OperationId("getInstitution")
async getInstitution(@Path() institutionId: string, @Query() group?: string) {
2024-11-05 10:43:50 +07:00
return await prisma.institution.findFirst({
where: { id: institutionId, group },
2024-11-05 10:43:50 +07:00
});
}
@Post()
@Security("keycloak")
2024-11-07 14:20:52 +07:00
@OperationId("createInstitution")
2024-11-05 10:43:50 +07:00
async createInstitution(@Body() body: InstitutionPayload) {
return await prisma.$transaction(async (tx) => {
const last = await tx.runningNo.upsert({
where: {
key: `INST_${body.code}`,
},
create: {
key: `INST_${body.code}`,
value: 1,
},
update: { value: { increment: 1 } },
});
return await tx.institution.create({
data: {
...body,
code: `${body.code}${last.value.toString().padStart(5, "0")}`,
group: body.code,
},
});
2024-11-05 10:43:50 +07:00
});
}
@Put("{institutionId}")
@Security("keycloak")
2024-11-07 14:20:52 +07:00
@OperationId("updateInstitution")
2024-11-05 10:43:50 +07:00
async updateInstitution(@Path() institutionId: string, @Body() body: InstitutionPayload) {
return await prisma.institution.update({
where: { id: institutionId },
data: body,
});
}
@Delete("{institutionId}")
@Security("keycloak")
2024-11-07 14:20:52 +07:00
@OperationId("deleteInstitution")
2024-11-05 10:43:50 +07:00
async deleteInstitution(@Path() institutionId: string) {
return await prisma.$transaction(async (tx) => {
2024-11-05 20:00:42 +07:00
const record = await tx.institution.findFirst({
2024-11-05 10:43:50 +07:00
where: { id: institutionId },
});
2024-11-05 20:00:42 +07:00
if (!record) throw notFoundError("Institution");
2024-11-05 10:43:50 +07:00
2024-11-05 20:00:42 +07:00
return await tx.institution.delete({
where: { id: institutionId },
});
2024-11-05 10:43:50 +07:00
});
}
}