From 89cdee99b30712e4ba148c9924bb61a37cc2eb70 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 15 May 2024 13:38:00 +0700 Subject: [PATCH] validate citizenId --- src/controllers/ProfileController.ts | 59 +++++++++++++++++--- src/controllers/ProfileEmployeeController.ts | 52 +++++++++++++++-- 2 files changed, 98 insertions(+), 13 deletions(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 348182c3..b8f7c3d2 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -180,7 +180,7 @@ export class ProfileController extends Controller { if (await this.profileRepo.findOneBy({ citizenId: body.citizenId })) { throw new HttpError( HttpStatus.INTERNAL_SERVER_ERROR, - "เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว", + "รหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว", ); } @@ -195,6 +195,10 @@ export class ProfileController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } + if (body.citizenId.length !== 13) { + throw new HttpError(HttpStatus.NOT_FOUND, "รหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + if (body.citizenId) { const citizenIdDigits = body.citizenId.toString().split("").map(Number); const cal = @@ -215,16 +219,20 @@ export class ProfileController extends Controller { if(chkDigit === 10){ chkDigit = 1; }else if(chkDigit === 11){ - chkDigit = cal % 10; + chkDigit = chkDigit % 10; } - // if(chkDigit && citizenIdDigits[12] !== chkDigit){ - // throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + // else if(chkDigit === 11){ + // chkDigit = cal % 10; // } + + if(citizenIdDigits[12] !== chkDigit){ + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } } - if (body.citizenId && (await this.profileRepo.findOneBy({ citizenId: body.citizenId }))) { - throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว"); - } + // if (body.citizenId && (await this.profileRepo.findOneBy({ citizenId: body.citizenId }))) { + // throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว"); + // } const profile = Object.assign(new Profile(), body); profile.isProbation = false; @@ -307,7 +315,7 @@ export class ProfileController extends Controller { })); if (exists) { - throw new HttpError(HttpStatus.CONFLICT, "เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว"); + throw new HttpError(HttpStatus.CONFLICT, "รหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว"); } if (body.posLevelId === "") body.posLevelId = null; @@ -320,6 +328,41 @@ export class ProfileController extends Controller { if (body.posTypeId && !(await this.posTypeRepo.findOneBy({ id: body.posTypeId }))) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } + + if (body.citizenId && body.citizenId.length !== 13) { + throw new HttpError(HttpStatus.NOT_FOUND, "รหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + + if (body.citizenId) { + const citizenIdDigits = body.citizenId.toString().split("").map(Number); + const cal = + citizenIdDigits[0] * 13 + + citizenIdDigits[1] * 12 + + citizenIdDigits[2] * 11 + + citizenIdDigits[3] * 10 + + citizenIdDigits[4] * 9 + + citizenIdDigits[5] * 8 + + citizenIdDigits[6] * 7 + + citizenIdDigits[7] * 6 + + citizenIdDigits[8] * 5 + + citizenIdDigits[9] * 4 + + citizenIdDigits[10] * 3 + + citizenIdDigits[11] * 2; + const calStp2 = cal % 11; + let chkDigit = 11 - calStp2; + if(chkDigit === 10){ + chkDigit = 1; + }else if(chkDigit === 11){ + chkDigit = chkDigit % 10; + } + // else if(chkDigit === 11){ + // chkDigit = cal % 10; + // } + + if(citizenIdDigits[12] !== chkDigit){ + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + } const record = await this.profileRepo.findOneBy({ id }); diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index b3bcf409..2a569c72 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -59,7 +59,7 @@ export class ProfileEmployeeController extends Controller { if (await this.profileRepo.findOneBy({ citizenId: body.citizenId })) { throw new HttpError( HttpStatus.INTERNAL_SERVER_ERROR, - "เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว", + "รหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว", ); } @@ -74,6 +74,10 @@ export class ProfileEmployeeController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } + if (body.citizenId.length !== 13) { + throw new HttpError(HttpStatus.NOT_FOUND, "รหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + if (body.citizenId) { const citizenIdDigits = body.citizenId.toString().split("").map(Number); const cal = @@ -94,11 +98,15 @@ export class ProfileEmployeeController extends Controller { if(chkDigit === 10){ chkDigit = 1; }else if(chkDigit === 11){ - chkDigit = cal % 10; + chkDigit = chkDigit % 10; } - // if(chkDigit && citizenIdDigits[12] !== chkDigit){ - // throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + // else if(chkDigit === 11){ + // chkDigit = cal % 10; // } + + if(citizenIdDigits[12] !== chkDigit){ + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } } const profile = Object.assign(new ProfileEmployee(), body); @@ -131,7 +139,7 @@ export class ProfileEmployeeController extends Controller { })); if (exists) { - throw new HttpError(HttpStatus.CONFLICT, "เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว"); + throw new HttpError(HttpStatus.CONFLICT, "รหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว"); } if (body.posLevelId === "") body.posLevelId = null; @@ -144,6 +152,40 @@ export class ProfileEmployeeController extends Controller { if (body.posTypeId && !(await this.posTypeRepo.findOneBy({ id: body.posTypeId }))) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } + if (body.citizenId && body.citizenId.length !== 13) { + throw new HttpError(HttpStatus.NOT_FOUND, "รหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + + if (body.citizenId) { + const citizenIdDigits = body.citizenId.toString().split("").map(Number); + const cal = + citizenIdDigits[0] * 13 + + citizenIdDigits[1] * 12 + + citizenIdDigits[2] * 11 + + citizenIdDigits[3] * 10 + + citizenIdDigits[4] * 9 + + citizenIdDigits[5] * 8 + + citizenIdDigits[6] * 7 + + citizenIdDigits[7] * 6 + + citizenIdDigits[8] * 5 + + citizenIdDigits[9] * 4 + + citizenIdDigits[10] * 3 + + citizenIdDigits[11] * 2; + const calStp2 = cal % 11; + let chkDigit = 11 - calStp2; + if(chkDigit === 10){ + chkDigit = 1; + }else if(chkDigit === 11){ + chkDigit = chkDigit % 10; + } + // else if(chkDigit === 11){ + // chkDigit = cal % 10; + // } + + if(citizenIdDigits[12] !== chkDigit){ + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + } const record = await this.profileRepo.findOneBy({ id });