From ea31c06fba3c3ee65cf8a4139ab10a8aaf8ff5fb Mon Sep 17 00:00:00 2001 From: AnandaTon Date: Mon, 13 May 2024 17:17:33 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=82=E0=B9=89=E0=B8=AD=E0=B8=A1=E0=B8=B9?= =?UTF-8?q?=E0=B8=A5=E0=B8=A3=E0=B8=B2=E0=B8=8A=E0=B8=81=E0=B8=B2=E0=B8=A3?= =?UTF-8?q?=E0=B9=80=E0=B9=80=E0=B8=A5=E0=B8=B0=E0=B8=9A=E0=B8=B1=E0=B8=99?= =?UTF-8?q?=E0=B8=97=E0=B8=B6=E0=B8=81=E0=B9=84=E0=B8=A1=E0=B9=88=E0=B9=84?= =?UTF-8?q?=E0=B8=94=E0=B9=89=E0=B8=A3=E0=B8=B1=E0=B8=9A=E0=B9=80=E0=B8=87?= =?UTF-8?q?=E0=B8=B4=E0=B8=99=E0=B9=80=E0=B8=94=E0=B8=B7=E0=B8=AD=E0=B8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit วินัย การลา ปฎิบัติราชการพิเศษ --- .../ProfileEmployeeDisciplineController.ts | 185 ++++++++++++++ .../ProfileEmployeeDutyController.ts | 173 +++++++++++++ .../ProfileEmployeeLeaveController.ts | 237 ++++++++++++++++++ .../ProfileEmployeeNopaidController.ts | 153 +++++++++++ 4 files changed, 748 insertions(+) create mode 100644 src/controllers/ProfileEmployeeDisciplineController.ts create mode 100644 src/controllers/ProfileEmployeeDutyController.ts create mode 100644 src/controllers/ProfileEmployeeLeaveController.ts create mode 100644 src/controllers/ProfileEmployeeNopaidController.ts diff --git a/src/controllers/ProfileEmployeeDisciplineController.ts b/src/controllers/ProfileEmployeeDisciplineController.ts new file mode 100644 index 00000000..b311fa27 --- /dev/null +++ b/src/controllers/ProfileEmployeeDisciplineController.ts @@ -0,0 +1,185 @@ +import { + Body, + Controller, + Delete, + Example, + Get, + Patch, + Path, + Post, + Request, + Route, + Security, + Tags, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatus from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory"; +import { RequestWithUser } from "../middlewares/user"; +import { + CreateProfileDiscipline, + ProfileDiscipline, + UpdateProfileDiscipline, +} from "../entities/ProfileDiscipline"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; + +@Route("api/v1/org/profile-employee/discipline") +@Tags("ProfileDisciplineEmployee") +@Security("bearerAuth") +export class ProfileDisciplineEmployeeController extends Controller { + private profileRepository = AppDataSource.getRepository(ProfileEmployee); + private disciplineRepository = AppDataSource.getRepository(ProfileDiscipline); + private disciplineHistoryRepository = AppDataSource.getRepository(ProfileDisciplineHistory); + + @Get("{profileId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + date: "2024-03-12T10:09:47.000Z", + level: "string", + detail: "string", + unStigma: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + ], + }) + public async getDiscipline(@Path() profileId: string) { + const lists = await this.disciplineRepository.find({ + where: { profileEmployeeId: profileId }, + select: [ + "id", + "date", + "level", + "detail", + "unStigma", + "refCommandNo", + "refCommandDate", + "lastUpdateFullName", + "lastUpdatedAt", + ], + }); + return new HttpSuccess(lists); + } + + @Get("history/{disciplineId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + date: "2024-03-12T10:09:47.000Z", + level: "string", + detail: "string", + unStigma: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + { + id: "ba0e2f82-014e-46c6-8b82-a7c28eb5325f", + date: "2024-03-12T10:09:47.000Z", + level: "string", + detail: "string", + unStigma: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + ], + }) + public async disciplineHistory(@Path() disciplineId: string) { + const record = await this.disciplineHistoryRepository.find({ + where: { profileDisciplineId: disciplineId }, + select: [ + "id", + "date", + "level", + "detail", + "unStigma", + "refCommandNo", + "refCommandDate", + "lastUpdateFullName", + "lastUpdatedAt", + ], + order: { createdAt: "DESC" }, + }); + return new HttpSuccess(record); + } + + @Post() + public async newDiscipline( + @Request() req: RequestWithUser, + @Body() body: CreateProfileDiscipline, + ) { + if (!body.profileEmployeeId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepository.findOneBy({ id: body.profileEmployeeId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const data = new ProfileDiscipline(); + + 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.disciplineRepository.save(data); + + return new HttpSuccess(); + } + + @Patch("{disciplineId}") + public async editDiscipline( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileDiscipline, + @Path() disciplineId: string, + ) { + const record = await this.disciplineRepository.findOneBy({ id: disciplineId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileDisciplineHistory(); + + Object.assign(history, { ...record, id: undefined }); + Object.assign(record, body); + history.profileDisciplineId = disciplineId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([ + this.disciplineRepository.save(record), + this.disciplineHistoryRepository.save(history), + ]); + + return new HttpSuccess(); + } + + @Delete("{disciplineId}") + public async deleteTraning(@Path() disciplineId: string) { + await this.disciplineHistoryRepository.delete({ + profileDisciplineId: disciplineId, + }); + + const result = await this.disciplineRepository.delete({ id: disciplineId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} diff --git a/src/controllers/ProfileEmployeeDutyController.ts b/src/controllers/ProfileEmployeeDutyController.ts new file mode 100644 index 00000000..83c61952 --- /dev/null +++ b/src/controllers/ProfileEmployeeDutyController.ts @@ -0,0 +1,173 @@ +import { + Body, + Controller, + Delete, + Example, + Get, + Patch, + Path, + Post, + Request, + Route, + Security, + Tags, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatus from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import { ProfileDutyHistory } from "../entities/ProfileDutyHistory"; +import { RequestWithUser } from "../middlewares/user"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; +import { CreateProfileDuty, ProfileDuty, UpdateProfileDuty } from "../entities/ProfileDuty"; + +@Route("api/v1/org/profile/duty") +@Tags("ProfileDuty") +@Security("bearerAuth") +export class ProfileDutyController extends Controller { + private profileRepository = AppDataSource.getRepository(ProfileEmployee); + private dutyRepository = AppDataSource.getRepository(ProfileDuty); + private dutyHistoryRepository = AppDataSource.getRepository(ProfileDutyHistory); + + @Get("{profileId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + dateStart: "2024-03-12T10:09:47.000Z", + dateEnd: "string", + reference: "string", + detail: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + ], + }) + public async getDuty(@Path() profileId: string) { + const lists = await this.dutyRepository.find({ + where: { profileEmployeeId: profileId }, + select: [ + "id", + "dateStart", + "dateEnd", + "reference", + "detail", + "refCommandNo", + "refCommandDate", + ], + }); + return new HttpSuccess(lists); + } + + @Get("history/{dutyId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + dateStart: "2024-03-12T10:09:47.000Z", + dateEnd: "string", + reference: "string", + detail: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + dateStart: "2024-03-12T10:09:47.000Z", + dateEnd: "string", + reference: "string", + detail: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + ], + }) + public async dutyHistory(@Path() dutyId: string) { + const record = await this.dutyHistoryRepository.find({ + where: { profileDutyId: dutyId }, + select: [ + "id", + "dateStart", + "dateEnd", + "reference", + "detail", + "refCommandNo", + "refCommandDate", + "lastUpdateFullName", + "lastUpdatedAt", + ], + order: { createdAt: "DESC" }, + }); + return new HttpSuccess(record); + } + + @Post() + public async newDuty(@Request() req: RequestWithUser, @Body() body: CreateProfileDuty) { + if (!body.profileEmployeeId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepository.findOneBy({ id: body.profileEmployeeId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const data = new ProfileDuty(); + + 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.dutyRepository.save(data); + + return new HttpSuccess(); + } + + @Patch("{dutyId}") + public async editDuty( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileDuty, + @Path() dutyId: string, + ) { + const record = await this.dutyRepository.findOneBy({ id: dutyId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileDutyHistory(); + + Object.assign(history, { ...record, id: undefined }); + Object.assign(record, body); + history.profileDutyId = dutyId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([this.dutyRepository.save(record), this.dutyHistoryRepository.save(history)]); + + return new HttpSuccess(); + } + + @Delete("{dutyId}") + public async deleteTraning(@Path() dutyId: string) { + await this.dutyHistoryRepository.delete({ + profileDutyId: dutyId, + }); + + const result = await this.dutyRepository.delete({ id: dutyId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} diff --git a/src/controllers/ProfileEmployeeLeaveController.ts b/src/controllers/ProfileEmployeeLeaveController.ts new file mode 100644 index 00000000..bc49b8b9 --- /dev/null +++ b/src/controllers/ProfileEmployeeLeaveController.ts @@ -0,0 +1,237 @@ +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 { LeaveType } from "../entities/LeaveType"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; + +@Route("api/v1/org/profile/leave") +@Tags("ProfileLeave") +@Security("bearerAuth") +export class ProfileLeaveController extends Controller { + private profileRepo = AppDataSource.getRepository(ProfileEmployee); + private leaveRepo = AppDataSource.getRepository(ProfileLeave); + private leaveHistoryRepo = AppDataSource.getRepository(ProfileLeaveHistory); + private leaveTypeRepository = AppDataSource.getRepository(LeaveType); + + @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.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 deleteTraning(@Path() leaveId: string) { + await this.leaveHistoryRepo.delete({ + profileLeaveId: leaveId, + }); + + const result = await this.leaveRepo.delete({ id: leaveId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} diff --git a/src/controllers/ProfileEmployeeNopaidController.ts b/src/controllers/ProfileEmployeeNopaidController.ts new file mode 100644 index 00000000..2ac06c90 --- /dev/null +++ b/src/controllers/ProfileEmployeeNopaidController.ts @@ -0,0 +1,153 @@ +import { + Body, + Controller, + Delete, + Example, + Get, + Patch, + Path, + Post, + Request, + Route, + Security, + Tags, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatus from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import { ProfileNopaidHistory } from "../entities/ProfileNopaidHistory"; +import { RequestWithUser } from "../middlewares/user"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; +import { CreateProfileNopaid, ProfileNopaid, UpdateProfileNopaid } from "../entities/ProfileNopaid"; + +@Route("api/v1/org/profile/nopaid") +@Tags("ProfileNopaid") +@Security("bearerAuth") +export class ProfileNopaidController extends Controller { + private profileRepository = AppDataSource.getRepository(ProfileEmployee); + private nopaidRepository = AppDataSource.getRepository(ProfileNopaid); + private nopaidHistoryRepository = AppDataSource.getRepository(ProfileNopaidHistory); + + @Get("{profileId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + date: "2024-03-12T10:09:47.000Z", + reference: "string", + detail: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + ], + }) + public async getNopaid(@Path() profileId: string) { + const lists = await this.nopaidRepository.find({ + where: { profileId }, + }); + return new HttpSuccess(lists); + } + + @Get("history/{nopaidId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + date: "2024-03-12T10:09:47.000Z", + reference: "string", + detail: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + date: "2024-03-12T10:09:47.000Z", + reference: "string", + detail: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + ], + }) + public async nopaidHistory(@Path() nopaidId: string) { + const record = await this.nopaidHistoryRepository.find({ + where: { profileNopaidId: nopaidId }, + order: { createdAt: "DESC" }, + }); + return new HttpSuccess(record); + } + + @Post() + public async newNopaid(@Request() req: RequestWithUser, @Body() body: CreateProfileNopaid) { + if (!body.profileEmployeeId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepository.findOneBy({ id: body.profileEmployeeId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const data = new ProfileNopaid(); + + 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.nopaidRepository.save(data); + + return new HttpSuccess(); + } + + @Patch("{nopaidId}") + public async editNopaid( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileNopaid, + @Path() nopaidId: string, + ) { + const record = await this.nopaidRepository.findOneBy({ id: nopaidId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileNopaidHistory(); + + Object.assign(history, { ...record, id: undefined }); + Object.assign(record, body); + history.profileNopaidId = nopaidId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([ + this.nopaidRepository.save(record), + this.nopaidHistoryRepository.save(history), + ]); + + return new HttpSuccess(); + } + + @Delete("{nopaidId}") + public async deleteTraning(@Path() nopaidId: string) { + await this.nopaidHistoryRepository.delete({ + profileNopaidId: nopaidId, + }); + + const result = await this.nopaidRepository.delete({ id: nopaidId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +}