jws-backend/src/controllers/03-employee-visa-controller.ts

122 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-09-13 17:39:12 +07:00
import {
Body,
Controller,
Delete,
Get,
Middlewares,
Path,
Post,
Put,
Route,
Security,
Tags,
} from "tsoa";
import { RequestWithUser } from "../interfaces/user";
import prisma from "../db";
import HttpStatus from "../interfaces/http-status";
import { permissionCheck } from "../middlewares/employee";
import { notFoundError } from "../utils/error";
2024-09-17 12:46:19 +07:00
import { deleteFile, fileLocation } from "../utils/minio";
2024-09-13 17:39:12 +07:00
const MANAGE_ROLES = [
"system",
"head_of_admin",
"admin",
"head_of_account",
"account",
"head_of_sale",
];
function globalAllow(user: RequestWithUser["user"]) {
const allowList = ["system", "head_of_admin", "head_of_account", "head_of_sale"];
2024-09-13 17:39:12 +07:00
return allowList.some((v) => user.roles?.includes(v));
}
type EmployeeVisaPayload = {
number: string;
type: string;
entryCount: number;
issueCountry: string;
issuePlace: string;
issueDate: Date;
expireDate: Date;
mrz?: string;
remark?: string;
};
2024-09-13 17:46:22 +07:00
@Route("api/v1/employee/{employeeId}/visa")
2024-09-13 17:50:56 +07:00
@Tags("Employee Visa")
2024-09-13 17:39:12 +07:00
@Middlewares(permissionCheck(globalAllow))
export class EmployeeVisaController extends Controller {
@Get()
@Security("keycloak")
async list(@Path() employeeId: string) {
return prisma.employeeVisa.findMany({
2024-10-04 12:59:02 +07:00
orderBy: { expireDate: "desc" },
2024-09-13 17:39:12 +07:00
where: { employeeId },
});
}
2024-09-17 12:46:19 +07:00
@Get("{visaId}")
2024-09-13 17:39:12 +07:00
@Security("keycloak")
2024-09-17 12:46:19 +07:00
async getById(@Path() employeeId: string, @Path() visaId: string) {
2024-09-13 17:39:12 +07:00
const record = await prisma.employeeVisa.findFirst({
2024-09-17 12:46:19 +07:00
where: { id: visaId, employeeId },
2024-09-13 17:39:12 +07:00
});
2024-09-17 12:46:19 +07:00
if (!record) throw notFoundError("Visa");
2024-09-13 17:39:12 +07:00
return record;
}
@Post()
@Security("keycloak", MANAGE_ROLES)
async create(@Path() employeeId: string, @Body() body: EmployeeVisaPayload) {
const record = await prisma.employeeVisa.create({
data: {
...body,
employee: { connect: { id: employeeId } },
},
});
this.setStatus(HttpStatus.CREATED);
return record;
}
2024-09-17 12:46:19 +07:00
@Put("{visaId}")
2024-09-13 17:39:12 +07:00
@Security("keycloak", MANAGE_ROLES)
async editById(
@Path() employeeId: string,
2024-09-17 12:46:19 +07:00
@Path() visaId: string,
2024-09-13 17:39:12 +07:00
@Body() body: EmployeeVisaPayload,
) {
const work = await prisma.employeeVisa.findUnique({
2024-09-17 12:46:19 +07:00
where: { id: visaId, employeeId },
2024-09-13 17:39:12 +07:00
});
2024-09-17 12:46:19 +07:00
if (!work) throw notFoundError("Visa");
2024-09-13 17:39:12 +07:00
const record = await prisma.employeeVisa.update({
2024-09-17 12:46:19 +07:00
where: { id: visaId, employeeId },
2024-09-13 17:39:12 +07:00
data: { ...body },
});
this.setStatus(HttpStatus.CREATED);
return record;
}
2024-09-17 12:46:19 +07:00
@Delete("{visaId}")
2024-09-13 17:39:12 +07:00
@Security("keycloak", MANAGE_ROLES)
2024-09-17 12:46:19 +07:00
async deleteById(@Path() employeeId: string, @Path() visaId: string) {
2024-09-13 17:39:12 +07:00
const record = await prisma.employeeVisa.findFirst({
2024-09-17 12:46:19 +07:00
where: { id: visaId, employeeId },
2024-09-13 17:39:12 +07:00
});
2024-09-17 12:46:19 +07:00
if (!record) throw notFoundError("Visa");
2024-09-13 17:39:12 +07:00
2024-09-17 12:46:19 +07:00
await deleteFile(fileLocation.employee.visa(employeeId, visaId));
return await prisma.employeeVisa.delete({ where: { id: visaId, employeeId } });
2024-09-13 17:39:12 +07:00
}
}