fix format get

This commit is contained in:
AdisakKanthawilang 2024-04-17 13:56:10 +07:00
parent f3d2de51f7
commit 8a73302f6b

View file

@ -52,16 +52,45 @@ export class StrategyController extends Controller {
private strategy3Repo = AppDataSource.getRepository(StrategyChild3); private strategy3Repo = AppDataSource.getRepository(StrategyChild3);
private strategy4Repo = AppDataSource.getRepository(StrategyChild4); private strategy4Repo = AppDataSource.getRepository(StrategyChild4);
private strategy5Repo = AppDataSource.getRepository(StrategyChild5); private strategy5Repo = AppDataSource.getRepository(StrategyChild5);
@Get() @Get()
public async listStrategyChild1() { public async listStrategyChild1() {
const listStrategyChild1 = await this.strategy1Repo.find({ const listStrategyChild1 = await this.strategy1Repo.find({
relations: ["strategyChild2s", "strategyChild3s", "strategyChild4s", "strategyChild5s"], relations: ["strategyChild2s", "strategyChild2s.strategyChild3s", "strategyChild2s.strategyChild3s.strategyChild4s", "strategyChild2s.strategyChild3s.strategyChild4s.strategyChild5s"],
}); });
if (!listStrategyChild1) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); if (!listStrategyChild1 || listStrategyChild1.length === 0) {
} throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
return new HttpSuccess(listStrategyChild1); }
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() @Post()
@ -90,15 +119,15 @@ export class StrategyController extends Controller {
repoSave = this.strategy2Repo; repoSave = this.strategy2Repo;
strategyChild = new StrategyChild2(); strategyChild = new StrategyChild2();
strategyChild.strategyChild2Name = body.name; strategyChild.strategyChild2Name = body.name;
if(body.idnode){ if (body.idnode) {
const chk1 = await this.strategy1Repo.findOne({ const chk1 = await this.strategy1Repo.findOne({
where: { id: body.idnode }, where: { id: body.idnode },
}) });
if(chk1){ if (chk1) {
strategyChild.strategyChild1Id = chk1.id strategyChild.strategyChild1Id = chk1.id;
}else{ } else {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
} }
break; break;
case 2: case 2:
@ -106,16 +135,16 @@ export class StrategyController extends Controller {
repoSave = this.strategy3Repo; repoSave = this.strategy3Repo;
strategyChild = new StrategyChild3(); strategyChild = new StrategyChild3();
strategyChild.strategyChild3Name = body.name; strategyChild.strategyChild3Name = body.name;
if(body.idnode){ if (body.idnode) {
const chk2 = await this.strategy2Repo.findOne({ const chk2 = await this.strategy2Repo.findOne({
where: { id: body.idnode }, where: { id: body.idnode },
}) });
if(chk2){ if (chk2) {
strategyChild.strategyChild1Id = chk2.strategyChild1Id strategyChild.strategyChild1Id = chk2.strategyChild1Id;
strategyChild.strategyChild2Id = chk2.id strategyChild.strategyChild2Id = chk2.id;
}else{ } else {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
} }
break; break;
case 3: case 3:
@ -123,17 +152,17 @@ export class StrategyController extends Controller {
repoSave = this.strategy4Repo; repoSave = this.strategy4Repo;
strategyChild = new StrategyChild4(); strategyChild = new StrategyChild4();
strategyChild.strategyChild4Name = body.name; strategyChild.strategyChild4Name = body.name;
if(body.idnode){ if (body.idnode) {
const chk3 = await this.strategy3Repo.findOne({ const chk3 = await this.strategy3Repo.findOne({
where: { id: body.idnode }, where: { id: body.idnode },
}) });
if(chk3){ if (chk3) {
strategyChild.strategyChild1Id = chk3.strategyChild1Id strategyChild.strategyChild1Id = chk3.strategyChild1Id;
strategyChild.strategyChild2Id = chk3.strategyChild2Id strategyChild.strategyChild2Id = chk3.strategyChild2Id;
strategyChild.strategyChild3Id = chk3.id strategyChild.strategyChild3Id = chk3.id;
}else{ } else {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
} }
break; break;
case 4: case 4:
@ -141,18 +170,18 @@ export class StrategyController extends Controller {
repoSave = this.strategy5Repo; repoSave = this.strategy5Repo;
strategyChild = new StrategyChild5(); strategyChild = new StrategyChild5();
strategyChild.strategyChild5Name = body.name; strategyChild.strategyChild5Name = body.name;
if(body.idnode){ if (body.idnode) {
const chk4 = await this.strategy4Repo.findOne({ const chk4 = await this.strategy4Repo.findOne({
where: { id: body.idnode }, where: { id: body.idnode },
}) });
if(chk4){ if (chk4) {
strategyChild.strategyChild1Id = chk4.strategyChild1Id strategyChild.strategyChild1Id = chk4.strategyChild1Id;
strategyChild.strategyChild2Id = chk4.strategyChild2Id strategyChild.strategyChild2Id = chk4.strategyChild2Id;
strategyChild.strategyChild3Id = chk4.strategyChild3Id strategyChild.strategyChild3Id = chk4.strategyChild3Id;
strategyChild.strategyChild4Id = chk4.id strategyChild.strategyChild4Id = chk4.id;
}else{ } else {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
} }
break; break;
default: default:
@ -183,57 +212,55 @@ export class StrategyController extends Controller {
let strategyChild: any; let strategyChild: any;
let repoSave: any; let repoSave: any;
switch (body.levelnode) { switch (body.levelnode) {
case 1: case 1:
strategyRepo = this.strategy1Repo; strategyRepo = this.strategy1Repo;
strategyChild = await strategyRepo.findOne({ strategyChild = await strategyRepo.findOne({
where: { id: body.idnode }, where: { id: body.idnode },
}) });
if(!strategyChild){ if (!strategyChild) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
strategyChild.strategyChild1Name = body.name; strategyChild.strategyChild1Name = body.name;
break; break;
case 2: case 2:
strategyRepo = this.strategy2Repo; strategyRepo = this.strategy2Repo;
strategyChild = await strategyRepo.findOne({ strategyChild = await strategyRepo.findOne({
where: { id: body.idnode }, where: { id: body.idnode },
}) });
if(!strategyChild){ if (!strategyChild) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
repoSave = this.strategy2Repo; repoSave = this.strategy2Repo;
break; break;
case 3: case 3:
strategyRepo = this.strategy3Repo; strategyRepo = this.strategy3Repo;
strategyChild = await strategyRepo.findOne({ strategyChild = await strategyRepo.findOne({
where: { id: body.idnode }, where: { id: body.idnode },
}) });
if(!strategyChild){ if (!strategyChild) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
strategyChild.strategyChild3Name = body.name; strategyChild.strategyChild3Name = body.name;
break; break;
case 4: case 4:
strategyRepo = this.strategy4Repo; strategyRepo = this.strategy4Repo;
strategyChild = await strategyRepo.findOne({ strategyChild = await strategyRepo.findOne({
where: { id: body.idnode }, where: { id: body.idnode },
}) });
if(!strategyChild){ if (!strategyChild) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
strategyChild.strategyChild4Name = body.name; strategyChild.strategyChild4Name = body.name;
break; break;
case 5: case 5:
strategyRepo = this.strategy5Repo; strategyRepo = this.strategy5Repo;
strategyChild = await this.strategy5Repo.findOne({ strategyChild = await this.strategy5Repo.findOne({
where: { id: body.idnode }, where: { id: body.idnode },
}) });
if(!strategyChild){ if (!strategyChild) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
strategyChild.strategyChild5Name = body.name; strategyChild.strategyChild5Name = body.name;
break; break;
default: default:
@ -249,12 +276,13 @@ export class StrategyController extends Controller {
@Delete() @Delete()
public async deleteStrategyChild( public async deleteStrategyChild(
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
@Body() @Body()
body: { body: {
levelnode: number; levelnode: number;
idnode: string; idnode: string;
},) { },
) {
let strategyRepo: any; let strategyRepo: any;
let data: any; let data: any;
@ -262,37 +290,37 @@ export class StrategyController extends Controller {
case 1: case 1:
strategyRepo = this.strategy1Repo; strategyRepo = this.strategy1Repo;
data = await strategyRepo.find({ data = await strategyRepo.find({
where: { id: body.idnode }, where: { id: body.idnode },
}); });
if(!data){ if (!data) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
await this.strategy5Repo.delete({ strategyChild1Id: body.idnode }); await this.strategy5Repo.delete({ strategyChild1Id: body.idnode });
await this.strategy4Repo.delete({ strategyChild1Id: body.idnode }); await this.strategy4Repo.delete({ strategyChild1Id: body.idnode });
await this.strategy3Repo.delete({ strategyChild1Id: body.idnode }); await this.strategy3Repo.delete({ strategyChild1Id: body.idnode });
await this.strategy2Repo.delete({ strategyChild1Id: body.idnode }); await this.strategy2Repo.delete({ strategyChild1Id: body.idnode });
await this.strategy1Repo.delete({ id : body.idnode }); await this.strategy1Repo.delete({ id: body.idnode });
break; break;
case 2: case 2:
strategyRepo = this.strategy2Repo; strategyRepo = this.strategy2Repo;
data = await strategyRepo.find({ data = await strategyRepo.find({
where: { id: body.idnode }, where: { id: body.idnode },
}); });
if(!data){ if (!data) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
await this.strategy5Repo.delete({ strategyChild2Id: body.idnode }); await this.strategy5Repo.delete({ strategyChild2Id: body.idnode });
await this.strategy4Repo.delete({ strategyChild2Id: body.idnode }); await this.strategy4Repo.delete({ strategyChild2Id: body.idnode });
await this.strategy3Repo.delete({ strategyChild2Id: body.idnode }); await this.strategy3Repo.delete({ strategyChild2Id: body.idnode });
await this.strategy2Repo.delete({ id : body.idnode }); await this.strategy2Repo.delete({ id: body.idnode });
break; break;
case 3: case 3:
strategyRepo = this.strategy3Repo; strategyRepo = this.strategy3Repo;
data = await strategyRepo.find({ data = await strategyRepo.find({
where: { id: body.idnode }, where: { id: body.idnode },
}); });
if(!data){ if (!data) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
await this.strategy5Repo.delete({ strategyChild3Id: body.idnode }); await this.strategy5Repo.delete({ strategyChild3Id: body.idnode });
await this.strategy4Repo.delete({ strategyChild3Id: body.idnode }); await this.strategy4Repo.delete({ strategyChild3Id: body.idnode });
@ -301,10 +329,10 @@ export class StrategyController extends Controller {
case 4: case 4:
strategyRepo = this.strategy4Repo; strategyRepo = this.strategy4Repo;
data = await strategyRepo.find({ data = await strategyRepo.find({
where: { id: body.idnode }, where: { id: body.idnode },
}); });
if(!data){ if (!data) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
await this.strategy5Repo.delete({ strategyChild4Id: body.idnode }); await this.strategy5Repo.delete({ strategyChild4Id: body.idnode });
await this.strategy4Repo.delete({ id: body.idnode }); await this.strategy4Repo.delete({ id: body.idnode });
@ -312,10 +340,10 @@ export class StrategyController extends Controller {
case 5: case 5:
strategyRepo = this.strategy5Repo; strategyRepo = this.strategy5Repo;
data = await strategyRepo.find({ data = await strategyRepo.find({
where: { id: body.idnode }, where: { id: body.idnode },
}); });
if(!data){ if (!data) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์");
} }
await this.strategy5Repo.delete({ id: body.idnode }); await this.strategy5Repo.delete({ id: body.idnode });
break; break;