jws-backend/src/controllers/employee-checkup-controller.ts

199 lines
5.1 KiB
TypeScript
Raw Normal View History

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";
2024-07-03 15:42:49 +07:00
const MANAGE_ROLES = [
"system",
"head_of_admin",
"admin",
"branch_admin",
"branch_manager",
"head_of_sale",
"sale",
];
2024-06-11 11:27:00 +07:00
type EmployeeCheckupPayload = {
checkupType?: string | null;
checkupResult?: string | null;
provinceId?: string | null;
2024-06-11 11:27:00 +07:00
hospitalName?: string | null;
remark?: string | null;
medicalBenefitScheme?: string | null;
insuranceCompany?: string | null;
coverageStartDate?: Date | null;
coverageExpireDate?: Date | null;
};
2024-06-06 09:42:02 +07:00
@Route("api/v1/employee/{employeeId}/checkup")
@Tags("Employee Checkup")
export class EmployeeCheckupController extends Controller {
@Get()
2024-07-03 15:42:49 +07:00
@Security("keycloak")
async list(@Path() employeeId: string) {
return prisma.employeeCheckup.findMany({
2024-07-01 14:38:07 +07:00
include: {
createdBy: true,
updatedBy: true,
},
orderBy: { createdAt: "asc" },
where: { employeeId },
});
}
@Get("{checkupId}")
2024-07-03 15:42:49 +07:00
@Security("keycloak")
async getById(@Path() employeeId: string, @Path() checkupId: string) {
const record = await prisma.employeeCheckup.findFirst({
2024-07-01 14:38:07 +07:00
include: {
createdBy: true,
updatedBy: true,
},
where: { id: checkupId, employeeId },
});
if (!record) {
throw new HttpError(
HttpStatus.NOT_FOUND,
"Employee checkup cannot be found.",
2024-06-14 06:19:15 +00:00
"employeeCheckupNotFound",
);
}
return record;
}
@Post()
2024-07-03 15:42:49 +07:00
@Security("keycloak", MANAGE_ROLES)
async create(
@Request() req: RequestWithUser,
@Path() employeeId: string,
2024-06-11 11:27:00 +07:00
@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.",
2024-06-14 06:19:15 +00:00
"provinceNotFound",
);
if (!employee)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Employee cannot be found.",
2024-06-14 06:19:15 +00:00
"employeeNotFound",
);
}
const { provinceId, ...rest } = body;
const record = await prisma.employeeCheckup.create({
2024-07-01 14:38:07 +07:00
include: { province: true, createdBy: true, updatedBy: true },
data: {
...rest,
province: { connect: provinceId ? { id: provinceId } : undefined },
employee: { connect: { id: employeeId } },
2024-07-01 13:24:02 +07:00
createdBy: { connect: { id: req.user.sub } },
updatedBy: { connect: { id: req.user.sub } },
},
});
this.setStatus(HttpStatus.CREATED);
return record;
}
@Put("{checkupId}")
2024-07-03 15:42:49 +07:00
@Security("keycloak", MANAGE_ROLES)
async editById(
@Request() req: RequestWithUser,
@Path() employeeId: string,
@Path() checkupId: string,
2024-06-11 11:27:00 +07:00
@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.",
2024-06-14 06:19:15 +00:00
"provinceNotFound",
);
if (!employee)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Employee cannot be found.",
2024-06-14 06:19:15 +00:00
"employeeNotFound",
);
}
const { provinceId, ...rest } = body;
2024-07-01 14:38:07 +07:00
if (
!(await prisma.employeeCheckup.findUnique({
include: { createdBy: true, updatedBy: true },
where: { id: checkupId, employeeId },
}))
) {
throw new HttpError(
HttpStatus.NOT_FOUND,
"Employee checkup cannot be found.",
2024-06-14 06:19:15 +00:00
"employeeCheckupNotFound",
);
}
const record = await prisma.employeeCheckup.update({
2024-07-01 14:38:07 +07:00
include: { province: true, createdBy: true, updatedBy: true },
where: { id: checkupId, employeeId },
data: {
...rest,
province: { connect: provinceId ? { id: provinceId } : undefined },
2024-07-01 13:24:02 +07:00
updatedBy: { connect: { id: req.user.sub } },
},
});
this.setStatus(HttpStatus.CREATED);
return record;
}
@Delete("{checkupId}")
2024-07-03 15:42:49 +07:00
@Security("keycloak", MANAGE_ROLES)
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.",
2024-06-14 06:19:15 +00:00
"employeeCheckupNotFound",
);
}
2024-07-01 14:38:07 +07:00
return await prisma.employeeCheckup.delete({
include: { createdBy: true, updatedBy: true },
where: { id: checkupId, employeeId },
});
}
}