hrms-api-org/src/entities/view/viewRegistryOfficer.ts

326 lines
7.8 KiB
TypeScript
Raw Normal View History

2025-02-19 11:03:30 +07:00
import { ViewColumn, ViewEntity } from "typeorm";
@ViewEntity({
expression: `
WITH Position AS (
SELECT
posExecutive.posExecutiveName,
pn.posMasterId,
2025-03-07 18:09:29 +07:00
pn.positionArea,
2025-02-19 11:03:30 +07:00
ROW_NUMBER() OVER (PARTITION BY pn.posMasterId) AS pn_number
FROM position pn
LEFT JOIN posExecutive ON pn.posExecutiveId = posExecutive.id
WHERE pn.positionIsSelected IS TRUE
),
PosMaster AS (
SELECT
2025-03-07 18:09:29 +07:00
pn.positionArea,
2025-02-19 11:03:30 +07:00
pm.current_holderId,
pm.posMasterNo,
pm.orgRootId,
pm.orgChild1Id,
pm.orgChild2Id,
pm.orgChild3Id,
pm.orgChild4Id,
orgRoot.orgRootName,
orgChild1.orgChild1Name,
orgChild2.orgChild2Name,
orgChild3.orgChild3Name,
orgChild4.orgChild4Name,
pn.posExecutiveName,
CASE
WHEN pm.orgChild1Id IS NULL THEN CONCAT(orgRoot.orgRootShortName, pm.posMasterNo)
WHEN pm.orgChild2Id IS NULL THEN CONCAT(orgChild1.orgChild1ShortName, pm.posMasterNo)
WHEN pm.orgChild3Id IS NULL THEN CONCAT(orgChild2.orgChild2ShortName, pm.posMasterNo)
WHEN pm.orgChild4Id IS NULL THEN CONCAT(orgChild3.orgChild3ShortName, pm.posMasterNo)
ELSE CONCAT(orgChild4.orgChild4ShortName, pm.posMasterNo)
END AS searchShortName,
ROW_NUMBER() OVER (PARTITION BY pm.current_holderId ORDER BY pm.posMasterNo DESC) AS pm_number
FROM posMaster pm
LEFT JOIN orgRevision ON orgRevision.id = pm.orgRevisionId
LEFT JOIN orgRoot ON orgRoot.id = pm.orgRootId
LEFT JOIN orgChild1 ON orgChild1.id = pm.orgChild1Id
LEFT JOIN orgChild2 ON orgChild2.id = pm.orgChild2Id
LEFT JOIN orgChild3 ON orgChild3.id = pm.orgChild3Id
LEFT JOIN orgChild4 ON orgChild4.id = pm.orgChild4Id
LEFT JOIN Position pn ON pm.id = pn.posMasterId AND pn.pn_number = 1
WHERE
orgRevision.orgRevisionIsCurrent IS TRUE
AND orgRevision.orgRevisionIsDraft IS FALSE
),
2025-03-07 18:09:29 +07:00
Educations AS (
SELECT
eds.profileId,
JSON_ARRAYAGG(
JSON_OBJECT(
'degree', eds.degree,
'field', eds.field,
'educationLevel', eds.educationLevel,
'isEducation', eds.isEducation,
'isHigh', eds.isHigh
)
) AS Educations
FROM (
SELECT DISTINCT
eds.degree,
eds.field,
eds.educationLevel,
eds.isEducation,
eds.isHigh,
2025-03-11 10:36:59 +07:00
eds.profileId,
eds.level
2025-03-07 18:09:29 +07:00
FROM profileEducation eds
) AS eds
GROUP BY eds.profileId
2025-03-11 10:36:59 +07:00
ORDER BY eds.level DESC
2025-03-07 18:09:29 +07:00
),
EducationLevels AS (
SELECT
edls.profileId,
GROUP_CONCAT(DISTINCT edls.educationLevel ORDER BY edls.educationLevel SEPARATOR ', ') AS educationLevels
FROM profileEducation edls
WHERE edls.educationLevel IS NOT NULL AND edls.educationLevel != ''
GROUP BY edls.profileId
ORDER BY edls.level DESC
),
Degrees AS (
2025-02-19 11:03:30 +07:00
SELECT
2025-03-07 18:09:29 +07:00
degs.profileId,
GROUP_CONCAT(DISTINCT degs.degree ORDER BY degs.degree SEPARATOR ', ') AS degrees
FROM profileEducation degs
WHERE degs.degree IS NOT NULL AND degs.degree != ''
GROUP BY degs.profileId
ORDER BY degs.level DESC
),
Fields AS (
SELECT
fies.profileId,
GROUP_CONCAT(DISTINCT fies.field ORDER BY fies.field SEPARATOR ', ') AS fields
FROM profileEducation fies
WHERE fies.field IS NOT NULL AND fies.field != ''
GROUP BY fies.profileId
ORDER BY fies.level DESC
2025-02-28 11:45:04 +07:00
),
PositionDate AS (
SELECT
vcto.Years,
vcto.Months,
vcto.Days,
2025-03-05 18:14:40 +07:00
vcto.profileId
FROM tenurePositionOfficer vcto
2025-03-04 15:49:24 +07:00
),
PositionLevelDate AS (
SELECT
vctlo.Years,
vctlo.Months,
vctlo.Days,
2025-03-05 18:14:40 +07:00
vctlo.profileId
FROM tenureLevelOfficer vctlo
2025-02-19 11:03:30 +07:00
)
SELECT
p.id as profileId,
2025-02-19 11:03:30 +07:00
p.citizenId,
2025-02-20 12:12:04 +07:00
p.rank,
2025-02-19 11:03:30 +07:00
p.prefix,
p.firstName,
p.lastName,
p.isProbation,
p.isLeave,
p.isRetirement,
p.leaveType,
pm.posMasterNo,
pm.orgRootId,
pm.orgChild1Id,
pm.orgChild2Id,
pm.orgChild3Id,
pm.orgChild4Id,
pm.orgRootName,
pm.orgChild1Name,
pm.orgChild2Name,
pm.orgChild3Name,
pm.orgChild4Name,
CASE
WHEN pm.orgChild1Id IS NULL THEN pm.orgRootName
WHEN pm.orgChild2Id IS NULL THEN CONCAT(pm.orgChild1Name, " ", pm.orgRootName)
WHEN pm.orgChild3Id IS NULL THEN CONCAT(pm.orgChild2Name, " ", pm.orgChild1Name, " ", pm.orgRootName)
WHEN pm.orgChild4Id IS NULL THEN CONCAT(pm.orgChild3Name, " ", pm.orgChild2Name, " ", pm.orgChild1Name, " ", pm.orgRootName)
ELSE CONCAT(pm.orgChild4Name, " ", pm.orgChild3Name, " ", pm.orgChild2Name, " ", pm.orgChild1Name, " ", pm.orgRootName)
END AS org,
pm.searchShortName,
pm.posExecutiveName,
2025-03-07 18:09:29 +07:00
pm.positionArea,
2025-02-19 11:03:30 +07:00
p.position,
posType.posTypeName,
posLevel.posLevelName,
p.gender,
p.relationship,
p.dateAppoint,
2025-02-24 12:16:47 +07:00
p.dateRetire,
p.dateRetireLaw,
2025-02-19 11:03:30 +07:00
p.birthdate,
2025-03-07 18:09:29 +07:00
eds.Educations,
edls.educationLevels,
degs.degrees,
fies.fields,
2025-02-27 18:24:11 +07:00
TIMESTAMPDIFF(YEAR, p.birthdate, CURDATE()) AS age,
vcto.Years,
vcto.Months,
2025-02-28 11:45:04 +07:00
vcto.Days,
2025-03-04 15:49:24 +07:00
vctlo.Years AS levelYears,
vctlo.Months AS levelMonths,
vctlo.Days AS levelDays
2025-02-19 11:03:30 +07:00
FROM profile p
LEFT JOIN posLevel ON p.posLevelId = posLevel.id
LEFT JOIN posType ON p.posTypeId = posType.id
LEFT JOIN PosMaster pm ON p.id = pm.current_holderId AND pm.pm_number = 1
2025-03-07 18:09:29 +07:00
LEFT JOIN Educations eds ON p.id = eds.profileId
LEFT JOIN EducationLevels edls ON p.id = edls.profileId
LEFT JOIN Degrees degs ON p.id = degs.profileId
LEFT JOIN Fields fies ON p.id = fies.profileId
2025-03-05 18:14:40 +07:00
LEFT JOIN PositionDate vcto ON p.id = vcto.profileId
LEFT JOIN PositionLevelDate vctlo ON p.id = vctlo.profileId
2025-02-19 11:03:30 +07:00
`,
})
export class viewRegistryOfficer {
@ViewColumn()
profileId: string;
@ViewColumn()
citizenId: string;
2025-02-19 11:03:30 +07:00
@ViewColumn()
prefix: string;
@ViewColumn()
firstName: string;
@ViewColumn()
lastName: string;
@ViewColumn()
isProbation: boolean;
@ViewColumn()
isLeave: boolean;
@ViewColumn()
isRetirement: boolean;
@ViewColumn()
leaveType: string;
@ViewColumn()
posMasterNo: string;
@ViewColumn()
orgRootId: string;
@ViewColumn()
orgChild1Id: string;
@ViewColumn()
orgChild2Id: string;
@ViewColumn()
orgChild3Id: string;
@ViewColumn()
orgChild4Id: string;
@ViewColumn()
orgRootName: string;
@ViewColumn()
orgChild1Name: string;
@ViewColumn()
orgChild2Name: string;
@ViewColumn()
orgChild3Name: string;
@ViewColumn()
orgChild4Name: string;
@ViewColumn()
org: string;
@ViewColumn()
searchShortName: string;
@ViewColumn()
posExecutiveName: string;
@ViewColumn()
position: string;
@ViewColumn()
posTypeName: string;
@ViewColumn()
posLevelName: string;
@ViewColumn()
gender: string;
@ViewColumn()
relationship: string;
@ViewColumn()
dateAppoint: Date;
2025-02-24 14:08:11 +07:00
@ViewColumn()
dateRetire: Date;
@ViewColumn()
dateRetireLaw: Date;
2025-02-19 11:03:30 +07:00
@ViewColumn()
birthdate: Date;
@ViewColumn()
2025-03-10 09:48:05 +07:00
degrees: string;
2025-02-19 11:03:30 +07:00
@ViewColumn()
age: number;
2025-02-27 18:24:11 +07:00
@ViewColumn()
Years: number;
@ViewColumn()
Months: number;
@ViewColumn()
Days: number;
2025-03-04 15:49:24 +07:00
@ViewColumn()
levelYears: number;
@ViewColumn()
levelMonths: number;
@ViewColumn()
levelDays: number;
2025-03-07 18:09:29 +07:00
2025-05-26 10:10:28 +07:00
@ViewColumn()
posExecutiveYears: number;
@ViewColumn()
posExecutiveMonths: number;
@ViewColumn()
posExecutiveDays: number;
2025-03-07 18:09:29 +07:00
@ViewColumn()
positionArea: string;
@ViewColumn()
Educations: object;
@ViewColumn()
educationLevels: string;
@ViewColumn()
fields: string;
2025-02-19 11:03:30 +07:00
}