validate citizenId

This commit is contained in:
AdisakKanthawilang 2024-05-15 13:38:00 +07:00
parent a6f575bff3
commit 89cdee99b3
2 changed files with 98 additions and 13 deletions

View file

@ -180,7 +180,7 @@ export class ProfileController extends Controller {
if (await this.profileRepo.findOneBy({ citizenId: body.citizenId })) { if (await this.profileRepo.findOneBy({ citizenId: body.citizenId })) {
throw new HttpError( throw new HttpError(
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
"เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว", "รหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว",
); );
} }
@ -195,6 +195,10 @@ export class ProfileController extends Controller {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
} }
if (body.citizenId.length !== 13) {
throw new HttpError(HttpStatus.NOT_FOUND, "รหัสบัตรประจำตัวประชาชนไม่ถูกต้อง");
}
if (body.citizenId) { if (body.citizenId) {
const citizenIdDigits = body.citizenId.toString().split("").map(Number); const citizenIdDigits = body.citizenId.toString().split("").map(Number);
const cal = const cal =
@ -215,16 +219,20 @@ export class ProfileController extends Controller {
if(chkDigit === 10){ if(chkDigit === 10){
chkDigit = 1; chkDigit = 1;
}else if(chkDigit === 11){ }else if(chkDigit === 11){
chkDigit = cal % 10; chkDigit = chkDigit % 10;
} }
// if(chkDigit && citizenIdDigits[12] !== chkDigit){ // else if(chkDigit === 11){
// throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); // chkDigit = cal % 10;
// } // }
if(citizenIdDigits[12] !== chkDigit){
throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง");
}
} }
if (body.citizenId && (await this.profileRepo.findOneBy({ citizenId: body.citizenId }))) { // if (body.citizenId && (await this.profileRepo.findOneBy({ citizenId: body.citizenId }))) {
throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว"); // throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว");
} // }
const profile = Object.assign(new Profile(), body); const profile = Object.assign(new Profile(), body);
profile.isProbation = false; profile.isProbation = false;
@ -307,7 +315,7 @@ export class ProfileController extends Controller {
})); }));
if (exists) { if (exists) {
throw new HttpError(HttpStatus.CONFLICT, "เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว"); throw new HttpError(HttpStatus.CONFLICT, "รหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว");
} }
if (body.posLevelId === "") body.posLevelId = null; if (body.posLevelId === "") body.posLevelId = null;
@ -321,6 +329,41 @@ export class ProfileController extends Controller {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); 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 }); const record = await this.profileRepo.findOneBy({ id });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้");

View file

@ -59,7 +59,7 @@ export class ProfileEmployeeController extends Controller {
if (await this.profileRepo.findOneBy({ citizenId: body.citizenId })) { if (await this.profileRepo.findOneBy({ citizenId: body.citizenId })) {
throw new HttpError( throw new HttpError(
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
"เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว", "รหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว",
); );
} }
@ -74,6 +74,10 @@ export class ProfileEmployeeController extends Controller {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
} }
if (body.citizenId.length !== 13) {
throw new HttpError(HttpStatus.NOT_FOUND, "รหัสบัตรประจำตัวประชาชนไม่ถูกต้อง");
}
if (body.citizenId) { if (body.citizenId) {
const citizenIdDigits = body.citizenId.toString().split("").map(Number); const citizenIdDigits = body.citizenId.toString().split("").map(Number);
const cal = const cal =
@ -94,11 +98,15 @@ export class ProfileEmployeeController extends Controller {
if(chkDigit === 10){ if(chkDigit === 10){
chkDigit = 1; chkDigit = 1;
}else if(chkDigit === 11){ }else if(chkDigit === 11){
chkDigit = cal % 10; chkDigit = chkDigit % 10;
} }
// if(chkDigit && citizenIdDigits[12] !== chkDigit){ // else if(chkDigit === 11){
// throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); // chkDigit = cal % 10;
// } // }
if(citizenIdDigits[12] !== chkDigit){
throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง");
}
} }
const profile = Object.assign(new ProfileEmployee(), body); const profile = Object.assign(new ProfileEmployee(), body);
@ -131,7 +139,7 @@ export class ProfileEmployeeController extends Controller {
})); }));
if (exists) { if (exists) {
throw new HttpError(HttpStatus.CONFLICT, "เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว"); throw new HttpError(HttpStatus.CONFLICT, "รหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว");
} }
if (body.posLevelId === "") body.posLevelId = null; 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 }))) { if (body.posTypeId && !(await this.posTypeRepo.findOneBy({ id: body.posTypeId }))) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); 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 }); const record = await this.profileRepo.findOneBy({ id });