jws-backend/src/controllers/00-employment-office-controller.ts
Methapon2001 f2d0c20ece
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 8s
feat: add endpoint for get same office district
2025-04-17 12:56:31 +07:00

103 lines
2.8 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";
import { Prisma } from "@prisma/client";
@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-same-office-area")
async getSameOfficeArea(@Body() body: { districtId: string }) {
const office = await prisma.employmentOffice.findFirst({
include: {
province: {
include: {
district: true,
},
},
district: true,
},
where: {
OR: [
{
province: { district: { some: { id: body.districtId } } },
district: { none: {} },
},
{
district: {
some: { districtId: body.districtId },
},
},
],
},
});
if (!office) return [];
return [
...office.district.map((v) => v.districtId),
...office.province.district.map((v) => v.id),
];
}
@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, mode: "insensitive" } },
{ nameEN: { contains: query, mode: "insensitive" } },
],
[],
) satisfies Prisma.EmploymentOfficeWhereInput["OR"]),
...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;
}
}