jws-backend/src/controllers/03-employee-other-info-controller.ts
2024-11-13 10:24:44 +07:00

140 lines
3.4 KiB
TypeScript

import {
Body,
Controller,
Delete,
Get,
Put,
Path,
Post,
Request,
Route,
Security,
Tags,
Middlewares,
} from "tsoa";
import prisma from "../db";
import HttpStatus from "../interfaces/http-status";
import { RequestWithUser } from "../interfaces/user";
import { permissionCheck } from "../middlewares/employee";
import { notFoundError } from "../utils/error";
const MANAGE_ROLES = [
"system",
"head_of_admin",
"admin",
"head_of_accountant",
"accountant",
"head_of_sale",
];
function globalAllow(user: RequestWithUser["user"]) {
const allowList = ["system", "head_of_admin", "head_of_accountant", "head_of_sale"];
return allowList.some((v) => user.roles?.includes(v));
}
type EmployeeOtherInfoPayload = {
telephoneNo?: string | null;
citizenId?: string | null;
fatherFirstName?: string | null;
fatherLastName?: string | null;
fatherBirthPlace?: string | null;
motherFirstName?: string | null;
motherLastName?: string | null;
motherBirthPlace?: string | null;
fatherFirstNameEN?: string | null;
fatherLastNameEN?: string | null;
motherFirstNameEN?: string | null;
motherLastNameEN?: string | null;
};
@Route("api/v1/employee/{employeeId}/other-info")
@Tags("Employee Other Info")
@Middlewares(permissionCheck(globalAllow))
export class EmployeeOtherInfo extends Controller {
@Get()
@Security("keycloak")
async list(@Path() employeeId: string) {
return prisma.employeeOtherInfo.findFirst({
include: {
createdBy: true,
updatedBy: true,
},
orderBy: { createdAt: "asc" },
where: { employeeId },
});
}
@Post()
@Security("keycloak", MANAGE_ROLES)
async create(
@Request() req: RequestWithUser,
@Path() employeeId: string,
@Body() body: EmployeeOtherInfoPayload,
) {
const record = await prisma.employeeOtherInfo.create({
include: {
createdBy: true,
updatedBy: true,
},
data: {
...body,
employee: { connect: { id: employeeId } },
createdBy: { connect: { id: req.user.sub } },
updatedBy: { connect: { id: req.user.sub } },
},
});
this.setStatus(HttpStatus.CREATED);
return record;
}
@Put("{otherInfoId}")
@Security("keycloak", MANAGE_ROLES)
async editById(
@Request() req: RequestWithUser,
@Path() employeeId: string,
@Path() otherInfoId: string,
@Body() body: EmployeeOtherInfoPayload,
) {
const otherInfo = await prisma.employeeOtherInfo.findUnique({
where: { id: otherInfoId, employeeId },
});
if (!otherInfo) throw notFoundError("Employee Other Info");
const record = await prisma.employeeOtherInfo.update({
include: {
createdBy: true,
updatedBy: true,
},
where: { id: otherInfoId, employeeId },
data: { ...body, updatedByUserId: req.user.sub },
});
this.setStatus(HttpStatus.CREATED);
return record;
}
@Delete("{otherInfoId}")
@Security("keycloak", MANAGE_ROLES)
async deleteById(@Path() employeeId: string, @Path() otherInfoId: string) {
const record = await prisma.employeeOtherInfo.findFirst({
where: { id: otherInfoId, employeeId },
});
if (!record) throw notFoundError("Employee Other Info");
return await prisma.employeeOtherInfo.delete({
include: {
createdBy: true,
updatedBy: true,
},
where: { id: otherInfoId, employeeId },
});
}
}