From e8f5f905bd5f393108123b1ac524c556d659df08 Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 31 Jan 2024 17:12:38 +0700 Subject: [PATCH] =?UTF-8?q?api=20=E0=B8=88=E0=B8=B1=E0=B8=94=E0=B8=A5?= =?UTF-8?q?=E0=B8=B3=E0=B8=94=E0=B8=B1=E0=B8=9A=E0=B9=82=E0=B8=84=E0=B8=A3?= =?UTF-8?q?=E0=B8=87=E0=B8=AA=E0=B8=A3=E0=B9=89=E0=B8=B2=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/OrganizationController.ts | 124 ++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 5eeffbd7..788320d1 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -788,4 +788,128 @@ export class OrganizationController extends Controller { return new HttpSuccess(_data); } } + + /** + * API จัดลำดับโครงสร้าง + * + * @summary ORG_038 - จัดลำดับโครงสร้าง (ADMIN) #41 + * + */ + @Post("sort") + async Sort( + @Body() requestBody: { + id: string, type: number, sortId: string[] + } + ){ + try { + switch(requestBody.type){ + case 0 :{ + const revisionId = await this.orgRevisionRepository.findOne({ where: {id : requestBody.id} }) + if(!revisionId?.id){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found revisionId: "+ requestBody.id); + } + const listRootId = await this.orgRootRepository.find({ + where: { orgRevisionId: requestBody.id}, + select: ["id", "orgRootOrder"], + }) + if(!listRootId){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId."); + } + const sortData = listRootId.map((data) => ({ + id : data.id, + orgRootOrder: requestBody.sortId.indexOf(data.id) + 1 + })); + await this.orgRootRepository.save(sortData); + break; + } + + case 1 :{ + const rootId = await this.orgRootRepository.findOne({ where: {id : requestBody.id} }) + if(!rootId?.id){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId: "+ requestBody.id); + } + const listChild1Id = await this.child1Repository.find({ + where: { orgRootId: requestBody.id}, + select: ["id", "orgChild1Order"], + }) + if(!listChild1Id){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id."); + } + const sortData = listChild1Id.map((data) => ({ + id : data.id, + orgChild1Order: requestBody.sortId.indexOf(data.id) + 1 + })); + await this.child1Repository.save(sortData); + break; + } + + case 2 :{ + const child1Id = await this.child1Repository.findOne({ where: {id : requestBody.id} }) + if(!child1Id?.id){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id: "+ requestBody.id); + } + const listChild2Id = await this.child2Repository.find({ + where: { orgChild1Id: requestBody.id}, + select: ["id", "orgChild2Order"], + }) + if(!listChild2Id){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id."); + } + const sortData = listChild2Id.map((data) => ({ + id : data.id, + orgChild2Order: requestBody.sortId.indexOf(data.id) + 1 + })); + await this.child2Repository.save(sortData); + break; + } + + case 3 :{ + const child2Id = await this.child2Repository.findOne({ where: {id : requestBody.id} }) + if(!child2Id?.id){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id: "+ requestBody.id); + } + const listChild3Id = await this.child3Repository.find({ + where: { orgChild2Id: requestBody.id}, + select: ["id", "orgChild3Order"], + }) + if(!listChild3Id){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3Id."); + } + const sortData = listChild3Id.map((data) => ({ + id : data.id, + orgChild3Order: requestBody.sortId.indexOf(data.id) + 1 + })); + await this.child3Repository.save(sortData); + break; + } + + case 4 :{ + const child3Id = await this.child3Repository.findOne({ where: {id : requestBody.id} }) + if(!child3Id?.id){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3Id: "+ requestBody.id); + } + const listChild4Id = await this.child4Repository.find({ + where: { orgChild3Id: requestBody.id}, + select: ["id", "orgChild4Order"], + }) + if(!listChild4Id){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4Id."); + } + const sortData = listChild4Id.map((data) => ({ + id : data.id, + orgChild4Order: requestBody.sortId.indexOf(data.id) + 1 + })); + await this.child4Repository.save(sortData); + break; + } + + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: "+ requestBody.type); + } + return new HttpSuccess(); + } + catch(error){ + return error; + } + } }