fix exec command 01, 02

This commit is contained in:
Bright 2024-10-09 15:45:42 +07:00
parent 428ed28b64
commit 8c7a0bb792

View file

@ -2048,12 +2048,6 @@ export class CommandController extends Controller {
) { ) {
await Promise.all( await Promise.all(
body.data.map(async (item) => { body.data.map(async (item) => {
const profile = await this.profileRepository.findOneBy({
citizenId: item.bodyProfile.citizenId,
});
if (profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "พบ profile ซ้ำ");
}
const before = null; const before = null;
const meta = { const meta = {
createdUserId: req.user.sub, createdUserId: req.user.sub,
@ -2063,41 +2057,25 @@ export class CommandController extends Controller {
createdAt: new Date(), createdAt: new Date(),
lastUpdatedAt: new Date(), lastUpdatedAt: new Date(),
}; };
const _null: any = null;
if (item.bodyProfile.posLevelId === "") item.bodyProfile.posLevelId = null; if (item.bodyProfile.posLevelId === "") item.bodyProfile.posLevelId = null;
if (item.bodyProfile.posTypeId === "") item.bodyProfile.posTypeId = null; if (item.bodyProfile.posTypeId === "") item.bodyProfile.posTypeId = null;
if (item.bodyProfile.posLevelId && !(await this.posLevelRepo.findOneBy({ id: item.bodyProfile.posLevelId }))) {
if (
item.bodyProfile.posLevelId &&
!(await this.posLevelRepo.findOneBy({ id: item.bodyProfile.posLevelId }))
) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้");
} }
if (item.bodyProfile.posTypeId && !(await this.posTypeRepo.findOneBy({ id: item.bodyProfile.posTypeId }))) {
if (
item.bodyProfile.posTypeId &&
!(await this.posTypeRepo.findOneBy({ id: item.bodyProfile.posTypeId }))
) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
} }
const profile_: Profile = Object.assign({ ...item.bodyProfile, ...meta }); let profile:any = await this.profileRepository.findOneBy({ citizenId: item.bodyProfile.citizenId });
const _null: any = null; if(!profile) {
profile_.dateRetire = profile = Object.assign({ ...item.bodyProfile, ...meta });
item.bodyProfile.birthDate == null profile.dateRetire = item.bodyProfile.birthDate == null ? _null : calculateRetireDate(item.bodyProfile.birthDate);
? _null profile.dateRetireLaw = item.bodyProfile.birthDate == null ? _null : calculateRetireLaw(item.bodyProfile.birthDate);
: calculateRetireDate(item.bodyProfile.birthDate); const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, {
profile_.dateRetireLaw = firstName: profile.firstName,
item.bodyProfile.birthDate == null lastName: profile.lastName,
? _null email: profile.email,
: calculateRetireLaw(item.bodyProfile.birthDate);
await this.profileRepository.save(profile_);
// setLogDataDiff(req, { before, after: profile_ });
if (profile_ && profile_.id) {
const userKeycloakId = await createUser(profile_.citizenId, profile_.citizenId, {
//User, Password
firstName: profile_.firstName,
lastName: profile_.lastName,
email: profile_.email,
requiredActions: ["UPDATE_PASSWORD"], requiredActions: ["UPDATE_PASSWORD"],
}); });
if (typeof userKeycloakId !== "string") { if (typeof userKeycloakId !== "string") {
@ -2116,7 +2094,12 @@ export class CommandController extends Controller {
})), })),
); );
if (!result) throw new Error("Failed. Cannot set user's role."); if (!result) throw new Error("Failed. Cannot set user's role.");
profile.keycloak = userKeycloakId;
await this.profileRepository.save(profile);
setLogDataDiff(req, { before, after: profile });
}
if(profile && profile.id) {
//Educations //Educations
await Promise.all( await Promise.all(
item.bodyEducations.map(async (education) => { item.bodyEducations.map(async (education) => {
@ -2124,14 +2107,13 @@ export class CommandController extends Controller {
Object.assign(profileEdu, { ...education, ...meta }); Object.assign(profileEdu, { ...education, ...meta });
const eduHistory = new ProfileEducationHistory(); const eduHistory = new ProfileEducationHistory();
Object.assign(eduHistory, { ...profileEdu, id: undefined }); Object.assign(eduHistory, { ...profileEdu, id: undefined });
profileEdu.profileId = profile_.id; profileEdu.profileId = profile.id;
await this.profileEducationRepo.save(profileEdu, { data: req }); await this.profileEducationRepo.save(profileEdu, { data: req });
setLogDataDiff(req, { before, after: profileEdu }); setLogDataDiff(req, { before, after: profileEdu });
eduHistory.profileEducationId = profileEdu.id; eduHistory.profileEducationId = profileEdu.id;
await this.profileEducationHistoryRepo.save(eduHistory, { data: req }); await this.profileEducationHistoryRepo.save(eduHistory, { data: req });
}), }),
); );
//Certificates //Certificates
await Promise.all( await Promise.all(
item.bodyCertificates.map(async (cer) => { item.bodyCertificates.map(async (cer) => {
@ -2139,40 +2121,38 @@ export class CommandController extends Controller {
Object.assign(profileCer, { ...cer, ...meta }); Object.assign(profileCer, { ...cer, ...meta });
const cerHistory = new ProfileCertificateHistory(); const cerHistory = new ProfileCertificateHistory();
Object.assign(cerHistory, { ...profileCer, id: undefined }); Object.assign(cerHistory, { ...profileCer, id: undefined });
profileCer.profileId = profile_.id; profileCer.profileId = profile.id
await this.certificateRepo.save(profileCer, { data: req }); await this.certificateRepo.save(profileCer, { data: req });
setLogDataDiff(req, { before, after: profileCer }); setLogDataDiff(req, { before, after: profileCer });
cerHistory.profileCertificateId = profileCer.id; cerHistory.profileCertificateId = profileCer.id;
await this.certificateHistoryRepo.save(cerHistory, { data: req }); await this.certificateHistoryRepo.save(cerHistory, { data: req });
}), }),
); );
//Salary //Salary
const dest_item = await this.salaryRepo.findOne({ const dest_item = await this.salaryRepo.findOne({
where: { profileId: profile_.id }, where: { profileId: profile.id },
order: { order: "DESC" }, order: { order: "DESC" },
}); });
const profileSal = new ProfileSalary(); const profileSal = new ProfileSalary();
Object.assign(profileSal, { ...item.bodySalarys, ...meta }); Object.assign(profileSal, { ...item.bodySalarys, ...meta });
const salaryHistory = new ProfileSalaryHistory(); const salaryHistory = new ProfileSalaryHistory();
Object.assign(history, { ...profileSal, id: undefined }); Object.assign(salaryHistory, { ...profileSal, id: undefined });
(profileSal.order = dest_item == null ? 1 : dest_item.order + 1), profileSal.order = dest_item == null ? 1 : dest_item.order + 1,
(profileSal.profileId = profile_.id); profileSal.profileId = profile.id
await this.salaryRepo.save(profileSal, { data: req }); await this.salaryRepo.save(profileSal, { data: req });
setLogDataDiff(req, { before, after: profileSal }); setLogDataDiff(req, { before, after: profileSal });
salaryHistory.profileSalaryId = profileSal.id; salaryHistory.profileSalaryId = profileSal.id;
await this.salaryHistoryRepo.save(salaryHistory, { data: req }); await this.salaryHistoryRepo.save(salaryHistory, { data: req });
//Position //Position
const posMaster = await this.posMasterRepository.findOne({ const posMaster = await this.posMasterRepository.findOne({
where: { id: item.bodyPosition.posmasterId }, where: { id: item.bodyPosition.posmasterId },
}); });
if (posMaster == null) if (posMaster == null)
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
const posMasterOld = await this.posMasterRepository.findOne({ const posMasterOld = await this.posMasterRepository.findOne({
where: { where: {
current_holderId: profile_.id, current_holderId: profile.id,
orgRevisionId: posMaster.orgRevisionId, orgRevisionId: posMaster.orgRevisionId,
}, },
}); });
@ -2203,7 +2183,7 @@ export class CommandController extends Controller {
await this.positionRepository.save(clearPosition); await this.positionRepository.save(clearPosition);
} }
posMaster.current_holderId = profile_.id; posMaster.current_holderId = profile.id;
if (posMasterOld != null) await this.posMasterRepository.save(posMasterOld); if (posMasterOld != null) await this.posMasterRepository.save(posMasterOld);
await this.posMasterRepository.save(posMaster); await this.posMasterRepository.save(posMaster);
@ -2215,15 +2195,15 @@ export class CommandController extends Controller {
}); });
if (positionNew != null) { if (positionNew != null) {
positionNew.positionIsSelected = true; positionNew.positionIsSelected = true;
profile_.posLevelId = positionNew.posLevelId; profile.posLevelId = positionNew.posLevelId;
profile_.posTypeId = positionNew.posTypeId; profile.posTypeId = positionNew.posTypeId;
profile_.position = positionNew.positionName; profile.position = positionNew.positionName;
profile_.keycloak = userKeycloakId; // Update KeyCloak await this.profileRepository.save(profile, { data: req });
await this.profileRepository.save(profile_, { data: req }); setLogDataDiff(req, { before, after: profile });
setLogDataDiff(req, { before, after: profile_ });
await this.positionRepository.save(positionNew, { data: req }); await this.positionRepository.save(positionNew, { data: req });
} }
} }
// console.log(`${item.bodyProfile.citizenId} ==> Success`)
}), }),
); );
return new HttpSuccess(); return new HttpSuccess();