hrms-api-org/src/controllers/ImportDataController.ts

966 lines
41 KiB
TypeScript
Raw Normal View History

2024-06-06 11:15:26 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Query,
UploadedFile,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
2024-07-26 14:44:33 +07:00
import { Brackets, Double } from "typeorm";
2024-06-06 11:15:26 +07:00
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { UseInterceptors } from "@nestjs/common";
2024-06-10 17:11:54 +07:00
import { Profile } from "../entities/Profile";
2024-06-06 11:15:26 +07:00
import { FileInterceptor } from "@nestjs/platform-express";
import * as xlsx from "xlsx";
import { ProfileEducation } from "../entities/ProfileEducation";
2024-06-10 17:11:54 +07:00
import { ProfileSalary } from "../entities/ProfileSalary";
import { ProfileFamilyCouple } from "../entities/ProfileFamilyCouple";
import { ProfileFamilyMother } from "../entities/ProfileFamilyMother";
import { ProfileFamilyFather } from "../entities/ProfileFamilyFather";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { PosLevel } from "../entities/PosLevel";
import { PosType } from "../entities/PosType";
2024-07-25 16:20:48 +07:00
import Extension from "../interfaces/extension";
import {
calculateAge,
calculateRetireDate,
calculateRetireLaw,
calculateRetireYear,
removeProfileInOrganize,
} from "../interfaces/utils";
2024-07-26 14:44:33 +07:00
import * as fs from "fs";
import * as path from "path";
import csv from "csv-parser"; // src/importCsv.ts
const { createConnection } = require("typeorm");
import csvParser from "csv-parser";
import { HR_POSITION_OFFICER } from "../entities/HR_POSITION_OFFICER";
import { HR_PERSONAL_OFFICER_FAMILY } from "../entities/HR_PERSONAL_OFFICER_FAMILY";
const BATCH_SIZE = 1000;
// import { EducationMis } from "../entities/EducationMis";
2024-06-06 11:15:26 +07:00
2024-06-06 11:20:37 +07:00
@Route("api/v1/org/upload")
@Tags("UPLOAD")
2024-06-06 11:15:26 +07:00
@Security("bearerAuth")
export class ImportDataController extends Controller {
private educationRepository = AppDataSource.getRepository(ProfileEducation);
2024-06-10 17:11:54 +07:00
private profileFamilyCoupleRepository = AppDataSource.getRepository(ProfileFamilyCouple);
private profileFamilyMotherRepository = AppDataSource.getRepository(ProfileFamilyMother);
private profileFamilyFatherRepository = AppDataSource.getRepository(ProfileFamilyFather);
private salaryRepository = AppDataSource.getRepository(ProfileSalary);
private profileRepo = AppDataSource.getRepository(Profile);
private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee);
private posLevelRepo = AppDataSource.getRepository(PosLevel);
private posTypeRepo = AppDataSource.getRepository(PosType);
2024-07-26 14:44:33 +07:00
private HR_POSITION_OFFICERRepo = AppDataSource.getRepository(HR_POSITION_OFFICER);
private HR_PERSONAL_OFFICER_FAMILYRepo = AppDataSource.getRepository(HR_PERSONAL_OFFICER_FAMILY);
2024-06-06 11:15:26 +07:00
/**
2024-07-25 16:20:48 +07:00
* @summary
2024-06-06 11:15:26 +07:00
*/
2024-07-25 16:20:48 +07:00
@Post("uploadProfile-Officer")
2024-07-26 14:44:33 +07:00
async UploadFileSqlOfficer(@Request() request: { user: Record<string, any> }) {
let users = [];
let rowCount = 0;
const filePath = path.resolve(__dirname, "OFFICER.csv"); // Corrected file path
const readStream = fs.createReadStream(filePath).pipe(csvParser());
2024-06-10 17:11:54 +07:00
let profiles: any = [];
2024-07-25 16:20:48 +07:00
let null_: any = null;
2024-07-26 14:44:33 +07:00
for await (const item of readStream) {
// readStream.map(async (item: any) => {
rowCount++;
let type_: any = null;
let level_: any = null;
const profile = new Profile();
const existingProfile = await this.profileRepo.findOne({
where: { citizenId: item["ID"] },
});
if (existingProfile) {
continue;
}
if (item["FLAG_RETIRE_STATUS"] != "" && item["FLAG_RETIRE_STATUS"] != null) {
continue;
}
if (item["FLAG_PERSON_TYPE"] != "1") {
continue;
}
if (item["MP_CEE_TYPE"]) {
type_ = await this.posTypeRepo.findOne({
where: { posTypeName: item["MP_CEE_TYPE"] },
2024-06-10 17:11:54 +07:00
});
2024-07-26 14:44:33 +07:00
}
if (item["MP_CEE_POSITION"]) {
if (type_ == null) {
level_ = await this.posLevelRepo.findOne({
where: {
posLevelName: item["MP_CEE_POSITION"],
},
});
} else {
2024-07-25 16:20:48 +07:00
level_ = await this.posLevelRepo.findOne({
2024-07-26 14:44:33 +07:00
where: {
2024-07-25 16:20:48 +07:00
posLevelName: item["MP_CEE_POSITION"],
2024-07-26 14:44:33 +07:00
posTypeId: type_.id,
},
});
2024-07-25 16:20:48 +07:00
}
2024-07-26 14:44:33 +07:00
}
let dateRetire = Extension.ConvertToDateTime(item["MP_FORCE_DATE"]);
profile.citizenId = item["ID"] == "" ? "" : item["ID"];
profile.rank = item["RANK_NAME"] == "" ? null : item["RANK_NAME"];
profile.prefix = 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_;
profile.birthDate = item["BORN"] == "" ? null_ : Extension.ConvertToDateTime(item["BORN"]);
profile.dateAppoint =
item["BEGIN_ENTRY_DATE"] == ""
? null_
: Extension.ConvertToDateTime(item["BEGIN_ENTRY_DATE"]);
profile.dateStart =
item["MP_FORCE_DATE"] == "" ? null_ : Extension.ConvertToDateTime(item["MP_FORCE_DATE"]);
profile.dateRetire = dateRetire == null ? null_ : calculateRetireDate(dateRetire);
profile.dateRetireLaw = dateRetire == null ? null_ : calculateRetireLaw(dateRetire);
profile.position = item["WORK_LINE_NAME"] == "" ? null : item["WORK_LINE_NAME"];
profile.posTypeId =
type_ != null && type_.posTypeName == item["MP_CEE_TYPE"] && type_ ? type_.id : null;
profile.posLevelId =
level_ != null && level_.posLevelName == item["MP_CEE_POSITION"] && level_
? level_.id
: null;
profile.relationship =
item["MARRIAGE_STATE"] == "" ? "" : Extension.CheckRelationship(item["MARRIAGE_STATE"]);
profile.isLeave =
item["FLAG_RETIRE_STATUS"] == "" || item["FLAG_RETIRE_STATUS"] == null ? false : true;
profile.createdUserId = request.user.sub;
profile.createdFullName = request.user.name;
profile.lastUpdateUserId = request.user.sub;
profile.lastUpdateFullName = request.user.name;
profiles.push(profile);
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
2024-07-25 16:20:48 +07:00
2024-07-26 14:44:33 +07:00
if (profiles.length === BATCH_SIZE) {
await this.profileRepo.save(profiles);
profiles = [];
if (global.gc) {
global.gc();
}
}
}
// )
// );
console.log(rowCount);
2024-07-25 16:20:48 +07:00
await this.profileRepo.save(profiles);
2024-07-25 16:27:43 +07:00
return new HttpSuccess();
2024-06-10 17:11:54 +07:00
}
2024-07-25 16:20:48 +07:00
/**
* @summary
*/
@Post("uploadProfile-Employee")
2024-07-26 14:44:33 +07:00
async UploadFileSQL(@Request() request: { user: Record<string, any> }) {
let users = [];
let rowCount = 0;
const filePath = path.resolve(__dirname, "EMP.csv"); // Corrected file path
const readStream = fs.createReadStream(filePath).pipe(csvParser());
2024-06-10 17:11:54 +07:00
let profiles: any = [];
2024-07-26 14:44:33 +07:00
let null_: any = null;
for await (const item of readStream) {
rowCount++;
const profileEmp = new ProfileEmployee();
const existingProfile = await this.profileEmpRepo.findOne({
where: { citizenId: item["ID"] },
});
console.log(item);
console.log(item["ID"]);
// console.log(existingProfile);
if (existingProfile) {
continue;
}
if (item["FLAG_PERSON_TYPE"] != "6") {
continue;
}
let dateRetire = Extension.ConvertToDateTime(item["MP_FORCE_DATE"]);
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.prefix = 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_;
profileEmp.birthDate = item["BORN"] == "" ? null_ : Extension.ConvertToDateTime(item["BORN"]);
profileEmp.dateAppoint =
item["BEGIN_ENTRY_DATE"] == ""
? null_
: Extension.ConvertToDateTime(item["BEGIN_ENTRY_DATE"]);
profileEmp.dateStart =
item["MP_FORCE_DATE"] == "" ? null_ : Extension.ConvertToDateTime(item["MP_FORCE_DATE"]);
profileEmp.dateRetire = dateRetire == null ? null_ : calculateRetireDate(dateRetire);
profileEmp.dateRetireLaw = dateRetire == null ? null_ : calculateRetireLaw(dateRetire);
profileEmp.position = item["WORK_LINE_NAME"] == "" ? null : item["WORK_LINE_NAME"];
profileEmp.salaryLevel = item["SALARY_LEVEL_CODE"] == "" ? null : item["SALARY_LEVEL_CODE"];
profileEmp.relationship =
item["MARRIAGE_STATE"] == "" ? "" : Extension.CheckRelationship(item["MARRIAGE_STATE"]);
profileEmp.createdUserId = request.user.sub;
profileEmp.createdFullName = request.user.name;
profileEmp.lastUpdateUserId = request.user.sub;
profileEmp.lastUpdateFullName = request.user.name;
profiles.push(profileEmp);
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
if (profiles.length === BATCH_SIZE) {
await this.profileEmpRepo.save(profiles);
profiles = [];
if (global.gc) {
global.gc();
2024-06-10 17:11:54 +07:00
}
2024-07-26 14:44:33 +07:00
}
}
console.log(rowCount);
2024-07-25 16:20:48 +07:00
await this.profileEmpRepo.save(profiles);
2024-07-25 16:27:43 +07:00
return new HttpSuccess();
2024-06-10 17:11:54 +07:00
}
2024-07-25 16:20:48 +07:00
/**
* @summary
*/
@Post("uploadProfileSalary-Officer")
2024-07-26 14:44:33 +07:00
async UploadFileSQLSalary(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
2024-07-25 16:20:48 +07:00
let profileSalarys: any = [];
2024-07-26 14:44:33 +07:00
let null_: any = null;
const [_profiles, total] = await AppDataSource.getRepository(Profile)
.createQueryBuilder("profile")
.select(["profile.id"])
.getManyAndCount();
for (var i = 1; i <= total / BATCH_SIZE; i++) {
const profiles = await AppDataSource.getRepository(Profile)
.createQueryBuilder("profile")
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
.skip((i - 1) * BATCH_SIZE)
.take(BATCH_SIZE)
.getMany();
await Promise.all(
profiles.map(async (_item) => {
const existingProfile = await this.HR_POSITION_OFFICERRepo.find({
where: { CIT: _item.citizenId },
select: [
"CIT",
"MP_POS_DATE",
"SALARY",
"MP_COMMAND_NUM",
"POS_NUM_NAME",
"POS_NUM_CODE",
"FLAG_TO_NAME",
"WORK_LINE_NAME",
"SPECIALIST_NAME",
"ADMIN_NAME",
"REMARK",
"ORDER_MOVE_POSITION",
],
});
profileSalarys = await [];
await Promise.all(
existingProfile.map(async (item) => {
rowCount++;
const profileSalary = new ProfileSalary();
profileSalary.date =
item.MP_POS_DATE == "" ? null_ : Extension.ConvertToDateTime(item.MP_POS_DATE);
const SALARY: any =
item.SALARY == null || item.SALARY == "" ? null_ : Number(item.SALARY);
profileSalary.amount = SALARY;
profileSalary.profileId = _item.id;
profileSalary.refCommandNo = item.MP_COMMAND_NUM;
profileSalary.posNo = item.POS_NUM_NAME + item.POS_NUM_CODE;
profileSalary.position = item.FLAG_TO_NAME;
profileSalary.positionLine = item.WORK_LINE_NAME;
profileSalary.positionPathSide = item.SPECIALIST_NAME;
profileSalary.positionExecutive = item.ADMIN_NAME;
profileSalary.templateDoc = item.REMARK;
const ORDER_MOVE_POSITION: any =
item.ORDER_MOVE_POSITION == null || item.ORDER_MOVE_POSITION == ""
? null_
: Number(item.ORDER_MOVE_POSITION);
profileSalary.order = ORDER_MOVE_POSITION;
profileSalary.createdUserId = request.user.sub;
profileSalary.createdFullName = request.user.name;
profileSalary.lastUpdateUserId = request.user.sub;
profileSalary.lastUpdateFullName = request.user.name;
profileSalarys.push(profileSalary);
}),
);
await this.salaryRepository.save(profileSalarys);
profileSalarys = [];
}),
);
}
console.log(rowCount);
return new HttpSuccess(profileSalarys);
2024-06-10 17:11:54 +07:00
}
2024-07-25 16:20:48 +07:00
/**
* @summary
*/
@Post("uploadProfileSalary-Employee")
2024-07-26 14:44:33 +07:00
async UploadFileSQLSalaryEmp(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
2024-07-25 16:20:48 +07:00
let profileSalarys: any = [];
2024-07-26 14:44:33 +07:00
let null_: any = null;
const [_profiles, total] = await AppDataSource.getRepository(ProfileEmployee)
.createQueryBuilder("profile")
.select(["profile.id"])
.getManyAndCount();
for (var i = 1; i <= total / BATCH_SIZE; i++) {
const profiles = await AppDataSource.getRepository(ProfileEmployee)
.createQueryBuilder("profile")
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
.skip((i - 1) * BATCH_SIZE)
.take(BATCH_SIZE)
.getMany();
await Promise.all(
profiles.map(async (_item) => {
const existingProfile = await this.HR_POSITION_OFFICERRepo.find({
where: { CIT: _item.citizenId },
select: [
"CIT",
"MP_POS_DATE",
"SALARY",
"MP_COMMAND_NUM",
"POS_NUM_NAME",
"POS_NUM_CODE",
"FLAG_TO_NAME",
"WORK_LINE_NAME",
"SPECIALIST_NAME",
"ADMIN_NAME",
"REMARK",
"ORDER_MOVE_POSITION",
],
});
profileSalarys = await [];
await Promise.all(
existingProfile.map(async (item) => {
rowCount++;
const profileSalary = new ProfileSalary();
profileSalary.date =
item.MP_POS_DATE == "" ? null_ : Extension.ConvertToDateTime(item.MP_POS_DATE);
const SALARY: any =
item.SALARY == null || item.SALARY == "" ? null_ : Number(item.SALARY);
profileSalary.amount = SALARY;
profileSalary.profileEmployeeId = _item.id;
profileSalary.refCommandNo = item.MP_COMMAND_NUM;
profileSalary.posNo = item.POS_NUM_NAME + item.POS_NUM_CODE;
profileSalary.position = item.FLAG_TO_NAME;
profileSalary.positionLine = item.WORK_LINE_NAME;
profileSalary.positionPathSide = item.SPECIALIST_NAME;
profileSalary.positionExecutive = item.ADMIN_NAME;
profileSalary.templateDoc = item.REMARK;
const ORDER_MOVE_POSITION: any =
item.ORDER_MOVE_POSITION == null || item.ORDER_MOVE_POSITION == ""
? null_
: Number(item.ORDER_MOVE_POSITION);
profileSalary.order = ORDER_MOVE_POSITION;
profileSalary.createdUserId = request.user.sub;
profileSalary.createdFullName = request.user.name;
profileSalary.lastUpdateUserId = request.user.sub;
profileSalary.lastUpdateFullName = request.user.name;
profileSalarys.push(profileSalary);
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
}),
);
await this.salaryRepository.save(profileSalarys);
profileSalarys = [];
}),
);
}
console.log(rowCount);
2024-07-25 16:20:48 +07:00
await this.salaryRepository.save(profileSalarys);
2024-07-25 16:27:43 +07:00
return new HttpSuccess();
2024-06-10 17:11:54 +07:00
}
2024-07-25 16:20:48 +07:00
/**
* @summary
*/
@Post("uploadProfileFamily-Officer")
2024-07-26 14:44:33 +07:00
async UploadFileSQLFamily(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
2024-07-25 16:20:48 +07:00
let fathers: any = [];
let mothers: any = [];
let couples: any = [];
2024-07-26 14:44:33 +07:00
const [_profiles, total] = await AppDataSource.getRepository(Profile)
.createQueryBuilder("profile")
.select(["profile.id"])
.getManyAndCount();
for (var i = 1; i <= total / BATCH_SIZE; i++) {
const profiles = await AppDataSource.getRepository(Profile)
.createQueryBuilder("profile")
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
.skip((i - 1) * BATCH_SIZE)
.take(BATCH_SIZE)
.getMany();
await Promise.all(
profiles.map(async (_item) => {
const existingProfile = await this.HR_PERSONAL_OFFICER_FAMILYRepo.findOne({
where: { CIT: _item.citizenId },
select: [
"CIT",
"FATHER_RANK_NAME",
"FATHER_FNAME",
"FATHER_LNAME",
"MOTHER_RANK_NAME",
"MOTHER_FNAME",
"MOTHER_LNAME",
"SPOUSE_RANK_NAME",
"SPOUSE_FNAME",
"SPOUSE_LNAME",
"SPOUSE_ID",
"MARRIAGE_STATE",
],
});
if (!existingProfile) {
return;
}
// fathers = await [];
// mothers = await [];
// couples = await [];
// await Promise.all(
// existingProfile.map(async (item) => {
rowCount++;
const profileFather = new ProfileFamilyFather();
const profileMother = new ProfileFamilyMother();
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.createdUserId = request.user.sub;
profileFather.createdFullName = request.user.name;
profileFather.lastUpdateUserId = request.user.sub;
profileFather.lastUpdateFullName = request.user.name;
profileMother.profileId = _item.id;
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;
profileMother.lastUpdateFullName = request.user.name;
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.relationship = existingProfile.MARRIAGE_STATE;
// profileCouple.coupleLive = existingProfile.LIFE_SPOUSE;
profileCouple.createdUserId = request.user.sub;
profileCouple.createdFullName = request.user.name;
profileCouple.lastUpdateUserId = request.user.sub;
profileCouple.lastUpdateFullName = request.user.name;
fathers.push(profileFather);
mothers.push(profileMother);
couples.push(profileCouple);
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
if (fathers.length === BATCH_SIZE) {
await this.profileFamilyFatherRepository.save(fathers);
fathers = await [];
// if (global.gc) {
// global.gc();
// }
}
if (mothers.length === BATCH_SIZE) {
await this.profileFamilyMotherRepository.save(mothers);
mothers = await [];
// if (global.gc) {
// global.gc();
// }
}
if (couples.length === BATCH_SIZE) {
await this.profileFamilyCoupleRepository.save(couples);
couples = await [];
// if (global.gc) {
// global.gc();
// }
}
// }),
// );
// await this.salaryRepository.save(profileSalarys);
// profileSalarys = [];
}),
);
}
// }
console.log(rowCount);
2024-07-25 16:20:48 +07:00
2024-07-25 16:27:43 +07:00
await Promise.all([
2024-07-26 14:44:33 +07:00
this.profileFamilyFatherRepository.save(fathers),
this.profileFamilyMotherRepository.save(mothers),
this.profileFamilyCoupleRepository.save(couples),
2024-07-25 16:27:43 +07:00
]);
2024-07-25 16:20:48 +07:00
2024-07-25 16:27:43 +07:00
return new HttpSuccess();
2024-06-10 17:11:54 +07:00
}
2024-07-25 16:20:48 +07:00
/**
* @summary
*/
@Post("uploadProfileFamily-Employee")
2024-07-26 14:44:33 +07:00
async UploadFileSQLFamilyEmp(@Request() request: { user: Record<string, any> }) {
let rowCount = 0;
2024-07-25 16:20:48 +07:00
let fathers: any = [];
let mothers: any = [];
let couples: any = [];
2024-06-10 17:11:54 +07:00
2024-07-26 14:44:33 +07:00
const [_profiles, total] = await AppDataSource.getRepository(ProfileEmployee)
.createQueryBuilder("profile")
.select(["profile.id"])
.getManyAndCount();
for (var i = 1; i <= total / BATCH_SIZE; i++) {
const profiles = await AppDataSource.getRepository(ProfileEmployee)
.createQueryBuilder("profile")
.select(["profile.citizenId", "profile.id"])
.orderBy("profile.citizenId", "ASC")
.skip((i - 1) * BATCH_SIZE)
.take(BATCH_SIZE)
.getMany();
await Promise.all(
profiles.map(async (_item) => {
const existingProfile = await this.HR_PERSONAL_OFFICER_FAMILYRepo.findOne({
where: { CIT: _item.citizenId },
select: [
"CIT",
"FATHER_RANK_NAME",
"FATHER_FNAME",
"FATHER_LNAME",
"MOTHER_RANK_NAME",
"MOTHER_FNAME",
"MOTHER_LNAME",
"SPOUSE_RANK_NAME",
"SPOUSE_FNAME",
"SPOUSE_LNAME",
"SPOUSE_ID",
"MARRIAGE_STATE",
],
});
if (!existingProfile) {
return;
}
rowCount++;
const profileFather = new ProfileFamilyFather();
const profileMother = new ProfileFamilyMother();
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.createdUserId = request.user.sub;
profileFather.createdFullName = request.user.name;
profileFather.lastUpdateUserId = request.user.sub;
profileFather.lastUpdateFullName = request.user.name;
profileMother.profileEmployeeId = _item.id;
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;
profileMother.lastUpdateFullName = request.user.name;
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.relationship = existingProfile.MARRIAGE_STATE;
// profileCouple.coupleLive = existingProfile.LIFE_SPOUSE;
profileCouple.createdUserId = request.user.sub;
profileCouple.createdFullName = request.user.name;
profileCouple.lastUpdateUserId = request.user.sub;
profileCouple.lastUpdateFullName = request.user.name;
fathers.push(profileFather);
mothers.push(profileMother);
couples.push(profileCouple);
console.log(">>>>>>>>>>>>>>>>>>>" + rowCount);
if (fathers.length === BATCH_SIZE) {
await this.profileFamilyFatherRepository.save(fathers);
fathers = await [];
}
if (mothers.length === BATCH_SIZE) {
await this.profileFamilyMotherRepository.save(mothers);
mothers = await [];
}
if (couples.length === BATCH_SIZE) {
await this.profileFamilyCoupleRepository.save(couples);
couples = await [];
}
}),
);
}
console.log(rowCount);
2024-07-25 16:20:48 +07:00
2024-07-25 16:27:43 +07:00
await Promise.all([
2024-07-26 14:44:33 +07:00
this.profileFamilyFatherRepository.save(fathers),
this.profileFamilyMotherRepository.save(mothers),
this.profileFamilyCoupleRepository.save(couples),
2024-07-25 16:27:43 +07:00
]);
2024-07-25 16:20:48 +07:00
2024-07-25 16:27:43 +07:00
return new HttpSuccess();
}
2024-07-25 16:20:48 +07:00
2024-07-25 16:27:43 +07:00
// /**
// * @summary Import Education Code
// */
// @Post("uploadEducation-Code")
// @UseInterceptors(FileInterceptor("file"))
// async UploadFileSQLEducationCode(
// @UploadedFile() file: Express.Multer.File,
// @Request() request: { user: Record<string, any> },
// ) {
// const workbook = xlsx.read(file.buffer, { type: "buffer" });
// const sheetName = workbook.SheetNames[0]
// const sheet = workbook.Sheets[sheetName];
// const getExcel = xlsx.utils.sheet_to_json(sheet);
// let educationMis_: any = [];
2024-07-26 14:44:33 +07:00
2024-07-25 16:27:43 +07:00
// await Promise.all(
// getExcel.map(async (item: any) => {
// const educationMis = new EducationMis();
// educationMis.EDUCATION_CODE = item.EDUCATION_CODE;
// educationMis.EDUCATION_CODE = item.EDUCATION_CODE;
// educationMis_.push(educationMis);
2024-07-25 16:20:48 +07:00
2024-07-25 16:27:43 +07:00
// }),
// );
// await this.educationMisRepo.save(educationMis_);
// return new HttpSuccess(educationMis_);
// }
2024-07-25 16:20:48 +07:00
// /**
// * API upload EDU
// *
// * @summary DEV_0 - upload EDU #
// *
// */
// @Post("")
// @UseInterceptors(FileInterceptor("file"))
// async UploadUserDevelopemtById(@UploadedFile() file: Express.Multer.File) {
// const workbook = xlsx.read(file.buffer, { type: "buffer" });
// const sheetName = workbook.SheetNames[1];
// const sheet = workbook.Sheets[sheetName];
// const getEducations = xlsx.utils.sheet_to_json(sheet);
// await Promise.all(
// getEducations.map(async (item: any) => {
// if (item["id"] === undefined) {
// return;
// }
// const education = new ProfileEducation();
// education.id = item["id"];
// education.createdAt = item["createdAt"];
// education.createdUserId = item["createdUserId"];
// education.lastUpdatedAt = item["lastUpdatedAt"];
// education.lastUpdateUserId = item["lastUpdateUserId"];
// education.createdFullName = item["createdFullName"];
// education.lastUpdateFullName = item["lastUpdateFullName"];
// education.profileId = item["profileId"];
// education.country = item["country"];
// education.degree = item["degree"];
// education.duration = item["duration"];
// education.durationYear = item["durationYear"];
// education.field = item["field"];
// education.finishDate = item["finishDate"];
// education.fundName = item["fundName"];
// education.institute = item["institute"];
// education.other = item["other"];
// education.startDate = item["startDate"];
// education.endDate = item["endDate"];
// education.educationLevel = item["educationLevel"];
// education.educationLevelId = item["educationLevelId"];
// education.positionPath = item["positionPath"];
// education.positionPathId = item["positionPathId"];
// education.isDate = item["isDate"];
// education.isEducation = item["isEducation"];
// education.note = item["note"];
// education.profileEmployeeId = item["profileEmployeeId"];
// await this.educationRepository.save(education);
// }),
// );
// }
// @Post("uploadSQLEdu")
// @UseInterceptors(FileInterceptor("file"))
// async UploadFileSQLEdu(
// @UploadedFile() file: Express.Multer.File,
// @Request() request: { user: Record<string, any> },
// ) {
// const workbook = xlsx.read(file.buffer, { type: "buffer" });
// const sheetName = workbook.SheetNames[0]; // Assuming we're reading the first sheet
// const sheet = workbook.Sheets[sheetName];
// const getProFile = xlsx.utils.sheet_to_json(sheet);
// let profiles: any = [];
// await Promise.all(
// getProFile.map(async (item: any) => {
// // Create a new Profile entity and assign fields from the parsed data
// const profile = new ProfileEducation();
// // Check if the profile already exists
// const existingProfile = await this.educationRepository.findOne({
// where: { id: item["id"] },
// });
// // If profile exists, skip saving
// if (existingProfile) {
// return;
// }
// // Assign fields from the item to the profile entit
// profile.id = item["id"] == "NULL" ? null : item["id"];
// // profile.createdAt = item["createdAt"] == "NULL" ? null : new Date(item["createdAt"]);
// profile.createdUserId = item["createdUserId"] == "NULL" ? null : item["createdUserId"];
// // profile.lastUpdatedAt =
// // item["lastUpdatedAt"] == "NULL" ? null : new Date(item["lastUpdatedAt"]);
// profile.lastUpdateUserId =
// item["lastUpdateUserId"] == "NULL" ? null : item["lastUpdateUserId"];
// profile.createdFullName =
// item["createdFullName"] == "NULL" ? null : item["createdFullName"];
// profile.lastUpdateFullName =
// item["lastUpdateFullName"] == "NULL" ? null : item["lastUpdateFullName"];
// profile.profileId = item["profileId"] == "NULL" ? null : item["profileId"];
// profile.country = item["country"] == "NULL" ? null : item["country"];
// profile.degree = item["degree"] == "NULL" ? null : item["degree"];
// profile.duration = item["duration"] == "NULL" ? null : item["duration"];
// profile.durationYear = item["durationYear"] == "NULL" ? null : item["durationYear"];
// profile.field = item["field"] == "NULL" ? null : item["field"];
// // profile.finishDate = item["finishDate"] == "NULL" ? null : new Date(item["finishDate"]);
// profile.fundName = item["fundName"] == "NULL" ? null : item["fundName"];
// profile.gpa = item["gpa"] == "NULL" ? null : item["gpa"];
// profile.institute = item["institute"] == "NULL" ? null : item["institute"];
// profile.other = item["other"] == "NULL" ? null : item["other"];
// // profile.startDate = item["startDate"] == "NULL" ? null : new Date(item["startDate"]);
// // profile.endDate = item["endDate"] == "NULL" ? null : new Date(item["endDate"]);
// profile.educationLevel = item["educationLevel"] == "NULL" ? null : item["educationLevel"];
// profile.educationLevelId =
// item["educationLevelId"] == "NULL" ? null : item["educationLevelId"];
// profile.positionPath = item["positionPath"] == "NULL" ? null : item["positionPath"];
// profile.positionPathId = item["positionPathId"] == "NULL" ? null : item["positionPathId"];
// profile.isDate = item["isDate"] == "NULL" ? null : item["isDate"];
// profile.isEducation = item["isEducation"] == "NULL" ? null : item["isEducation"];
// profile.note = item["note"] == "NULL" ? null : item["note"];
// profile.profileEmployeeId =
// item["profileEmployeeId"] == "NULL" ? null : item["profileEmployeeId"];
// profiles.push(profile);
// }),
// );
// await this.educationRepository.save(profiles);
// return new HttpSuccess(getProFile);
// }
// @Post("uploadSQLFamilly-Father")
// @UseInterceptors(FileInterceptor("file"))
// async UploadFileSQLFamilly(
// @UploadedFile() file: Express.Multer.File,
// // @Request() request: { user: Record<string, any> },
// ) {
// const workbook = xlsx.read(file.buffer, { type: "buffer" });
// const sheetName = workbook.SheetNames[0]; // Assuming we're reading the first sheet
// const sheet = workbook.Sheets[sheetName];
// const getProFile = xlsx.utils.sheet_to_json(sheet);
// let profiles: any = [];
// await Promise.all(
// getProFile.map(async (item: any) => {
// // Create a new Profile entity and assign fields from the parsed data
// const profile = new ProfileFamilyFather();
// // Check if the profile already exists
// const existingProfile = await this.profileFamilyFatherRepository.findOne({
// where: { id: item["id"] },
// });
// // If profile exists, skip saving
// if (existingProfile) {
// return;
// }
// // Assign fields from the item to the profile entit
// profile.id = item["id"] == "NULL" ? null : item["id"];
// // profile.createdAt = item["createdAt"] == "NULL" ? null : new Date(item["createdAt"]);
// profile.createdUserId = item["createdUserId"] == "NULL" ? null : item["createdUserId"];
// // profile.lastUpdatedAt =
// // item["lastUpdatedAt"] == "NULL" ? null : new Date(item["lastUpdatedAt"]);
// profile.lastUpdateUserId =
// item["lastUpdateUserId"] == "NULL" ? null : item["lastUpdateUserId"];
// profile.createdFullName =
// item["createdFullName"] == "NULL" ? null : item["createdFullName"];
// profile.lastUpdateFullName =
// item["lastUpdateFullName"] == "NULL" ? null : item["lastUpdateFullName"];
// profile.fatherPrefix = item["fatherPrefix"] == "NULL" ? null : item["fatherPrefix"];
// profile.fatherFirstName =
// item["fatherFirstName"] == "NULL" ? null : item["fatherFirstName"];
// profile.fatherLastName = item["fatherLastName"] == "NULL" ? null : item["fatherLastName"];
// profile.fatherCareer = item["fatherCareer"] == "NULL" ? null : item["fatherCareer"];
// profile.fatherCitizenId =
// item["fatherCitizenId"] == "NULL" ? null : item["fatherCitizenId"];
// profile.fatherLive = item["fatherLive"] == "NULL" ? null : item["fatherLive"];
// profile.profileId = item["profileId"] == "NULL" ? null : item["profileId"];
// profile.profileEmployeeId =
// item["profileEmployeeId"] == "NULL" ? null : item["profileEmployeeId"];
// profiles.push(profile);
// }),
// );
// await this.profileFamilyFatherRepository.save(profiles);
// return new HttpSuccess(getProFile);
// }
// @Post("uploadSQLFamilly-Mother")
// @UseInterceptors(FileInterceptor("file"))
// async UploadFileSQLFamillyMother(
// @UploadedFile() file: Express.Multer.File,
// @Request() request: { user: Record<string, any> },
// ) {
// const workbook = xlsx.read(file.buffer, { type: "buffer" });
// const sheetName = workbook.SheetNames[0]; // Assuming we're reading the first sheet
// const sheet = workbook.Sheets[sheetName];
// const getProFile = xlsx.utils.sheet_to_json(sheet);
// let profiles: any = [];
// await Promise.all(
// getProFile.map(async (item: any) => {
// // Create a new Profile entity and assign fields from the parsed data
// const profile = new ProfileFamilyMother();
// // Check if the profile already exists
// const existingProfile = await this.profileFamilyMotherRepository.findOne({
// where: { id: item["id"] },
// });
// // If profile exists, skip saving
// if (existingProfile) {
// return;
// }
// // Assign fields from the item to the profile entit
// profile.id = item["id"] == "NULL" ? null : item["id"];
// // profile.createdAt = item["createdAt"] == "NULL" ? null : new Date(item["createdAt"]);
// profile.createdUserId = item["createdUserId"] == "NULL" ? null : item["createdUserId"];
// // profile.lastUpdatedAt =
// // item["lastUpdatedAt"] == "NULL" ? null : new Date(item["lastUpdatedAt"]);
// profile.lastUpdateUserId =
// item["lastUpdateUserId"] == "NULL" ? null : item["lastUpdateUserId"];
// profile.createdFullName =
// item["createdFullName"] == "NULL" ? null : item["createdFullName"];
// profile.lastUpdateFullName =
// item["lastUpdateFullName"] == "NULL" ? null : item["lastUpdateFullName"];
// profile.motherPrefix = item["motherPrefix"] == "NULL" ? null : item["motherPrefix"];
// profile.motherFirstName =
// item["motherFirstName"] == "NULL" ? null : item["motherFirstName"];
// profile.motherLastName = item["motherLastName"] == "NULL" ? null : item["motherLastName"];
// profile.motherCareer = item["motherCareer"] == "NULL" ? null : item["motherCareer"];
// profile.motherCitizenId =
// item["motherCitizenId"] == "NULL" ? null : item["motherCitizenId"];
// profile.motherLive = item["motherLive"] == "NULL" ? null : item["motherLive"];
// profile.profileId = item["profileId"] == "NULL" ? null : item["profileId"];
// profile.profileEmployeeId =
// item["profileEmployeeId"] == "NULL" ? null : item["profileEmployeeId"];
// profiles.push(profile);
// }),
// );
// await this.profileFamilyMotherRepository.save(profiles);
// return new HttpSuccess(getProFile);
// }
// @Post("uploadSQLFamilly-Couple")
// @UseInterceptors(FileInterceptor("file"))
// async UploadFileSQLFamillyCouple(
// @UploadedFile() file: Express.Multer.File,
// // @Request() request: { user: Record<string, any> },
// ) {
// const workbook = xlsx.read(file.buffer, { type: "buffer" });
// const sheetName = workbook.SheetNames[0]; // Assuming we're reading the first sheet
// const sheet = workbook.Sheets[sheetName];
// const getProFile = xlsx.utils.sheet_to_json(sheet);
// let profiles: any = [];
// await Promise.all(
// getProFile.map(async (item: any) => {
// // Create a new Profile entity and assign fields from the parsed data
// const profile = new ProfileFamilyCouple();
// // Check if the profile already exists
// const existingProfile = await this.profileFamilyCoupleRepository.findOne({
// where: { id: item["id"] },
// });
// // If profile exists, skip saving
// if (existingProfile) {
// return;
// }
// // Assign fields from the item to the profile entit
// profile.id = item["id"] == "NULL" ? null : item["id"];
// // profile.createdAt = item["createdAt"] == "NULL" ? null : new Date(item["createdAt"]);
// profile.createdUserId = item["createdUserId"] == "NULL" ? null : item["createdUserId"];
// // profile.lastUpdatedAt =
// // item["lastUpdatedAt"] == "NULL" ? null : new Date(item["lastUpdatedAt"]);
// profile.lastUpdateUserId =
// item["lastUpdateUserId"] == "NULL" ? null : item["lastUpdateUserId"];
// profile.createdFullName =
// item["createdFullName"] == "NULL" ? null : item["createdFullName"];
// profile.lastUpdateFullName =
// item["lastUpdateFullName"] == "NULL" ? null : item["lastUpdateFullName"];
// profile.couple = item["couple"] == "NULL" ? null : item["couple"];
// profile.couplePrefix = item["couplePrefix"] == "NULL" ? null : item["couplePrefix"];
// profile.coupleFirstName =
// item["coupleFirstName"] == "NULL" ? null : item["coupleFirstName"];
// profile.coupleLastName = item["coupleLastName"] == "NULL" ? null : item["coupleLastName"];
// profile.coupleLastNameOld =
// item["coupleLastNameOld"] == "NULL" ? null : item["coupleLastNameOld"];
// profile.coupleCareer = item["coupleCareer"] == "NULL" ? null : item["coupleCareer"];
// profile.coupleCitizenId =
// item["coupleCitizenId"] == "NULL" ? null : item["coupleCitizenId"];
// profile.coupleLive = item["coupleLive"] == "NULL" ? null : item["coupleLive"];
// profile.relationship = item["relationship"] == "NULL" ? null : item["relationship"];
// profile.profileId = item["profileId"] == "NULL" ? null : item["profileId"];
// profile.profileEmployeeId =
// item["profileEmployeeId"] == "NULL" ? null : item["profileEmployeeId"];
// profiles.push(profile);
// }),
// );
// await this.profileFamilyCoupleRepository.save(profiles);
// return new HttpSuccess(getProFile);
// }
2024-06-06 11:15:26 +07:00
}