diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 0b1e00f3..fa0cefbd 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -54,6 +54,7 @@ import { ProfileFamilyFather } from "../entities/ProfileFamilyFather"; import CallAPI from "../interfaces/call-api"; import Extension from "../interfaces/extension"; import { Prefixe } from "../entities/Prefixe"; +import { ProfileInsignia } from "../entities/ProfileInsignia"; @Route("api/v1/org/profile") @Tags("Profile") @@ -88,6 +89,8 @@ export class ProfileController extends Controller { private provinceRepo = AppDataSource.getRepository(Province); private districtRepo = AppDataSource.getRepository(District); private subDistrictRepo = AppDataSource.getRepository(SubDistrict); + private profileInsigniaRepo = AppDataSource.getRepository(ProfileInsignia); + private profileDisciplineRepo = AppDataSource.getRepository(ProfileDiscipline); /** * report ประวัติแบบย่อ ข้าราชการ * @@ -196,11 +199,7 @@ export class ProfileController extends Controller { profile.profileSalary.length > 0 && profile.profileSalary[0].amount != null ? Extension.ToThaiNumber(profile.profileSalary[0].amount.toLocaleString()) : "", - Education: - profile.profileEducations.length > 0 && - profile.profileEducations[profile.profileEducations.length - 1].institute != null - ? profile.profileEducations[profile.profileEducations.length - 1].institute - : "", + Education: profile.profileEducations, AppointText: profile.dateAppoint != null ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear(profile.dateAppoint)) @@ -474,6 +473,7 @@ export class ProfileController extends Controller { Rank: item.positionLevel ?? null, RefAll: item.templateDoc ? Extension.ToThaiNumber(item.templateDoc) : null, PositionLevel: item.positionLevel ?? null, + PositionType: item.positionType ?? null, PositionAmount: item.positionSalaryAmount == null ? null @@ -496,79 +496,7 @@ export class ProfileController extends Controller { * @param {string} id Id ทะเบียนประวัติ */ @Get("placement/{id}") - async getProfilePlacement(@Path() id: string) { - const profile = await this.profileRepo.findOne({ - where: { id }, - }); - - if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - - const _caregiver = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .andWhere({ keycloak: Not(IsNull()) }) - .skip((1 - 1) * 20) - .take(20) - .getMany(); - const _commander = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .andWhere({ keycloak: Not(IsNull()) }) - .skip((1 - 1) * 20) - .take(20) - .getMany(); - const _chairman = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .andWhere({ keycloak: Not(IsNull()) }) - .skip((1 - 1) * 20) - .take(20) - .getMany(); - - const caregiver = _caregiver.map((_data) => ({ - id: _data.id, - prefix: _data.prefix, - firstName: _data.firstName, - lastName: _data.lastName, - citizenId: _data.citizenId, - position: _data.position, - posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, - posType: _data.posType == null ? null : _data.posType.posTypeName, - isDirector: true, - })); - const commander = _commander.map((_data) => ({ - id: _data.id, - prefix: _data.prefix, - firstName: _data.firstName, - lastName: _data.lastName, - citizenId: _data.citizenId, - position: _data.position, - posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, - posType: _data.posType == null ? null : _data.posType.posTypeName, - isDirector: true, - })); - const chairman = _chairman.map((_data) => ({ - id: _data.id, - prefix: _data.prefix, - firstName: _data.firstName, - lastName: _data.lastName, - citizenId: _data.citizenId, - position: _data.position, - posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, - posType: _data.posType == null ? null : _data.posType.posTypeName, - isDirector: true, - })); - return new HttpSuccess({ caregiver, commander, chairman }); - } - /** - * - * - */ - @Get("commander") - async getProfileCommander(@Request() request: RequestWithUser) { + async getProfilePlacement(@Request() request: RequestWithUser, @Path() id: string) { const profile = await this.profileRepo.findOne({ where: { keycloak: request.user.sub }, }); @@ -582,65 +510,75 @@ export class ProfileController extends Controller { const orgChild2 = posMaster.orgChild2Id; const orgChild3 = posMaster.orgChild3Id; const orgChild4 = posMaster.orgChild4Id; - - const findChild4 = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.current_holders", "current_holders") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .andWhere({ keycloak: Not(IsNull()) }) - .where("current_holders.current_holderId IS NOT NULL") - .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) - .andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId }) - .getMany(); - const findChild3 = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.current_holders", "current_holders") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .andWhere({ keycloak: Not(IsNull()) }) - .where("current_holders.current_holderId IS NOT NULL") - .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) - .andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId }) - .andWhere("current_holders.orgChild3Id = :orgChild3Id", { orgChild3Id: orgChild3 }) - .andWhere("current_holders.orgChild4Id IS NULL") - .getMany(); - const findChild2 = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.current_holders", "current_holders") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .andWhere({ keycloak: Not(IsNull()) }) - .where("current_holders.current_holderId IS NOT NULL") - .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) - .andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId }) - .andWhere("current_holders.orgChild2Id = :orgChild2Id", { orgChild2Id: orgChild2 }) - .andWhere("current_holders.orgChild3Id IS NULL") - .getMany(); - const findChild1 = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.current_holders", "current_holders") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .andWhere({ keycloak: Not(IsNull()) }) - .where("current_holders.current_holderId IS NOT NULL") - .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) - .andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId }) - .andWhere("current_holders.orgChild1Id = :orgChild1Id", { orgChild1Id: orgChild1 }) - .andWhere("current_holders.orgChild2Id IS NULL") - .getMany(); - const findRoot = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.current_holders", "current_holders") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .andWhere({ keycloak: Not(IsNull()) }) - .where("current_holders.current_holderId IS NOT NULL") - .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) - .andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId }) - .andWhere("current_holders.orgChild1Id IS NULL") - .getMany(); - + + const findChild4 = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .getMany(); + const findChild3 = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .andWhere("current_holders.orgChild3Id = :orgChild3Id", { orgChild3Id: orgChild3 }) + .andWhere("current_holders.orgChild4Id IS NULL") + .getMany(); + const findChild2 = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .andWhere("current_holders.orgChild2Id = :orgChild2Id", { orgChild2Id: orgChild2 }) + .andWhere("current_holders.orgChild3Id IS NULL") + .getMany(); + const findChild1 = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .andWhere("current_holders.orgChild1Id = :orgChild1Id", { orgChild1Id: orgChild1 }) + .andWhere("current_holders.orgChild2Id IS NULL") + .getMany(); + const findRoot = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .andWhere("current_holders.orgChild1Id IS NULL") + .getMany(); + let _caregiver: any; let caregiver: any; let _commander: any; @@ -712,48 +650,251 @@ export class ProfileController extends Controller { _commander = findRoot; _chairman = findRoot; } - } else if (orgRoot) { + } else if (orgRoot) { if (findRoot) { _caregiver = findRoot; _commander = findRoot; _chairman = findRoot; } } - caregiver = _caregiver.map((_data:any) => ({ - id: _data.id, - prefix: _data.prefix, - firstName: _data.firstName, - lastName: _data.lastName, - citizenId: _data.citizenId, - position: _data.position, - posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, - posType: _data.posType == null ? null : _data.posType.posTypeName, - isDirector: true, + caregiver = _caregiver.map((_data: any) => ({ + id: _data.id, + prefix: _data.prefix, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + position: _data.position, + posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + posType: _data.posType == null ? null : _data.posType.posTypeName, + isDirector: true, })); - commander = _commander.map((_data:any) => ({ - id: _data.id, - prefix: _data.prefix, - firstName: _data.firstName, - lastName: _data.lastName, - citizenId: _data.citizenId, - position: _data.position, - posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, - posType: _data.posType == null ? null : _data.posType.posTypeName, - isDirector: true, + commander = _commander.map((_data: any) => ({ + id: _data.id, + prefix: _data.prefix, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + position: _data.position, + posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + posType: _data.posType == null ? null : _data.posType.posTypeName, + isDirector: true, })); - chairman = _chairman.map((_data:any) => ({ - id: _data.id, - prefix: _data.prefix, - firstName: _data.firstName, - lastName: _data.lastName, - citizenId: _data.citizenId, - position: _data.position, - posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, - posType: _data.posType == null ? null : _data.posType.posTypeName, - isDirector: true, + chairman = _chairman.map((_data: any) => ({ + id: _data.id, + prefix: _data.prefix, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + position: _data.position, + posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + posType: _data.posType == null ? null : _data.posType.posTypeName, + isDirector: true, })); } - + + return new HttpSuccess({ caregiver, commander, chairman }); + } + /** + * + * + */ + @Get("commander") + async getProfileCommander(@Request() request: RequestWithUser) { + const profile = await this.profileRepo.findOne({ + where: { keycloak: request.user.sub }, + }); + + const posMaster = await this.posMasterRepo.findOne({ + where: { current_holderId: profile?.id }, + }); + if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลการครองตำแหน่ง"); + const orgRoot = posMaster.orgRootId; + const orgChild1 = posMaster.orgChild1Id; + const orgChild2 = posMaster.orgChild2Id; + const orgChild3 = posMaster.orgChild3Id; + const orgChild4 = posMaster.orgChild4Id; + + const findChild4 = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .getMany(); + const findChild3 = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .andWhere("current_holders.orgChild3Id = :orgChild3Id", { orgChild3Id: orgChild3 }) + .andWhere("current_holders.orgChild4Id IS NULL") + .getMany(); + const findChild2 = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .andWhere("current_holders.orgChild2Id = :orgChild2Id", { orgChild2Id: orgChild2 }) + .andWhere("current_holders.orgChild3Id IS NULL") + .getMany(); + const findChild1 = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .andWhere("current_holders.orgChild1Id = :orgChild1Id", { orgChild1Id: orgChild1 }) + .andWhere("current_holders.orgChild2Id IS NULL") + .getMany(); + const findRoot = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .andWhere("current_holders.orgChild1Id IS NULL") + .getMany(); + + let _caregiver: any; + let caregiver: any; + let _commander: any; + let commander: any; + let _chairman: any; + let chairman: any; + if (posMaster) { + if (orgChild4) { + if (findChild4) { + _caregiver = findChild4; + _commander = findChild3; + _chairman = findChild2; + } else if (findChild3) { + _caregiver = findChild3; + _commander = findChild2; + _chairman = findChild1; + } else if (findChild2) { + _caregiver = findChild2; + _commander = findChild1; + _chairman = findRoot; + } else if (findChild1) { + _caregiver = findChild1; + _commander = findRoot; + _chairman = findRoot; + } else if (findRoot) { + _caregiver = findRoot; + _commander = findRoot; + _chairman = findRoot; + } + } else if (orgChild3) { + if (findChild3) { + _caregiver = findChild3; + _commander = findChild2; + _chairman = findChild1; + } else if (findChild2) { + _caregiver = findChild2; + _commander = findChild1; + _chairman = findRoot; + } else if (findChild1) { + _caregiver = findChild1; + _commander = findRoot; + _chairman = findRoot; + } else if (findRoot) { + _caregiver = findRoot; + _commander = findRoot; + _chairman = findRoot; + } + } else if (orgChild2) { + if (findChild2) { + _caregiver = findChild2; + _commander = findChild1; + _chairman = findRoot; + } else if (findChild1) { + _caregiver = findChild1; + _commander = findRoot; + _chairman = findRoot; + } else if (findRoot) { + _caregiver = findRoot; + _commander = findRoot; + _chairman = findRoot; + } + } else if (orgChild1) { + if (findChild1) { + _caregiver = findChild1; + _commander = findRoot; + _chairman = findRoot; + } else if (findRoot) { + _caregiver = findRoot; + _commander = findRoot; + _chairman = findRoot; + } + } else if (orgRoot) { + if (findRoot) { + _caregiver = findRoot; + _commander = findRoot; + _chairman = findRoot; + } + } + caregiver = _caregiver.map((_data: any) => ({ + id: _data.id, + prefix: _data.prefix, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + position: _data.position, + posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + posType: _data.posType == null ? null : _data.posType.posTypeName, + isDirector: true, + })); + commander = _commander.map((_data: any) => ({ + id: _data.id, + prefix: _data.prefix, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + position: _data.position, + posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + posType: _data.posType == null ? null : _data.posType.posTypeName, + isDirector: true, + })); + chairman = _chairman.map((_data: any) => ({ + id: _data.id, + prefix: _data.prefix, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + position: _data.position, + posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + posType: _data.posType == null ? null : _data.posType.posTypeName, + isDirector: true, + })); + } + return new HttpSuccess({ caregiver, commander, chairman }); } @@ -4841,4 +4982,39 @@ export class ProfileController extends Controller { await this.profileRepo.save(profiles); return new HttpSuccess("zxczx"); } + /** + * + * @summary (ADMIN) + * + */ + @Get("salarym/zxczxcsa") + async zxczxczxczxczxc() { + const profile = await this.profileRepo.find({ relations: ["current_holders", "next_holders"] }); + const profiles = await Promise.all( + profile.map(async (_data) => { + if (_data.current_holders.length == 0 && _data.next_holders.length == 0) { + const salary = await this.salaryRepository.find({ + where: { + profileId: _data.id, + }, + }); + const insignia = await this.profileInsigniaRepo.find({ + where: { + profileId: _data.id, + }, + }); + const discipline = await this.profileDisciplineRepo.find({ + where: { + profileId: _data.id, + }, + }); + await this.salaryRepository.remove(salary); + await this.profileInsigniaRepo.remove(insignia); + await this.profileDisciplineRepo.remove(discipline); + await this.profileRepo.remove(_data); + } + }), + ); + return new HttpSuccess("zxczx"); + } }