From 3167fe271e97c0cc70e69496aaec2699c165b57a Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Mon, 27 May 2024 11:36:55 +0700 Subject: [PATCH] API checkRole and history/user --- src/controllers/ProfileAbilityController.ts | 13 ++++--- .../ProfileAbilityEmployeeController.ts | 5 +++ .../ProfileAssessmentsController.ts | 29 +++++++++++++++ .../ProfileAssessmentsEmployeeController.ts | 36 +++++++++++++++++-- .../ProfileCertificateController.ts | 21 +++++++++++ .../ProfileCertificateEmployeeController.ts | 21 +++++++++++ src/controllers/ProfileController.ts | 23 ++++++++++++ 7 files changed, 142 insertions(+), 6 deletions(-) diff --git a/src/controllers/ProfileAbilityController.ts b/src/controllers/ProfileAbilityController.ts index 9f795bd9..f0ad9a54 100644 --- a/src/controllers/ProfileAbilityController.ts +++ b/src/controllers/ProfileAbilityController.ts @@ -79,12 +79,17 @@ export class ProfileAbilityController extends Controller { return new HttpSuccess(getProfileAbilityId); } + /** + * + * @summary ประวัติแก้ไขความสามารถ by keycloak + * + */ @Get("history/user") public async getProfileAbilityHistoryUser(@Request() request: RequestWithUser) { - const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); - if (!profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - } + const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } const record = await this.profileAbilityHistoryRepo.find({ where: { histories: { diff --git a/src/controllers/ProfileAbilityEmployeeController.ts b/src/controllers/ProfileAbilityEmployeeController.ts index e3303a3d..6df1d56a 100644 --- a/src/controllers/ProfileAbilityEmployeeController.ts +++ b/src/controllers/ProfileAbilityEmployeeController.ts @@ -81,6 +81,11 @@ export class ProfileAbilityEmployeeController extends Controller { return new HttpSuccess(getProfileAbilityId); } + /** + * + * @summary ประวัติแก้ไขความสามารถ by keycloak + * + */ @Get("history/user") public async getProfileAbilityHistoryUser(@Request() request: RequestWithUser) { const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub }); diff --git a/src/controllers/ProfileAssessmentsController.ts b/src/controllers/ProfileAssessmentsController.ts index 5b008045..a53eb43c 100644 --- a/src/controllers/ProfileAssessmentsController.ts +++ b/src/controllers/ProfileAssessmentsController.ts @@ -87,6 +87,35 @@ export class ProfileAssessmentsController extends Controller { return new HttpSuccess(getProfileAssessments); } + /** + * + * @summary ประวัติแก้ไขการประเมิน by keycloak + * + */ + @Get("history/user") + public async getProfileAssessmentsHistoryUser(@Request() request: RequestWithUser) { + const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const record = await this.profileAssessmentsHistoryRepository.find({ + relations: { + histories: true, + }, + where: { + histories: { + profileId: profile.id, + }, + }, + }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(record); + } + @Get("history/{assessmentId}") @Example({ status: 200, diff --git a/src/controllers/ProfileAssessmentsEmployeeController.ts b/src/controllers/ProfileAssessmentsEmployeeController.ts index ef2fad00..7d39e5f5 100644 --- a/src/controllers/ProfileAssessmentsEmployeeController.ts +++ b/src/controllers/ProfileAssessmentsEmployeeController.ts @@ -36,7 +36,8 @@ import { RequestWithUser } from "../middlewares/user"; export class ProfileAssessmentsEmployeeController extends Controller { private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private profileAssessmentsRepository = AppDataSource.getRepository(ProfileAssessment); - private profileAssessmentsHistoryRepository = AppDataSource.getRepository(ProfileAssessmentHistory); + private profileAssessmentsHistoryRepository = + AppDataSource.getRepository(ProfileAssessmentHistory); @Get("user") public async detailProfileAssessmentsUser(@Request() request: { user: Record }) { @@ -79,13 +80,44 @@ export class ProfileAssessmentsEmployeeController extends Controller { ], }) public async detailProfileAssessments(@Path() profileEmployeeId: string) { - const getProfileAssessments = await this.profileAssessmentsRepository.findBy({ profileEmployeeId }); + const getProfileAssessments = await this.profileAssessmentsRepository.findBy({ + profileEmployeeId, + }); if (!getProfileAssessments) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } return new HttpSuccess(getProfileAssessments); } + /** + * + * @summary ประวัติแก้ไขการประเมิน by keycloak + * + */ + @Get("history/user") + public async getProfileAssessmentsHistoryUser(@Request() request: RequestWithUser) { + const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const record = await this.profileAssessmentsHistoryRepository.find({ + relations: { + histories: true, + }, + where: { + histories: { + profileEmployeeId: profile.id, + }, + }, + }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(record); + } + @Get("history/{assessmentId}") @Example({ status: 200, diff --git a/src/controllers/ProfileCertificateController.ts b/src/controllers/ProfileCertificateController.ts index 10185798..54517353 100644 --- a/src/controllers/ProfileCertificateController.ts +++ b/src/controllers/ProfileCertificateController.ts @@ -72,6 +72,27 @@ export class ProfileCertificateController extends Controller { return new HttpSuccess(record); } + /** + * + * @summary ประวัติแก้ไขใบรับรอง by keycloak + * + */ + @Get("history/user") + public async certificateHistoryUser(@Request() request: RequestWithUser) { + const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const record = await this.certificateHistoryRepo.find({ + where: { + histories: { + profileId: profile.id, + }, + }, + }); + return new HttpSuccess(record); + } + @Get("history/{certificateId}") @Example({ status: 200, diff --git a/src/controllers/ProfileCertificateEmployeeController.ts b/src/controllers/ProfileCertificateEmployeeController.ts index 5fbcfc55..764ea77d 100644 --- a/src/controllers/ProfileCertificateEmployeeController.ts +++ b/src/controllers/ProfileCertificateEmployeeController.ts @@ -72,6 +72,27 @@ export class ProfileCertificateEmployeeController extends Controller { return new HttpSuccess(record); } + /** + * + * @summary ประวัติแก้ไขใบรับรอง by keycloak + * + */ + @Get("history/user") + public async certificateHistoryUser(@Request() request: RequestWithUser) { + const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const record = await this.certificateHistoryRepo.find({ + where: { + histories: { + profileEmployeeId: profile.id, + }, + }, + }); + return new HttpSuccess(record); + } + @Get("history/{certificateId}") @Example({ status: 200, diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index c66ad90b..77d6ac21 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -803,6 +803,29 @@ export class ProfileController extends Controller { return new HttpSuccess(profile); } + @Get("type") + async checkRole(@Request() request: RequestWithUser) { + let role: any; + const checkProfile = await this.profileRepo.findOne({ + where: { keycloak: request.user.sub }, + }); + role = "OFFICER"; + if (!checkProfile) { + const checkEmployee = await this.profileEmpRepo.findOne({ + where: { keycloak: request.user.sub }, + select: ["employeeClass"], + }); + if (checkEmployee?.employeeClass === "PERM" || checkEmployee?.employeeClass === "TEMP") { + role = checkEmployee.employeeClass.toUpperCase(); + } else { + role = "EMPLOYEE"; + } + if (!checkProfile && !checkEmployee) + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); + } + return new HttpSuccess(role); + } + /** * API ประวัติการแก้ไขรายการทะเบียนประวัติ *