diff --git a/src/controllers/RelationshipController.ts b/src/controllers/RelationshipController.ts new file mode 100644 index 00000000..13b7a96d --- /dev/null +++ b/src/controllers/RelationshipController.ts @@ -0,0 +1,141 @@ +import { + Controller, + Post, + Put, + Delete, + Route, + Security, + Tags, + Body, + Path, + Request, + SuccessResponse, + Response, + Get, + } from "tsoa"; + import { AppDataSource } from "../database/data-source"; + import HttpSuccess from "../interfaces/http-success"; + import HttpStatusCode from "../interfaces/http-status"; + import HttpError from "../interfaces/http-error"; + import { CreateRelationship, Relationship } from "../entities/Relationship"; + @Route("api/v1/org/relationship") + @Tags("Relationship") + @Security("bearerAuth") + @Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", + ) + @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") + export class RelationshipController extends Controller { + private relationshipRepository = AppDataSource.getRepository(Relationship); + + /** + * API สร้างสถานภาพ + * + * @summary ORG_060 - สร้างสถานภาพ (ADMIN) #65 + * + */ + @Post("relationship") + async createRelationship( + @Body() + requestBody: CreateRelationship, + @Request() request: { user: Record }, + ) { + const relationship = Object.assign(new Relationship(), requestBody); + if (!relationship) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + try { + relationship.createdUserId = request.user.sub; + relationship.createdFullName = request.user.name; + relationship.lastUpdateUserId = request.user.sub; + relationship.lastUpdateFullName = request.user.name; + await this.relationshipRepository.save(relationship); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API แก้ไขสถานภาพ + * + * @summary ORG_060 - แก้ไขสถานภาพ (ADMIN) #65 + * + * @param {string} id Id ตำแหน่งทางการบริหาร + */ + @Put("relationship/{id}") + async updateRelationship( + @Path() id: string, + @Body() + requestBody: CreateRelationship, + @Request() request: { user: Record }, + ) { + const relationship = await this.relationshipRepository.findOne({where: {id: id}}); + if (!relationship) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); + } + try { + relationship.lastUpdateUserId = request.user.sub; + relationship.lastUpdateFullName = request.user.name; + this.relationshipRepository.merge(relationship, requestBody); + await this.relationshipRepository.save(relationship); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + // /** + // * API ลบอัตรากำลัง + // * + // * @summary ORG_043 - ลบตำแหน่งทางการบริหาร (ADMIN) #46 + // * + // * @param {string} id Id ตำแหน่งทางการบริหาร + // */ + // @Delete("executive/{id}") + // async deletePosExecutive(@Path() id: string) { + // const delPosExecutive = await this.posExecutiveRepository.findOne({ + // where: { id }, + // }); + // if (!delPosExecutive) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); + // } + // try { + // await this.positionRepository.delete({ posExecutiveId: id }); + // await this.posExecutiveRepository.delete({ id }); + // return new HttpSuccess(); + // } catch (error) { + // return error; + // } + // } + + // /** + // * API รายละเอียดตำแหน่งทางการบริหาร + // * + // * @summary ORG_044 - รายละเอียดตำแหน่งทางการบริหาร (ADMIN) #47 + // * + // * @param {string} id Id ตำแหน่งทางการบริหาร + // */ + // @Get("executive/{id}") + // async detailPosExecutive(@Path() id: string) { + + // const posExecutive = await this.posExecutiveRepository.findOne({ + // where: { id }, + // select:[ + // "id", + // "posExecutiveName", + // "posExecutivePriority" + // ] + // }); + // if (!posExecutive) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + // } + // try { + // return new HttpSuccess(posExecutive); + // } catch (error) { + // return error; + // } + // } + } + \ No newline at end of file diff --git a/src/entities/BloodGroup.ts b/src/entities/BloodGroup.ts index fcf66db4..004c5095 100644 --- a/src/entities/BloodGroup.ts +++ b/src/entities/BloodGroup.ts @@ -14,7 +14,7 @@ export class BloodGroup extends EntityBase { export class CreateBloodGroup { @Column() - bloodGroup: string; + name: string; } export type UpdateBloodGroup = Partial; diff --git a/src/entities/EducationLevel.ts b/src/entities/EducationLevel.ts index bddc8530..13ed34f2 100644 --- a/src/entities/EducationLevel.ts +++ b/src/entities/EducationLevel.ts @@ -21,7 +21,7 @@ export class EducationLevel extends EntityBase { export class CreateEducationLevel { @Column() - educationLevel: string; + name: string; @Column() rank: number; diff --git a/src/entities/Gender.ts b/src/entities/Gender.ts index 625bbd4b..dd9cae1e 100644 --- a/src/entities/Gender.ts +++ b/src/entities/Gender.ts @@ -14,7 +14,7 @@ export class Gender extends EntityBase { export class CreateGender { @Column() - gender: string; + name: string; } export type UpdateGender = Partial; diff --git a/src/entities/Prefixe.ts b/src/entities/Prefixe.ts index 3d26ba10..96faa92f 100644 --- a/src/entities/Prefixe.ts +++ b/src/entities/Prefixe.ts @@ -14,7 +14,7 @@ export class Prefixe extends EntityBase { export class CreatePrefixe { @Column() - prefixe: string; + name: string; } export type UpdatePrefixe = Partial; diff --git a/src/entities/Rank.ts b/src/entities/Rank.ts index 6953a304..3d31d73e 100644 --- a/src/entities/Rank.ts +++ b/src/entities/Rank.ts @@ -14,7 +14,7 @@ export class Rank extends EntityBase { export class CreateRank { @Column() - rank: string; + name: string; } export type UpdateRank = Partial; diff --git a/src/entities/Relationship.ts b/src/entities/Relationship.ts index 08f0d026..6a2bbc85 100644 --- a/src/entities/Relationship.ts +++ b/src/entities/Relationship.ts @@ -14,7 +14,7 @@ export class Relationship extends EntityBase { export class CreateRelationship { @Column() - relationship: string; + name: string; } export type UpdateRelationship = Partial; diff --git a/src/entities/Religion.ts b/src/entities/Religion.ts index 96f6c0f2..04840bf4 100644 --- a/src/entities/Religion.ts +++ b/src/entities/Religion.ts @@ -14,7 +14,7 @@ export class Religion extends EntityBase { export class CreateReligion { @Column() - religion: string; + name: string; } export type UpdateReligion = Partial;