From 3dc653bfb00c447502d08828f1ae9fa16b4aee77 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Mon, 19 May 2025 03:30:56 +0700 Subject: [PATCH] create user emp --- src/controllers/ImportDataController.ts | 228 +++++++++++++----------- src/controllers/UserController.ts | 62 +++++++ src/entities/EMPLOYEE.ts | 7 + 3 files changed, 190 insertions(+), 107 deletions(-) diff --git a/src/controllers/ImportDataController.ts b/src/controllers/ImportDataController.ts index ccaefbf2..84e8b5b3 100644 --- a/src/controllers/ImportDataController.ts +++ b/src/controllers/ImportDataController.ts @@ -153,7 +153,6 @@ export class ImportDataController extends Controller { rowCount++; let type_: any = null; let level_: any = null; - profile = null; profile = new Profile(); const existingProfile = await this.profileRepo.findOne({ @@ -237,18 +236,18 @@ export class ImportDataController extends Controller { } } - let BORN = ""; - if (item.BORN) { - const [datePart] = item.BORN.split(" "); - const [day, month, year] = datePart.split("/"); - BORN = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`; - } - let BEGIN_ENTRY_DATE = ""; - if (item.BEGIN_ENTRY_DATE) { - const [datePart] = item.BEGIN_ENTRY_DATE.split(" "); - const [day, month, year] = datePart.split("/"); - BEGIN_ENTRY_DATE = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`; - } + let BORN = item.BORN; + // if (item.BORN) { + // const [datePart] = item.BORN.split(" "); + // const [day, month, year] = datePart.split("/"); + // BORN = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`; + // } + let BEGIN_ENTRY_DATE = item.BEGIN_ENTRY_DATE; + // if (item.BEGIN_ENTRY_DATE) { + // const [datePart] = item.BEGIN_ENTRY_DATE.split(" "); + // const [day, month, year] = datePart.split("/"); + // BEGIN_ENTRY_DATE = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`; + // } profile.citizenId = item.CIT; profile.rank = @@ -319,13 +318,19 @@ export class ImportDataController extends Controller { }); if (existingProfile) { profile.id = existingProfile.id; - // continue; - } else { continue; + } else { + // continue; } var positionType = ""; var positionLevel = 0; + const workLevel = item.WORK_LEVEL; + const part1 = workLevel.split("/")[0]; // "ส 2" + const value2 = part1.split(" ")[1]; // "2" + if (value2) { + positionLevel = parseInt(value2); + } if (item.CATEGORY_SAL_CODE == "11") { positionType = "บริการพื้นฐาน"; // positionLevel = "ปฏิบัติงาน"; @@ -341,73 +346,73 @@ export class ImportDataController extends Controller { where: { posTypeName: positionType }, }); } - // if (positionLevel) { - // if (type_ == null) { - // level_ = await this.posLevelEmpRepo.findOne({ - // where: { - // posLevelName: positionLevel, - // }, - // }); - // } else { - // level_ = await this.posLevelEmpRepo.findOne({ - // where: { - // posLevelName: positionLevel, - // posTypeId: type_.id, - // }, - // }); - // } - // } + if (positionLevel) { + if (type_ == null) { + level_ = await this.posLevelEmpRepo.findOne({ + where: { + posLevelName: positionLevel, + }, + }); + } else { + level_ = await this.posLevelEmpRepo.findOne({ + where: { + posLevelName: positionLevel, + posTypeId: type_.id, + }, + }); + } + } - // let BORN = ""; - // if (item.BORN) { - // const [datePart] = item.BORN.split(" "); - // const [day, month, year] = datePart.split("/"); - // BORN = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`; - // } - // let BEGIN_ENTRY_DATE = ""; - // if (item.BEGIN_ENTRY_DATE) { - // const [datePart] = item.BEGIN_ENTRY_DATE.split(" "); - // const [day, month, year] = datePart.split("/"); - // BEGIN_ENTRY_DATE = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`; - // } + let BORN = item.BORN; + if (item.BORN) { + // const [datePart] = item.BORN.split(" "); + // const [day, month, year] = datePart.split("/"); + // BORN = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`; + } + let BEGIN_ENTRY_DATE = item.BEGIN_ENTRY_DATE; + if (item.BEGIN_ENTRY_DATE) { + // const [datePart] = item.BEGIN_ENTRY_DATE.split(" "); + // const [day, month, year] = datePart.split("/"); + // BEGIN_ENTRY_DATE = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`; + } - // profile.citizenId = item.CIT; - // 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.employeeClass = "PERM"; - // profile.gender = item.SEX == "1" ? "ชาย" : item.SEX == "2" ? "หญิง" : _null; - // profile.birthDate = BORN == "" ? _null : new Date(BORN); - // profile.dateAppoint = BEGIN_ENTRY_DATE == "" ? _null : new Date(BEGIN_ENTRY_DATE); - // profile.dateStart = BEGIN_ENTRY_DATE == "" ? _null : new Date(BEGIN_ENTRY_DATE); - // profile.dateRetire = BORN == "" ? _null : calculateRetireDate(new Date(BORN)); - // profile.dateRetireLaw = BORN == "" ? _null : calculateRetireLaw(new Date(BORN)); - // profile.position = item.WORK_LINE_NAME == "" ? null : item.WORK_LINE_NAME.split(" ")[0]; + profile.citizenId = item.CIT; + 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.employeeClass = "PERM"; + profile.gender = item.SEX == "1" ? "ชาย" : item.SEX == "2" ? "หญิง" : _null; + profile.birthDate = BORN == "" ? _null : new Date(BORN); + profile.dateAppoint = BEGIN_ENTRY_DATE == "" ? _null : new Date(BEGIN_ENTRY_DATE); + profile.dateStart = BEGIN_ENTRY_DATE == "" ? _null : new Date(BEGIN_ENTRY_DATE); + profile.dateRetire = BORN == "" ? _null : calculateRetireDate(new Date(BORN)); + profile.dateRetireLaw = BORN == "" ? _null : calculateRetireLaw(new Date(BORN)); + profile.position = item.WORK_LINE_NAME == "" ? null : item.WORK_LINE_NAME.split(" ")[0]; profile.posTypeId = type_ != null && type_.posTypeName == positionType && type_ ? type_.id : null; - // profile.posLevelId = - // level_ != null && level_.posLevelName == positionLevel && level_ ? level_.id : null; + profile.posLevelId = + level_ != null && level_.posLevelName == positionLevel && level_ ? level_.id : null; - // profile.amount = item.SALARY == "" ? 0 : Number(item.SALARY); - // profile.salaryLevel = item.SALARY == "" ? null : Number(item.SALARY_LEVEL_CODE); - // profile.createdUserId = request.user.sub; - // profile.createdFullName = request.user.name; - // profile.lastUpdateUserId = request.user.sub; - // profile.lastUpdateFullName = request.user.name; - // profile.createdAt = new Date(); - // profile.lastUpdatedAt = new Date(); + profile.amount = item.SALARY == "" ? 0 : Number(item.SALARY); + profile.salaryLevel = item.SALARY == "" ? null : Number(item.SALARY_LEVEL_CODE); + profile.createdUserId = request.user.sub; + profile.createdFullName = request.user.name; + profile.lastUpdateUserId = request.user.sub; + profile.lastUpdateFullName = request.user.name; + profile.createdAt = new Date(); + profile.lastUpdatedAt = new Date(); console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); @@ -863,6 +868,8 @@ export class ImportDataController extends Controller { const special_amt: any = item.SPECIAL_AMT == null || item.SPECIAL_AMT == "" ? _null : Number(item.SPECIAL_AMT); profileSalary.amountSpecial = special_amt; + profileSalary.posNumCodeSit = item.POS_NUM_CODE_SIT; + profileSalary.posNumCodeSitAbb = item.POS_NUM_CODE_SIT_ABB; profileSalary.createdUserId = request.user.sub; profileSalary.createdFullName = request.user.name; @@ -1300,6 +1307,8 @@ export class ImportDataController extends Controller { const special_amt: any = item.SPECIAL_AMT == null || item.SPECIAL_AMT == "" ? _null : Number(item.SPECIAL_AMT); profileSalary.amountSpecial = special_amt; + profileSalary.posNumCodeSit = item.POS_NUM_CODE_SIT; + profileSalary.posNumCodeSitAbb = item.POS_NUM_CODE_SIT_ABB; profileSalary.createdUserId = request.user.sub; profileSalary.createdFullName = request.user.name; @@ -1374,9 +1383,9 @@ export class ImportDataController extends Controller { const profileCouple = new ProfileFamilyCouple(); profileFather.profileId = _item.id; - profileFather.fatherPrefix = existingProfile.FATHER_RANK_NAME; - profileFather.fatherFirstName = existingProfile.FATHER_FNAME; - profileFather.fatherLastName = existingProfile.FATHER_LNAME; + profileFather.fatherPrefix = existingProfile.FATHER_RANK_NAME ?? ""; + profileFather.fatherFirstName = existingProfile.FATHER_FNAME ?? ""; + profileFather.fatherLastName = existingProfile.FATHER_LNAME ?? ""; profileFather.createdUserId = request.user.sub; profileFather.createdFullName = request.user.name; profileFather.lastUpdateUserId = request.user.sub; @@ -1385,9 +1394,9 @@ export class ImportDataController extends Controller { profileFather.lastUpdatedAt = new Date(); profileMother.profileId = _item.id; - profileMother.motherPrefix = existingProfile.MOTHER_RANK_NAME; - profileMother.motherFirstName = existingProfile.MOTHER_FNAME; - profileMother.motherLastName = existingProfile.MOTHER_LNAME; + profileMother.motherPrefix = existingProfile.MOTHER_RANK_NAME ?? ""; + profileMother.motherFirstName = existingProfile.MOTHER_FNAME ?? ""; + profileMother.motherLastName = existingProfile.MOTHER_LNAME ?? ""; profileMother.createdUserId = request.user.sub; profileMother.createdFullName = request.user.name; profileMother.lastUpdateUserId = request.user.sub; @@ -1396,10 +1405,10 @@ export class ImportDataController extends Controller { profileMother.lastUpdatedAt = new Date(); profileCouple.profileId = _item.id; - profileCouple.couplePrefix = existingProfile.SPOUSE_RANK_NAME; - profileCouple.coupleFirstName = existingProfile.SPOUSE_FNAME; - profileCouple.coupleLastName = existingProfile.SPOUSE_LNAME; - profileCouple.coupleCitizenId = existingProfile.SPOUSE_ID; + profileCouple.couplePrefix = existingProfile.SPOUSE_RANK_NAME ?? ""; + profileCouple.coupleFirstName = existingProfile.SPOUSE_FNAME ?? ""; + profileCouple.coupleLastName = existingProfile.SPOUSE_LNAME ?? ""; + profileCouple.coupleCitizenId = existingProfile.SPOUSE_ID ?? ""; profileCouple.relationship = existingProfile.MARRIAGE_STATE == "1" ? "โสด" @@ -1502,9 +1511,9 @@ export class ImportDataController extends Controller { const profileCouple = new ProfileFamilyCouple(); profileFather.profileEmployeeId = _item.id; - profileFather.fatherPrefix = existingProfile.FATHER_RANK_NAME; - profileFather.fatherFirstName = existingProfile.FATHER_FNAME; - profileFather.fatherLastName = existingProfile.FATHER_LNAME; + profileFather.fatherPrefix = existingProfile.FATHER_RANK_NAME ?? ""; + profileFather.fatherFirstName = existingProfile.FATHER_FNAME ?? ""; + profileFather.fatherLastName = existingProfile.FATHER_LNAME ?? ""; profileFather.createdUserId = request.user.sub; profileFather.createdFullName = request.user.name; profileFather.lastUpdateUserId = request.user.sub; @@ -1513,9 +1522,9 @@ export class ImportDataController extends Controller { profileFather.lastUpdatedAt = new Date(); profileMother.profileEmployeeId = _item.id; - profileMother.motherPrefix = existingProfile.MOTHER_RANK_NAME; - profileMother.motherFirstName = existingProfile.MOTHER_FNAME; - profileMother.motherLastName = existingProfile.MOTHER_LNAME; + profileMother.motherPrefix = existingProfile.MOTHER_RANK_NAME ?? ""; + profileMother.motherFirstName = existingProfile.MOTHER_FNAME ?? ""; + profileMother.motherLastName = existingProfile.MOTHER_LNAME ?? ""; profileMother.createdUserId = request.user.sub; profileMother.createdFullName = request.user.name; profileMother.lastUpdateUserId = request.user.sub; @@ -1524,10 +1533,10 @@ export class ImportDataController extends Controller { profileMother.lastUpdatedAt = new Date(); profileCouple.profileEmployeeId = _item.id; - profileCouple.couplePrefix = existingProfile.SPOUSE_RANK_NAME; - profileCouple.coupleFirstName = existingProfile.SPOUSE_FNAME; - profileCouple.coupleLastName = existingProfile.SPOUSE_LNAME; - profileCouple.coupleCitizenId = existingProfile.SPOUSE_ID; + profileCouple.couplePrefix = existingProfile.SPOUSE_RANK_NAME ?? ""; + profileCouple.coupleFirstName = existingProfile.SPOUSE_FNAME ?? ""; + profileCouple.coupleLastName = existingProfile.SPOUSE_LNAME ?? ""; + profileCouple.coupleCitizenId = existingProfile.SPOUSE_ID ?? ""; profileCouple.relationship = existingProfile.MARRIAGE_STATE == "1" ? "โสด" @@ -2800,7 +2809,7 @@ export class ImportDataController extends Controller { let type_: any = null; let level_: any = null; const orgRevision = await this.orgRevisionRepo.findOne({ - where: { orgRevisionIsCurrent: false, orgRevisionIsDraft: true }, + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, // where: { id: "30e594c9-a65b-485e-a4c5-71aa497e6b8a" }, }); if (orgRevision == null) return new HttpSuccess(); @@ -2897,7 +2906,7 @@ export class ImportDataController extends Controller { posMaster.isStaff = false; posMaster.isDirector = false; posMaster.isSit = false; - posMaster.next_holderId = _item.id; + posMaster.current_holderId = _item.id; posMaster.posMasterNo = item.POS_NUM_CODE == "" || item.POS_NUM_CODE == null ? _null : Number(item.POS_NUM_CODE); posMaster.orgRevisionId = orgRevision.id; @@ -3171,8 +3180,13 @@ export class ImportDataController extends Controller { let position = new EmployeePosition(); var positionType = ""; - var positionLevel = - item.LEVEL_NAME == "" || item.LEVEL_NAME == null ? 0 : Number(item.LEVEL_NAME); + var positionLevel = 0; + const workLevel = item.WORK_LEVEL; + const part1 = workLevel.split("/")[0]; // "ส 2" + const value2 = part1.split(" ")[1]; // "2" + if (value2) { + positionLevel = parseInt(value2); + } if (item.CATEGORY_SAL_CODE == "11") { positionType = "บริการพื้นฐาน"; } else if (item.CATEGORY_SAL_CODE == "12") { @@ -3952,9 +3966,9 @@ export class ImportDataController extends Controller { rowCount++; const changeName = new ProfileChangeName(); changeName.profileId = _item.id; - changeName.prefix = item.NEW_RANK_NAME == "" ? _item.prefix : item.NEW_RANK_NAME; - changeName.firstName = item.NEW_FNAME == "" ? _item.firstName : item.NEW_FNAME; - changeName.lastName = item.NEW_LNAME == "" ? _item.lastName : item.NEW_LNAME; + changeName.prefix = item.NEW_RANK_NAME == "" ? item.LAST_RANK_NAME : item.NEW_RANK_NAME; + changeName.firstName = item.NEW_FNAME == "" ? item.LAST_FNAME : item.NEW_FNAME; + changeName.lastName = item.NEW_LNAME == "" ? item.LAST_LNAME : item.NEW_LNAME; changeName.createdUserId = request.user.sub; changeName.createdFullName = request.user.name; changeName.lastUpdateUserId = request.user.sub; @@ -3998,9 +4012,9 @@ export class ImportDataController extends Controller { rowCount++; const changeName = new ProfileChangeName(); changeName.profileEmployeeId = _item.id; - changeName.prefix = item.NEW_RANK_NAME == "" ? _item.prefix : item.NEW_RANK_NAME; - changeName.firstName = item.NEW_FNAME == "" ? _item.firstName : item.NEW_FNAME; - changeName.lastName = item.NEW_LNAME == "" ? _item.lastName : item.NEW_LNAME; + changeName.prefix = item.NEW_RANK_NAME == "" ? item.LAST_RANK_NAME : item.NEW_RANK_NAME; + changeName.firstName = item.NEW_FNAME == "" ? item.LAST_FNAME : item.NEW_FNAME; + changeName.lastName = item.NEW_LNAME == "" ? item.LAST_LNAME : item.NEW_LNAME; changeName.createdUserId = request.user.sub; changeName.createdFullName = request.user.name; changeName.lastUpdateUserId = request.user.sub; diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index f96630b4..b35966dd 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -892,6 +892,68 @@ export class KeycloakController extends Controller { return ""; } + @Post("user-emp/create") + @Security("bearerAuth", ["system", "admin"]) + async createUserImportEmp( + @Request() request: { user: { sub: string; preferred_username: string } }, + ) { + const profiles = await this.profileEmpRepo.find({ + where: { + keycloak: IsNull(), + }, + relations: ["roleKeycloaks"], + }); + + for await (const _item of profiles) { + let password = _item.citizenId; + if (_item.birthDate != null) { + const gregorianYear = _item.birthDate.getFullYear() + 543; + + const formattedDate = + _item.birthDate.toISOString().slice(8, 10) + + _item.birthDate.toISOString().slice(5, 7) + + gregorianYear; + password = formattedDate; + } + const checkUser = await getUserByUsername(_item.citizenId); + let userId: any = ""; + if (checkUser.length == 0) { + userId = await createUser(_item.citizenId, password, { + firstName: _item.firstName, + lastName: _item.lastName, + // email: _item.email, + }); + if (typeof userId !== "string") { + throw new Error(userId.errorMessage); + } + } else { + userId = checkUser[0].id; + } + + const list = await getRoles(); + if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); + const result = await addUserRoles( + userId, + list.filter((v) => v.id == "8a1a0dc9-304c-4e5b-a90a-65f841048212"), + ); + + if (!result) { + throw new Error("Failed. Cannot set user's role."); + } + if (typeof userId === "string") { + _item.keycloak = userId; + } + const roleKeycloak = await this.roleKeycloakRepo.find({ + where: { id: "8a1a0dc9-304c-4e5b-a90a-65f841048212" }, + }); + if (_item) { + _item.roleKeycloaks = Array.from(new Set([..._item.roleKeycloaks, ...roleKeycloak])); + this.profileEmpRepo.save(_item); + } + } + return ""; + } + @Post("user/change-password-all") async changeUserPasswordAll( @Request() request: { user: { sub: string; preferred_username: string } }, diff --git a/src/entities/EMPLOYEE.ts b/src/entities/EMPLOYEE.ts index a20b6f65..b90c7d1a 100644 --- a/src/entities/EMPLOYEE.ts +++ b/src/entities/EMPLOYEE.ts @@ -186,4 +186,11 @@ export class EMPLOYEE { default: null, }) LEVEL_NAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + WORK_LEVEL: string; }