From 3fac128e5b8a6fe8b1298028a6b0a8cc9f3bc973 Mon Sep 17 00:00:00 2001 From: Bright Date: Mon, 29 Jan 2024 18:07:43 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=9B=E0=B8=A3=E0=B8=B1=E0=B8=9A=20validate?= =?UTF-8?q?=20orgRevision=20(root)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/OrgRootController.ts | 45 +++++++++++++--------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/src/controllers/OrgRootController.ts b/src/controllers/OrgRootController.ts index ea059974..09bd8a87 100644 --- a/src/controllers/OrgRootController.ts +++ b/src/controllers/OrgRootController.ts @@ -103,25 +103,20 @@ export class OrgRootController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); } - const orgRevision = await this.orgRevisionRepository.findOne({ - where: { - id: requestBody.orgRevisionId, - orgRevisionIsDraft: true, - orgRevisionIsCurrent: false, - }, - }); - - if (orgRevision) { - orgRoot.createdUserId = request.user.sub; - orgRoot.createdFullName = request.user.name; - orgRoot.lastUpdateUserId = request.user.sub; - orgRoot.lastUpdateFullName = request.user.name; - await this.orgRootRepository.save(orgRoot); - } else { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี Revision"); + const orgRevision = await this.orgRevisionRepository.findOne({ where: { id: requestBody.orgRevisionId }}); + if (!orgRevision) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); - } + } + if(orgRevision.orgRevisionIsDraft != true && orgRevision.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } + + orgRoot.createdUserId = request.user.sub; + orgRoot.createdFullName = request.user.name; + orgRoot.lastUpdateUserId = request.user.sub; + orgRoot.lastUpdateFullName = request.user.name; + await this.orgRootRepository.save(orgRoot); return new HttpSuccess(); } catch (error) { return error; @@ -159,16 +154,13 @@ export class OrgRootController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootRank"); } - const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { - id: requestBody.orgRevisionId, - orgRevisionIsDraft: true, - orgRevisionIsCurrent: false, - }, - }); + const revisionIdExits = await this.orgRevisionRepository.findOne({ where: {id: requestBody.orgRevisionId} }); if (!revisionIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } const chkCode = await this.orgRootRepository.findOne({ where: { orgRootCode: requestBody.orgRootCode }, @@ -218,11 +210,14 @@ export class OrgRootController extends Controller { } const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: orgRoot.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + where: { id: orgRoot.orgRevisionId }, }); if (!revisionIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } await this.orgRootRepository.remove(orgRoot); return new HttpSuccess();