import { Body, Controller, Delete, Example, Get, Patch, Path, Post, Request, Route, Security, Tags, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import { ProfileLeaveHistory, CreateProfileLeave, 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 { Profile } from "../entities/Profile"; import { LeaveType } from "../entities/LeaveType"; import { Brackets } from "typeorm"; @Route("api/v1/org/profile/leave") @Tags("ProfileLeave") @Security("bearerAuth") export class ProfileLeaveController extends Controller { private profileRepo = AppDataSource.getRepository(Profile); private leaveRepo = AppDataSource.getRepository(ProfileLeave); private leaveHistoryRepo = AppDataSource.getRepository(ProfileLeaveHistory); private leaveTypeRepository = AppDataSource.getRepository(LeaveType); // @Post("search") // public async searchProfile( // @Body() // body: { // citizenId?: string | null; // firstName?: string | null; // lastName?: string | null; // }, // ) { // const profileRepository = AppDataSource.getRepository(Profile); // const queryBuilder = profileRepository // .createQueryBuilder("profile") // .leftJoinAndSelect("profile.posLevel", "posLevel") // .leftJoinAndSelect("profile.posType", "posType"); // if (body.citizenId || body.firstName || body.lastName) { // queryBuilder.where( // new Brackets((qb) => { // if (body.citizenId) { // qb.orWhere("profile.citizenId LIKE :citizenId", { citizenId: `%${body.citizenId}%` }); // } // if (body.firstName) { // qb.orWhere("profile.firstName LIKE :firstName", { firstName: `%${body.firstName}%` }); // } // if (body.lastName) { // qb.orWhere("profile.lastName LIKE :lastName", { lastName: `%${body.lastName}%` }); // } // }), // ); // } // const profiles = await queryBuilder.getMany(); // if (!profiles.length) { // throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบข้อมูลโปรไฟล์"); // } // const formattedProfiles = profiles.map((profile) => ({ // avatar: profile.avatar, // avatarName: profile.avatarName, // rank: profile.rank, // prefix: profile.prefix, // firstName: profile.firstName, // lastName: profile.lastName, // citizenId: profile.citizenId, // position: profile.position, // posLevelId: profile.posLevelId, // posLevelName: profile.posLevel.posLevelName, // posTypeId: profile.posTypeId, // posTypeName: profile.posType.posTypeName, // email: profile.email, // phone: profile.phone, // keycloak: profile.keycloak, // isProbation: profile.isProbation, // isLeave: profile.isLeave, // leaveReason: profile.leaveReason, // dateRetire: profile.dateRetire, // dateAppoint: profile.dateAppoint, // dateRetireLaw: profile.dateRetireLaw, // dateStart: profile.dateStart, // govAgeAbsent: profile.govAgeAbsent, // govAgePlus: profile.govAgePlus, // birthDate: profile.birthDate, // reasonSameDate: profile.reasonSameDate, // ethnicity: profile.ethnicity, // telephoneNumber: profile.telephoneNumber, // nationality: profile.nationality, // gender: profile.gender, // relationship: profile.relationship, // religion: profile.religion, // bloodGroup: profile.bloodGroup, // })); // return new HttpSuccess(formattedProfiles); // } @Get("user") public async getLeaveUser(@Request() request: { user: Record }) { 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: { profileId: profile.id }, }); return new HttpSuccess(record); } @Get("{profileId}") @Example({ status: 200, message: "สำเร็จ", result: { id: "adbb08a6-d2f4-41b0-a9c1-49e883ca96bc", createdAt: "2024-03-20T23:35:45.230Z", createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", lastUpdatedAt: "2024-03-20T23:40:06.000Z", lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", createdFullName: "สาวิตรี ศรีสมัย", lastUpdateFullName: "สาวิตรี ศรีสมัย", profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", leaveTypeId: "8dc5e672-b416-4323-b086-06dde8c4353c", dateLeaveStart: "2024-03-21T06:39:46.000Z", dateLeaveEnd: "2024-03-21T06:39:46.000Z", leaveDays: 0, leaveCount: null, totalLeave: 0, status: "string", reason: "string", leaveType: { id: "8dc5e672-b416-4323-b086-06dde8c4353c", createdAt: "2024-02-04T21:28:40.536Z", createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", lastUpdatedAt: "2024-02-04T21:28:40.536Z", lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", createdFullName: "สาวิตรี ศรีสมัย", lastUpdateFullName: "สาวิตรี ศรีสมัย", name: "ลาป่วย", code: "CM-002", limit: 1, }, }, }) public async getLeave(@Path() profileId: string) { const record = await this.leaveRepo.find({ relations: { leaveType: true }, where: { profileId }, }); return new HttpSuccess(record); } @Get("history/{leaveId}") @Example({ status: 200, message: "สำเร็จ", result: [ { id: "7eed2e72-d71c-4b3b-a90b-e1b7abdaa838", createdAt: "2024-03-20T23:35:45.230Z", createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", lastUpdatedAt: "2024-03-20T23:40:06.000Z", lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", createdFullName: "สาวิตรี ศรีสมัย", lastUpdateFullName: "สาวิตรี ศรีสมัย", profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", leaveTypeId: "8dc5e672-b416-4323-b086-06dde8c4353c", dateLeaveStart: "2024-03-21T06:39:46.000Z", dateLeaveEnd: "2024-03-21T06:39:46.000Z", leaveDays: 0, leaveCount: null, totalLeave: 0, status: "string", reason: "string", leaveType: { id: "8dc5e672-b416-4323-b086-06dde8c4353c", createdAt: "2024-02-04T21:28:40.536Z", createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", lastUpdatedAt: "2024-02-04T21:28:40.536Z", lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", createdFullName: "สาวิตรี ศรีสมัย", lastUpdateFullName: "สาวิตรี ศรีสมัย", name: "ลาป่วย", code: "CM-002", limit: 1, }, profileLeaveId: "adbb08a6-d2f4-41b0-a9c1-49e883ca96bc", }, { id: "b1b9c291-9c96-4cbb-9309-6ff5a2a6e0e8", createdAt: "2024-03-20T23:35:45.230Z", createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", lastUpdatedAt: "2024-03-20T23:35:45.230Z", lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", createdFullName: "สาวิตรี ศรีสมัย", lastUpdateFullName: "สาวิตรี ศรีสมัย", profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", leaveTypeId: "7dc4e314-b456-4323-b086-06dde8c4353c", dateLeaveStart: "2024-03-21T06:34:49.000Z", dateLeaveEnd: "2024-03-21T06:34:49.000Z", leaveDays: 2, leaveCount: null, totalLeave: 200, status: "ไม่ผ่าน", reason: "ติดงานสำคัญ", leaveType: { id: "7dc4e314-b456-4323-b086-06dde8c4353c", createdAt: "2024-02-04T21:28:40.536Z", createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", lastUpdatedAt: "2024-02-04T21:28:40.536Z", lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", createdFullName: "สาวิตรี ศรีสมัย", lastUpdateFullName: "สาวิตรี ศรีสมัย", name: "ลาพักร้อน", code: "CM-001", limit: 356, }, profileLeaveId: "adbb08a6-d2f4-41b0-a9c1-49e883ca96bc", }, ], }) 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: CreateProfileLeave) { if (!body.profileId) { throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); } const profile = await this.profileRepo.findOneBy({ id: body.profileId }); 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(); } @Post("dump-db") public async newLeaveDumpDB(@Request() req: RequestWithUser, @Body() body: CreateProfileLeave) { if (!body.profileId) { throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); } const profile = await this.profileRepo.findOneBy({ id: body.profileId }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const leaveType = await this.leaveTypeRepository.findOne({ where: { name: 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 }); data.leaveTypeId = leaveType.id; await this.leaveRepo.save(data); return new HttpSuccess(); } }