diff --git a/src/controllers/OrgChild1Controller.ts b/src/controllers/OrgChild1Controller.ts index 384fef28..bc47b23b 100644 --- a/src/controllers/OrgChild1Controller.ts +++ b/src/controllers/OrgChild1Controller.ts @@ -1,6 +1,8 @@ import { AppDataSource } from "../database/data-source"; +import { OrgRoot } from "../entities/OrgRoot"; import { OrgChild1, CreateOrgChild1, UpdateOrgChild1 } from "../entities/OrgChild1"; import { OrgChild2 } from "../entities/OrgChild2"; +import { OrgChild3 } from "../entities/OrgChild3"; import { Body, Delete, @@ -28,8 +30,64 @@ import HttpError from "../interfaces/http-error"; ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class OrgChild1Controller { + private orgRootRepository = AppDataSource.getRepository(OrgRoot) private child1Repository = AppDataSource.getRepository(OrgChild1); private child2Repository = AppDataSource.getRepository(OrgChild2); + private child3Repository = AppDataSource.getRepository(OrgChild3); + + /** + * API รายละเอียดโครงสร้างระดับ1 + * + * @summary ORG_017 - รายละเอียดโครงสร้างระดับ1 (ADMIN) #17 + * + * @param {string} id id โครงสร้างระดับ1 + */ + // @Get("{id}") + // async Get(@Path() id: string) { + // try { + // const child2s = await this.child2Repository.find({ + // where: { + // orgChild1Id: id, + // }, + // select: ["id", "orgChild2Name", "orgChild2ShortName", "orgChild2Code", "orgChild2Order", "orgRootId"], + // order: { + // orgChild2Order: "ASC", + // }, + // }); + + // const orgRoots = await Promise.all(child2s.map(async (child2) => { + // const orgRoot = await this.orgRootRepository.findOne({ + // where: { + // id: child2.orgRootId, + // }, + // select: ["orgRootCode"], + // }); + + // const orgChild3s = await this.child3Repository.find({ + // where: { + // orgChild2Id: child2.id, + // }, + // select: ["id"], + // }); + + // const orgChild3Ids = orgChild3s.map((orgChild3) => orgChild3.id); + + // return { + // orgChild2Id: child2.id, + // orgChild3Id: orgChild3Ids, + // orgChild2Name: child2.orgChild2Name, + // orgChild2ShortName: child2.orgChild2ShortName, + // orgChild2Code: child2.orgChild2Code, + // orgChild2Order: child2.orgChild2Order, + // orgRootCode: orgRoot?.orgRootCode, + // }; + // })); + + // return new HttpSuccess(orgRoots); + // } catch (error) { + // return error; + // } + // } /** * API สร้างโครงสร้างระดับ1 @@ -43,24 +101,32 @@ export class OrgChild1Controller { @Request() request: { user: Record }, ) { try { + const rootIdExits = await this.orgRootRepository.findOne({ where: {id:requestBody.orgRootId} }); + if(!rootIdExits){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootId"); + } + + const validOrgChild1Ranks = ["DEPARTMENT", "OFFICE", "DIVISION", "SECTION"]; + if (!validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase())) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild1Rank"); + } + const chkCode = await this.child1Repository.findOne({ where: { orgRootId: requestBody.orgRootId, orgChild1Code: requestBody.orgChild1Code }, }); if (chkCode != null) { throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); } + const child1 = Object.assign(new OrgChild1(), requestBody) as OrgChild1; child1.orgChild1Name = requestBody.orgChild1Name; child1.createdUserId = request.user.sub; child1.createdFullName = request.user.name; - child1.createdAt = new Date(); child1.lastUpdateUserId = request.user.sub; child1.lastUpdateFullName = request.user.name; - child1.lastUpdatedAt = new Date(); await this.child1Repository.save(child1); return new HttpSuccess(); } catch (error) { - // return new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, String(error)) return error; } } @@ -79,16 +145,21 @@ export class OrgChild1Controller { @Request() request: { user: Record }, ) { try { - const chkCode = await this.child1Repository.findOne({ - where: { id: id, orgChild1Code: requestBody.orgChild1Code }, - }); - if (chkCode?.id != id && chkCode != null) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); + const rootIdExits = await this.orgRootRepository.findOne({ where: {id:requestBody.orgRootId} }); + if(!rootIdExits){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RootId"); } + const child1 = await this.child1Repository.findOne({ where: { id } }); if (!child1) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } + const chkCode = await this.child1Repository.findOne({ + where: { id: id, orgRootId: requestBody.orgRootId, orgChild1Code: requestBody.orgChild1Code }, + }); + if (chkCode != null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); + } child1.lastUpdateUserId = request.user.sub; child1.lastUpdateFullName = request.user.name; child1.lastUpdatedAt = new Date(); @@ -117,7 +188,7 @@ export class OrgChild1Controller { const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } }); if (exitsChild2) { throw new HttpError( - HttpStatusCode.NOT_FOUND, + HttpStatusCode.INTERNAL_SERVER_ERROR, "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2", ); }