แก้apiแสดงข้อมูลtree

This commit is contained in:
Kittapath 2024-01-31 11:00:51 +07:00
parent 49c468f53d
commit 9166faf102
6 changed files with 486 additions and 144 deletions

View file

@ -25,6 +25,14 @@ import { PosLevel } from "../entities/PosLevel";
import { CreatePosDict, PosDict } from "../entities/PosDict";
import HttpError from "../interfaces/http-error";
import { Like } from "typeorm";
import { CreatePosMaster, PosMaster } from "../entities/PosMaster";
import { OrgRevision } from "../entities/OrgRevision";
import { OrgRoot } from "../entities/OrgRoot";
import { OrgChild1 } from "../entities/OrgChild1";
import { OrgChild2 } from "../entities/OrgChild2";
import { OrgChild3 } from "../entities/OrgChild3";
import { OrgChild4 } from "../entities/OrgChild4";
import { Position } from "../entities/Position";
@Route("api/v1/org/pos")
@Tags("Position")
@Security("bearerAuth")
@ -38,6 +46,15 @@ export class PositionController extends Controller {
private posTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
private posDictRepository = AppDataSource.getRepository(PosDict);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private posPositionRepository = AppDataSource.getRepository(Position);
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3);
private child4Repository = AppDataSource.getRepository(OrgChild4);
/**
* API
@ -50,17 +67,13 @@ export class PositionController extends Controller {
{
id: "00000000-0000-0000-0000-000000000000",
posExecutiveName: "นักบริหาร",
posExecutivePriority: 1
posExecutivePriority: 1,
},
])
async GetPosExecutive() {
try {
const posExecutive = await this.posExecutiveRepository.find({
select: [
"id",
"posExecutiveName",
"posExecutivePriority"
]
select: ["id", "posExecutiveName", "posExecutivePriority"],
});
if (!posExecutive) {
return new HttpSuccess([]);
@ -85,37 +98,33 @@ export class PositionController extends Controller {
posTypeRank: 1,
posLevels: [
{
id : "00000000-0000-0000-0000-000000000000",
posLevelName : "นักบริหาร",
posLevelRank : 1,
posLevelAuthority : "HEAD",
}
]
id: "00000000-0000-0000-0000-000000000000",
posLevelName: "นักบริหาร",
posLevelRank: 1,
posLevelAuthority: "HEAD",
},
],
},
])
async GetPosType() {
try {
const posType = await this.posTypeRepository.find({
select: [
"id",
"posTypeName",
"posTypeRank"
],
select: ["id", "posTypeName", "posTypeRank"],
relations: ["posLevels"],
});
if (!posType) {
return new HttpSuccess([]);
}
const mapPosType = posType.map(item => ({
const mapPosType = posType.map((item) => ({
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
}))
posLevelRank: posLevel.posLevelRank,
posLevelAuthority: posLevel.posLevelAuthority,
})),
}));
return new HttpSuccess(mapPosType);
} catch (error) {
@ -139,26 +148,20 @@ export class PositionController extends Controller {
posTypes: {
id: "00000000-0000-0000-0000-000000000000",
posTypeName: "นักบริหาร",
posTypeRank: 1
}
posTypeRank: 1,
},
},
])
async GetPosLevel() {
try {
const posLevel = await this.posLevelRepository.find({
select: [
"id",
"posLevelName",
"posLevelRank",
"posLevelAuthority",
"posTypeId"
],
select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority", "posTypeId"],
relations: ["posType"],
});
if (!posLevel) {
return new HttpSuccess([]);
}
const mapPosLevel = posLevel.map(item => ({
const mapPosLevel = posLevel.map((item) => ({
id: item.id,
posLevelName: item.posLevelName,
posLevelRank: item.posLevelRank,
@ -167,7 +170,7 @@ export class PositionController extends Controller {
id: item.posType.id,
posTypeName: item.posType.posTypeName,
posTypeRank: item.posType.posTypeRank,
}
},
}));
return new HttpSuccess(mapPosLevel);
} catch (error) {
@ -190,29 +193,36 @@ export class PositionController extends Controller {
posLevelId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
posExecutiveId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
positionExecutiveField: "นักบริหาร",
positionArea: "บริหาร"
positionArea: "บริหาร",
},
])
async createPosition(
@Body()
requestBody: CreatePosDict,
@Request() request: { user: Record<string, any> },) {
async createPosition(
@Body()
requestBody: CreatePosDict,
@Request() request: { user: Record<string, any> },
) {
const posDict = Object.assign(new PosDict(), requestBody);
if (!posDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const checkPosTypeId = await this.posTypeRepository.findOne({where: { id: requestBody.posTypeId }});
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 }});
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 }});
const checkPosExecutiveId = await this.posExecutiveRepository.findOne({
where: { id: requestBody.posExecutiveId },
});
if (!checkPosExecutiveId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosExecutiveId");
}
@ -249,7 +259,7 @@ export class PositionController extends Controller {
return error;
}
}
/**
* API
*
@ -257,8 +267,7 @@ export class PositionController extends Controller {
*
*/
@Get("position")
async findPosition(@Query("keyword") keyword: string, @Query("type") type: string){
async findPosition(@Query("keyword") keyword: string, @Query("type") type: string) {
// { id: "positionName", name: "ตำแหน่งในสายงาน" }
// { id: "positionField", name: "สายงาน" }
// { id: "positionType", name: "ประเภทตำแหน่ง" }
@ -267,52 +276,301 @@ export class PositionController extends Controller {
// { id: "positionExecutiveField", name: "ด้านทางการบริหาร" }
// { id: "positionArea", name: "ด้าน/สาขา" }
try{
try {
let findPosDict: any;
console.log("type: ", type)
console.log("keyword: ", keyword)
console.log("type: ", type);
console.log("keyword: ", keyword);
switch(type){
switch (type) {
case "positionName":
findPosDict = await this.posDictRepository.find({ where: { posDictName: Like(`%${keyword}%`) } });
findPosDict = await this.posDictRepository.find({
where: { posDictName: Like(`%${keyword}%`) },
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword);
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
}
case "positionField":
findPosDict = await this.posDictRepository.find({ where: { posDictField: Like(`%${keyword}%`) } });
findPosDict = await this.posDictRepository.find({
where: { posDictField: Like(`%${keyword}%`) },
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword);
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
}
case "positionType":
findPosDict = await this.posDictRepository.find({ where: { posTypeId: Like(`%${keyword}%`) } });
findPosDict = await this.posDictRepository.find({
where: { posTypeId: Like(`%${keyword}%`) },
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword);
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
}
case "positionLevel":
findPosDict = await this.posDictRepository.find({ where: { posLevelId: Like(`%${keyword}%`) } });
findPosDict = await this.posDictRepository.find({
where: { posLevelId: Like(`%${keyword}%`) },
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword);
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
}
case "positionExecutive":
findPosDict = await this.posDictRepository.find({ where: { posExecutiveId: Like(`%${keyword}%`) } });
findPosDict = await this.posDictRepository.find({
where: { posExecutiveId: Like(`%${keyword}%`) },
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword);
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
}
case "positionExecutiveField":
findPosDict = await this.posDictRepository.find({ where: { posDictExecutiveField: Like(`%${keyword}%`) } });
findPosDict = await this.posDictRepository.find({
where: { posDictExecutiveField: Like(`%${keyword}%`) },
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword);
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
}
case "positionArea":
findPosDict = await this.posDictRepository.find({ where: { posDictArea: Like(`%${keyword}%`) } });
findPosDict = await this.posDictRepository.find({
where: { posDictArea: Like(`%${keyword}%`) },
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword);
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
}
}
return new HttpSuccess(findPosDict);
}
catch (error) {
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_033 - (ADMIN) #35
*
*/
@Post("master")
@Example({
posMasterNoPrefix: "กบ.",
posMasterNo: 1,
posMasterNoSuffix: "ช",
posId: ["08db9e81-fc46-4e95-8b33-be4ca0016abf", "08db9e81-fc46-4e95-8b33-be4ca0016abf"],
orgRootId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
orgChild1Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
orgChild2Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
orgChild3Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
orgChild4Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
positions: [
{
posDictName: "นักบริหาร",
posDictField: "บริหาร",
posTypeId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
posLevelId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
posExecutiveId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
posDictExecutiveField: "นักบริหาร",
posDictArea: "บริหาร",
},
],
})
async createMaster(
@Body()
requestBody: CreatePosMaster,
@Request() request: { user: Record<string, any> },
) {
const posMaster = Object.assign(new PosMaster(), requestBody);
if (!posMaster) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const orgRoot = await this.orgRootRepository.findOne({
where: { id: requestBody.orgRootId },
});
if (!orgRoot) {
const orgChild1 = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
if (!orgChild1) {
const orgChild2 = await this.child2Repository.findOne({
where: { id: requestBody.orgChild2Id },
});
if (!orgChild2) {
const orgChild3 = await this.child3Repository.findOne({
where: { id: requestBody.orgChild3Id },
});
if (!orgChild3) {
const orgChild4 = await this.child4Repository.findOne({
where: { id: requestBody.orgChild4Id },
});
if (!orgChild4) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้าง");
} else {
posMaster.orgRootId = orgChild4.orgRootId;
posMaster.orgChild1Id = orgChild4.orgChild1Id;
posMaster.orgChild2Id = orgChild4.orgChild2Id;
posMaster.orgChild3Id = orgChild4.orgChild3Id;
posMaster.orgChild4Id = orgChild4.id;
posMaster.orgRevisionId = orgChild4.orgRevisionId;
}
} else {
posMaster.orgRootId = orgChild3.orgRootId;
posMaster.orgChild1Id = orgChild3.orgChild1Id;
posMaster.orgChild2Id = orgChild3.orgChild2Id;
posMaster.orgChild3Id = orgChild3.id;
posMaster.orgRevisionId = orgChild3.orgRevisionId;
}
} else {
posMaster.orgRootId = orgChild2.orgRootId;
posMaster.orgChild1Id = orgChild2.orgChild1Id;
posMaster.orgChild2Id = orgChild2.id;
posMaster.orgRevisionId = orgChild2.orgRevisionId;
}
} else {
posMaster.orgRootId = orgChild1.orgRootId;
posMaster.orgChild1Id = orgChild1.id;
posMaster.orgRevisionId = orgChild1.orgRevisionId;
}
} else {
posMaster.orgRootId = orgRoot.id;
posMaster.orgRevisionId = orgRoot.orgRevisionId;
}
posMaster.createdUserId = request.user.sub;
posMaster.createdFullName = request.user.name;
posMaster.lastUpdateUserId = request.user.sub;
posMaster.lastUpdateFullName = request.user.name;
await this.posMasterRepository.save(posMaster);
requestBody.positions.forEach(async (x: any) => {
const position = Object.assign(new Position());
position.positionName = x.posDictName;
position.positionField = x.posDictField;
position.posTypeId = x.posTypeId;
position.posLevelId = x.posLevelId;
position.posExecutiveId = x.posExecutiveId;
position.positionExecutiveField = x.posDictExecutiveField;
position.positionArea = x.posDictArea;
position.positionIsSelected = false;
position.posMasterId = posMaster.id;
position.createdUserId = request.user.sub;
position.createdFullName = request.user.name;
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
await this.posPositionRepository.save(position);
});
return new HttpSuccess(posMaster.id);
}
/**
* API
*
* @summary ORG_033 - (ADMIN) #35
*
*/
@Put("master/{id}")
@Example({
posMasterNoPrefix: "กบ.",
posMasterNo: 1,
posMasterNoSuffix: "ช",
posId: ["08db9e81-fc46-4e95-8b33-be4ca0016abf", "08db9e81-fc46-4e95-8b33-be4ca0016abf"],
orgRootId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
orgChild1Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
orgChild2Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
orgChild3Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
orgChild4Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
positions: [
{
posDictName: "นักบริหาร",
posDictField: "บริหาร",
posTypeId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
posLevelId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
posExecutiveId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
posDictExecutiveField: "นักบริหาร",
posDictArea: "บริหาร",
},
],
})
async updateMaster(
@Path() id: string,
@Body()
requestBody: CreatePosMaster,
@Request() request: { user: Record<string, any> },
) {
const posMaster = await this.posMasterRepository.findOne({ where: { id: id } });
if (!posMaster) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลอัตรากำลัง");
}
const orgRoot = await this.orgRootRepository.findOne({
where: { id: requestBody.orgRootId },
});
if (!orgRoot) {
const orgChild1 = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
if (!orgChild1) {
const orgChild2 = await this.child2Repository.findOne({
where: { id: requestBody.orgChild2Id },
});
if (!orgChild2) {
const orgChild3 = await this.child3Repository.findOne({
where: { id: requestBody.orgChild3Id },
});
if (!orgChild3) {
const orgChild4 = await this.child4Repository.findOne({
where: { id: requestBody.orgChild4Id },
});
if (!orgChild4) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้าง");
} else {
posMaster.orgRootId = orgChild4.orgRootId;
posMaster.orgChild1Id = orgChild4.orgChild1Id;
posMaster.orgChild2Id = orgChild4.orgChild2Id;
posMaster.orgChild3Id = orgChild4.orgChild3Id;
posMaster.orgChild4Id = orgChild4.id;
posMaster.orgRevisionId = orgChild4.orgRevisionId;
}
} else {
posMaster.orgRootId = orgChild3.orgRootId;
posMaster.orgChild1Id = orgChild3.orgChild1Id;
posMaster.orgChild2Id = orgChild3.orgChild2Id;
posMaster.orgChild3Id = orgChild3.id;
posMaster.orgRevisionId = orgChild3.orgRevisionId;
}
} else {
posMaster.orgRootId = orgChild2.orgRootId;
posMaster.orgChild1Id = orgChild2.orgChild1Id;
posMaster.orgChild2Id = orgChild2.id;
posMaster.orgRevisionId = orgChild2.orgRevisionId;
}
} else {
posMaster.orgRootId = orgChild1.orgRootId;
posMaster.orgChild1Id = orgChild1.id;
posMaster.orgRevisionId = orgChild1.orgRevisionId;
}
} else {
posMaster.orgRootId = orgRoot.id;
posMaster.orgRevisionId = orgRoot.orgRevisionId;
}
posMaster.createdUserId = request.user.sub;
posMaster.createdFullName = request.user.name;
posMaster.lastUpdateUserId = request.user.sub;
posMaster.lastUpdateFullName = request.user.name;
await this.posMasterRepository.save(posMaster);
await this.posPositionRepository.delete({ posMasterId: posMaster.id });
requestBody.positions.forEach(async (x: any) => {
const position = Object.assign(new Position());
position.positionName = x.posDictName;
position.positionField = x.posDictField;
position.posTypeId = x.posTypeId;
position.posLevelId = x.posLevelId;
position.posExecutiveId = x.posExecutiveId;
position.positionExecutiveField = x.posDictExecutiveField;
position.positionArea = x.posDictArea;
position.positionIsSelected = false;
position.posMasterId = posMaster.id;
position.createdUserId = request.user.sub;
position.createdFullName = request.user.name;
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
await this.posPositionRepository.save(position);
});
return new HttpSuccess(posMaster.id);
}
}