hrms-api-org/src/controllers/ProfileLeaveEmployeeController.ts

148 lines
4.5 KiB
TypeScript

import {
Body,
Controller,
Delete,
Example,
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import {
ProfileLeaveHistory,
CreateProfileEmployeeLeave,
ProfileLeave,
UpdateProfileLeave,
} from "../entities/ProfileLeave";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { RequestWithUser } from "../middlewares/user";
import { LeaveType } from "../entities/LeaveType";
import { ProfileEmployee } from "../entities/ProfileEmployee";
@Route("api/v1/org/profile-employee/leave")
@Tags("ProfileLeave")
@Security("bearerAuth")
export class ProfileLeaveEmployeeController extends Controller {
private profileRepo = AppDataSource.getRepository(ProfileEmployee);
private leaveRepo = AppDataSource.getRepository(ProfileLeave);
private leaveHistoryRepo = AppDataSource.getRepository(ProfileLeaveHistory);
private leaveTypeRepository = AppDataSource.getRepository(LeaveType);
@Get("user")
public async getLeaveUser(@Request() request: { user: Record<string, any> }) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.leaveRepo.find({
relations: { leaveType: true },
where: { profileEmployeeId: profile.id },
});
return new HttpSuccess(record);
}
@Get("{profileId}")
public async getLeave(@Path() profileId: string) {
const record = await this.leaveRepo.find({
relations: { leaveType: true },
where: { profileEmployeeId: profileId },
});
return new HttpSuccess(record);
}
@Get("history/{leaveId}")
public async leaveHistory(@Path() leaveId: string) {
const record = await this.leaveHistoryRepo.find({
relations: { leaveType: true },
where: { profileLeaveId: leaveId },
});
return new HttpSuccess(record);
}
@Post()
public async newLeave(@Request() req: RequestWithUser, @Body() body: CreateProfileEmployeeLeave) {
if (!body.profileEmployeeId) {
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
}
const profile = await this.profileRepo.findOneBy({ id: body.profileEmployeeId });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const leaveType = await this.leaveTypeRepository.findOne({
where: { id: body.leaveTypeId },
});
if (!leaveType) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทลานี้");
}
const data = new ProfileLeave();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
Object.assign(data, { ...body, ...meta });
await this.leaveRepo.save(data);
return new HttpSuccess();
}
@Patch("{leaveId}")
public async editLeave(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileLeave,
@Path() leaveId: string,
) {
const record = await this.leaveRepo.findOneBy({ id: leaveId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const leaveType = await this.leaveTypeRepository.findOne({
where: { id: body.leaveTypeId },
});
if (!leaveType) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทลานี้");
}
const history = new ProfileLeaveHistory();
Object.assign(history, { ...record, id: undefined });
Object.assign(record, body);
history.profileLeaveId = leaveId;
record.lastUpdateFullName = req.user.name;
history.lastUpdateFullName = req.user.name;
await Promise.all([this.leaveRepo.save(record), this.leaveHistoryRepo.save(history)]);
return new HttpSuccess();
}
@Delete("{leaveId}")
public async deleteLeave(@Path() leaveId: string) {
await this.leaveHistoryRepo.delete({
profileLeaveId: leaveId,
});
const result = await this.leaveRepo.delete({ id: leaveId });
if (result.affected == undefined || result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}