diff --git a/src/controllers/PosExecutiveController.ts b/src/controllers/PosExecutiveController.ts index 4e5b5b78..eee0e55e 100644 --- a/src/controllers/PosExecutiveController.ts +++ b/src/controllers/PosExecutiveController.ts @@ -80,7 +80,7 @@ export class PosExecutiveController extends Controller { posExecutive.lastUpdateUserId = request.user.sub; posExecutive.lastUpdateFullName = request.user.name; await this.posExecutiveRepository.save(posExecutive); - return new HttpSuccess(); + return new HttpSuccess(posExecutive.id); } /** diff --git a/src/controllers/PosTypeController.ts b/src/controllers/PosTypeController.ts index 38523097..950d7645 100644 --- a/src/controllers/PosTypeController.ts +++ b/src/controllers/PosTypeController.ts @@ -62,10 +62,7 @@ export class PosTypeController extends Controller { }, }); if (chkPosTypeName) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ชื่อประเภทตำแหน่งนี้มีอยู่ในระบบแล้ว", - ); + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อประเภทตำแหน่งนี้มีอยู่ในระบบแล้ว"); } posType.createdUserId = request.user.sub; posType.createdFullName = request.user.name; @@ -103,10 +100,7 @@ export class PosTypeController extends Controller { }, }); if (chkPosTypeName) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ชื่อประเภทตำแหน่งนี้มีอยู่ในระบบแล้ว", - ); + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อประเภทตำแหน่งนี้มีอยู่ในระบบแล้ว"); } posType.lastUpdateUserId = request.user.sub; posType.lastUpdateFullName = request.user.name; @@ -179,12 +173,14 @@ export class PosTypeController extends Controller { id: getPosType.id, posTypeName: getPosType.posTypeName, posTypeRank: getPosType.posTypeRank, - posLevels: getPosType.posLevels.map((posLevel) => ({ - id: posLevel.id, - posLevelName: posLevel.posLevelName, - posLevelRank: posLevel.posLevelRank, - posLevelAuthority: posLevel.posLevelAuthority, - })), + posLevels: getPosType.posLevels + .sort((a, b) => a.posLevelRank - b.posLevelRank) + .map((posLevel) => ({ + id: posLevel.id, + posLevelName: posLevel.posLevelName, + posLevelRank: posLevel.posLevelRank, + posLevelAuthority: posLevel.posLevelAuthority, + })), }; return new HttpSuccess(mapGetPosType); @@ -216,6 +212,7 @@ export class PosTypeController extends Controller { const posType = await this.posTypeRepository.find({ select: ["id", "posTypeName", "posTypeRank"], relations: ["posLevels"], + order: { posTypeRank: "ASC" }, }); // if (!posType) { // return new HttpSuccess([]); @@ -224,12 +221,14 @@ export class PosTypeController extends Controller { id: item.id, posTypeName: item.posTypeName, posTypeRank: item.posTypeRank, - posLevels: item.posLevels.map((posLevel) => ({ - id: posLevel.id, - posLevelName: posLevel.posLevelName, - posLevelRank: posLevel.posLevelRank, - posLevelAuthority: posLevel.posLevelAuthority, - })), + posLevels: item.posLevels + .sort((a, b) => a.posLevelRank - b.posLevelRank) + .map((posLevel) => ({ + id: posLevel.id, + posLevelName: posLevel.posLevelName, + posLevelRank: posLevel.posLevelRank, + posLevelAuthority: posLevel.posLevelAuthority, + })), })); return new HttpSuccess(mapPosType); } diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 79d0cca5..929e02a9 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -22,7 +22,7 @@ import HttpStatusCode from "../interfaces/http-status"; import { PosExecutive } from "../entities/PosExecutive"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; -import { CreatePosDict, PosDict, UpdatePosDict } from "../entities/PosDict"; +import { CreatePosDict, CreatePosDictExe, PosDict, UpdatePosDict } from "../entities/PosDict"; import HttpError from "../interfaces/http-error"; import { Equal, ILike, In, IsNull, Like, Not, Brackets } from "typeorm"; import { CreatePosMaster, PosMaster } from "../entities/PosMaster"; @@ -136,6 +136,99 @@ export class PositionController extends Controller { return new HttpSuccess(posDict.id); } + /** + * API เพิ่มตำแหน่ง + * + * @summary ORG_030 - เพิ่มตำแหน่ง (ADMIN) #33 + * + */ + @Post("position/executive") + @Example([ + { + positionName: "นักบริหาร", + positionField: "บริหาร", + posTypeId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", + posLevelId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", + posExecutiveId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", + positionExecutiveField: "นักบริหาร", + positionArea: "บริหาร", + }, + ]) + async createPositionNameExe( + @Body() + requestBody: CreatePosDictExe, + @Request() request: { user: Record }, + ) { + const posDict = Object.assign(new PosDict(), requestBody); + if (!posDict) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const checkPosTypeId = await this.posTypeRepository.findOne({ + where: { id: posDict.posTypeId }, + }); + if (!checkPosTypeId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); + } + + const checkPosLevelId = await this.posLevelRepository.findOne({ + where: { id: posDict.posLevelId }, + }); + if (!checkPosLevelId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosLevelId"); + } + + let posExecutive: any = null; + if (requestBody.posExecutive != null && requestBody.posExecutive != "") { + const checkName = await this.posExecutiveRepository.findOne({ + where: { posExecutiveName: requestBody.posExecutive }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + posExecutive.posExecutiveName = requestBody.posExecutive; + + const checkPriority = await this.posExecutiveRepository.findOne({ + order: { posExecutivePriority: "DESC" }, + }); + if (checkPriority == null) { + posExecutive.posExecutivePriority = 1; + } else { + posExecutive.posExecutivePriority = checkPriority.posExecutivePriority + 1; + } + + posExecutive.createdUserId = request.user.sub; + posExecutive.createdFullName = request.user.name; + posExecutive.lastUpdateUserId = request.user.sub; + posExecutive.lastUpdateFullName = request.user.name; + await this.posExecutiveRepository.save(posExecutive); + } + + const rowRepeated = await this.posDictRepository.findOne({ + where: { + posDictName: posDict.posDictName, + posDictField: posDict.posDictField, + posTypeId: posDict.posTypeId, + posLevelId: posDict.posLevelId, + posExecutiveId: posExecutive == null ? null : posExecutive.id, + posDictExecutiveField: posDict.posDictExecutiveField, + posDictArea: posDict.posDictArea, + isSpecial: posDict.isSpecial, + }, + }); + if (rowRepeated) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว"); + } + + posDict.createdUserId = request.user.sub; + posDict.createdFullName = request.user.name; + posDict.lastUpdateUserId = request.user.sub; + posDict.lastUpdateFullName = request.user.name; + await this.posDictRepository.save(posDict); + return new HttpSuccess(posDict.id); + } + /** * API แก้ไขตำแหน่ง * diff --git a/src/entities/PosDict.ts b/src/entities/PosDict.ts index 544d860a..9de18e35 100644 --- a/src/entities/PosDict.ts +++ b/src/entities/PosDict.ts @@ -103,6 +103,32 @@ export class CreatePosDict { isSpecial: boolean; } +export class CreatePosDictExe { + @Column() + posDictName: string | null; + + @Column() + posDictField: string | null; + + @Column("uuid") + posTypeId: string | null; + + @Column("uuid") + posLevelId: string | null; + + @Column() + posExecutive?: string; + + @Column() + posDictExecutiveField: string | null; + + @Column() + posDictArea: string | null; + + @Column() + isSpecial: boolean; +} + export class UpdatePosDict { @Column() posDictName: string;