From 77e0cee23ea306385dd040fc2c70a910375aed77 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Fri, 17 May 2024 11:27:28 +0700 Subject: [PATCH 1/2] migrate --- ...ble_ProfileSalaryHistory_delete_profileId1.ts | 4 ++-- ...ble_ProfileSalaryHistory_delete_profileId2.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 src/migration/1715919796147-update_table_ProfileSalaryHistory_delete_profileId2.ts diff --git a/src/migration/1715914699824-update_table_ProfileSalaryHistory_delete_profileId1.ts b/src/migration/1715914699824-update_table_ProfileSalaryHistory_delete_profileId1.ts index 70f5c8fe..c1464a54 100644 --- a/src/migration/1715914699824-update_table_ProfileSalaryHistory_delete_profileId1.ts +++ b/src/migration/1715914699824-update_table_ProfileSalaryHistory_delete_profileId1.ts @@ -6,11 +6,11 @@ export class UpdateTableProfileSalaryHistoryDeleteProfileId11715914699824 implem public async up(queryRunner: QueryRunner): Promise { await queryRunner.query(`ALTER TABLE \`profileEmployee\` ADD \`nationality\` varchar(255) NULL COMMENT 'สัญชาติ'`); await queryRunner.query(`ALTER TABLE \`profileEmployeeHistory\` ADD \`nationality\` varchar(255) NULL COMMENT 'สัญชาติ'`); - await queryRunner.query(`ALTER TABLE \`profileSalary\` ADD CONSTRAINT \`FK_f5758428d496b6d2a051c8af92b\` FOREIGN KEY (\`profileEmployeeId\`) REFERENCES \`profileEmployee\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + // await queryRunner.query(`ALTER TABLE \`profileSalary\` ADD CONSTRAINT \`FK_f5758428d496b6d2a051c8af92b\` FOREIGN KEY (\`profileEmployeeId\`) REFERENCES \`profileEmployee\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); } public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE \`profileSalary\` DROP FOREIGN KEY \`FK_f5758428d496b6d2a051c8af92b\``); + // await queryRunner.query(`ALTER TABLE \`profileSalary\` DROP FOREIGN KEY \`FK_f5758428d496b6d2a051c8af92b\``); await queryRunner.query(`ALTER TABLE \`profileEmployeeHistory\` DROP COLUMN \`nationality\``); await queryRunner.query(`ALTER TABLE \`profileEmployee\` DROP COLUMN \`nationality\``); } diff --git a/src/migration/1715919796147-update_table_ProfileSalaryHistory_delete_profileId2.ts b/src/migration/1715919796147-update_table_ProfileSalaryHistory_delete_profileId2.ts new file mode 100644 index 00000000..ae204e7c --- /dev/null +++ b/src/migration/1715919796147-update_table_ProfileSalaryHistory_delete_profileId2.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableProfileSalaryHistoryDeleteProfileId21715919796147 implements MigrationInterface { + name = 'UpdateTableProfileSalaryHistoryDeleteProfileId21715919796147' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileSalary\` ADD \`profileEmployeeId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง ProfileEmployee'`); + await queryRunner.query(`ALTER TABLE \`profileSalary\` ADD CONSTRAINT \`FK_f5758428d496b6d2a051c8af92b\` FOREIGN KEY (\`profileEmployeeId\`) REFERENCES \`profileEmployee\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileSalary\` DROP FOREIGN KEY \`FK_f5758428d496b6d2a051c8af92b\``); + await queryRunner.query(`ALTER TABLE \`profileSalary\` DROP COLUMN \`profileEmployeeId\``); + } + +} From a52920769279cb8c31be29dba392db182e8f7890 Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 17 May 2024 12:54:18 +0700 Subject: [PATCH 2/2] =?UTF-8?q?=E0=B9=80=E0=B8=8A=E0=B9=87=E0=B8=84?= =?UTF-8?q?=E0=B9=80=E0=B8=A5=E0=B8=82=E0=B8=9A=E0=B8=B1=E0=B8=95=E0=B8=A3?= =?UTF-8?q?=20=E0=B8=9B=E0=B8=8A=E0=B8=8A.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileChildrenController.ts | 4 ++- .../ProfileChildrenEmployeeController.ts | 4 ++- .../ProfileFamilyCoupleController.ts | 4 ++- .../ProfileFamilyCoupleEmployeeController.ts | 4 ++- .../ProfileFamilyFatherController.ts | 5 +-- .../ProfileFamilyFatherEmployeeController.ts | 5 +-- .../ProfileFamilyMotherController.ts | 5 +-- .../ProfileFamilyMotherEmployeeController.ts | 5 +-- src/interfaces/extension.ts | 36 +++++++++++++++++++ 9 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/controllers/ProfileChildrenController.ts b/src/controllers/ProfileChildrenController.ts index eacb151d..36ff9839 100644 --- a/src/controllers/ProfileChildrenController.ts +++ b/src/controllers/ProfileChildrenController.ts @@ -24,7 +24,7 @@ import { ProfileChildren, UpdateProfileChildren, } from "../entities/ProfileChildren"; - +import Extension from "../interfaces/extension"; @Route("api/v1/org/profile/family/children") @Tags("ProfileChildren") @Security("bearerAuth") @@ -67,6 +67,7 @@ export class ProfileChildrenController extends Controller { }; Object.assign(data, { ...body, ...meta }); + data.childrenCitizenId = Extension.CheckCitizen(String(data.childrenCitizenId)); await this.childrenRepository.save(data); if(data){ const history: ProfileChildrenHistory = Object.assign(new ProfileChildrenHistory(), { @@ -101,6 +102,7 @@ export class ProfileChildrenController extends Controller { Object.assign(record, body); record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + record.childrenCitizenId = Extension.CheckCitizen(String(record.childrenCitizenId)); history.profileChildrenId = record.id; history.childrenCareer = record.childrenCareer; history.childrenFirstName = record.childrenFirstName; diff --git a/src/controllers/ProfileChildrenEmployeeController.ts b/src/controllers/ProfileChildrenEmployeeController.ts index 64c28c61..9fe7945f 100644 --- a/src/controllers/ProfileChildrenEmployeeController.ts +++ b/src/controllers/ProfileChildrenEmployeeController.ts @@ -26,7 +26,7 @@ import { UpdateProfileChildren, } from "../entities/ProfileChildren"; import { ProfileEmployee } from "../entities/ProfileEmployee"; - +import Extension from "../interfaces/extension"; @Route("api/v1/org/profile-employee/family/children") @Tags("ProfileChildren") @Security("bearerAuth") @@ -73,6 +73,7 @@ export class ProfileChildrenEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); + data.childrenCitizenId = Extension.CheckCitizen(String(data.childrenCitizenId)); await this.childrenRepository.save(data); if(data){ const history: ProfileChildrenHistory = Object.assign(new ProfileChildrenHistory(), { @@ -108,6 +109,7 @@ export class ProfileChildrenEmployeeController extends Controller { Object.assign(record, body); record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + record.childrenCitizenId = Extension.CheckCitizen(String(record.childrenCitizenId)); history.profileChildrenId = record.id; history.childrenCareer = record.childrenCareer; history.childrenFirstName = record.childrenFirstName; diff --git a/src/controllers/ProfileFamilyCoupleController.ts b/src/controllers/ProfileFamilyCoupleController.ts index 265ead62..8e23a6a3 100644 --- a/src/controllers/ProfileFamilyCoupleController.ts +++ b/src/controllers/ProfileFamilyCoupleController.ts @@ -20,7 +20,7 @@ import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import { ProfileFamilyCouple, CreateProfileFamilyCouple, UpdateProfileFamilyCouple } from "../entities/ProfileFamilyCouple"; import { ProfileFamilyCoupleHistory } from "../entities/ProfileFamilyCoupleHistory"; - +import Extension from "../interfaces/extension"; @Route("api/v1/org/profile/family/couple") @Tags("ProfileFamilyCouple") @Security("bearerAuth") @@ -142,6 +142,7 @@ export class ProfileFamilyCoupleController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } + familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId)); familyCouple.createdUserId = req.user.sub; familyCouple.createdFullName = req.user.name; familyCouple.lastUpdateUserId = req.user.sub; @@ -187,6 +188,7 @@ export class ProfileFamilyCoupleController extends Controller { Object.assign(familyCouple, body); familyCouple.lastUpdateUserId = req.user.sub, familyCouple.lastUpdateFullName = req.user.name; + familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId)); history.profileFamilyCoupleId = familyCouple.id; history.couplePrefix = familyCouple.couplePrefix, history.coupleFirstName = familyCouple.coupleFirstName, diff --git a/src/controllers/ProfileFamilyCoupleEmployeeController.ts b/src/controllers/ProfileFamilyCoupleEmployeeController.ts index b6c3c1e7..f37b93c5 100644 --- a/src/controllers/ProfileFamilyCoupleEmployeeController.ts +++ b/src/controllers/ProfileFamilyCoupleEmployeeController.ts @@ -20,7 +20,7 @@ import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { ProfileFamilyCouple, CreateProfileEmployeeFamilyCouple, UpdateProfileFamilyCouple } from "../entities/ProfileFamilyCouple"; import { ProfileFamilyCoupleHistory } from "../entities/ProfileFamilyCoupleHistory"; - +import Extension from "../interfaces/extension"; @Route("api/v1/org/profile-employee/family/couple") @Tags("ProfileEmployeeFamilyCouple") @Security("bearerAuth") @@ -142,6 +142,7 @@ export class ProfileFamilyCoupleEmployeeController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } + familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId)); familyCouple.createdUserId = req.user.sub; familyCouple.createdFullName = req.user.name; familyCouple.lastUpdateUserId = req.user.sub; @@ -187,6 +188,7 @@ export class ProfileFamilyCoupleEmployeeController extends Controller { Object.assign(familyCouple, body); familyCouple.lastUpdateUserId = req.user.sub, familyCouple.lastUpdateFullName = req.user.name; + familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId)); history.profileFamilyCoupleId = familyCouple.id; history.couplePrefix = familyCouple.couplePrefix, history.coupleFirstName = familyCouple.coupleFirstName, diff --git a/src/controllers/ProfileFamilyFatherController.ts b/src/controllers/ProfileFamilyFatherController.ts index 4334df05..fd20707b 100644 --- a/src/controllers/ProfileFamilyFatherController.ts +++ b/src/controllers/ProfileFamilyFatherController.ts @@ -20,7 +20,7 @@ import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import { ProfileFamilyFather, CreateProfileFamilyFather, UpdateProfileFamilyFather } from "../entities/ProfileFamilyFather"; import { ProfileFamilyFatherHistory } from "../entities/ProfileFamilyFatherHistory"; - +import Extension from "../interfaces/extension"; @Route("api/v1/org/profile/family/father") @Tags("ProfileFamilyFather") @Security("bearerAuth") @@ -136,6 +136,7 @@ export class ProfileFamilyFatherController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } + familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); familyFather.createdUserId = req.user.sub; familyFather.createdFullName = req.user.name; familyFather.lastUpdateUserId = req.user.sub; @@ -175,7 +176,7 @@ export class ProfileFamilyFatherController extends Controller { Object.assign(familyFather, body); familyFather.lastUpdateUserId = req.user.sub, familyFather.lastUpdateFullName = req.user.name; - + familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); history.profileFamilyFatherId = familyFather.id; //profileFamilyFatherId history.fatherPrefix = familyFather.fatherPrefix, history.fatherFirstName = familyFather.fatherFirstName, diff --git a/src/controllers/ProfileFamilyFatherEmployeeController.ts b/src/controllers/ProfileFamilyFatherEmployeeController.ts index 0ab6e963..67fbae04 100644 --- a/src/controllers/ProfileFamilyFatherEmployeeController.ts +++ b/src/controllers/ProfileFamilyFatherEmployeeController.ts @@ -20,7 +20,7 @@ import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { ProfileFamilyFather, CreateProfileEmployeeFamilyFather, UpdateProfileFamilyFather } from "../entities/ProfileFamilyFather"; import { ProfileFamilyFatherHistory } from "../entities/ProfileFamilyFatherHistory"; - +import Extension from "../interfaces/extension"; @Route("api/v1/org/profile-employee/family/father") @Tags("ProfileEmployeeFamilyFather") @Security("bearerAuth") @@ -136,6 +136,7 @@ export class ProfileFamilyFatherEmployeeController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } + familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); familyFather.createdUserId = req.user.sub; familyFather.createdFullName = req.user.name; familyFather.lastUpdateUserId = req.user.sub; @@ -175,7 +176,7 @@ export class ProfileFamilyFatherEmployeeController extends Controller { Object.assign(familyFather, body); familyFather.lastUpdateUserId = req.user.sub, familyFather.lastUpdateFullName = req.user.name; - + familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); history.profileFamilyFatherId = familyFather.id; history.fatherPrefix = familyFather.fatherPrefix, history.fatherFirstName = familyFather.fatherFirstName, diff --git a/src/controllers/ProfileFamilyMotherController.ts b/src/controllers/ProfileFamilyMotherController.ts index 923331d7..122fe977 100644 --- a/src/controllers/ProfileFamilyMotherController.ts +++ b/src/controllers/ProfileFamilyMotherController.ts @@ -20,7 +20,7 @@ import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import { ProfileFamilyMother, CreateProfileFamilyMother, UpdateProfileFamilyMother } from "../entities/ProfileFamilyMother"; import { ProfileFamilyMotherHistory } from "../entities/ProfileFamilyMotherHistory"; - +import Extension from "../interfaces/extension"; @Route("api/v1/org/profile/family/mother") @Tags("ProfileFamilyMother") @Security("bearerAuth") @@ -136,6 +136,7 @@ export class ProfileFamilyMotherController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } + familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId)); familyMother.createdUserId = req.user.sub; familyMother.createdFullName = req.user.name; familyMother.lastUpdateUserId = req.user.sub; @@ -175,7 +176,7 @@ export class ProfileFamilyMotherController extends Controller { Object.assign(familyMother, body); familyMother.lastUpdateUserId = req.user.sub, familyMother.lastUpdateFullName = req.user.name; - + familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId)); history.profileFamilyMotherId = familyMother.id; history.motherPrefix = familyMother.motherPrefix, history.motherFirstName = familyMother.motherFirstName, diff --git a/src/controllers/ProfileFamilyMotherEmployeeController.ts b/src/controllers/ProfileFamilyMotherEmployeeController.ts index 9569937f..21ece9f7 100644 --- a/src/controllers/ProfileFamilyMotherEmployeeController.ts +++ b/src/controllers/ProfileFamilyMotherEmployeeController.ts @@ -20,7 +20,7 @@ import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { ProfileFamilyMother, CreateProfileEmployeeFamilyMother, UpdateProfileFamilyMother } from "../entities/ProfileFamilyMother"; import { ProfileFamilyMotherHistory } from "../entities/ProfileFamilyMotherHistory"; - +import Extension from "../interfaces/extension"; @Route("api/v1/org/profile-employee/family/mother") @Tags("ProfileEmployeeFamilyMother") @Security("bearerAuth") @@ -136,6 +136,7 @@ export class ProfileFamilyMotherEmployeeController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } + familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId)); familyMother.createdUserId = req.user.sub; familyMother.createdFullName = req.user.name; familyMother.lastUpdateUserId = req.user.sub; @@ -175,7 +176,7 @@ export class ProfileFamilyMotherEmployeeController extends Controller { Object.assign(familyMother, body); familyMother.lastUpdateUserId = req.user.sub, familyMother.lastUpdateFullName = req.user.name; - + familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId)); history.profileFamilyMotherId = familyMother.id; history.motherPrefix = familyMother.motherPrefix, history.motherFirstName = familyMother.motherFirstName, diff --git a/src/interfaces/extension.ts b/src/interfaces/extension.ts index 48975b64..012cbb48 100644 --- a/src/interfaces/extension.ts +++ b/src/interfaces/extension.ts @@ -1,3 +1,5 @@ +import HttpStatus from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; class Extension { public static ToThaiMonth(value: number) { switch (value) { @@ -119,6 +121,40 @@ class Extension { } return sum; } + + public static CheckCitizen(value: string) { + let citizen = value + if (citizen.length !== 13) { + throw new HttpError(HttpStatus.NOT_FOUND, "กรุณากรอกข้อมูลรหัสบัตรประจำตัวประชาชนให้ครบ 13 หลัก",); + } + const citizenIdDigits = citizen.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, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + return citizen; + } } export default Extension;