feat: emplyee checkup & other info endpoints
This commit is contained in:
parent
c59e0f537a
commit
ff5b3ee640
2 changed files with 310 additions and 0 deletions
127
src/controllers/employee-other-info-controller.ts
Normal file
127
src/controllers/employee-other-info-controller.ts
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
import { Prisma, Status } from "@prisma/client";
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Put,
|
||||
Path,
|
||||
Post,
|
||||
Query,
|
||||
Request,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
} from "tsoa";
|
||||
|
||||
import prisma from "../db";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import { RequestWithUser } from "../interfaces/user";
|
||||
|
||||
type EmployeeOtherInfoCreate = {
|
||||
citizenId: string;
|
||||
fatherFullName: string;
|
||||
motherFullName: string;
|
||||
birthPlace: string;
|
||||
};
|
||||
|
||||
type EmployeeOtherInfoUpdate = {
|
||||
citizenId: string;
|
||||
fatherFullName: string;
|
||||
motherFullName: string;
|
||||
birthPlace: string;
|
||||
};
|
||||
|
||||
@Route("api/employee/{employeeId}/other-info")
|
||||
@Tags("Employee Other Info")
|
||||
@Security("keycloak")
|
||||
export class EmployeeOtherInfo extends Controller {
|
||||
@Get()
|
||||
async list(@Path() employeeId: string) {
|
||||
return prisma.employeeOtherInfo.findMany({
|
||||
orderBy: { createdAt: "asc" },
|
||||
where: { employeeId },
|
||||
});
|
||||
}
|
||||
|
||||
@Get("{otherInfoId}")
|
||||
async getById(@Path() employeeId: string, @Path() otherInfoId: string) {
|
||||
const record = await prisma.employeeOtherInfo.findFirst({
|
||||
where: { id: otherInfoId, employeeId },
|
||||
});
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Employee info cannot be found.", "data_not_found");
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(
|
||||
@Request() req: RequestWithUser,
|
||||
@Path() employeeId: string,
|
||||
@Body() body: EmployeeOtherInfoCreate,
|
||||
) {
|
||||
if (!(await prisma.employee.findUnique({ where: { id: employeeId } })))
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Employee cannot be found.",
|
||||
"missing_or_invalid_parameter",
|
||||
);
|
||||
|
||||
const record = await prisma.employeeOtherInfo.create({
|
||||
data: {
|
||||
...body,
|
||||
employee: { connect: { id: employeeId } },
|
||||
createdBy: req.user.name,
|
||||
updateBy: req.user.name,
|
||||
},
|
||||
});
|
||||
|
||||
this.setStatus(HttpStatus.CREATED);
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
@Put("{otherInfoId}")
|
||||
async editById(
|
||||
@Request() req: RequestWithUser,
|
||||
@Path() employeeId: string,
|
||||
@Path() otherInfoId: string,
|
||||
@Body() body: EmployeeOtherInfoUpdate,
|
||||
) {
|
||||
if (!(await prisma.employeeOtherInfo.findUnique({ where: { id: otherInfoId, employeeId } }))) {
|
||||
throw new HttpError(
|
||||
HttpStatus.NOT_FOUND,
|
||||
"Employee other info cannot be found.",
|
||||
"data_not_found",
|
||||
);
|
||||
}
|
||||
|
||||
const record = await prisma.employeeOtherInfo.update({
|
||||
where: { id: otherInfoId, employeeId },
|
||||
data: { ...body, createdBy: req.user.name, updateBy: req.user.name },
|
||||
});
|
||||
|
||||
this.setStatus(HttpStatus.CREATED);
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
@Delete("{otherInfoId}")
|
||||
async deleteById(@Path() employeeId: string, @Path() otherInfoId: string) {
|
||||
const record = await prisma.employeeOtherInfo.findFirst({
|
||||
where: { id: otherInfoId, employeeId },
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(
|
||||
HttpStatus.NOT_FOUND,
|
||||
"Employee other info cannot be found.",
|
||||
"data_not_found",
|
||||
);
|
||||
}
|
||||
|
||||
return await prisma.employeeOtherInfo.delete({ where: { id: otherInfoId, employeeId } });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue