diff --git a/src/controllers/DevelopmentController.ts b/src/controllers/DevelopmentController.ts index 6fbafd9..0c8c908 100644 --- a/src/controllers/DevelopmentController.ts +++ b/src/controllers/DevelopmentController.ts @@ -588,7 +588,7 @@ export class DevelopmentController extends Controller { }); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; - await this.developmentRepository.save(development); + await this.developmentProjectTypeRepository.remove(development.developmentProjectTypes); await this.developmentProjectTechniquePlannedRepository.remove( development.developmentProjectTechniquePlanneds, @@ -645,88 +645,98 @@ export class DevelopmentController extends Controller { ) { switch (requestBody.strategyChildPlannedNode) { case 1: { - const checkId = await this.strategyChild1Repository.findOne({ - where: { id: requestBody.strategyChildPlannedId }, - }); - if (!checkId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 1", - ); + if (requestBody.strategyChildPlannedId) { + const checkId = await this.strategyChild1Repository.findOne({ + where: { id: requestBody.strategyChildPlannedId }, + }); + if (!checkId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 1", + ); + } + development.strategyChild1PlannedId = checkId.id; + development.strategyChild2PlannedId = _null; + development.strategyChild3PlannedId = _null; + development.strategyChild4PlannedId = _null; + development.strategyChild5PlannedId = _null; } - development.strategyChild1PlannedId = checkId.id; - development.strategyChild2ActualId = _null; - development.strategyChild3ActualId = _null; - development.strategyChild4ActualId = _null; - development.strategyChild5ActualId = _null; break; } case 2: { - const checkId = await this.strategyChild2Repository.findOne({ - where: { id: requestBody.strategyChildPlannedId }, - }); - if (!checkId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 2", - ); + if (requestBody.strategyChildPlannedId) { + const checkId = await this.strategyChild2Repository.findOne({ + where: { id: requestBody.strategyChildPlannedId }, + }); + if (!checkId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 2", + ); + } + development.strategyChild1PlannedId = checkId.strategyChild1Id; + development.strategyChild2PlannedId = checkId.id; + development.strategyChild3PlannedId = _null; + development.strategyChild4PlannedId = _null; + development.strategyChild5PlannedId = _null; } - development.strategyChild1PlannedId = checkId.strategyChild1Id; - development.strategyChild2PlannedId = checkId.id; - development.strategyChild3ActualId = _null; - development.strategyChild4ActualId = _null; - development.strategyChild5ActualId = _null; break; } case 3: { - const checkId = await this.strategyChild3Repository.findOne({ - where: { id: requestBody.strategyChildPlannedId }, - }); - if (!checkId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 2", - ); + if (requestBody.strategyChildPlannedId) { + const checkId = await this.strategyChild3Repository.findOne({ + where: { id: requestBody.strategyChildPlannedId }, + }); + if (!checkId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 2", + ); + } + development.strategyChild1PlannedId = checkId.strategyChild1Id; + development.strategyChild2PlannedId = checkId.strategyChild2Id; + development.strategyChild3PlannedId = checkId.id; + development.strategyChild4PlannedId = _null; + development.strategyChild5PlannedId = _null; } - development.strategyChild1PlannedId = checkId.strategyChild1Id; - development.strategyChild2PlannedId = checkId.strategyChild2Id; - development.strategyChild3PlannedId = checkId.id; - development.strategyChild4ActualId = _null; - development.strategyChild5ActualId = _null; break; } case 4: { - const checkId = await this.strategyChild4Repository.findOne({ - where: { id: requestBody.strategyChildPlannedId }, - }); - if (!checkId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 3", - ); + if (requestBody.strategyChildPlannedId) { + const checkId = await this.strategyChild4Repository.findOne({ + where: { id: requestBody.strategyChildPlannedId }, + }); + if (!checkId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 3", + ); + } + development.strategyChild1PlannedId = checkId.strategyChild1Id; + development.strategyChild2PlannedId = checkId.strategyChild2Id; + development.strategyChild3PlannedId = checkId.strategyChild3Id; + development.strategyChild4PlannedId = checkId.id; + development.strategyChild5PlannedId = _null; } - development.strategyChild1PlannedId = checkId.strategyChild1Id; - development.strategyChild2PlannedId = checkId.strategyChild2Id; - development.strategyChild3PlannedId = checkId.strategyChild3Id; - development.strategyChild4PlannedId = checkId.id; - development.strategyChild5ActualId = _null; break; } case 5: { - const checkId = await this.strategyChild5Repository.findOne({ - where: { id: requestBody.strategyChildPlannedId }, - }); - if (!checkId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 2", - ); + if (requestBody.strategyChildPlannedId) { + const checkId = await this.strategyChild5Repository.findOne({ + where: { id: requestBody.strategyChildPlannedId }, + }); + if (!checkId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 2", + ); + } + development.strategyChild1PlannedId = checkId.strategyChild1Id; + development.strategyChild2PlannedId = checkId.strategyChild2Id; + development.strategyChild3PlannedId = checkId.strategyChild3Id; + development.strategyChild4PlannedId = checkId.strategyChild4Id; + development.strategyChild5PlannedId = checkId.id; } - development.strategyChild1PlannedId = checkId.strategyChild1Id; - development.strategyChild2PlannedId = checkId.strategyChild2Id; - development.strategyChild3PlannedId = checkId.strategyChild3Id; - development.strategyChild4PlannedId = checkId.strategyChild4Id; - development.strategyChild5PlannedId = checkId.id; break; } @@ -740,88 +750,98 @@ export class DevelopmentController extends Controller { ) { switch (requestBody.strategyChildActualNode) { case 1: { - const checkId = await this.strategyChild1Repository.findOne({ - where: { id: requestBody.strategyChildActualId }, - }); - if (!checkId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 1", - ); + if (requestBody.strategyChildActualId) { + const checkId = await this.strategyChild1Repository.findOne({ + where: { id: requestBody.strategyChildActualId }, + }); + if (!checkId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 1", + ); + } + development.strategyChild1ActualId = checkId.id; + development.strategyChild2ActualId = _null; + development.strategyChild3ActualId = _null; + development.strategyChild4ActualId = _null; + development.strategyChild5ActualId = _null; } - development.strategyChild1ActualId = checkId.id; - development.strategyChild2ActualId = _null; - development.strategyChild3ActualId = _null; - development.strategyChild4ActualId = _null; - development.strategyChild5ActualId = _null; break; } case 2: { - const checkId = await this.strategyChild2Repository.findOne({ - where: { id: requestBody.strategyChildActualId }, - }); - if (!checkId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 2", - ); + if (requestBody.strategyChildActualId) { + const checkId = await this.strategyChild2Repository.findOne({ + where: { id: requestBody.strategyChildActualId }, + }); + if (!checkId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 2", + ); + } + development.strategyChild1ActualId = checkId.strategyChild1Id; + development.strategyChild2ActualId = checkId.id; + development.strategyChild3ActualId = _null; + development.strategyChild4ActualId = _null; + development.strategyChild5ActualId = _null; } - development.strategyChild1ActualId = checkId.strategyChild1Id; - development.strategyChild2ActualId = checkId.id; - development.strategyChild3ActualId = _null; - development.strategyChild4ActualId = _null; - development.strategyChild5ActualId = _null; break; } case 3: { - const checkId = await this.strategyChild3Repository.findOne({ - where: { id: requestBody.strategyChildActualId }, - }); - if (!checkId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 3", - ); + if (requestBody.strategyChildActualId) { + const checkId = await this.strategyChild3Repository.findOne({ + where: { id: requestBody.strategyChildActualId }, + }); + if (!checkId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 3", + ); + } + development.strategyChild1ActualId = checkId.strategyChild1Id; + development.strategyChild2ActualId = checkId.strategyChild2Id; + development.strategyChild3ActualId = checkId.id; + development.strategyChild4ActualId = _null; + development.strategyChild5ActualId = _null; } - development.strategyChild1ActualId = checkId.strategyChild1Id; - development.strategyChild2ActualId = checkId.strategyChild2Id; - development.strategyChild3ActualId = checkId.id; - development.strategyChild4ActualId = _null; - development.strategyChild5ActualId = _null; break; } case 4: { - const checkId = await this.strategyChild4Repository.findOne({ - where: { id: requestBody.strategyChildActualId }, - }); - if (!checkId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 4", - ); + if (requestBody.strategyChildActualId) { + const checkId = await this.strategyChild4Repository.findOne({ + where: { id: requestBody.strategyChildActualId }, + }); + if (!checkId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 4", + ); + } + development.strategyChild1ActualId = checkId.strategyChild1Id; + development.strategyChild2ActualId = checkId.strategyChild2Id; + development.strategyChild3ActualId = checkId.strategyChild3Id; + development.strategyChild4ActualId = checkId.id; + development.strategyChild5ActualId = _null; } - development.strategyChild1ActualId = checkId.strategyChild1Id; - development.strategyChild2ActualId = checkId.strategyChild2Id; - development.strategyChild3ActualId = checkId.strategyChild3Id; - development.strategyChild4ActualId = checkId.id; - development.strategyChild5ActualId = _null; break; } case 5: { - const checkId = await this.strategyChild5Repository.findOne({ - where: { id: requestBody.strategyChildActualId }, - }); - if (!checkId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 5", - ); + if (requestBody.strategyChildActualId) { + const checkId = await this.strategyChild5Repository.findOne({ + where: { id: requestBody.strategyChildActualId }, + }); + if (!checkId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 5", + ); + } + development.strategyChild1ActualId = checkId.strategyChild1Id; + development.strategyChild2ActualId = checkId.strategyChild2Id; + development.strategyChild3ActualId = checkId.strategyChild3Id; + development.strategyChild4ActualId = checkId.strategyChild4Id; + development.strategyChild5ActualId = checkId.id; } - development.strategyChild1ActualId = checkId.strategyChild1Id; - development.strategyChild2ActualId = checkId.strategyChild2Id; - development.strategyChild3ActualId = checkId.strategyChild3Id; - development.strategyChild4ActualId = checkId.strategyChild4Id; - development.strategyChild5ActualId = checkId.id; break; } @@ -829,6 +849,7 @@ export class DevelopmentController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริง"); } } + await this.developmentRepository.save(development); return new HttpSuccess(development.id); } @@ -1253,6 +1274,7 @@ export class DevelopmentController extends Controller { ), ) .map((x) => ({ + id: x.id, groupTarget: x.groupTarget, groupTargetSub: x.groupTargetSub, position: x.position, @@ -1273,10 +1295,12 @@ export class DevelopmentController extends Controller { ), ) .map((x) => ({ + id: x.id, groupTarget: x.groupTarget, groupTargetSub: x.groupTargetSub, type: x.type, position: x.plannedGoalPositions.map((y) => ({ + id: y.id, position: y.position, posTypeId: y.posTypePlannedId, posType: y.posTypePlanned == null ? null : y.posTypePlanned.posTypeName, diff --git a/src/entities/Development.ts b/src/entities/Development.ts index efafab1..898b86a 100644 --- a/src/entities/Development.ts +++ b/src/entities/Development.ts @@ -610,13 +610,13 @@ export class UpdateDevelopment3 { @Column() developmentProjectTechniqueActuals?: string[]; @Column() - strategyChildPlannedId?: string; + strategyChildPlannedId?: string | null; @Column() - strategyChildPlannedNode?: number; + strategyChildPlannedNode?: number | null; @Column() - strategyChildActualId?: string; + strategyChildActualId?: string | null; @Column() - strategyChildActualNode?: number; + strategyChildActualNode?: number | null; } export class UpdateDevelopment4 { // @Column()