แก้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

@ -56,18 +56,18 @@ export class OrgChild3Controller {
} }
try { try {
const getOrgChild3 = { const getOrgChild3 = {
"orgChild3Id" : orgChild3.id, orgChild3Id: orgChild3.id,
"orgChild3Name" : orgChild3.orgChild3Name, orgChild3Name: orgChild3.orgChild3Name,
"orgChild3ShortName" : orgChild3.orgChild3ShortName, orgChild3ShortName: orgChild3.orgChild3ShortName,
"orgChild3Code" : orgChild3.orgChild3Code, orgChild3Code: orgChild3.orgChild3Code,
"orgChild3Rank" : orgChild3.orgChild3Rank, orgChild3Rank: orgChild3.orgChild3Rank,
"orgChild3Order" : orgChild3.orgChild3Order, orgChild3Order: orgChild3.orgChild3Order,
"orgChild3PhoneEx" : orgChild3.orgChild3PhoneEx, orgChild3PhoneEx: orgChild3.orgChild3PhoneEx,
"orgChild3PhoneIn" : orgChild3.orgChild3PhoneIn, orgChild3PhoneIn: orgChild3.orgChild3PhoneIn,
"orgChild3Fax" : orgChild3.orgChild3Fax, orgChild3Fax: orgChild3.orgChild3Fax,
"orgRevisionId" : orgChild3.orgRevisionId, orgRevisionId: orgChild3.orgRevisionId,
"orgCode" : orgRoot.orgRootCode + orgChild3.orgChild3Code orgCode: orgRoot.orgRootCode + orgChild3.orgChild3Code,
} };
return new HttpSuccess(getOrgChild3); return new HttpSuccess(getOrgChild3);
} catch (error) { } catch (error) {
return error; return error;
@ -98,8 +98,14 @@ export class OrgChild3Controller {
if (!revisionIdExits) { if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
} }
if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ if (
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); revisionIdExits.orgRevisionIsDraft != true &&
revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
} }
const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"]; const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"];
@ -150,8 +156,14 @@ export class OrgChild3Controller {
if (!revisionIdExits) { if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
} }
if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ if (
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); revisionIdExits.orgRevisionIsDraft != true &&
revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
} }
const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"]; const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"];
@ -202,8 +214,14 @@ export class OrgChild3Controller {
if (!revisionIdExits) { if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
} }
if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ if (
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); revisionIdExits.orgRevisionIsDraft != true &&
revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
} }
const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } }); const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } });

View file

@ -39,42 +39,42 @@ export class OrgChild4Controller extends Controller {
private child4Repository = AppDataSource.getRepository(OrgChild4); private child4Repository = AppDataSource.getRepository(OrgChild4);
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
/** /**
* API 4 * API 4
* *
* @summary ORG_019 - 4 (ADMIN) #26 * @summary ORG_019 - 4 (ADMIN) #26
* *
* @param {string} id Id Child4 * @param {string} id Id Child4
*/ */
@Get("{id}") @Get("{id}")
async GetChild4(@Path() id: string) { async GetChild4(@Path() id: string) {
const orgChild4 = await this.child4Repository.findOne({ where: { id } }); const orgChild4 = await this.child4Repository.findOne({ where: { id } });
if (!orgChild4) { if (!orgChild4) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 4"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 4");
} }
const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild4.orgRootId } }); const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild4.orgRootId } });
if (!orgRoot) { if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root");
} }
try { try {
const getOrgChild4 = { const getOrgChild4 = {
"orgChild4Id" : orgChild4.id, orgChild4Id: orgChild4.id,
"orgChild4Name" : orgChild4.orgChild4Name, orgChild4Name: orgChild4.orgChild4Name,
"orgChild4ShortName" : orgChild4.orgChild4ShortName, orgChild4ShortName: orgChild4.orgChild4ShortName,
"orgChild4Code" : orgChild4.orgChild4Code, orgChild4Code: orgChild4.orgChild4Code,
"orgChild4Rank" : orgChild4.orgChild4Rank, orgChild4Rank: orgChild4.orgChild4Rank,
"orgChild4Order" : orgChild4.orgChild4Order, orgChild4Order: orgChild4.orgChild4Order,
"orgChild4PhoneEx" : orgChild4.orgChild4PhoneEx, orgChild4PhoneEx: orgChild4.orgChild4PhoneEx,
"orgChild4PhoneIn" : orgChild4.orgChild4PhoneIn, orgChild4PhoneIn: orgChild4.orgChild4PhoneIn,
"orgChild4Fax" : orgChild4.orgChild4Fax, orgChild4Fax: orgChild4.orgChild4Fax,
"orgRevisionId" : orgChild4.orgRevisionId, orgRevisionId: orgChild4.orgRevisionId,
"orgCode" : orgRoot.orgRootCode + orgChild4.orgChild4Code orgCode: orgRoot.orgRootCode + orgChild4.orgChild4Code,
} };
return new HttpSuccess(getOrgChild4); return new HttpSuccess(getOrgChild4);
} catch (error) { } catch (error) {
return error; return error;
} }
} }
/** /**
* 4 Child4 * 4 Child4
@ -101,27 +101,33 @@ export class OrgChild4Controller extends Controller {
requestBody: CreateOrgChild4, requestBody: CreateOrgChild4,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const child3 = await this.child3Repository.findOne({ const child3 = await this.child3Repository.findOne({
where: { id: requestBody.orgChild3Id }, where: { id: requestBody.orgChild3Id },
}); });
if (!child3) { if (!child3) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
} }
const revisionIdExits = await this.orgRevisionRepository.findOne({ const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child3.orgRevisionId }, where: { id: child3.orgRevisionId },
}); });
if (!revisionIdExits) { if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
} }
if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ if (
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); revisionIdExits.orgRevisionIsDraft != true &&
} revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
}
const validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"]; const validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (!validOrgChild4Ranks.includes(requestBody.orgChild4Rank.toUpperCase())) { if (!validOrgChild4Ranks.includes(requestBody.orgChild4Rank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild4Rank"); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild4Rank");
} }
try { try {
const child4 = Object.assign(new OrgChild4(), requestBody) as OrgChild4; const child4 = Object.assign(new OrgChild4(), requestBody) as OrgChild4;
child4.orgChild4Name = requestBody.orgChild4Name; child4.orgChild4Name = requestBody.orgChild4Name;
@ -182,8 +188,14 @@ export class OrgChild4Controller extends Controller {
if (!revisionIdExits) { if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
} }
if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ if (
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); revisionIdExits.orgRevisionIsDraft != true &&
revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
} }
const validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"]; const validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"];
@ -235,8 +247,14 @@ export class OrgChild4Controller extends Controller {
if (!revisionIdExits) { if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
} }
if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ if (
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); revisionIdExits.orgRevisionIsDraft != true &&
revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
} }
try { try {
await this.child4Repository.remove(child4); await this.child4Repository.remove(child4);

View file

@ -97,7 +97,7 @@ export class OrgRootController extends Controller {
} }
const chkCode = await this.orgRootRepository.findOne({ const chkCode = await this.orgRootRepository.findOne({
where: { orgRevisionId: requestBody.orgRevisionId , orgRootCode: requestBody.orgRootCode }, where: { orgRevisionId: requestBody.orgRevisionId, orgRootCode: requestBody.orgRootCode },
}); });
if (chkCode != null) { if (chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
@ -176,7 +176,7 @@ export class OrgRootController extends Controller {
} }
const chkCode = await this.orgRootRepository.findOne({ const chkCode = await this.orgRootRepository.findOne({
where: { orgRevisionId: requestBody.orgRevisionId , orgRootCode: requestBody.orgRootCode }, where: { orgRevisionId: requestBody.orgRevisionId, orgRootCode: requestBody.orgRootCode },
}); });
if (chkCode?.id != id && chkCode != null) { if (chkCode?.id != id && chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
@ -184,7 +184,10 @@ export class OrgRootController extends Controller {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } }); const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) { if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้างระดับ Root ตามไอดีนี้ :" + id); throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลโครงสร้างระดับ Root ตามไอดีนี้ :" + id,
);
} }
try { try {
orgRoot.lastUpdateUserId = request.user.sub; orgRoot.lastUpdateUserId = request.user.sub;
@ -210,7 +213,10 @@ export class OrgRootController extends Controller {
async delete(@Path() id: string) { async delete(@Path() id: string) {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } }); const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) { if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้างระดับ Root ตามไอดีนี้ :" + id); throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลโครงสร้างระดับ Root ตามไอดีนี้ :" + id,
);
} }
const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } }); const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } });

View file

@ -555,12 +555,14 @@ export class OrganizationController extends Controller {
orgTreePhoneIn: orgRoot.orgRootPhoneIn, orgTreePhoneIn: orgRoot.orgRootPhoneIn,
orgTreeFax: orgRoot.orgRootFax, orgTreeFax: orgRoot.orgRootFax,
orgRevisionId: orgRoot.orgRevisionId, orgRevisionId: orgRoot.orgRevisionId,
orgRootName: orgRoot.orgRootName,
children: orgChild1Data children: orgChild1Data
.filter((orgChild1) => orgChild1.orgRootId === orgRoot.id) .filter((orgChild1) => orgChild1.orgRootId === orgRoot.id)
.map((orgChild1) => ({ .map((orgChild1) => ({
orgTreeId: orgChild1.id, orgTreeId: orgChild1.id,
orgRootId: orgRoot.id, orgRootId: orgRoot.id,
orgLevel: 1, orgLevel: 1,
orgName: orgChild1.orgChild1Name,
orgTreeName: orgChild1.orgChild1Name, orgTreeName: orgChild1.orgChild1Name,
orgTreeShortName: orgChild1.orgChild1ShortName, orgTreeShortName: orgChild1.orgChild1ShortName,
orgTreeCode: orgChild1.orgChild1Code, orgTreeCode: orgChild1.orgChild1Code,
@ -572,12 +574,14 @@ export class OrganizationController extends Controller {
orgTreePhoneIn: orgChild1.orgChild1PhoneIn, orgTreePhoneIn: orgChild1.orgChild1PhoneIn,
orgTreeFax: orgChild1.orgChild1Fax, orgTreeFax: orgChild1.orgChild1Fax,
orgRevisionId: orgRoot.orgRevisionId, orgRevisionId: orgRoot.orgRevisionId,
orgRootName: orgRoot.orgRootName,
children: orgChild2Data children: orgChild2Data
.filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id) .filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id)
.map((orgChild2) => ({ .map((orgChild2) => ({
orgTreeId: orgChild2.id, orgTreeId: orgChild2.id,
orgRootId: orgChild1.id, orgRootId: orgChild1.id,
orgLevel: 2, orgLevel: 2,
orgName: `${orgChild1.orgChild1Name}/${orgChild2.orgChild2Name}`,
orgTreeName: orgChild2.orgChild2Name, orgTreeName: orgChild2.orgChild2Name,
orgTreeShortName: orgChild2.orgChild2ShortName, orgTreeShortName: orgChild2.orgChild2ShortName,
orgTreeCode: orgChild2.orgChild2Code, orgTreeCode: orgChild2.orgChild2Code,
@ -595,6 +599,7 @@ export class OrganizationController extends Controller {
orgTreeId: orgChild3.id, orgTreeId: orgChild3.id,
orgRootId: orgChild2.id, orgRootId: orgChild2.id,
orgLevel: 3, orgLevel: 3,
orgName: `${orgChild1.orgChild1Name}/${orgChild2.orgChild2Name}/${orgChild3.orgChild3Name}`,
orgTreeName: orgChild3.orgChild3Name, orgTreeName: orgChild3.orgChild3Name,
orgTreeShortName: orgChild3.orgChild3ShortName, orgTreeShortName: orgChild3.orgChild3ShortName,
orgTreeCode: orgChild3.orgChild3Code, orgTreeCode: orgChild3.orgChild3Code,
@ -606,12 +611,14 @@ export class OrganizationController extends Controller {
orgTreePhoneIn: orgChild3.orgChild3PhoneIn, orgTreePhoneIn: orgChild3.orgChild3PhoneIn,
orgTreeFax: orgChild3.orgChild3Fax, orgTreeFax: orgChild3.orgChild3Fax,
orgRevisionId: orgRoot.orgRevisionId, orgRevisionId: orgRoot.orgRevisionId,
orgRootName: orgRoot.orgRootName,
children: orgChild4Data children: orgChild4Data
.filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id) .filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id)
.map((orgChild4) => ({ .map((orgChild4) => ({
orgTreeId: orgChild4.id, orgTreeId: orgChild4.id,
orgRootId: orgChild3.id, orgRootId: orgChild3.id,
orgLevel: 4, orgLevel: 4,
orgName: `${orgChild1.orgChild1Name}/${orgChild2.orgChild2Name}/${orgChild3.orgChild3Name}/${orgChild4.orgChild4Name}`,
orgTreeName: orgChild4.orgChild4Name, orgTreeName: orgChild4.orgChild4Name,
orgTreeShortName: orgChild4.orgChild4ShortName, orgTreeShortName: orgChild4.orgChild4ShortName,
orgTreeCode: orgChild4.orgChild4Code, orgTreeCode: orgChild4.orgChild4Code,
@ -623,6 +630,7 @@ export class OrganizationController extends Controller {
orgTreePhoneIn: orgChild4.orgChild4PhoneIn, orgTreePhoneIn: orgChild4.orgChild4PhoneIn,
orgTreeFax: orgChild4.orgChild4Fax, orgTreeFax: orgChild4.orgChild4Fax,
orgRevisionId: orgRoot.orgRevisionId, orgRevisionId: orgRoot.orgRevisionId,
orgRootName: orgRoot.orgRootName,
})), })),
})), })),
})), })),

View file

@ -25,6 +25,14 @@ import { PosLevel } from "../entities/PosLevel";
import { CreatePosDict, PosDict } from "../entities/PosDict"; import { CreatePosDict, PosDict } from "../entities/PosDict";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { Like } from "typeorm"; 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") @Route("api/v1/org/pos")
@Tags("Position") @Tags("Position")
@Security("bearerAuth") @Security("bearerAuth")
@ -38,6 +46,15 @@ export class PositionController extends Controller {
private posTypeRepository = AppDataSource.getRepository(PosType); private posTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel); private posLevelRepository = AppDataSource.getRepository(PosLevel);
private posDictRepository = AppDataSource.getRepository(PosDict); 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 * API
@ -50,17 +67,13 @@ export class PositionController extends Controller {
{ {
id: "00000000-0000-0000-0000-000000000000", id: "00000000-0000-0000-0000-000000000000",
posExecutiveName: "นักบริหาร", posExecutiveName: "นักบริหาร",
posExecutivePriority: 1 posExecutivePriority: 1,
}, },
]) ])
async GetPosExecutive() { async GetPosExecutive() {
try { try {
const posExecutive = await this.posExecutiveRepository.find({ const posExecutive = await this.posExecutiveRepository.find({
select: [ select: ["id", "posExecutiveName", "posExecutivePriority"],
"id",
"posExecutiveName",
"posExecutivePriority"
]
}); });
if (!posExecutive) { if (!posExecutive) {
return new HttpSuccess([]); return new HttpSuccess([]);
@ -85,28 +98,24 @@ export class PositionController extends Controller {
posTypeRank: 1, posTypeRank: 1,
posLevels: [ posLevels: [
{ {
id : "00000000-0000-0000-0000-000000000000", id: "00000000-0000-0000-0000-000000000000",
posLevelName : "นักบริหาร", posLevelName: "นักบริหาร",
posLevelRank : 1, posLevelRank: 1,
posLevelAuthority : "HEAD", posLevelAuthority: "HEAD",
} },
] ],
}, },
]) ])
async GetPosType() { async GetPosType() {
try { try {
const posType = await this.posTypeRepository.find({ const posType = await this.posTypeRepository.find({
select: [ select: ["id", "posTypeName", "posTypeRank"],
"id",
"posTypeName",
"posTypeRank"
],
relations: ["posLevels"], relations: ["posLevels"],
}); });
if (!posType) { if (!posType) {
return new HttpSuccess([]); return new HttpSuccess([]);
} }
const mapPosType = posType.map(item => ({ const mapPosType = posType.map((item) => ({
id: item.id, id: item.id,
posTypeName: item.posTypeName, posTypeName: item.posTypeName,
posTypeRank: item.posTypeRank, posTypeRank: item.posTypeRank,
@ -114,8 +123,8 @@ export class PositionController extends Controller {
id: posLevel.id, id: posLevel.id,
posLevelName: posLevel.posLevelName, posLevelName: posLevel.posLevelName,
posLevelRank: posLevel.posLevelRank, posLevelRank: posLevel.posLevelRank,
posLevelAuthority: posLevel.posLevelAuthority posLevelAuthority: posLevel.posLevelAuthority,
})) })),
})); }));
return new HttpSuccess(mapPosType); return new HttpSuccess(mapPosType);
} catch (error) { } catch (error) {
@ -139,26 +148,20 @@ export class PositionController extends Controller {
posTypes: { posTypes: {
id: "00000000-0000-0000-0000-000000000000", id: "00000000-0000-0000-0000-000000000000",
posTypeName: "นักบริหาร", posTypeName: "นักบริหาร",
posTypeRank: 1 posTypeRank: 1,
} },
}, },
]) ])
async GetPosLevel() { async GetPosLevel() {
try { try {
const posLevel = await this.posLevelRepository.find({ const posLevel = await this.posLevelRepository.find({
select: [ select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority", "posTypeId"],
"id",
"posLevelName",
"posLevelRank",
"posLevelAuthority",
"posTypeId"
],
relations: ["posType"], relations: ["posType"],
}); });
if (!posLevel) { if (!posLevel) {
return new HttpSuccess([]); return new HttpSuccess([]);
} }
const mapPosLevel = posLevel.map(item => ({ const mapPosLevel = posLevel.map((item) => ({
id: item.id, id: item.id,
posLevelName: item.posLevelName, posLevelName: item.posLevelName,
posLevelRank: item.posLevelRank, posLevelRank: item.posLevelRank,
@ -167,7 +170,7 @@ export class PositionController extends Controller {
id: item.posType.id, id: item.posType.id,
posTypeName: item.posType.posTypeName, posTypeName: item.posType.posTypeName,
posTypeRank: item.posType.posTypeRank, posTypeRank: item.posType.posTypeRank,
} },
})); }));
return new HttpSuccess(mapPosLevel); return new HttpSuccess(mapPosLevel);
} catch (error) { } catch (error) {
@ -190,29 +193,36 @@ export class PositionController extends Controller {
posLevelId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", posLevelId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
posExecutiveId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", posExecutiveId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
positionExecutiveField: "นักบริหาร", positionExecutiveField: "นักบริหาร",
positionArea: "บริหาร" positionArea: "บริหาร",
}, },
]) ])
async createPosition( async createPosition(
@Body() @Body()
requestBody: CreatePosDict, requestBody: CreatePosDict,
@Request() request: { user: Record<string, any> },) { @Request() request: { user: Record<string, any> },
) {
const posDict = Object.assign(new PosDict(), requestBody); const posDict = Object.assign(new PosDict(), requestBody);
if (!posDict) { if (!posDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); 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) { if (!checkPosTypeId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosTypeId"); 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) { if (!checkPosLevelId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosLevelId"); 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) { if (!checkPosExecutiveId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosExecutiveId"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosExecutiveId");
} }
@ -257,8 +267,7 @@ export class PositionController extends Controller {
* *
*/ */
@Get("position") @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: "positionName", name: "ตำแหน่งในสายงาน" }
// { id: "positionField", name: "สายงาน" } // { id: "positionField", name: "สายงาน" }
// { id: "positionType", name: "ประเภทตำแหน่ง" } // { id: "positionType", name: "ประเภทตำแหน่ง" }
@ -267,52 +276,301 @@ export class PositionController extends Controller {
// { id: "positionExecutiveField", name: "ด้านทางการบริหาร" } // { id: "positionExecutiveField", name: "ด้านทางการบริหาร" }
// { id: "positionArea", name: "ด้าน/สาขา" } // { id: "positionArea", name: "ด้าน/สาขา" }
try{ try {
let findPosDict: any; let findPosDict: any;
console.log("type: ", type) console.log("type: ", type);
console.log("keyword: ", keyword) console.log("keyword: ", keyword);
switch(type){ switch (type) {
case "positionName": case "positionName":
findPosDict = await this.posDictRepository.find({ where: { posDictName: Like(`%${keyword}%`) } }); findPosDict = await this.posDictRepository.find({
where: { posDictName: Like(`%${keyword}%`) },
});
if (!findPosDict) { if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
} }
case "positionField": case "positionField":
findPosDict = await this.posDictRepository.find({ where: { posDictField: Like(`%${keyword}%`) } }); findPosDict = await this.posDictRepository.find({
where: { posDictField: Like(`%${keyword}%`) },
});
if (!findPosDict) { if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
} }
case "positionType": case "positionType":
findPosDict = await this.posDictRepository.find({ where: { posTypeId: Like(`%${keyword}%`) } }); findPosDict = await this.posDictRepository.find({
where: { posTypeId: Like(`%${keyword}%`) },
});
if (!findPosDict) { if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
} }
case "positionLevel": case "positionLevel":
findPosDict = await this.posDictRepository.find({ where: { posLevelId: Like(`%${keyword}%`) } }); findPosDict = await this.posDictRepository.find({
where: { posLevelId: Like(`%${keyword}%`) },
});
if (!findPosDict) { if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
} }
case "positionExecutive": case "positionExecutive":
findPosDict = await this.posDictRepository.find({ where: { posExecutiveId: Like(`%${keyword}%`) } }); findPosDict = await this.posDictRepository.find({
where: { posExecutiveId: Like(`%${keyword}%`) },
});
if (!findPosDict) { if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
} }
case "positionExecutiveField": case "positionExecutiveField":
findPosDict = await this.posDictRepository.find({ where: { posDictExecutiveField: Like(`%${keyword}%`) } }); findPosDict = await this.posDictRepository.find({
where: { posDictExecutiveField: Like(`%${keyword}%`) },
});
if (!findPosDict) { if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
} }
case "positionArea": case "positionArea":
findPosDict = await this.posDictRepository.find({ where: { posDictArea: Like(`%${keyword}%`) } }); findPosDict = await this.posDictRepository.find({
where: { posDictArea: Like(`%${keyword}%`) },
});
if (!findPosDict) { if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
} }
} }
return new HttpSuccess(findPosDict); return new HttpSuccess(findPosDict);
} } catch (error) {
catch (error) {
return 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);
}
} }

View file

@ -1,5 +1,6 @@
import { Entity, Column, ManyToOne, JoinColumn, OneToOne, OneToMany } from "typeorm"; import { Entity, Column, ManyToOne, JoinColumn, OneToOne, OneToMany } from "typeorm";
import { EntityBase } from "./base/Base"; import { EntityBase } from "./base/Base";
import { CreatePosDict } from "./PosDict";
enum PosMasterLine { enum PosMasterLine {
MAIN = "MAIN", MAIN = "MAIN",
@ -106,7 +107,8 @@ export class PosMaster extends EntityBase {
@Column({ @Column({
nullable: true, nullable: true,
length: 40, length: 40,
comment: "คนครองปัจจุบัน เมื่อทำสำเนาโครงสร้างและตำแหน่งพร้อมกับคนครองมา คนครองจะอยู่ในฟิลด์นี้", comment:
"คนครองปัจจุบัน เมื่อทำสำเนาโครงสร้างและตำแหน่งพร้อมกับคนครองมา คนครองจะอยู่ในฟิลด์นี้",
default: "string", default: "string",
}) })
profileIdCurrentHolder: string; profileIdCurrentHolder: string;
@ -114,7 +116,8 @@ export class PosMaster extends EntityBase {
@Column({ @Column({
nullable: true, nullable: true,
length: 40, length: 40,
comment: "คนที่กำลังจะมาครอง ตอนปรับโครงสร้าง ถ้าเลือกให้ใครมาครอง ProfileId ของคนนั้นจะมาอยู่ในช่องนี้ รวมทั้งตอนเลือกตำแหน่งเพื่อบรรจุ แต่งตั้ง เลื่อน ย้าย ในระบบบรรจุแต่งตั้งด้วย", comment:
"คนที่กำลังจะมาครอง ตอนปรับโครงสร้าง ถ้าเลือกให้ใครมาครอง ProfileId ของคนนั้นจะมาอยู่ในช่องนี้ รวมทั้งตอนเลือกตำแหน่งเพื่อบรรจุ แต่งตั้ง เลื่อน ย้าย ในระบบบรรจุแต่งตั้งด้วย",
default: "string", default: "string",
}) })
profileIdNextHolder: string; profileIdNextHolder: string;
@ -123,5 +126,36 @@ export class PosMaster extends EntityBase {
length: 40, length: 40,
default: "00000000-0000-0000-0000-000000000000", default: "00000000-0000-0000-0000-000000000000",
}) })
orgRevisionId: string;//fk orgRevisionId: string; //fk
} }
export class CreatePosMaster {
@Column()
posMasterNoPrefix: string;
@Column()
posMasterNo: string;
@Column()
posMasterNoSuffix: string;
@Column("uuid")
positions: CreatePosDict[];
@Column("uuid")
orgRootId: string;
@Column("uuid")
orgChild1Id: string;
@Column("uuid")
orgChild2Id: string;
@Column("uuid")
orgChild3Id: string;
@Column("uuid")
orgChild4Id: string;
}
export type UpdatePosMaster = Partial<PosMaster>;