diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index ff0370b5..aaaa3dd7 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -24,7 +24,7 @@ import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { CreatePosDict, PosDict } from "../entities/PosDict"; import HttpError from "../interfaces/http-error"; -import { In, IsNull, Like } from "typeorm"; +import { In, IsNull, Like, Not } from "typeorm"; import { CreatePosMaster, PosMaster } from "../entities/PosMaster"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; @@ -818,4 +818,168 @@ export class PositionController extends Controller { return error; } } + + /** + * API จัดลำดับตำแหน่ง + * + * @summary ORG_040 - จัดลำดับตำแหน่ง (ADMIN) #43 + * + */ + @Post("sort") + async Sort(@Body() requestBody: { id: string; type: number; sortId: string[] }) { + try { + switch (requestBody.type) { + case 0: { + const rootId = await this.posMasterRepository.findOne({ + where: { orgRootId: requestBody.id }, + }); + if (!rootId?.id) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "not found rootId: " + requestBody.id, + ); + } + const listPosMasterId_0 = await this.posMasterRepository.find({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull(), orgChild2Id: IsNull(), + orgChild3Id: IsNull(), orgChild4Id: IsNull(), + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 0."); + } + const sortData_0 = listPosMasterId_0.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.posMasterRepository.save(sortData_0); + break; + } + + case 1: { + const child1Id = await this.posMasterRepository.findOne({ + where: { orgChild1Id: requestBody.id }, + }); + if (!child1Id?.id) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "not found child1Id: " + requestBody.id, + ); + } + const listPosMasterId_1 = await this.posMasterRepository.find({ + where: { + orgRootId: Not(IsNull()), + orgChild1Id: requestBody.id, orgChild2Id: IsNull(), + orgChild3Id: IsNull(), orgChild4Id: IsNull(), + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_1) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 1."); + } + const sortData_1 = listPosMasterId_1.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.posMasterRepository.save(sortData_1); + break; + } + + case 2: { + const child2Id = await this.posMasterRepository.findOne({ + where: { orgChild2Id: requestBody.id }, + }); + if (!child2Id?.id) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "not found child2Id: " + requestBody.id, + ); + } + const listPosMasterId_2 = await this.posMasterRepository.find({ + where: { + orgRootId: Not(IsNull()), + orgChild1Id: Not(IsNull()), orgChild2Id: requestBody.id, + orgChild3Id: IsNull(), orgChild4Id: IsNull(), + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_2) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 2."); + } + const sortData_2 = listPosMasterId_2.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.posMasterRepository.save(sortData_2); + break; + } + + case 3: { + const child3Id = await this.posMasterRepository.findOne({ + where: { orgChild3Id: requestBody.id }, + }); + if (!child3Id?.id) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "not found chil3Id: " + requestBody.id, + ); + } + const listPosMasterId_3 = await this.posMasterRepository.find({ + where: { + orgRootId: Not(IsNull()), + orgChild1Id: Not(IsNull()), orgChild2Id: Not(IsNull()), + orgChild3Id: requestBody.id, orgChild4Id: IsNull(), + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_3) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 3."); + } + const sortData_3 = listPosMasterId_3.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.posMasterRepository.save(sortData_3); + break; + } + + case 4: { + const child4Id = await this.posMasterRepository.findOne({ + where: { orgChild4Id: requestBody.id }, + }); + if (!child4Id?.id) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "not found child4Id: " + requestBody.id, + ); + } + const listPosMasterId_4 = await this.posMasterRepository.find({ + where: { + orgRootId: Not(IsNull()), + orgChild1Id: Not(IsNull()), orgChild2Id: Not(IsNull()), + orgChild3Id: Not(IsNull()), orgChild4Id: requestBody.id, + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 4."); + } + const sortData_4 = listPosMasterId_4.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.posMasterRepository.save(sortData_4); + break; + } + + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: " + requestBody.type); + } + return new HttpSuccess(); + } catch (error) { + return error; + } + } }