import { Body, Controller, Delete, Example, Get, Patch, Path, Post, Request, Route, Security, Tags, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import { CreateStrategyChild1, StrategyChild1, UpdateStrategyChild1, } from "../entities/StrategyChild1"; import { CreateStrategyChild2, StrategyChild2, UpdateStrategyChild2, } from "../entities/StrategyChild2"; import { CreateStrategyChild3, StrategyChild3, UpdateStrategyChild3, } from "../entities/StrategyChild3"; import { CreateStrategyChild4, StrategyChild4, UpdateStrategyChild4, } from "../entities/StrategyChild4"; import { CreateStrategyChild5, StrategyChild5, UpdateStrategyChild5, } from "../entities/StrategyChild5"; import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import { Check } from "typeorm"; @Route("api/v1/development/strategy") @Tags("Strategy") @Security("bearerAuth") export class StrategyController extends Controller { private strategy1Repo = AppDataSource.getRepository(StrategyChild1); private strategy2Repo = AppDataSource.getRepository(StrategyChild2); private strategy3Repo = AppDataSource.getRepository(StrategyChild3); private strategy4Repo = AppDataSource.getRepository(StrategyChild4); private strategy5Repo = AppDataSource.getRepository(StrategyChild5); @Get() public async listStrategyChild1() { const listStrategyChild1 = await this.strategy1Repo.find({ relations: [ "strategyChild2s", "strategyChild2s.strategyChild3s", "strategyChild2s.strategyChild3s.strategyChild4s", "strategyChild2s.strategyChild3s.strategyChild4s.strategyChild5s", ], order: { createdAt: "ASC" }, }); if (!listStrategyChild1 || listStrategyChild1.length === 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } const formattedData = listStrategyChild1.map((item) => ({ id: item.id, level: 1, name: item.strategyChild1Name, children: item.strategyChild2s.map((child2) => ({ id: child2.id, level: 2, name: child2.strategyChild2Name, children: child2.strategyChild3s ? child2.strategyChild3s.map((child3) => ({ id: child3.id, level: 3, name: child3.strategyChild3Name, children: child3.strategyChild4s ? child3.strategyChild4s.map((child4) => ({ id: child4.id, level: 4, name: child4.strategyChild4Name, children: child4.strategyChild5s ? child4.strategyChild5s.map((child5) => ({ id: child5.id, level: 5, name: child5.strategyChild5Name, })) : [], })) : [], })) : [], })), })); return new HttpSuccess(formattedData); } @Post() public async newStrategyChild( @Request() request: { user: Record }, @Body() body: { name: string; levelnode: number; idnode?: string | null; }, ) { let strategyRepo: any; let strategyChild: any; let repoSave: any; switch (body.levelnode) { case 0: strategyRepo = this.strategy1Repo; repoSave = this.strategy1Repo; strategyChild = new StrategyChild1(); strategyChild.strategyChild1Name = body.name; break; case 1: strategyRepo = this.strategy1Repo; repoSave = this.strategy2Repo; strategyChild = new StrategyChild2(); strategyChild.strategyChild2Name = body.name; if (body.idnode) { const chk1 = await this.strategy1Repo.findOne({ where: { id: body.idnode }, }); if (chk1) { strategyChild.strategyChild1Id = chk1.id; } else { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } } break; case 2: strategyRepo = this.strategy2Repo; repoSave = this.strategy3Repo; strategyChild = new StrategyChild3(); strategyChild.strategyChild3Name = body.name; if (body.idnode) { const chk2 = await this.strategy2Repo.findOne({ where: { id: body.idnode }, }); if (chk2) { strategyChild.strategyChild1Id = chk2.strategyChild1Id; strategyChild.strategyChild2Id = chk2.id; } else { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } } break; case 3: strategyRepo = this.strategy3Repo; repoSave = this.strategy4Repo; strategyChild = new StrategyChild4(); strategyChild.strategyChild4Name = body.name; if (body.idnode) { const chk3 = await this.strategy3Repo.findOne({ where: { id: body.idnode }, }); if (chk3) { strategyChild.strategyChild1Id = chk3.strategyChild1Id; strategyChild.strategyChild2Id = chk3.strategyChild2Id; strategyChild.strategyChild3Id = chk3.id; } else { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } } break; case 4: strategyRepo = this.strategy4Repo; repoSave = this.strategy5Repo; strategyChild = new StrategyChild5(); strategyChild.strategyChild5Name = body.name; if (body.idnode) { const chk4 = await this.strategy4Repo.findOne({ where: { id: body.idnode }, }); if (chk4) { strategyChild.strategyChild1Id = chk4.strategyChild1Id; strategyChild.strategyChild2Id = chk4.strategyChild2Id; strategyChild.strategyChild3Id = chk4.strategyChild3Id; strategyChild.strategyChild4Id = chk4.id; } else { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } } break; default: throw new HttpError(HttpStatus.BAD_REQUEST, "levelnode ไม่ถูกต้อง"); } strategyChild.createdUserId = request.user.sub; strategyChild.createdFullName = request.user.name; strategyChild.lastUpdateUserId = request.user.sub; strategyChild.lastUpdateFullName = request.user.name; await repoSave.save(strategyChild); return new HttpSuccess(strategyChild.id); } @Patch() public async editStrategyChild1( @Request() request: { user: Record }, @Body() body: { name: string; levelnode: number; idnode: string; }, ) { let strategyRepo: any; let strategyChild: any; switch (body.levelnode) { case 1: strategyRepo = this.strategy1Repo; strategyChild = await strategyRepo.findOne({ where: { id: body.idnode }, }); if (!strategyChild) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } strategyChild.strategyChild1Name = body.name; break; case 2: strategyRepo = this.strategy2Repo; strategyChild = await strategyRepo.findOne({ where: { id: body.idnode }, }); if (!strategyChild) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } strategyChild.strategyChild2Name = body.name; break; case 3: strategyRepo = this.strategy3Repo; strategyChild = await strategyRepo.findOne({ where: { id: body.idnode }, }); if (!strategyChild) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } strategyChild.strategyChild3Name = body.name; break; case 4: strategyRepo = this.strategy4Repo; strategyChild = await strategyRepo.findOne({ where: { id: body.idnode }, }); if (!strategyChild) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } strategyChild.strategyChild4Name = body.name; break; case 5: strategyRepo = this.strategy5Repo; strategyChild = await this.strategy5Repo.findOne({ where: { id: body.idnode }, }); if (!strategyChild) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } strategyChild.strategyChild5Name = body.name; break; default: throw new HttpError(HttpStatus.BAD_REQUEST, "levelnode ไม่ถูกต้อง"); } strategyChild.lastUpdateUserId = request.user.sub; strategyChild.lastUpdateFullName = request.user.name; await strategyRepo.save(strategyChild); return new HttpSuccess(); } @Delete() public async deleteStrategyChild( @Request() request: { user: Record }, @Body() body: { levelnode: number; idnode: string; }, ) { let strategyRepo: any; let data: any; switch (body.levelnode) { case 1: strategyRepo = this.strategy1Repo; data = await strategyRepo.find({ where: { id: body.idnode }, }); if (!data) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } await this.strategy5Repo.delete({ strategyChild1Id: body.idnode }); await this.strategy4Repo.delete({ strategyChild1Id: body.idnode }); await this.strategy3Repo.delete({ strategyChild1Id: body.idnode }); await this.strategy2Repo.delete({ strategyChild1Id: body.idnode }); await this.strategy1Repo.delete({ id: body.idnode }); break; case 2: strategyRepo = this.strategy2Repo; data = await strategyRepo.find({ where: { id: body.idnode }, }); if (!data) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } await this.strategy5Repo.delete({ strategyChild2Id: body.idnode }); await this.strategy4Repo.delete({ strategyChild2Id: body.idnode }); await this.strategy3Repo.delete({ strategyChild2Id: body.idnode }); await this.strategy2Repo.delete({ id: body.idnode }); break; case 3: strategyRepo = this.strategy3Repo; data = await strategyRepo.find({ where: { id: body.idnode }, }); if (!data) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } await this.strategy5Repo.delete({ strategyChild3Id: body.idnode }); await this.strategy4Repo.delete({ strategyChild3Id: body.idnode }); await this.strategy3Repo.delete({ id: body.idnode }); break; case 4: strategyRepo = this.strategy4Repo; data = await strategyRepo.find({ where: { id: body.idnode }, }); if (!data) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } await this.strategy5Repo.delete({ strategyChild4Id: body.idnode }); await this.strategy4Repo.delete({ id: body.idnode }); break; case 5: strategyRepo = this.strategy5Repo; data = await strategyRepo.find({ where: { id: body.idnode }, }); if (!data) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); } await this.strategy5Repo.delete({ id: body.idnode }); break; default: throw new HttpError(HttpStatus.BAD_REQUEST, "levelnode ไม่ถูกต้อง"); } if (!data) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } return new HttpSuccess(); } /** * API เช็ค node detail * * @summary เช็ค node detail (ADMIN) * */ @Post("find/all") async findNodeAllDetail(@Body() requestBody: { strategy: number; strategyId: string }) { switch (requestBody.strategy) { case 1: { const data = await this.strategy1Repo.findOne({ where: { id: requestBody.strategyId }, }); if (data == null) { throw new HttpError(HttpStatus.NOT_FOUND, "not found rootId."); } return new HttpSuccess({ strategyChild1Id: data.id, strategyChild1: data.strategyChild1Name, }); } case 2: { const data = await this.strategy2Repo.findOne({ where: { id: requestBody.strategyId }, relations: { strategyChild1: true, }, }); if (data == null) { throw new HttpError(HttpStatus.NOT_FOUND, "not found child1."); } return new HttpSuccess({ strategyChild1Id: data.strategyChild1Id, strategyChild1: data.strategyChild1 == null ? null : data.strategyChild1.strategyChild1Name, strategyChild2Id: data.id, strategyChild2: data.strategyChild2Name, }); } case 3: { const data = await this.strategy3Repo.findOne({ where: { id: requestBody.strategyId }, relations: { strategyChild1: true, strategyChild2: true, }, }); if (data == null) { throw new HttpError(HttpStatus.NOT_FOUND, "not found child2."); } return new HttpSuccess({ strategyChild1Id: data.strategyChild1Id, strategyChild1: data.strategyChild1 == null ? null : data.strategyChild1.strategyChild1Name, strategyChild2Id: data.strategyChild2Id, strategyChild2: data.strategyChild2 == null ? null : data.strategyChild2.strategyChild2Name, strategyChild3Id: data.id, strategyChild3: data.strategyChild3Name, }); } case 4: { const data = await this.strategy4Repo.findOne({ where: { id: requestBody.strategyId }, relations: { strategyChild1: true, strategyChild2: true, strategyChild3: true, }, }); if (data == null) { throw new HttpError(HttpStatus.NOT_FOUND, "not found child3."); } return new HttpSuccess({ strategyChild1Id: data.strategyChild1Id, strategyChild1: data.strategyChild1 == null ? null : data.strategyChild1.strategyChild1Name, strategyChild2Id: data.strategyChild2Id, strategyChild2: data.strategyChild2 == null ? null : data.strategyChild2.strategyChild2Name, strategyChild3Id: data.strategyChild3Id, strategyChild3: data.strategyChild3 == null ? null : data.strategyChild3.strategyChild3Name, strategyChild4Id: data.id, strategyChild4: data.strategyChild4Name, }); } case 5: { const data = await this.strategy5Repo.findOne({ where: { id: requestBody.strategyId }, relations: { strategyChild1: true, strategyChild2: true, strategyChild3: true, strategyChild4: true, }, }); if (data == null) { throw new HttpError(HttpStatus.NOT_FOUND, "not found child4."); } return new HttpSuccess({ strategyChild1Id: data.strategyChild1Id, strategyChild1: data.strategyChild1 == null ? null : data.strategyChild1.strategyChild1Name, strategyChild2Id: data.strategyChild2Id, strategyChild2: data.strategyChild2 == null ? null : data.strategyChild2.strategyChild2Name, strategyChild3Id: data.strategyChild3Id, strategyChild3: data.strategyChild3 == null ? null : data.strategyChild3.strategyChild3Name, strategyChild4Id: data.strategyChild4Id, strategyChild4: data.strategyChild4 == null ? null : data.strategyChild4.strategyChild4Name, strategyChild5Id: data.id, strategyChild5: data.strategyChild5Name, }); } default: throw new HttpError(HttpStatus.NOT_FOUND, "not found type: " + requestBody.strategy); } } }