From be804c356aae28ffa53e6dd7a1560b3b7732bdde Mon Sep 17 00:00:00 2001 From: AnandaTon Date: Mon, 13 May 2024 17:38:21 +0700 Subject: [PATCH 1/4] =?UTF-8?q?=E0=B8=82=E0=B9=89=E0=B8=AD=E0=B8=A1?= =?UTF-8?q?=E0=B8=B9=E0=B8=A5=E0=B8=AD=E0=B8=B7=E0=B9=88=E0=B8=99=E0=B9=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProfileEmployeeOtherController.ts | 155 ++++++++++++++++++ src/entities/ProfileEmployee.ts | 6 +- src/entities/ProfileOther.ts | 19 +++ 3 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 src/controllers/ProfileEmployeeOtherController.ts diff --git a/src/controllers/ProfileEmployeeOtherController.ts b/src/controllers/ProfileEmployeeOtherController.ts new file mode 100644 index 00000000..548bd458 --- /dev/null +++ b/src/controllers/ProfileEmployeeOtherController.ts @@ -0,0 +1,155 @@ +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 { ProfileOtherHistory } from "../entities/ProfileOtherHistory"; +import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; +import { + CreateProfileEmployeeOther, + ProfileOther, + UpdateProfileOther, +} from "../entities/ProfileOther"; + +@Route("api/v1/org/profile/other") +@Tags("ProfileOther") +@Security("bearerAuth") +export class ProfileOtherController extends Controller { + private profileRepository = AppDataSource.getRepository(ProfileEmployee); + private otherRepository = AppDataSource.getRepository(ProfileOther); + private otherHistoryRepository = AppDataSource.getRepository(ProfileOtherHistory); + + @Get("{profileId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + isActive: true, + date: "2024-03-12T10:09:47.000Z", + reference: "string", + detail: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + ], + }) + public async getOther(@Path() profileId: string) { + const lists = await this.otherRepository.find({ + where: { profileEmployeeId: profileId }, + }); + return new HttpSuccess(lists); + } + + @Get("history/{otherId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + isActive: true, + date: "2024-03-12T10:09:47.000Z", + detail: "string", + }, + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + isActive: true, + date: "2024-03-12T10:09:47.000Z", + detail: "string", + }, + ], + }) + public async otherHistory(@Path() otherId: string) { + const record = await this.otherHistoryRepository.find({ + where: { profileOtherId: otherId }, + order: { createdAt: "DESC" }, + }); + return new HttpSuccess(record); + } + + @Post() + public async newOther(@Request() req: RequestWithUser, @Body() body: CreateProfileEmployeeOther) { + 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 ProfileOther(); + + 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.otherRepository.save(data); + + return new HttpSuccess(); + } + + @Patch("{otherId}") + public async editOther( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileOther, + @Path() otherId: string, + ) { + const record = await this.otherRepository.findOneBy({ id: otherId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileOtherHistory(); + + Object.assign(history, { ...record, id: undefined }); + Object.assign(record, body); + history.profileOtherId = otherId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([ + this.otherRepository.save(record), + this.otherHistoryRepository.save(history), + ]); + + return new HttpSuccess(); + } + + @Delete("{otherId}") + public async deleteTraning(@Path() otherId: string) { + await this.otherHistoryRepository.delete({ + profileOtherId: otherId, + }); + + const result = await this.otherRepository.delete({ id: otherId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} diff --git a/src/entities/ProfileEmployee.ts b/src/entities/ProfileEmployee.ts index 614828f7..6490a24b 100644 --- a/src/entities/ProfileEmployee.ts +++ b/src/entities/ProfileEmployee.ts @@ -18,6 +18,7 @@ import { ProfileChangeName } from "./ProfileChangeName"; import { ProfileFamilyHistory } from "./ProfileFamily"; import { ProfileEducation } from "./ProfileEducation"; import { ProfileAbility } from "./ProfileAbility"; +import { ProfileOther } from "./ProfileOther"; @Entity("profileEmployee") export class ProfileEmployee extends EntityBase { @Column({ @@ -248,6 +249,9 @@ export class ProfileEmployee extends EntityBase { @OneToMany(() => ProfileAbility, (v) => v.profileEmployee) profileAbilities: ProfileAbility[]; + @OneToMany(() => ProfileOther, (v) => v.profileEmployee) + profileOthers: ProfileOther[]; + @ManyToOne(() => EmployeePosLevel, (v) => v.profiles) posLevel: EmployeePosLevel; @@ -291,7 +295,7 @@ export class CreateProfileEmployee { gender: string | null; relationship: string | null; bloodGroup: string | null; - email: string | null ; + email: string | null; phone: string | null; } diff --git a/src/entities/ProfileOther.ts b/src/entities/ProfileOther.ts index db84c3d2..b9ac7a34 100644 --- a/src/entities/ProfileOther.ts +++ b/src/entities/ProfileOther.ts @@ -1,6 +1,7 @@ import { Entity, Column, OneToMany, ManyToOne, JoinColumn } from "typeorm"; import { EntityBase } from "./base/Base"; import { Profile } from "./Profile"; +import { ProfileEmployee } from "./ProfileEmployee"; import { ProfileOtherHistory } from "./ProfileOtherHistory"; @Entity("profileOther") @@ -13,6 +14,14 @@ export class ProfileOther extends EntityBase { }) profileId: string; + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง ProfileEmployee", + default: null, + }) + profileEmployeeId: string; + @Column({ nullable: true, comment: "รายละเอียด", @@ -35,6 +44,10 @@ export class ProfileOther extends EntityBase { @ManyToOne(() => Profile, (profile) => profile.profileOthers) @JoinColumn({ name: "profileId" }) profile: Profile; + + @ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileOthers) + @JoinColumn({ name: "profileEmployeeId" }) + profileEmployee: ProfileEmployee; } export class CreateProfileOther { @@ -43,6 +56,12 @@ export class CreateProfileOther { date: Date | null; } +export class CreateProfileEmployeeOther { + profileEmployeeId: string | null; + detail: string | null; + date: Date | null; +} + export type UpdateProfileOther = { detail?: string | null; date?: Date | null; From 660deeb3a4b29fdbff21dd779afb65a979c705bf Mon Sep 17 00:00:00 2001 From: Kittapath Date: Mon, 13 May 2024 17:52:55 +0700 Subject: [PATCH 2/4] migrate --- ...18469-update_table_profileemployee_add_pk3.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/migration/1715597418469-update_table_profileemployee_add_pk3.ts diff --git a/src/migration/1715597418469-update_table_profileemployee_add_pk3.ts b/src/migration/1715597418469-update_table_profileemployee_add_pk3.ts new file mode 100644 index 00000000..5e1c4c3b --- /dev/null +++ b/src/migration/1715597418469-update_table_profileemployee_add_pk3.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableProfileemployeeAddPk31715597418469 implements MigrationInterface { + name = 'UpdateTableProfileemployeeAddPk31715597418469' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileOther\` ADD \`profileEmployeeId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง ProfileEmployee'`); + await queryRunner.query(`ALTER TABLE \`profileOther\` ADD CONSTRAINT \`FK_b7ef3007bce554a41bdb896ed96\` FOREIGN KEY (\`profileEmployeeId\`) REFERENCES \`profileEmployee\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileOther\` DROP FOREIGN KEY \`FK_b7ef3007bce554a41bdb896ed96\``); + await queryRunner.query(`ALTER TABLE \`profileOther\` DROP COLUMN \`profileEmployeeId\``); + } + +} From 6127a594bd0de32a184e3f10ffbef762ddca2550 Mon Sep 17 00:00:00 2001 From: Bright Date: Mon, 13 May 2024 17:59:11 +0700 Subject: [PATCH 3/4] rename controller --- ...sController.ts => ProfileAssessmentsEmployeeController.ts} | 4 ++-- ...eController.ts => ProfileCertificateEmployeeController.ts} | 4 ++-- ...eeHonorController.ts => ProfileHonorEmployeeController.ts} | 4 ++-- ...gniaController.ts => ProfileInsigniaEmployeeController.ts} | 4 ++-- ...ningController.ts => ProfileTrainingEmployeeController.ts} | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) rename src/controllers/{ProfileEmployeeAssessmentsController.ts => ProfileAssessmentsEmployeeController.ts} (98%) rename src/controllers/{ProfileEmployeeCertificateController.ts => ProfileCertificateEmployeeController.ts} (97%) rename src/controllers/{ProfileEmployeeHonorController.ts => ProfileHonorEmployeeController.ts} (97%) rename src/controllers/{ProfileEmployeeInsigniaController.ts => ProfileInsigniaEmployeeController.ts} (98%) rename src/controllers/{ProfileEmployeeTrainingController.ts => ProfileTrainingEmployeeController.ts} (97%) diff --git a/src/controllers/ProfileEmployeeAssessmentsController.ts b/src/controllers/ProfileAssessmentsEmployeeController.ts similarity index 98% rename from src/controllers/ProfileEmployeeAssessmentsController.ts rename to src/controllers/ProfileAssessmentsEmployeeController.ts index 3862b106..4f65867d 100644 --- a/src/controllers/ProfileEmployeeAssessmentsController.ts +++ b/src/controllers/ProfileAssessmentsEmployeeController.ts @@ -33,7 +33,7 @@ import { RequestWithUser } from "../middlewares/user"; @Route("api/v1/org/profile-employee/assessments") @Tags("ProfileEmployeeAssessments") @Security("bearerAuth") -export class ProfileEmployeeAssessmentsController extends Controller { +export class ProfileAssessmentsEmployeeController extends Controller { private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private profileAssessmentsRepository = AppDataSource.getRepository(ProfileAssessment); private profileAssessmentsHistoryRepository = @@ -188,7 +188,7 @@ export class ProfileEmployeeAssessmentsController extends Controller { const result = await this.profileAssessmentsRepository.delete({ id: assessmentId }); - if (result.affected && result.affected <= 0) + if (result.affected == undefined || result.affected <= 0) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); return new HttpSuccess(); diff --git a/src/controllers/ProfileEmployeeCertificateController.ts b/src/controllers/ProfileCertificateEmployeeController.ts similarity index 97% rename from src/controllers/ProfileEmployeeCertificateController.ts rename to src/controllers/ProfileCertificateEmployeeController.ts index 03d05af9..255d38af 100644 --- a/src/controllers/ProfileEmployeeCertificateController.ts +++ b/src/controllers/ProfileCertificateEmployeeController.ts @@ -28,7 +28,7 @@ import { ProfileEmployee } from "../entities/ProfileEmployee"; @Route("api/v1/org/profile-employee/certificate") @Tags("ProfileEmployeeCertificate") @Security("bearerAuth") -export class ProfileEmployeeCertificateController extends Controller { +export class ProfileCertificateEmployeeController extends Controller { private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private certificateRepo = AppDataSource.getRepository(ProfileCertificate); private certificateHistoryRepo = AppDataSource.getRepository(ProfileCertificateHistory); @@ -171,7 +171,7 @@ export class ProfileEmployeeCertificateController extends Controller { id: certificateId, }); - if (certificateResult.affected && certificateResult.affected <= 0) { + if (certificateResult.affected == undefined || certificateResult.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } diff --git a/src/controllers/ProfileEmployeeHonorController.ts b/src/controllers/ProfileHonorEmployeeController.ts similarity index 97% rename from src/controllers/ProfileEmployeeHonorController.ts rename to src/controllers/ProfileHonorEmployeeController.ts index 77728751..44157de8 100644 --- a/src/controllers/ProfileEmployeeHonorController.ts +++ b/src/controllers/ProfileHonorEmployeeController.ts @@ -24,7 +24,7 @@ import { ProfileEmployee } from "../entities/ProfileEmployee"; @Route("api/v1/org/profile-employee/honor") @Tags("ProfileEmployeeHonor") @Security("bearerAuth") -export class ProfileEmployeeHonorController extends Controller { +export class ProfileHonorEmployeeController extends Controller { private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private honorRepo = AppDataSource.getRepository(ProfileHonor); private honorHistoryRepo = AppDataSource.getRepository(ProfileHonorHistory); @@ -162,7 +162,7 @@ export class ProfileEmployeeHonorController extends Controller { const result = await this.honorRepo.delete({ id: honorId }); - if (result.affected && result.affected <= 0) { + if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } diff --git a/src/controllers/ProfileEmployeeInsigniaController.ts b/src/controllers/ProfileInsigniaEmployeeController.ts similarity index 98% rename from src/controllers/ProfileEmployeeInsigniaController.ts rename to src/controllers/ProfileInsigniaEmployeeController.ts index c4340606..4089c8a1 100644 --- a/src/controllers/ProfileEmployeeInsigniaController.ts +++ b/src/controllers/ProfileInsigniaEmployeeController.ts @@ -29,7 +29,7 @@ import { Insignia } from "../entities/Insignia"; @Route("api/v1/org/profile-employee/insignia") @Tags("ProfileEmployeeInsignia") @Security("bearerAuth") -export class ProfileEmployeeInsigniaController extends Controller { +export class ProfileInsigniaEmployeeController extends Controller { private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private insigniaRepo = AppDataSource.getRepository(ProfileInsignia); private insigniaHistoryRepo = AppDataSource.getRepository(ProfileInsigniaHistory); @@ -220,7 +220,7 @@ export class ProfileEmployeeInsigniaController extends Controller { const result = await this.insigniaRepo.delete({ id: insigniaId }); - if (result.affected && result.affected <= 0) { + if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } diff --git a/src/controllers/ProfileEmployeeTrainingController.ts b/src/controllers/ProfileTrainingEmployeeController.ts similarity index 97% rename from src/controllers/ProfileEmployeeTrainingController.ts rename to src/controllers/ProfileTrainingEmployeeController.ts index 662e0708..8e8c76d4 100644 --- a/src/controllers/ProfileEmployeeTrainingController.ts +++ b/src/controllers/ProfileTrainingEmployeeController.ts @@ -28,7 +28,7 @@ import { ProfileEmployee } from "../entities/ProfileEmployee"; @Route("api/v1/org/profile-employee/training") @Tags("ProfileEmployeeTraining") @Security("bearerAuth") -export class ProfileEmployeeTrainingController extends Controller { +export class ProfileTrainingEmployeeController extends Controller { private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private trainingRepo = AppDataSource.getRepository(ProfileTraining); private trainingHistoryRepo = AppDataSource.getRepository(ProfileTrainingHistory); @@ -184,7 +184,7 @@ export class ProfileEmployeeTrainingController extends Controller { const result = await this.trainingRepo.delete({ id: trainingId }); - if (result.affected && result.affected <= 0) { + if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } From 56d620d02fc9a8de95dab608c2477ae41f7d87a4 Mon Sep 17 00:00:00 2001 From: AnandaTon Date: Mon, 13 May 2024 18:07:20 +0700 Subject: [PATCH 4/4] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B9=84?= =?UTF-8?q?=E0=B8=82=E0=B8=8A=E0=B8=B7=E0=B9=88=E0=B8=AD=20=E0=B9=81?= =?UTF-8?q?=E0=B8=A5=E0=B8=B0=E0=B9=80=E0=B8=87=E0=B8=B7=E0=B9=88=E0=B8=AD?= =?UTF-8?q?=E0=B8=99=E0=B9=84=E0=B8=82=E0=B8=95=E0=B8=AD=E0=B8=99=E0=B8=A5?= =?UTF-8?q?=E0=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...=> ProfileDisciplineEmployeeController.ts} | 26 +- ...er.ts => ProfileDutyEmployeeController.ts} | 45 +--- .../ProfileEmployeeLeaveController.ts | 237 ------------------ .../ProfileLeaveEmployeeController.ts | 135 ++++++++++ ....ts => ProfileNopaidEmployeeController.ts} | 28 +-- ...r.ts => ProfileOtherEmployeeController.ts} | 40 +-- 6 files changed, 145 insertions(+), 366 deletions(-) rename src/controllers/{ProfileEmployeeDisciplineController.ts => ProfileDisciplineEmployeeController.ts} (86%) rename src/controllers/{ProfileEmployeeDutyController.ts => ProfileDutyEmployeeController.ts} (75%) delete mode 100644 src/controllers/ProfileEmployeeLeaveController.ts create mode 100644 src/controllers/ProfileLeaveEmployeeController.ts rename src/controllers/{ProfileEmployeeNopaidController.ts => ProfileNopaidEmployeeController.ts} (83%) rename src/controllers/{ProfileEmployeeOtherController.ts => ProfileOtherEmployeeController.ts} (77%) diff --git a/src/controllers/ProfileEmployeeDisciplineController.ts b/src/controllers/ProfileDisciplineEmployeeController.ts similarity index 86% rename from src/controllers/ProfileEmployeeDisciplineController.ts rename to src/controllers/ProfileDisciplineEmployeeController.ts index 1f2479cf..9dc1ff47 100644 --- a/src/controllers/ProfileEmployeeDisciplineController.ts +++ b/src/controllers/ProfileDisciplineEmployeeController.ts @@ -68,30 +68,6 @@ export class ProfileDisciplineEmployeeController extends Controller { } @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 }, @@ -176,7 +152,7 @@ export class ProfileDisciplineEmployeeController extends Controller { const result = await this.disciplineRepository.delete({ id: disciplineId }); - if (result.affected && result.affected <= 0) { + if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } diff --git a/src/controllers/ProfileEmployeeDutyController.ts b/src/controllers/ProfileDutyEmployeeController.ts similarity index 75% rename from src/controllers/ProfileEmployeeDutyController.ts rename to src/controllers/ProfileDutyEmployeeController.ts index 62a42975..25f40f0e 100644 --- a/src/controllers/ProfileEmployeeDutyController.ts +++ b/src/controllers/ProfileDutyEmployeeController.ts @@ -21,30 +21,15 @@ import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { CreateProfileEmployeeDuty, ProfileDuty, UpdateProfileDuty } from "../entities/ProfileDuty"; -@Route("api/v1/org/profile/duty") +@Route("api/v1/org/profile-employee/duty") @Tags("ProfileDuty") @Security("bearerAuth") -export class ProfileDutyController extends Controller { +export class ProfileDutyEmployeeController 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 }, @@ -62,30 +47,6 @@ export class ProfileDutyController extends Controller { } @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 }, @@ -164,7 +125,7 @@ export class ProfileDutyController extends Controller { const result = await this.dutyRepository.delete({ id: dutyId }); - if (result.affected && result.affected <= 0) { + if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } diff --git a/src/controllers/ProfileEmployeeLeaveController.ts b/src/controllers/ProfileEmployeeLeaveController.ts deleted file mode 100644 index 6cc1c302..00000000 --- a/src/controllers/ProfileEmployeeLeaveController.ts +++ /dev/null @@ -1,237 +0,0 @@ -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/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: 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 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/ProfileLeaveEmployeeController.ts b/src/controllers/ProfileLeaveEmployeeController.ts new file mode 100644 index 00000000..2a88b9a6 --- /dev/null +++ b/src/controllers/ProfileLeaveEmployeeController.ts @@ -0,0 +1,135 @@ +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("{profileId}") + public async getLeave(@Path() profileId: string) { + const record = await this.leaveRepo.find({ + relations: { leaveType: true }, + where: { 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 deleteTraning(@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(); + } +} diff --git a/src/controllers/ProfileEmployeeNopaidController.ts b/src/controllers/ProfileNopaidEmployeeController.ts similarity index 83% rename from src/controllers/ProfileEmployeeNopaidController.ts rename to src/controllers/ProfileNopaidEmployeeController.ts index 6868bada..f7bba569 100644 --- a/src/controllers/ProfileEmployeeNopaidController.ts +++ b/src/controllers/ProfileNopaidEmployeeController.ts @@ -25,10 +25,10 @@ import { UpdateProfileNopaid, } from "../entities/ProfileNopaid"; -@Route("api/v1/org/profile/nopaid") +@Route("api/v1/org/profile-employee/nopaid") @Tags("ProfileNopaid") @Security("bearerAuth") -export class ProfileNopaidController extends Controller { +export class ProfileNopaidEmployeeController extends Controller { private profileRepository = AppDataSource.getRepository(ProfileEmployee); private nopaidRepository = AppDataSource.getRepository(ProfileNopaid); private nopaidHistoryRepository = AppDataSource.getRepository(ProfileNopaidHistory); @@ -56,28 +56,6 @@ export class ProfileNopaidController extends Controller { } @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 }, @@ -151,7 +129,7 @@ export class ProfileNopaidController extends Controller { const result = await this.nopaidRepository.delete({ id: nopaidId }); - if (result.affected && result.affected <= 0) { + if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } diff --git a/src/controllers/ProfileEmployeeOtherController.ts b/src/controllers/ProfileOtherEmployeeController.ts similarity index 77% rename from src/controllers/ProfileEmployeeOtherController.ts rename to src/controllers/ProfileOtherEmployeeController.ts index 548bd458..c6312bba 100644 --- a/src/controllers/ProfileEmployeeOtherController.ts +++ b/src/controllers/ProfileOtherEmployeeController.ts @@ -18,7 +18,6 @@ import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { ProfileOtherHistory } from "../entities/ProfileOtherHistory"; import { RequestWithUser } from "../middlewares/user"; -import { Profile } from "../entities/Profile"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { CreateProfileEmployeeOther, @@ -26,30 +25,15 @@ import { UpdateProfileOther, } from "../entities/ProfileOther"; -@Route("api/v1/org/profile/other") +@Route("api/v1/org/profile-employee/other") @Tags("ProfileOther") @Security("bearerAuth") -export class ProfileOtherController extends Controller { +export class ProfileOtherEmployeeController extends Controller { private profileRepository = AppDataSource.getRepository(ProfileEmployee); private otherRepository = AppDataSource.getRepository(ProfileOther); private otherHistoryRepository = AppDataSource.getRepository(ProfileOtherHistory); @Get("{profileId}") - @Example({ - status: 200, - message: "สำเร็จ", - result: [ - { - id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", - isActive: true, - date: "2024-03-12T10:09:47.000Z", - reference: "string", - detail: "string", - refCommandNo: "string", - refCommandDate: "2024-03-12T10:09:47.000Z", - }, - ], - }) public async getOther(@Path() profileId: string) { const lists = await this.otherRepository.find({ where: { profileEmployeeId: profileId }, @@ -58,24 +42,6 @@ export class ProfileOtherController extends Controller { } @Get("history/{otherId}") - @Example({ - status: 200, - message: "สำเร็จ", - result: [ - { - id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", - isActive: true, - date: "2024-03-12T10:09:47.000Z", - detail: "string", - }, - { - id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", - isActive: true, - date: "2024-03-12T10:09:47.000Z", - detail: "string", - }, - ], - }) public async otherHistory(@Path() otherId: string) { const record = await this.otherHistoryRepository.find({ where: { profileOtherId: otherId }, @@ -146,7 +112,7 @@ export class ProfileOtherController extends Controller { const result = await this.otherRepository.delete({ id: otherId }); - if (result.affected && result.affected <= 0) { + if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); }