diff --git a/src/controllers/OrgChild2Controller.ts b/src/controllers/OrgChild2Controller.ts index 8b374c85..572d68e3 100644 --- a/src/controllers/OrgChild2Controller.ts +++ b/src/controllers/OrgChild2Controller.ts @@ -58,18 +58,18 @@ export class OrgChild2Controller extends Controller { } try { const getOrgChild2 = { - "orgChild2Id" : orgChild2.id, - "orgChild2Name" : orgChild2.orgChild2Name, - "orgChild2ShortName" : orgChild2.orgChild2ShortName, - "orgChild2Code" : orgChild2.orgChild2Code, - "orgChild2Rank" : orgChild2.orgChild2Rank, - "orgChild2Order" : orgChild2.orgChild2Order, - "orgChild2PhoneEx" : orgChild2.orgChild2PhoneEx, - "orgChild2PhoneIn" : orgChild2.orgChild2PhoneIn, - "orgChild2Fax" : orgChild2.orgChild2Fax, - "orgRevisionId" : orgChild2.orgRevisionId, - "orgCode" : orgRoot.orgRootCode + orgChild2.orgChild2Code - } + orgChild2Id: orgChild2.id, + orgChild2Name: orgChild2.orgChild2Name, + orgChild2ShortName: orgChild2.orgChild2ShortName, + orgChild2Code: orgChild2.orgChild2Code, + orgChild2Rank: orgChild2.orgChild2Rank, + orgChild2Order: orgChild2.orgChild2Order, + orgChild2PhoneEx: orgChild2.orgChild2PhoneEx, + orgChild2PhoneIn: orgChild2.orgChild2PhoneIn, + orgChild2Fax: orgChild2.orgChild2Fax, + orgRevisionId: orgChild2.orgRevisionId, + orgCode: orgRoot.orgRootCode + orgChild2.orgChild2Code, + }; return new HttpSuccess(getOrgChild2); } catch (error) { return error; @@ -105,7 +105,7 @@ export class OrgChild2Controller extends Controller { where: { id: requestBody.orgChild1Id }, }); if (!child1) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id"); } const revisionIdExits = await this.orgRevisionRepository.findOne({ @@ -114,8 +114,14 @@ export class OrgChild2Controller extends Controller { if (!revisionIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + if ( + revisionIdExits.orgRevisionIsDraft != true && + revisionIdExits.orgRevisionIsCurrent != false + ) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false", + ); } const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"]; @@ -179,8 +185,14 @@ export class OrgChild2Controller extends Controller { if (!revisionIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + if ( + revisionIdExits.orgRevisionIsDraft != true && + revisionIdExits.orgRevisionIsCurrent != false + ) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false", + ); } const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"]; @@ -223,15 +235,21 @@ export class OrgChild2Controller extends Controller { if (!child2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } - + const revisionIdExits = await this.orgRevisionRepository.findOne({ where: { id: child2.orgRevisionId }, }); if (!revisionIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + if ( + revisionIdExits.orgRevisionIsDraft != true && + revisionIdExits.orgRevisionIsCurrent != false + ) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false", + ); } const exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } }); diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 1ef8ea2c..cc388abf 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -21,6 +21,8 @@ import HttpStatusCode from "../interfaces/http-status"; import { PosExecutive } from "../entities/PosExecutive"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; +import { CreatePosition, Position } from "../entities/Position"; +import HttpError from "../interfaces/http-error"; @Route("api/v1/org/pos") @Tags("Position") @Security("bearerAuth") @@ -31,8 +33,9 @@ import { PosLevel } from "../entities/PosLevel"; @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class PositionController extends Controller { private posExecutiveRepository = AppDataSource.getRepository(PosExecutive); - private posTypeRepository = AppDataSource.getRepository(PosType) + private posTypeRepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); + private positionRepository = AppDataSource.getRepository(Position); /** * API รายการตำแหน่งทางการบริหาร @@ -162,4 +165,60 @@ export class PositionController extends Controller { return error; } } + + /** + * API เพิ่มตำแหน่ง + * + * @summary ORG_030 - เพิ่มตำแหน่ง (ADMIN) #33 + * + */ + @Post("position") + @Example([ + { + positionName: "นักบริหาร", + positionField: "บริหาร", + posTypeId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", + posLevelId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", + posExecutiveId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", + positionExecutiveField: "นักบริหาร", + positionArea: "บริหาร" + }, + ]) + async createPosition( + @Body() + requestBody: CreatePosition, + @Request() request: { user: Record },) { + const position = Object.assign(new Position(), requestBody); + if (!position) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const checkPosTypeId = await this.posTypeRepository.findOne({where: { id: requestBody.posTypeId }}); + if (!checkPosTypeId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosTypeId"); + } + + const checkPosLevelId = await this.posLevelRepository.findOne({where: { id: requestBody.posLevelId }}); + if (!checkPosLevelId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosLevelId"); + } + + const checkPosExecutiveId = await this.posExecutiveRepository.findOne({where: { id: requestBody.posExecutiveId }}); + if (!checkPosExecutiveId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosExecutiveId"); + } + + try { + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + await this.positionRepository.save(position); + return new HttpSuccess(position.id); + } catch (error) { + return error; + } + } + + } diff --git a/src/entities/Position.ts b/src/entities/Position.ts index 4df27021..6d3b5f89 100644 --- a/src/entities/Position.ts +++ b/src/entities/Position.ts @@ -3,71 +3,94 @@ import { EntityBase } from "./base/Base"; @Entity("position") export class Position extends EntityBase { - @Column({ - nullable: true, - comment: "ชื่อตำแหน่งในสายงาน (ชื่อตำแหน่ง)", - length: 255, - default: "string", - }) - positionName: string; - - @Column({ - nullable: true, - comment: "สายงาน", - length: 45, - default: "string", - }) - positionField: string; + @Column({ + nullable: true, + comment: "ชื่อตำแหน่งในสายงาน (ชื่อตำแหน่ง)", + length: 255, + default: "string", + }) + positionName: string; - @Column({ - length: 40, - comment: "ประเภทตำแหน่ง", - default: "00000000-0000-0000-0000-000000000000", - }) - posTypeId: string; + @Column({ + nullable: true, + comment: "สายงาน", + length: 45, + default: "string", + }) + positionField: string; - @Column({ - length: 40, - comment: "ระดับตำแหน่ง", - default: "00000000-0000-0000-0000-000000000000", - }) - posLevelId: string; + @Column({ + length: 40, + comment: "ประเภทตำแหน่ง", + default: "00000000-0000-0000-0000-000000000000", + }) + posTypeId: string; - @Column({ - nullable: true, - length: 40, - comment: "ตำแหน่งทางการบริหาร", - default: "00000000-0000-0000-0000-000000000000", - }) - posExecutiveId: string; + @Column({ + length: 40, + comment: "ระดับตำแหน่ง", + default: "00000000-0000-0000-0000-000000000000", + }) + posLevelId: string; - @Column({ - nullable: true, - comment: "ด้านทางการบริหาร", - length: 255, - default: "string", - }) - positionExecutiveField: string; - - @Column({ - nullable: true, - comment: "ด้าน/สาขา", - length: 255, - default: "string", - }) - positionArea: string; + @Column({ + nullable: true, + length: 40, + comment: "ตำแหน่งทางการบริหาร", + default: "00000000-0000-0000-0000-000000000000", + }) + posExecutiveId: string; - @Column({ - nullable: true, - comment: "เป็นตำแหน่งที่ถูกเลือกในรอบนั้นๆ หรือไม่?", - }) - positionIsSelected: boolean; + @Column({ + nullable: true, + comment: "ด้านทางการบริหาร", + length: 255, + default: "string", + }) + positionExecutiveField: string; - @Column({ - length: 40, - comment: "เชื่อมโยงกับตารางเลขที่ตำแหน่ง", - default: "00000000-0000-0000-0000-000000000000", - }) - posMasterId: string; + @Column({ + nullable: true, + comment: "ด้าน/สาขา", + length: 255, + default: "string", + }) + positionArea: string; -} \ No newline at end of file + @Column({ + nullable: true, + comment: "เป็นตำแหน่งที่ถูกเลือกในรอบนั้นๆ หรือไม่?", + }) + positionIsSelected: boolean; + + @Column({ + length: 40, + comment: "เชื่อมโยงกับตารางเลขที่ตำแหน่ง", + default: "00000000-0000-0000-0000-000000000000", + }) + posMasterId: string; +} +export class CreatePosition { + @Column() + positionName: string; + + @Column() + positionField: string; + + @Column() + posTypeId: string; + + @Column() + posLevelId: string; + + @Column() + posExecutiveId: string; + + @Column() + positionExecutiveField: string; + + @Column() + positionArea: string; +} + +export type UpdatePosition = Partial; \ No newline at end of file