edit position

This commit is contained in:
AdisakKanthawilang 2024-02-20 18:04:13 +07:00
parent 7f01e34045
commit 352ced8053
2 changed files with 98 additions and 10 deletions

View file

@ -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<string, any> },
) {
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
*

View file

@ -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;