From eea123fbad61bff5e4bb95784ea3e4b6d209cd7c Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 24 May 2024 11:06:43 +0700 Subject: [PATCH] add field profile and profile-employee search-personal --- src/controllers/ProfileController.ts | 219 +++++++++++++++---- src/controllers/ProfileEmployeeController.ts | 215 ++++++++++++++---- 2 files changed, 348 insertions(+), 86 deletions(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index c0873efc..7518fc04 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -77,7 +77,7 @@ export class ProfileController extends Controller { const orgRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true }, }); - + const profile = await this.profileRepo.findOne({ relations: [ "profileSalary", @@ -87,16 +87,20 @@ export class ProfileController extends Controller { "current_holders.orgChild1", "current_holders.orgChild2", "current_holders.orgChild3", - "current_holders.orgChild4" + "current_holders.orgChild4", ], - where: { id: id, }, + where: { id: id }, }); if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - const province = await this.provinceRepository.findOneBy({ id: profile.registrationProvinceId }) - const district = await this.districtRepository.findOneBy({ id: profile.registrationDistrictId }) - const subDistrict = await this.subDistrict.findOneBy({ id: profile.registrationSubDistrictId }) - + const province = await this.provinceRepository.findOneBy({ + id: profile.registrationProvinceId, + }); + const district = await this.districtRepository.findOneBy({ + id: profile.registrationDistrictId, + }); + const subDistrict = await this.subDistrict.findOneBy({ id: profile.registrationSubDistrictId }); + const root = profile.current_holders == null || profile.current_holders.length == 0 || @@ -147,22 +151,18 @@ export class ProfileController extends Controller { DateOfBirth: profile.birthDate, DateRetire: profile.dateRetire, RegistrationAddress: `${profile.registrationAddress}\r\nตำบล/แขวง ${province?.name}\r\nเขต/อำเภอ ${district?.name}\r\nจังหวัด ${subDistrict?.name} รหัสไปรษณีย์ ${profile.registrationZipCode}`, - SalaryAmount: profile.profileSalary.length > 0 - ? profile.profileSalary[0].amount - : null, - Education: profile.profileEducations.length > 0 - ? profile.profileEducations[profile.profileEducations.length-1].institute - : null, + SalaryAmount: profile.profileSalary.length > 0 ? profile.profileSalary[0].amount : null, + Education: + profile.profileEducations.length > 0 + ? profile.profileEducations[profile.profileEducations.length - 1].institute + : null, AppointText: profile.dateAppoint, - SalaryDate: profile.profileSalary.length > 0 - ? profile.profileSalary[0].date - : null, + SalaryDate: profile.profileSalary.length > 0 ? profile.profileSalary[0].date : null, PositionName: profile.position, OcFullPath: `${_child4}${_child3}${_child2}${_child1}${_root}`, - } + }; return new HttpSuccess(mapData); - } /** @@ -945,7 +945,7 @@ export class ProfileController extends Controller { "profile.citizenId", "profileSalary.position", "profileSalary.posNo", - "profileSalary.date" + "profileSalary.date", ]) .andWhere( requestBody.position != null && requestBody.position != "" && requestBody.posNo == undefined @@ -965,27 +965,27 @@ export class ProfileController extends Controller { ) .getMany(); - const mapData = profiles.map(profile => { - let profileSalary; - if (profile.profileSalary && profile.profileSalary.length > 0) { - profileSalary = profile.profileSalary.reduce((latest, current) => { - return new Date(current.date) > new Date(latest.date) ? current : latest; - }); - } - return { - id: profile.id, - // prefix: profile.prefix, - // firstName: profile.firstName, - // lastName: profile.lastName, - fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}`, - citizenId: profile.citizenId, - position: profileSalary ? profileSalary.position : null, - posNo: profileSalary ? profileSalary.posNo : null, - date: profileSalary ? profileSalary.date : null - }; - }); - - return new HttpSuccess(mapData); + const mapData = profiles.map((profile) => { + let profileSalary; + if (profile.profileSalary && profile.profileSalary.length > 0) { + profileSalary = profile.profileSalary.reduce((latest, current) => { + return new Date(current.date) > new Date(latest.date) ? current : latest; + }); + } + return { + id: profile.id, + // prefix: profile.prefix, + // firstName: profile.firstName, + // lastName: profile.lastName, + fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}`, + citizenId: profile.citizenId, + position: profileSalary ? profileSalary.position : null, + posNo: profileSalary ? profileSalary.posNo : null, + date: profileSalary ? profileSalary.date : null, + }; + }); + + return new HttpSuccess(mapData); } /** @@ -1496,33 +1496,141 @@ export class ProfileController extends Controller { case "idcard": findProfile = await this.profileRepo.find({ where: { citizenId: Like(`%${body.keyword}%`) }, - relations: ["posType", "posLevel"], + relations: [ + "posType", + "posLevel", + "current_holders", + "profileSalary", + "current_holders.orgRoot", + "current_holders.orgChild1", + "current_holders.orgChild2", + "current_holders.orgChild3", + "current_holders.orgChild4", + ], }); break; case "firstname": findProfile = await this.profileRepo.find({ where: { firstName: Like(`%${body.keyword}%`) }, - relations: ["posType", "posLevel"], + relations: [ + "posType", + "posLevel", + "current_holders", + "profileSalary", + "current_holders.orgRoot", + "current_holders.orgChild1", + "current_holders.orgChild2", + "current_holders.orgChild3", + "current_holders.orgChild4", + ], }); break; case "lastname": findProfile = await this.profileRepo.find({ where: { lastName: Like(`%${body.keyword}%`) }, - relations: ["posType", "posLevel"], + relations: [ + "posType", + "posLevel", + "current_holders", + "profileSalary", + "current_holders.orgRoot", + "current_holders.orgChild1", + "current_holders.orgChild2", + "current_holders.orgChild3", + "current_holders.orgChild4", + ], }); break; default: findProfile = await this.profileRepo.find({ - relations: ["posType", "posLevel"], + relations: [ + "posType", + "posLevel", + "current_holders", + "profileSalary", + "current_holders.orgRoot", + "current_holders.orgChild1", + "current_holders.orgChild2", + "current_holders.orgChild3", + "current_holders.orgChild4", + ], }); break; } + const findRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true }, + }); + if (!findRevision) { + throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); + } + const mapDataProfile = await Promise.all( findProfile.map(async (item: Profile) => { + const fullName = `${item.prefix} ${item.firstName} ${item.lastName}`; + const shortName = + item.current_holders.length == 0 + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild2 != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild1 != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != + null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgRoot != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : null; + + const root = + item.current_holders.length == 0 || + (item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null) + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; + + let salary: any = ""; + if (item != null && item.profileSalary != null && item.profileSalary.length > 0) { + let _salary: any = item.profileSalary.sort( + (a, b) => + (b.date == null ? 0 : b.date.getTime()) - (a.date == null ? 0 : a.date.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + + const rootHolder = item.current_holders?.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgRoot; + const child1Holder = item.current_holders?.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgChild1; + const child2Holder = item.current_holders?.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgChild2; + const child3Holder = item.current_holders?.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgChild3; + const child4Holder = item.current_holders?.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgChild4; + return { id: item.id, prefix: item.prefix, @@ -1533,6 +1641,29 @@ export class ProfileController extends Controller { idcard: item.citizenId, email: item.email, phone: item.phone, + name: fullName, + positionLevel: item.posLevelId, + positionLevelName: item.posLevel?.posLevelName, + positionType: item.posTypeId, + positionTypeName: item.posType?.posTypeName, + posNo: shortName, + organization: root == null ? null : root.orgRootShortName, + salary: salary == "" ? "" : salary.amount, + root: rootHolder?.orgRootName ?? null, + rootId: rootHolder?.id ?? null, + rootShortName: rootHolder?.orgRootShortName ?? null, + child1: child1Holder?.orgChild1Name ?? null, + child1Id: child1Holder?.id ?? null, + child1ShortName: child1Holder?.orgChild1ShortName ?? null, + child2: child2Holder?.orgChild2Name ?? null, + child2Id: child2Holder?.id ?? null, + child2ShortName: child2Holder?.orgChild2ShortName ?? null, + child3: child3Holder?.orgChild3Name ?? null, + child3Id: child3Holder?.id ?? null, + child3ShortName: child3Holder?.orgChild3ShortName ?? null, + child4: child4Holder?.orgChild4Name ?? null, + child4Id: child4Holder?.id ?? null, + child4ShortName: child4Holder?.orgChild4ShortName ?? null, }; }), ); diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index ce1b30ae..aaf2fefe 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -67,7 +67,7 @@ export class ProfileEmployeeController extends Controller { const orgRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true }, }); - + const profile = await this.profileRepo.findOne({ relations: [ "profileSalarys", @@ -77,16 +77,20 @@ export class ProfileEmployeeController extends Controller { "current_holders.orgChild1", "current_holders.orgChild2", "current_holders.orgChild3", - "current_holders.orgChild4" + "current_holders.orgChild4", ], - where: { id: id, }, + where: { id: id }, }); if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - const province = await this.provinceRepository.findOneBy({ id: profile.registrationProvinceId }) - const district = await this.districtRepository.findOneBy({ id: profile.registrationDistrictId }) - const subDistrict = await this.subDistrict.findOneBy({ id: profile.registrationSubDistrictId }) - + const province = await this.provinceRepository.findOneBy({ + id: profile.registrationProvinceId, + }); + const district = await this.districtRepository.findOneBy({ + id: profile.registrationDistrictId, + }); + const subDistrict = await this.subDistrict.findOneBy({ id: profile.registrationSubDistrictId }); + const root = profile.current_holders == null || profile.current_holders.length == 0 || @@ -137,22 +141,18 @@ export class ProfileEmployeeController extends Controller { DateOfBirth: profile.birthDate, DateRetire: profile.dateRetire, RegistrationAddress: `${profile.registrationAddress}\r\nตำบล/แขวง ${province?.name}\r\nเขต/อำเภอ ${district?.name}\r\nจังหวัด ${subDistrict?.name} รหัสไปรษณีย์ ${profile.registrationZipCode}`, - SalaryAmount: profile.profileSalarys.length > 0 - ? profile.profileSalarys[0].amount - : null, - Education: profile.profileEducations.length > 0 - ? profile.profileEducations[profile.profileEducations.length-1].institute - : null, + SalaryAmount: profile.profileSalarys.length > 0 ? profile.profileSalarys[0].amount : null, + Education: + profile.profileEducations.length > 0 + ? profile.profileEducations[profile.profileEducations.length - 1].institute + : null, AppointText: profile.dateAppoint, - SalaryDate: profile.profileSalarys.length > 0 - ? profile.profileSalarys[0].date - : null, + SalaryDate: profile.profileSalarys.length > 0 ? profile.profileSalarys[0].date : null, PositionName: profile.position, OcFullPath: `${_child4}${_child3}${_child2}${_child1}${_root}`, - } + }; return new HttpSuccess(mapData); - } /** @@ -658,7 +658,7 @@ export class ProfileEmployeeController extends Controller { "profileEmployee.citizenId", "profileSalarys.position", "profileSalarys.posNo", - "profileSalarys.date" + "profileSalarys.date", ]) .andWhere( requestBody.position != null && requestBody.position != "" && requestBody.posNo == undefined @@ -678,24 +678,24 @@ export class ProfileEmployeeController extends Controller { ) .getMany(); - const mapData = profiles.map(profile => { - let profileSalary; - if (profile.profileSalarys && profile.profileSalarys.length > 0) { - profileSalary = profile.profileSalarys.reduce((latest, current) => { - return new Date(current.date) > new Date(latest.date) ? current : latest; - }); - } - return { - id: profile.id, - fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}`, - citizenId: profile.citizenId, - position: profileSalary ? profileSalary.position : null, - posNo: profileSalary ? profileSalary.posNo : null, - date: profileSalary ? profileSalary.date : null - }; - }); - - return new HttpSuccess(mapData); + const mapData = profiles.map((profile) => { + let profileSalary; + if (profile.profileSalarys && profile.profileSalarys.length > 0) { + profileSalary = profile.profileSalarys.reduce((latest, current) => { + return new Date(current.date) > new Date(latest.date) ? current : latest; + }); + } + return { + id: profile.id, + fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}`, + citizenId: profile.citizenId, + position: profileSalary ? profileSalary.position : null, + posNo: profileSalary ? profileSalary.posNo : null, + date: profileSalary ? profileSalary.date : null, + }; + }); + + return new HttpSuccess(mapData); } /** @@ -830,43 +830,174 @@ export class ProfileEmployeeController extends Controller { case "idcard": findProfile = await this.profileRepo.find({ where: { citizenId: Like(`%${body.keyword}%`) }, - relations: ["posType", "posLevel"], + relations: [ + "posType", + "posLevel", + "current_holders", + "profileSalary", + "current_holders.orgRoot", + "current_holders.orgChild1", + "current_holders.orgChild2", + "current_holders.orgChild3", + "current_holders.orgChild4", + ], }); break; case "firstname": findProfile = await this.profileRepo.find({ where: { firstName: Like(`%${body.keyword}%`) }, - relations: ["posType", "posLevel"], + relations: [ + "posType", + "posLevel", + "current_holders", + "profileSalarys", + "current_holders.orgRoot", + "current_holders.orgChild1", + "current_holders.orgChild2", + "current_holders.orgChild3", + "current_holders.orgChild4", + ], }); break; case "lastname": findProfile = await this.profileRepo.find({ where: { lastName: Like(`%${body.keyword}%`) }, - relations: ["posType", "posLevel"], + relations: [ + "posType", + "posLevel", + "current_holders", + "profileSalarys", + "current_holders.orgRoot", + "current_holders.orgChild1", + "current_holders.orgChild2", + "current_holders.orgChild3", + "current_holders.orgChild4", + ], }); break; default: findProfile = await this.profileRepo.find({ - relations: ["posType", "posLevel"], + relations: [ + "posType", + "posLevel", + "current_holders", + "profileSalarys", + "current_holders.orgRoot", + "current_holders.orgChild1", + "current_holders.orgChild2", + "current_holders.orgChild3", + "current_holders.orgChild4", + ], }); break; } + const findRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true }, + }); + if (!findRevision) { + throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); + } + const mapDataProfile = await Promise.all( findProfile.map(async (item: ProfileEmployee) => { + const fullName = `${item.prefix} ${item.firstName} ${item.lastName}`; + const shortName = + item.current_holders.length == 0 + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild2 != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild1 != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != + null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgRoot != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : null; + + const root = + item.current_holders.length == 0 || + (item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null) + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; + + let salary: any = ""; + if (item != null && item.profileSalarys != null && item.profileSalarys.length > 0) { + let _salary: any = item.profileSalarys.sort( + (a, b) => + (b.date == null ? 0 : b.date.getTime()) - (a.date == null ? 0 : a.date.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + + const rootHolder = item.current_holders?.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgRoot; + const child1Holder = item.current_holders?.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgChild1; + const child2Holder = item.current_holders?.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgChild2; + const child3Holder = item.current_holders?.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgChild3; + const child4Holder = item.current_holders?.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgChild4; + return { id: item.id, - rank: item.rank, prefix: item.prefix, + rank: item.rank, firstName: item.firstName, lastName: item.lastName, position: item.position, idcard: item.citizenId, email: item.email, phone: item.phone, + name: fullName, + positionLevel: item.posLevelId, + positionLevelName: item.posLevel?.posLevelName, + positionType: item.posTypeId, + positionTypeName: item.posType?.posTypeName, + posNo: shortName, + organization: root == null ? null : root.orgRootShortName, + salary: salary == "" ? "" : salary.amount, + root: rootHolder?.orgRootName ?? null, + rootId: rootHolder?.id ?? null, + rootShortName: rootHolder?.orgRootShortName ?? null, + child1: child1Holder?.orgChild1Name ?? null, + child1Id: child1Holder?.id ?? null, + child1ShortName: child1Holder?.orgChild1ShortName ?? null, + child2: child2Holder?.orgChild2Name ?? null, + child2Id: child2Holder?.id ?? null, + child2ShortName: child2Holder?.orgChild2ShortName ?? null, + child3: child3Holder?.orgChild3Name ?? null, + child3Id: child3Holder?.id ?? null, + child3ShortName: child3Holder?.orgChild3ShortName ?? null, + child4: child4Holder?.orgChild4Name ?? null, + child4Id: child4Holder?.id ?? null, + child4ShortName: child4Holder?.orgChild4ShortName ?? null, }; }), );