jws-backend/src/controllers/00-employment-office-controller.ts
2024-11-25 14:41:47 +07:00

66 lines
1.9 KiB
TypeScript

import { Body, Controller, Get, Path, Post, Query, Route, Tags } from "tsoa";
import prisma from "../db";
import { queryOrNot } from "../utils/relation";
import { notFoundError } from "../utils/error";
@Route("/api/v1/employment-office")
@Tags("Employment Office")
export class EmploymentOfficeController extends Controller {
@Get()
async getEmploymentOfficeList(@Query() districtId?: string, @Query() query: string = "") {
return this.getEmploymentOfficeListByCriteria(districtId, query);
}
@Post("list")
async getEmploymentOfficeListByCriteria(
@Query() districtId?: string,
@Query() query: string = "",
@Body()
body?: {
id?: string[];
},
) {
return await prisma.employmentOffice.findMany({
where: {
OR:
districtId || query || body?.id
? [
...queryOrNot(
!!districtId,
[
{
province: { district: { some: { id: districtId } } },
district: { none: {} },
},
{
district: {
some: { districtId },
},
},
],
[],
),
...queryOrNot(
query,
[{ name: { contains: query } }, { nameEN: { contains: query } }],
[],
),
...queryOrNot(!!body?.id, [{ id: { in: body?.id } }], []),
]
: undefined,
},
orderBy: [{ provinceId: "asc" }, { id: "asc" }],
});
}
@Get("{employmentOfficeId}")
async getEmploymentOffice(@Path() employmentOfficeId: string) {
const record = await prisma.employmentOffice.findFirst({
where: { id: employmentOfficeId },
});
if (!record) throw notFoundError("Employment Office");
return record;
}
}