169 lines
4.5 KiB
TypeScript
169 lines
4.5 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
Path,
|
|
Post,
|
|
Put,
|
|
Request,
|
|
Route,
|
|
Security,
|
|
Tags,
|
|
} from "tsoa";
|
|
import { RequestWithUser } from "../interfaces/user";
|
|
import prisma from "../db";
|
|
import HttpStatus from "../interfaces/http-status";
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
type EmployeeCheckupPayload = {
|
|
checkupType?: string | null;
|
|
checkupResult?: string | null;
|
|
|
|
provinceId?: string | null;
|
|
|
|
hospitalName?: string | null;
|
|
remark?: string | null;
|
|
medicalBenefitScheme?: string | null;
|
|
insuranceCompany?: string | null;
|
|
coverageStartDate?: Date | null;
|
|
coverageExpireDate?: Date | null;
|
|
};
|
|
|
|
@Route("api/v1/employee/{employeeId}/checkup")
|
|
@Tags("Employee Checkup")
|
|
@Security("keycloak")
|
|
export class EmployeeCheckupController extends Controller {
|
|
@Get()
|
|
async list(@Path() employeeId: string) {
|
|
return prisma.employeeCheckup.findMany({
|
|
orderBy: { createdAt: "asc" },
|
|
where: { employeeId },
|
|
});
|
|
}
|
|
|
|
@Get("{checkupId}")
|
|
async getById(@Path() employeeId: string, @Path() checkupId: string) {
|
|
const record = await prisma.employeeCheckup.findFirst({
|
|
where: { id: checkupId, employeeId },
|
|
});
|
|
if (!record) {
|
|
throw new HttpError(
|
|
HttpStatus.NOT_FOUND,
|
|
"Employee checkup cannot be found.",
|
|
"data_not_found",
|
|
);
|
|
}
|
|
return record;
|
|
}
|
|
|
|
@Post()
|
|
async create(
|
|
@Request() req: RequestWithUser,
|
|
@Path() employeeId: string,
|
|
@Body() body: EmployeeCheckupPayload,
|
|
) {
|
|
if (body.provinceId || employeeId) {
|
|
const [province, employee] = await prisma.$transaction([
|
|
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
|
prisma.employee.findFirst({ where: { id: employeeId } }),
|
|
]);
|
|
if (body.provinceId && !province)
|
|
throw new HttpError(
|
|
HttpStatus.BAD_REQUEST,
|
|
"Province cannot be found.",
|
|
"missing_or_invalid_parameter",
|
|
);
|
|
if (!employee)
|
|
throw new HttpError(
|
|
HttpStatus.BAD_REQUEST,
|
|
"Employee cannot be found.",
|
|
"missing_or_invalid_parameter",
|
|
);
|
|
}
|
|
|
|
const { provinceId, ...rest } = body;
|
|
|
|
const record = await prisma.employeeCheckup.create({
|
|
include: { province: true },
|
|
data: {
|
|
...rest,
|
|
province: { connect: provinceId ? { id: provinceId } : undefined },
|
|
employee: { connect: { id: employeeId } },
|
|
createdBy: req.user.name,
|
|
updateBy: req.user.name,
|
|
},
|
|
});
|
|
|
|
this.setStatus(HttpStatus.CREATED);
|
|
|
|
return record;
|
|
}
|
|
|
|
@Put("{checkupId}")
|
|
async editById(
|
|
@Request() req: RequestWithUser,
|
|
@Path() employeeId: string,
|
|
@Path() checkupId: string,
|
|
@Body() body: EmployeeCheckupPayload,
|
|
) {
|
|
if (body.provinceId || employeeId) {
|
|
const [province, employee] = await prisma.$transaction([
|
|
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
|
prisma.employee.findFirst({ where: { id: employeeId } }),
|
|
]);
|
|
if (body.provinceId && !province)
|
|
throw new HttpError(
|
|
HttpStatus.BAD_REQUEST,
|
|
"Province cannot be found.",
|
|
"missing_or_invalid_parameter",
|
|
);
|
|
if (!employee)
|
|
throw new HttpError(
|
|
HttpStatus.BAD_REQUEST,
|
|
"Employee cannot be found.",
|
|
"missing_or_invalid_parameter",
|
|
);
|
|
}
|
|
|
|
const { provinceId, ...rest } = body;
|
|
|
|
if (!(await prisma.employeeCheckup.findUnique({ where: { id: checkupId, employeeId } }))) {
|
|
throw new HttpError(
|
|
HttpStatus.NOT_FOUND,
|
|
"Employee checkup cannot be found.",
|
|
"data_not_found",
|
|
);
|
|
}
|
|
|
|
const record = await prisma.employeeCheckup.update({
|
|
include: { province: true },
|
|
where: { id: checkupId, employeeId },
|
|
data: {
|
|
...rest,
|
|
province: { connect: provinceId ? { id: provinceId } : undefined },
|
|
createdBy: req.user.name,
|
|
updateBy: req.user.name,
|
|
},
|
|
});
|
|
|
|
this.setStatus(HttpStatus.CREATED);
|
|
|
|
return record;
|
|
}
|
|
|
|
@Delete("{checkupId}")
|
|
async deleteById(@Path() employeeId: string, @Path() checkupId: string) {
|
|
const record = await prisma.employeeCheckup.findFirst({ where: { id: checkupId, employeeId } });
|
|
|
|
if (!record) {
|
|
throw new HttpError(
|
|
HttpStatus.NOT_FOUND,
|
|
"Employee checkup cannot be found.",
|
|
"data_not_found",
|
|
);
|
|
}
|
|
|
|
return await prisma.employeeCheckup.delete({ where: { id: checkupId, employeeId } });
|
|
}
|
|
}
|