From 102f1f66468a615d5fcf7a5ee0a898ee6a91f6f5 Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 22 Nov 2024 11:34:11 +0700 Subject: [PATCH 1/3] =?UTF-8?q?=E0=B9=80=E0=B8=8A=E0=B9=87=E0=B8=84=20upda?= =?UTF-8?q?te=20role=20keycloak=20#802?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 94 +++++++++++++++++----------- 1 file changed, 58 insertions(+), 36 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index b3993ebb..d9170f68 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -2172,26 +2172,30 @@ export class CommandController extends Controller { if (clearProfile) { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); - if (delUserKeycloak) profile.keycloak = _null; - profile.roleKeycloaks = []; + if (delUserKeycloak) { + profile.keycloak = _null; + profile.roleKeycloaks = []; + profile.isActive = false; + } } profile.position = _null; profile.posTypeId = _null; profile.posLevelId = _null; } const returnWork = await checkReturnCommandType(String(item.commandId)); + //คำสั่งบรรจุกลับเข้ารับราชการ หรือ ผู้ออกไปรับราชการทหารกลับเข้ารับราชการ solutionเดิม ให้ enable user เปลี่ยนเป็นสร้าง user ใหม่เลยเพราะยังไงตอนถูกพักก็ถูกลบ user if (returnWork && item.isGovernment) { - if (profile.keycloak != null) { + /*if (profile.keycloak != null) { const enableActive = await enableStatus(profile.keycloak, true); if (!enableActive) throw new Error("Failed. Cannot change enable status."); - } else { + } else {*/ const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, { firstName: profile.firstName, lastName: profile.lastName, }); - if (typeof userKeycloakId !== "string") { - throw new Error(userKeycloakId.errorMessage); - } + // if (typeof userKeycloakId !== "string") { + // throw new Error(userKeycloakId.errorMessage); + // } const list = await getRoles(); if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); @@ -2204,11 +2208,11 @@ export class CommandController extends Controller { name: x.name, })), ); - if (!result) throw new Error("Failed. Cannot set user's role."); - profile.keycloak = userKeycloakId; - } + // if (!result) throw new Error("Failed. Cannot set user's role."); + profile.keycloak = typeof userKeycloakId === "string" ? userKeycloakId : ""; + /*}*/ profile.isActive = true; - profile.roleKeycloaks = profile.roleKeycloaks ?? [roleKeycloak]; + profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : []; } await this.profileRepository.save(profile); }), @@ -2297,9 +2301,11 @@ export class CommandController extends Controller { if (clearProfile) { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); - if (delUserKeycloak) profile.keycloak = _null; - profile.roleKeycloaks = []; - profile.isActive = false; + if (delUserKeycloak) { + profile.keycloak = _null; + profile.roleKeycloaks = []; + profile.isActive = false; + } } profile.position = _null; profile.posTypeId = _null; @@ -2373,8 +2379,11 @@ export class CommandController extends Controller { if (clearProfile) { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); - if (delUserKeycloak) profile.keycloak = _null; - profile.roleKeycloaks = []; + if (delUserKeycloak) { + profile.keycloak = _null; + profile.roleKeycloaks = []; + profile.isActive = false; + } } profile.position = _null; profile.posTypeId = _null; @@ -2615,17 +2624,27 @@ export class CommandController extends Controller { const exceptClear = await checkExceptCommandType(String(item.commandId)); if (item.isLeave == true && !exceptClear) { await removeProfileInOrganize(_profile.id, "OFFICER"); - } else if (item.isLeave == true && exceptClear && _profile.keycloak != null) { - const enableActive = await enableStatus(_profile.keycloak, false); - if (!enableActive) throw new Error("Failed. Cannot change enable status."); - _profile.isActive = false; + } + //คำสั่งพักราชการ หรือ ให้ออกจากราชการไว้ก่อน solutionเดิม ให้ disable user ไว้แต่ยังไม่ลบ เปลี่ยนเป็นลบ user ออกเลย + else if (item.isLeave == true && exceptClear && _profile.keycloak != null) { + // const enableActive = await enableStatus(_profile.keycloak, false); + // if (!enableActive) throw new Error("Failed. Cannot change enable status."); + const delUserKeycloak = await deleteUser(_profile.keycloak); + if (delUserKeycloak) { + _profile.keycloak = _null; + _profile.roleKeycloaks = []; + _profile.isActive = false; + } } const clearProfile = await checkCommandType(String(item.commandId)); if (clearProfile) { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); - if (delUserKeycloak) _profile.keycloak = _null; - _profile.roleKeycloaks = []; + if (delUserKeycloak) { + _profile.keycloak = _null; + _profile.roleKeycloaks = []; + _profile.isActive = false; + } } _profile.position = _null; _profile.posTypeId = _null; @@ -2903,8 +2922,11 @@ export class CommandController extends Controller { if (clearProfile) { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); - if (delUserKeycloak) _profile.keycloak = _null; - _profile.roleKeycloaks = []; + if (delUserKeycloak) { + _profile.keycloak = _null; + _profile.roleKeycloaks = []; + _profile.isActive = false; + } } _profile.position = _null; _profile.posTypeId = _null; @@ -2989,9 +3011,9 @@ export class CommandController extends Controller { firstName: profile.firstName, lastName: profile.lastName, }); - if (typeof userKeycloakId !== "string") { - throw new Error(userKeycloakId.errorMessage); - } + // if (typeof userKeycloakId !== "string") { + // throw new Error(userKeycloakId.errorMessage); + // } const list = await getRoles(); if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); @@ -3004,9 +3026,9 @@ export class CommandController extends Controller { name: x.name, })), ); - if (!result) throw new Error("Failed. Cannot set user's role."); - profile.keycloak = userKeycloakId; - profile.roleKeycloaks = roleKeycloak ? [roleKeycloak] : []; + // if (!result) throw new Error("Failed. Cannot set user's role."); + profile.keycloak = typeof userKeycloakId === "string" ? userKeycloakId : ""; + profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : []; profile.email = item.bodyProfile.email; profile.dateStart = item.bodyProfile.dateStart; await this.profileRepository.save(profile); @@ -3322,9 +3344,9 @@ export class CommandController extends Controller { lastName: profile.lastName, // email: profile.email, }); - if (typeof userKeycloakId !== "string") { - throw new Error(userKeycloakId.errorMessage); - } + // if (typeof userKeycloakId !== "string") { + // throw new Error(userKeycloakId.errorMessage); + // } const list = await getRoles(); if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); @@ -3337,9 +3359,9 @@ export class CommandController extends Controller { name: x.name, })), ); - if (!result) throw new Error("Failed. Cannot set user's role."); - profile.keycloak = userKeycloakId; - profile.roleKeycloaks = roleKeycloak ? [roleKeycloak] : []; + // if (!result) throw new Error("Failed. Cannot set user's role."); + profile.keycloak = typeof userKeycloakId == "string" ? userKeycloakId : ""; + profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : []; // End Create Keycloak positionNew.positionIsSelected = true; From e9f645052a70c7912d489e06543c603f101ca957 Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 22 Nov 2024 12:05:39 +0700 Subject: [PATCH 2/3] =?UTF-8?q?add=20default=20=E0=B8=8A=E0=B9=88=E0=B8=AD?= =?UTF-8?q?=E0=B8=87=E0=B8=97=E0=B8=B2=E0=B8=87=E0=B8=81=E0=B8=B2=E0=B8=A3?= =?UTF-8?q?=E0=B8=AA=E0=B9=88=E0=B8=87=E0=B8=AA=E0=B8=B3=E0=B9=80=E0=B8=99?= =?UTF-8?q?=E0=B8=B2=20#99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index d9170f68..ae1d3241 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -698,6 +698,23 @@ export class CommandController extends Controller { commandSend.lastUpdateFullName = request.user.name; commandSend.lastUpdatedAt = new Date(); await this.commandSendRepository.save(commandSend); + if(commandSend && commandSend.id) { + let _ccName = new Array("INBOX", "EMAIL"); + let _dataSendCC = new Array(); + for(let i=0; i<_ccName.length; i++) { + _dataSendCC.push({ + commandSendId: commandSend.id, + name: _ccName[i], + createdUserId: request.user.sub, + createdFullName: request.user.name, + createdAt: new Date(), + lastUpdateUserId: request.user.sub, + lastUpdateFullName: request.user.name, + lastUpdatedAt: new Date(), + }); + } + await this.commandSendCCRepository.save(_dataSendCC); + } }), ); From 44b8b516eb6a5916b2aedfe92bba08b19c093abb Mon Sep 17 00:00:00 2001 From: kittapath Date: Fri, 22 Nov 2024 12:18:12 +0700 Subject: [PATCH 3/3] no message --- src/controllers/ImportDataController.ts | 26 +++- src/controllers/ProfileController.ts | 126 +++++++++++++++--- src/controllers/ProfileEmployeeController.ts | 63 ++++++++- src/entities/Profile.ts | 19 ++- src/entities/ProfileEmployee.ts | 8 ++ ...252198299-updata_profile_add_prefixmain.ts | 20 +++ 6 files changed, 241 insertions(+), 21 deletions(-) create mode 100644 src/migration/1732252198299-updata_profile_add_prefixmain.ts diff --git a/src/controllers/ImportDataController.ts b/src/controllers/ImportDataController.ts index 45eef18f..aed3e543 100644 --- a/src/controllers/ImportDataController.ts +++ b/src/controllers/ImportDataController.ts @@ -127,8 +127,19 @@ export class ImportDataController extends Controller { let dateRetire = new Date(item["BORN"]); profile.citizenId = item["ID"] == "" ? "" : item["ID"]; - profile.rank = item["RANK_NAME"] == "" ? null : item["RANK_NAME"]; + profile.rank = + item["RANK_NAME"] == "" || + item["RANK_NAME"] == "นาย" || + item["RANK_NAME"] == "นาง" || + item["RANK_NAME"] == "นางสาว" + ? null + : item["RANK_NAME"]; profile.prefix = item["RANK_NAME"] == "" ? null : item["RANK_NAME"]; + profile.prefixMain = + item["RANK_NAME"] == "" || + (item["RANK_NAME"] != "นาย" && item["RANK_NAME"] != "นาง" && item["RANK_NAME"] != "นางสาว") + ? null + : item["RANK_NAME"]; profile.firstName = item["FNAME"] == "" ? null : item["FNAME"]; profile.lastName = item["LNAME"] == "" ? null : item["LNAME"]; profile.gender = item["SEX"] == "1" ? "ชาย" : item["SEX"] == "2" ? "หญิง" : null_; @@ -209,8 +220,19 @@ export class ImportDataController extends Controller { profileEmp.citizenId = item["ID"] == "" ? "" : item["ID"]; profileEmp.employeeClass = item["FLAG_PERSON_TYPE"] == "6" ? "PERM" : item["FLAG_PERSON_TYPE"] == "7" ? "TEMP" : ""; - profileEmp.rank = item["RANK_NAME"] == "" ? null : item["RANK_NAME"]; + profileEmp.rank = + item["RANK_NAME"] == "" || + item["RANK_NAME"] == "นาย" || + item["RANK_NAME"] == "นาง" || + item["RANK_NAME"] == "นางสาว" + ? null + : item["RANK_NAME"]; profileEmp.prefix = item["RANK_NAME"] == "" ? null : item["RANK_NAME"]; + profileEmp.prefixMain = + item["RANK_NAME"] == "" || + (item["RANK_NAME"] != "นาย" && item["RANK_NAME"] != "นาง" && item["RANK_NAME"] != "นางสาว") + ? null + : item["RANK_NAME"]; profileEmp.firstName = item["FNAME"] == "" ? null : item["FNAME"]; profileEmp.lastName = item["LNAME"] == "" ? null : item["LNAME"]; profileEmp.gender = item["SEX"] == "1" ? "ชาย" : item["SEX"] == "2" ? "หญิง" : null_; diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 0752fb01..2345c3ba 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -70,8 +70,6 @@ import { ProfileFamilyCoupleHistory } from "../entities/ProfileFamilyCoupleHisto import { viewDirector } from "../entities/view/viewDirector"; import { viewDirectorActing } from "../entities/view/viewDirectorActing"; import CallAPI from "../interfaces/call-api"; -import { log } from "console"; -import { Subject } from "typeorm/persistence/Subject.js"; @Route("api/v1/org/profile") @Tags("Profile") @Security("bearerAuth") @@ -1734,6 +1732,33 @@ export class ProfileController extends Controller { 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; + } + + if (citizenIdDigits[12] !== chkDigit) { + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + } if (body.posTypeId && !(await this.posTypeRepo.findOneBy({ id: body.posTypeId }))) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } @@ -1781,6 +1806,8 @@ export class ProfileController extends Controller { // } const profile = Object.assign(new Profile(), body); + profile.prefixMain = profile.prefix; + profile.prefix = profile.rank ?? profile.prefixMain; profile.isProbation = false; profile.isLeave = false; profile.createdUserId = request.user.sub; @@ -1823,8 +1850,37 @@ export class ProfileController extends Controller { 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; + } + + if (citizenIdDigits[12] !== chkDigit) { + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + } const profile: Profile = Object.assign(new Profile(), body); const _null: any = null; + profile.prefixMain = profile.prefix; + profile.prefix = profile.rank ?? profile.prefixMain; profile.dateRetire = body.birthDate == null ? _null : calculateRetireDate(body.birthDate); profile.dateRetireLaw = body.birthDate == null ? _null : calculateRetireLaw(body.birthDate); profile.createdUserId = request.user.sub; @@ -1864,7 +1920,36 @@ export class ProfileController extends Controller { 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; + } + + if (citizenIdDigits[12] !== chkDigit) { + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + } const profile: Profile = Object.assign(new Profile(), body); + profile.prefixMain = profile.prefix; + profile.prefix = profile.rank ?? profile.prefixMain; profile.createdUserId = request.user.sub; profile.createdFullName = request.user.name; profile.lastUpdateUserId = request.user.sub; @@ -1880,15 +1965,19 @@ export class ProfileController extends Controller { @Body() body: { profileId: string; - email: string; - subject: string; - } - ) { - const jwt = require('jsonwebtoken'); - const token = jwt.sign({email_id: body.email, profileId: body.profileId}, process.env.AUTH_ACCOUNT_SECRET, {expiresIn: '15m'}); - // console.log("[token]",token); - const link = process.env.URL + "/verifyemail?upn=" + token; - // console.log("[link]",link); + email: string; + subject: string; + }, + ) { + const jwt = require("jsonwebtoken"); + const token = jwt.sign( + { email_id: body.email, profileId: body.profileId }, + process.env.AUTH_ACCOUNT_SECRET, + { expiresIn: "15m" }, + ); + // console.log("[token]",token); + const link = process.env.URL + "/verifyemail?upn=" + token; + // console.log("[link]",link); await new CallAPI() .PostData(req, "/placement/noti/send-mail", { @@ -3172,6 +3261,8 @@ export class ProfileController extends Controller { }); Object.assign(record, body); + record.prefixMain = record.prefix; + record.prefix = record.rank ?? record.prefixMain; record.createdUserId = request.user.sub; record.createdFullName = request.user.name; record.createdAt = new Date(); @@ -3418,13 +3509,10 @@ export class ProfileController extends Controller { } else if (chkDigit === 11) { chkDigit = chkDigit % 10; } - // else if(chkDigit === 11){ - // chkDigit = cal % 10; - // } - // if (citizenIdDigits[12] !== chkDigit) { - // throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); - // } + if (citizenIdDigits[12] !== chkDigit) { + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } } const record = await this.profileRepo.findOneBy({ id }); const before = structuredClone(record); @@ -3432,6 +3520,8 @@ export class ProfileController extends Controller { if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); Object.assign(record, body); + record.prefixMain = record.prefix; + record.prefix = record.rank ?? record.prefixMain; record.createdUserId = request.user.sub; record.createdFullName = request.user.name; record.createdAt = new Date(); @@ -7779,6 +7869,8 @@ export class ProfileController extends Controller { profile.registrationSubDistrictId = _null; } + profile.prefixMain = profile.prefix; + profile.prefix = profile.rank ?? profile.prefixMain; profile.createdUserId = request.user.sub; profile.createdFullName = request.user.name; profile.lastUpdateUserId = request.user.sub; diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 001ae53a..9611c8c9 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -794,6 +794,33 @@ export class ProfileEmployeeController extends Controller { 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; + } + + if (citizenIdDigits[12] !== chkDigit) { + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + } if (body.employeeClass == null || body.employeeClass == undefined || body.employeeClass == "") { body.employeeClass = "PERM"; } @@ -802,6 +829,8 @@ export class ProfileEmployeeController extends Controller { } const profile = Object.assign(new ProfileEmployee(), body); + profile.prefixMain = profile.prefix; + profile.prefix = profile.rank ?? profile.prefixMain; profile.createdUserId = request.user.sub; profile.createdFullName = request.user.name; profile.lastUpdateUserId = request.user.sub; @@ -830,7 +859,10 @@ export class ProfileEmployeeController extends Controller { @Path() id: string, @Body() body: UpdateProfileEmployee, ) { - await new permission().PermissionOrgUserUpdate(request, "SYS_REGISTRY_EMP", id); + const _record = await this.profileRepo.findOneBy({ id: id }); + if (_record) { + await new permission().PermissionOrgUserUpdate(request, "SYS_REGISTRY_EMP", _record.id); + } const exists = !!body.citizenId && (await this.profileRepo.findOne({ @@ -856,6 +888,33 @@ export class ProfileEmployeeController extends Controller { 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; + } + + if (citizenIdDigits[12] !== chkDigit) { + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + } const record = await this.profileRepo.findOneBy({ id }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); @@ -867,6 +926,8 @@ export class ProfileEmployeeController extends Controller { } Object.assign(record, body); + record.prefixMain = record.prefix; + record.prefix = record.rank ?? record.prefixMain; record.createdUserId = request.user.sub; record.createdFullName = request.user.name; record.createdAt = new Date(); diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index bff07fcd..06609028 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -1,4 +1,13 @@ -import { Entity, Column, OneToMany, JoinColumn, ManyToOne, Double, ManyToMany, JoinTable } from "typeorm"; +import { + Entity, + Column, + OneToMany, + JoinColumn, + ManyToOne, + Double, + ManyToMany, + JoinTable, +} from "typeorm"; import { EntityBase } from "./base/Base"; import { PosMaster } from "./PosMaster"; import { PosLevel } from "./PosLevel"; @@ -69,6 +78,14 @@ export class Profile extends EntityBase { }) prefix: string; + @Column({ + nullable: true, + comment: "คำนำหน้าชื่อตัวหลัก", + length: 40, + default: null, + }) + prefixMain: string; + @Column({ nullable: true, comment: "ชื่อ", diff --git a/src/entities/ProfileEmployee.ts b/src/entities/ProfileEmployee.ts index 31bad68c..c492a457 100644 --- a/src/entities/ProfileEmployee.ts +++ b/src/entities/ProfileEmployee.ts @@ -114,6 +114,14 @@ export class ProfileEmployee extends EntityBase { }) prefix: string; + @Column({ + nullable: true, + comment: "คำนำหน้าชื่อตัวหลัก", + length: 40, + default: null, + }) + prefixMain: string; + @Column({ nullable: true, comment: "ชื่อ", diff --git a/src/migration/1732252198299-updata_profile_add_prefixmain.ts b/src/migration/1732252198299-updata_profile_add_prefixmain.ts new file mode 100644 index 00000000..5f831bbf --- /dev/null +++ b/src/migration/1732252198299-updata_profile_add_prefixmain.ts @@ -0,0 +1,20 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdataProfileAddPrefixmain1732252198299 implements MigrationInterface { + name = 'UpdataProfileAddPrefixmain1732252198299' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileEmployee\` ADD \`prefixMain\` varchar(40) NULL COMMENT 'คำนำหน้าชื่อตัวหลัก'`); + await queryRunner.query(`ALTER TABLE \`profileEmployeeHistory\` ADD \`prefixMain\` varchar(40) NULL COMMENT 'คำนำหน้าชื่อตัวหลัก'`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`prefixMain\` varchar(40) NULL COMMENT 'คำนำหน้าชื่อตัวหลัก'`); + await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`prefixMain\` varchar(40) NULL COMMENT 'คำนำหน้าชื่อตัวหลัก'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`prefixMain\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`prefixMain\``); + await queryRunner.query(`ALTER TABLE \`profileEmployeeHistory\` DROP COLUMN \`prefixMain\``); + await queryRunner.query(`ALTER TABLE \`profileEmployee\` DROP COLUMN \`prefixMain\``); + } + +}