From 2e7bef04759a5f4108a46bf72e959fc9792e847d Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 11 Apr 2025 15:38:32 +0700 Subject: [PATCH] validate --- src/controllers/DevelopmentController.ts | 320 ++++++++++++++--------- 1 file changed, 192 insertions(+), 128 deletions(-) diff --git a/src/controllers/DevelopmentController.ts b/src/controllers/DevelopmentController.ts index 82c6d93..82a70e9 100644 --- a/src/controllers/DevelopmentController.ts +++ b/src/controllers/DevelopmentController.ts @@ -370,44 +370,54 @@ export class DevelopmentController extends Controller { await Promise.all( requestBody.positions.map(async (x) => { const _data = Object.assign(new PlannedGoalPosition(), x); - // if (x.posTypePlannedId != null) { - // let checkId:any - // if(requestBody.groupTarget == "PERSONNEL" && (requestBody.groupTargetSub == "EMPLOYEE" || requestBody.groupTargetSub == "EMPLOYEETEMP")) { - // checkId = await this.empPosTypeRepository.findOne({ - // where: { id: x.posTypePlannedId }, - // }); - // if (!checkId) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงาน"); - // } - // } - // else { - // checkId = await this.posTypeRepository.findOne({ - // where: { id: x.posTypePlannedId }, - // }); - // if (!checkId) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); - // } - // } - // } - // if (x.posLevelPlannedId != null) { - // let checkId:any - // if (requestBody.groupTarget == "PERSONNEL" && (requestBody.groupTargetSub == "EMPLOYEE" || requestBody.groupTargetSub == "EMPLOYEETEMP")) { - // checkId = await this.empPosLevelRepository.findOne({ - // where: { id: x.posLevelPlannedId }, - // }); - // if (!checkId) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงาน"); - // } - // } - // else { - // checkId = await this.posLevelRepository.findOne({ - // where: { id: x.posLevelPlannedId }, - // }); - // if (!checkId) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); - // } - // } - // } + let checkPosType:any = null + let posTypeShortName:any = null + if (x.posTypePlanned != null) { + if(requestBody.groupTarget == "PERSONNEL" && + (requestBody.groupTargetSub == "EMPLOYEE" || requestBody.groupTargetSub == "EMPLOYEETEMP")) { + checkPosType = await this.empPosTypeRepository.findOne({ + where: { posTypeName: x.posTypePlanned }, + }); + if (!checkPosType) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงาน"); + } + posTypeShortName = checkPosType.posTypeShortName + } + else { + checkPosType = await this.posTypeRepository.findOne({ + where: { posTypeName: x.posTypePlanned }, + }); + if (!checkPosType) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); + } + } + } + if (x.posLevelPlanned != null) { + let checkPosLevel:any + if (requestBody.groupTarget == "PERSONNEL" && + (requestBody.groupTargetSub == "EMPLOYEE" || requestBody.groupTargetSub == "EMPLOYEETEMP")) { + checkPosLevel = await this.empPosLevelRepository.find({ + where: { + posTypeId: checkPosType.id + }, + }); + const mapShortName = checkPosLevel.flatMap((x:any) => `${posTypeShortName} ${x.posLevelName}`); + if (checkPosLevel.length == 0 /*|| !mapShortName.includes(x.posLevelPlanned)*/) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงาน"); + } + } + else { + checkPosLevel = await this.posLevelRepository.findOne({ + where: { + posLevelName: x.posLevelPlanned, + posTypeId: checkPosType.id + }, + }); + if (!checkPosLevel) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); + } + } + } const before = structuredClone(development); _data.createdUserId = request.user.sub; _data.createdFullName = request.user.name; @@ -492,32 +502,54 @@ export class DevelopmentController extends Controller { if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } - // if (requestBody.posTypeActualId != null) { - // // addLogSequence(request, { - // // action: "database", - // // status: "success", - // // description: "Get Position Type.", - // // }); - // const checkId = await this.posTypeRepository.findOne({ - // where: { id: requestBody.posTypeActualId }, - // }); - // if (!checkId) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); - // } - // } - // if (requestBody.posLevelActualId != null) { - // // addLogSequence(request, { - // // action: "database", - // // status: "success", - // // description: "Get Position Level.", - // // }); - // const checkId = await this.posLevelRepository.findOne({ - // where: { id: requestBody.posLevelActualId }, - // }); - // if (!checkId) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); - // } - // } + let checkPosType:any = null + let posTypeShortName:any = null + if (requestBody.posTypeActual != null) { + if(requestBody.groupTarget == "PERSONNEL" && + (requestBody.groupTargetSub == "EMPLOYEE" || requestBody.groupTargetSub == "EMPLOYEETEMP")) { + checkPosType = await this.empPosTypeRepository.findOne({ + where: { posTypeName: requestBody.posTypeActual }, + }); + if (!checkPosType) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงาน"); + } + posTypeShortName = checkPosType.posTypeShortName + } + else { + checkPosType = await this.posTypeRepository.findOne({ + where: { posTypeName: requestBody.posTypeActual }, + }); + if (!checkPosType) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); + } + } + } + if (requestBody.posLevelActual != null) { + let checkPosLevel:any + if (requestBody.groupTarget == "PERSONNEL" && + (requestBody.groupTargetSub == "EMPLOYEE" || requestBody.groupTargetSub == "EMPLOYEETEMP")) { + checkPosLevel = await this.empPosLevelRepository.find({ + where: { + posTypeId: checkPosType.id + }, + }); + const mapShortName = checkPosLevel.flatMap((x:any) => `${posTypeShortName} ${x.posLevelName}`); + if (checkPosLevel.length == 0 /*|| !mapShortName.includes(x.posLevelPlanned)*/) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงาน"); + } + } + else { + checkPosLevel = await this.posLevelRepository.findOne({ + where: { + posLevelName: requestBody.posLevelActual, + posTypeId: checkPosType.id + }, + }); + if (!checkPosLevel) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); + } + } + } const before = structuredClone(development); const data = Object.assign(new ActualGoal(), requestBody); data.createdUserId = request.user.sub; @@ -633,44 +665,54 @@ export class DevelopmentController extends Controller { await Promise.all( requestBody.positions.map(async (x) => { const _data = Object.assign(new PlannedGoalPosition(), x); - // if (x.posTypePlannedId != null) { - // let checkId:any - // if(requestBody.groupTarget == "PERSONNEL" && (requestBody.groupTargetSub == "EMPLOYEE" || requestBody.groupTargetSub == "EMPLOYEETEMP")) { - // checkId = await this.empPosTypeRepository.findOne({ - // where: { id: x.posTypePlannedId }, - // }); - // if (!checkId) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงาน"); - // } - // } - // else { - // checkId = await this.posTypeRepository.findOne({ - // where: { id: x.posTypePlannedId }, - // }); - // if (!checkId) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); - // } - // } - // } - // if (x.posLevelPlannedId != null) { - // let checkId:any - // if (requestBody.groupTarget == "PERSONNEL" && (requestBody.groupTargetSub == "EMPLOYEE" || requestBody.groupTargetSub == "EMPLOYEETEMP")) { - // checkId = await this.empPosLevelRepository.findOne({ - // where: { id: x.posLevelPlannedId }, - // }); - // if (!checkId) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงาน"); - // } - // } - // else { - // checkId = await this.posLevelRepository.findOne({ - // where: { id: x.posLevelPlannedId }, - // }); - // if (!checkId) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); - // } - // } - // } + let checkPosType:any = null + let posTypeShortName:any = null + if (x.posTypePlanned != null) { + if(requestBody.groupTarget == "PERSONNEL" && + (requestBody.groupTargetSub == "EMPLOYEE" || requestBody.groupTargetSub == "EMPLOYEETEMP")) { + checkPosType = await this.empPosTypeRepository.findOne({ + where: { posTypeName: x.posTypePlanned }, + }); + if (!checkPosType) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงาน"); + } + posTypeShortName = checkPosType.posTypeShortName + } + else { + checkPosType = await this.posTypeRepository.findOne({ + where: { posTypeName: x.posTypePlanned }, + }); + if (!checkPosType) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); + } + } + } + if (x.posLevelPlanned != null) { + let checkPosLevel:any + if (requestBody.groupTarget == "PERSONNEL" && + (requestBody.groupTargetSub == "EMPLOYEE" || requestBody.groupTargetSub == "EMPLOYEETEMP")) { + checkPosLevel = await this.empPosLevelRepository.find({ + where: { + posTypeId: checkPosType.id + }, + }); + const mapShortName = checkPosLevel.flatMap((x:any) => `${posTypeShortName} ${x.posLevelName}`); + if (checkPosLevel.length == 0 /*|| !mapShortName.includes(x.posLevelPlanned)*/) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงาน"); + } + } + else { + checkPosLevel = await this.posLevelRepository.findOne({ + where: { + posLevelName: x.posLevelPlanned, + posTypeId: checkPosType.id + }, + }); + if (!checkPosLevel) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); + } + } + } _data.createdUserId = request.user.sub; _data.createdFullName = request.user.name; _data.lastUpdateUserId = request.user.sub; @@ -759,32 +801,54 @@ export class DevelopmentController extends Controller { if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } - // if (requestBody.posTypeActualId != null) { - // // addLogSequence(request, { - // // action: "database", - // // status: "success", - // // description: "Get Position Type.", - // // }); - // const checkId = await this.posTypeRepository.findOne({ - // where: { id: requestBody.posTypeActualId }, - // }); - // if (!checkId) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); - // } - // } - // if (requestBody.posLevelActualId != null) { - // // addLogSequence(request, { - // // action: "database", - // // status: "success", - // // description: "Get Position Level.", - // // }); - // const checkId = await this.posLevelRepository.findOne({ - // where: { id: requestBody.posLevelActualId }, - // }); - // if (!checkId) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); - // } - // } + let checkPosType:any = null + let posTypeShortName:any = null + if (requestBody.posTypeActual != null) { + if(requestBody.groupTarget == "PERSONNEL" && + (requestBody.groupTargetSub == "EMPLOYEE" || requestBody.groupTargetSub == "EMPLOYEETEMP")) { + checkPosType = await this.empPosTypeRepository.findOne({ + where: { posTypeName: requestBody.posTypeActual }, + }); + if (!checkPosType) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงาน"); + } + posTypeShortName = checkPosType.posTypeShortName + } + else { + checkPosType = await this.posTypeRepository.findOne({ + where: { posTypeName: requestBody.posTypeActual }, + }); + if (!checkPosType) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); + } + } + } + if (requestBody.posLevelActual != null) { + let checkPosLevel:any + if (requestBody.groupTarget == "PERSONNEL" && + (requestBody.groupTargetSub == "EMPLOYEE" || requestBody.groupTargetSub == "EMPLOYEETEMP")) { + checkPosLevel = await this.empPosLevelRepository.find({ + where: { + posTypeId: checkPosType.id + }, + }); + const mapShortName = checkPosLevel.flatMap((x:any) => `${posTypeShortName} ${x.posLevelName}`); + if (checkPosLevel.length == 0 /*|| !mapShortName.includes(x.posLevelPlanned)*/) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงาน"); + } + } + else { + checkPosLevel = await this.posLevelRepository.findOne({ + where: { + posLevelName: requestBody.posLevelActual, + posTypeId: checkPosType.id + }, + }); + if (!checkPosLevel) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); + } + } + } const before = structuredClone(development); Object.assign(development, requestBody); development.lastUpdateUserId = request.user.sub;