From 352ced8053f3ab6a150695a0c6bb7e7d110c09da Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 20 Feb 2024 18:04:13 +0700 Subject: [PATCH] edit position --- src/controllers/PositionController.ts | 96 +++++++++++++++++++++++++-- src/entities/PosDict.ts | 12 ++-- 2 files changed, 98 insertions(+), 10 deletions(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 4044f891..55fad487 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 } from "../entities/PosDict"; +import { CreatePosDict, 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"; @@ -98,9 +98,9 @@ export class PositionController extends Controller { if (!checkPosLevelId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosLevelId"); } - + const _null:any = null; if (posDict.posExecutiveId == "") { - posDict.posExecutiveId = null; + posDict.posExecutiveId = _null; } if (posDict.posExecutiveId != null) { @@ -139,7 +139,95 @@ export class PositionController extends Controller { return error; } } - + + /** + * API แก้ไขตำแหน่ง + * + * @summary แก้ไขตำแหน่ง (ADMIN) + * + */ + @Put("position/{id}") + @Example([ + { + positionName: "นักบริหาร", + positionField: "บริหาร", + posTypeId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", + posLevelId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", + posExecutiveId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", + positionExecutiveField: "นักบริหาร", + positionArea: "บริหาร", + }, + ]) + async updatePosition( + @Path() id : string, + @Body() + requestBody: UpdatePosDict, + @Request() request: { user: Record }, + ) { + + const posDict = await this.posDictRepository.findOne({ + where:{id:id} + }); + 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, "ไม่พบข้อมูล PosTypeId"); + } + + const checkPosLevelId = await this.posLevelRepository.findOne({ + where: { id: posDict.posLevelId }, + }); + if (!checkPosLevelId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosLevelId"); + } + + const _null:any = null; + if (posDict.posExecutiveId == "") { + posDict.posExecutiveId = _null; + } + + if (posDict.posExecutiveId != null) { + const checkPosExecutiveId = await this.posExecutiveRepository.findOne({ + where: { id: posDict.posExecutiveId }, + }); + if (!checkPosExecutiveId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosExecutiveId"); + } + } + + const rowRepeated = await this.posDictRepository.findOne({ + where: { + id: Not(id), + posDictName: posDict.posDictName, + posDictField: posDict.posDictField, + posTypeId: posDict.posTypeId, + posLevelId: posDict.posLevelId, + posExecutiveId: String(posDict.posExecutiveId), + posDictExecutiveField: posDict.posDictExecutiveField, + posDictArea: posDict.posDictArea, + isSpecial: posDict.isSpecial + }, + }); + if (rowRepeated) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว"); + } + + try { + posDict.lastUpdateUserId = request.user.sub; + posDict.lastUpdateFullName = request.user.name; + this.posDictRepository.merge(posDict, requestBody); + await this.posDictRepository.save(posDict); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + /** * API ลบตำแหน่ง * diff --git a/src/entities/PosDict.ts b/src/entities/PosDict.ts index a7f1b768..2ad52379 100644 --- a/src/entities/PosDict.ts +++ b/src/entities/PosDict.ts @@ -40,7 +40,7 @@ export class PosDict extends EntityBase { comment: "ตำแหน่งทางการบริหาร", default: null, }) - posExecutiveId: string | null; + posExecutiveId: string; @Column({ nullable: true, @@ -84,20 +84,20 @@ export class CreatePosDict { @Column() posDictField: string; - @Column() + @Column("uuid") posTypeId: string; - @Column() + @Column("uuid") posLevelId: string; @Column() - posExecutiveId: string | null; + posExecutiveId: string; @Column() - posDictExecutiveField: string | null; + posDictExecutiveField: string; @Column() - posDictArea: string | null; + posDictArea: string; @Column() isSpecial: boolean;