Merge branch 'develop' into refactor/cronjob-position

* develop:
  fixed#1568 แก้ไขรายการตำแหน่งติดเงื่อนไข
  fix bug
  update path sql script
  #231 และ #2438 checkpoint
This commit is contained in:
Warunee Tamkoo 2026-04-24 11:39:23 +07:00
commit c348a10207
3 changed files with 303 additions and 348 deletions

View file

@ -0,0 +1,154 @@
-- =====================================================
-- Update position fields in profile table
-- อัพเดทฟิลด์ตำแหน่งในตาราง profile
--
-- Fields:
-- - positionField (สายงาน)
-- - posExecutive (ตำแหน่งทางการบริหาร)
-- - positionArea (ด้าน/สาขา)
-- - positionExecutiveField (ด้านทางการบริหาร)
-- - posMasterNo (เลขที่ตำแหน่ง) - format: orgShortName + space + number
-- - org (สังกัด)
--
-- Run each query separately to verify results
-- =====================================================
-- 1. Update positionField (สายงาน)
UPDATE profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
INNER JOIN position pos ON pos.posMasterId = pm.id AND pos.positionIsSelected = 1
SET p.positionField = pos.positionField
WHERE p.positionField IS NULL;
-- 2. Update posExecutive (ตำแหน่งทางการบริหาร)
UPDATE profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
INNER JOIN position pos ON pos.posMasterId = pm.id AND pos.positionIsSelected = 1
INNER JOIN posExecutive pe ON pos.posExecutiveId = pe.id
SET p.posExecutive = pe.posExecutiveName
WHERE p.posExecutive IS NULL;
-- 3. Update positionArea (ด้าน/สาขา)
UPDATE profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
INNER JOIN position pos ON pos.posMasterId = pm.id AND pos.positionIsSelected = 1
SET p.positionArea = pos.positionArea
WHERE p.positionArea IS NULL;
-- 4. Update positionExecutiveField (ด้านทางการบริหาร)
UPDATE profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
INNER JOIN position pos ON pos.posMasterId = pm.id AND pos.positionIsSelected = 1
SET p.positionExecutiveField = pos.positionExecutiveField
WHERE p.positionExecutiveField IS NULL;
-- 5. Update posMasterNo (เลขที่ตำแหน่ง) - format: orgShortName + space + number
UPDATE profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
LEFT JOIN orgRoot r ON pm.orgRootId = r.id
LEFT JOIN orgChild1 c1 ON pm.orgChild1Id = c1.id
LEFT JOIN orgChild2 c2 ON pm.orgChild2Id = c2.id
LEFT JOIN orgChild3 c3 ON pm.orgChild3Id = c3.id
LEFT JOIN orgChild4 c4 ON pm.orgChild4Id = c4.id
SET p.posMasterNo = TRIM(CONCAT(
CASE
WHEN pm.orgChild1Id IS NULL THEN r.orgRootShortName
WHEN pm.orgChild2Id IS NULL THEN c1.orgChild1ShortName
WHEN pm.orgChild3Id IS NULL THEN c2.orgChild2ShortName
WHEN pm.orgChild4Id IS NULL THEN c3.orgChild3ShortName
ELSE c4.orgChild4ShortName
END,
' ',
pm.posMasterNo
))
WHERE p.posMasterNo IS NULL;
-- 6. Update org (สังกัด) - combine all org levels
UPDATE profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
LEFT JOIN orgRoot r ON pm.orgRootId = r.id
LEFT JOIN orgChild1 c1 ON pm.orgChild1Id = c1.id
LEFT JOIN orgChild2 c2 ON pm.orgChild2Id = c2.id
LEFT JOIN orgChild3 c3 ON pm.orgChild3Id = c3.id
LEFT JOIN orgChild4 c4 ON pm.orgChild4Id = c4.id
SET p.org = TRIM(CONCAT_WS(
' ',
r.orgRootName,
c1.orgChild1Name,
c2.orgChild2Name,
c3.orgChild3Name,
c4.orgChild4Name
))
WHERE p.org IS NULL;
-- =====================================================
-- เช็คผลลัพธ์ (Check results)
-- =====================================================
-- เช็คจำนวนที่ update ได้
SELECT
COUNT(CASE WHEN positionField IS NOT NULL THEN 1 END) AS has_positionField,
COUNT(CASE WHEN posExecutive IS NOT NULL THEN 1 END) AS has_posExecutive,
COUNT(CASE WHEN positionArea IS NOT NULL THEN 1 END) AS has_positionArea,
COUNT(CASE WHEN positionExecutiveField IS NOT NULL THEN 1 END) AS has_positionExecutiveField,
COUNT(CASE WHEN posMasterNo IS NOT NULL THEN 1 END) AS has_posMasterNo,
COUNT(CASE WHEN org IS NOT NULL THEN 1 END) AS has_org
FROM profile;
-- =====================================================
-- SELECT query สำหรับทดสอบก่อนรัน (Test before run)
-- =====================================================
SELECT
p.id,
p.firstName,
p.lastName,
p.citizenId,
p.positionField as old_positionField,
p.posExecutive as old_posExecutive,
p.positionArea as old_positionArea,
p.positionExecutiveField as old_positionExecutiveField,
p.posMasterNo as old_posMasterNo,
p.org as old_org,
pos.positionField as new_positionField,
pe.posExecutiveName as new_posExecutive,
pos.positionArea as new_positionArea,
pos.positionExecutiveField as new_positionExecutiveField,
TRIM(CONCAT(
CASE
WHEN pm.orgChild1Id IS NULL THEN r.orgRootShortName
WHEN pm.orgChild2Id IS NULL THEN c1.orgChild1ShortName
WHEN pm.orgChild3Id IS NULL THEN c2.orgChild2ShortName
WHEN pm.orgChild4Id IS NULL THEN c3.orgChild3ShortName
ELSE c4.orgChild4ShortName
END,
' ',
pm.posMasterNo
)) as new_posMasterNo,
TRIM(CONCAT_WS(' ', r.orgRootName, c1.orgChild1Name, c2.orgChild2Name, c3.orgChild3Name, c4.orgChild4Name)) as new_org
FROM profile p
INNER JOIN posMaster pm ON pm.current_holderId = p.id
INNER JOIN orgRevision oRev ON pm.orgRevisionId = oRev.id AND oRev.orgRevisionIsCurrent = 1 AND oRev.orgRevisionIsDraft = 0
INNER JOIN position pos ON pos.posMasterId = pm.id AND pos.positionIsSelected = 1
LEFT JOIN posExecutive pe ON pos.posExecutiveId = pe.id
LEFT JOIN orgRoot r ON pm.orgRootId = r.id
LEFT JOIN orgChild1 c1 ON pm.orgChild1Id = c1.id
LEFT JOIN orgChild2 c2 ON pm.orgChild2Id = c2.id
LEFT JOIN orgChild3 c3 ON pm.orgChild3Id = c3.id
LEFT JOIN orgChild4 c4 ON pm.orgChild4Id = c4.id
-- ใส่ WHERE ทดสอบ 1 คน (Test 1 person)
WHERE p.id = 'ใส่ profile_id ที่ต้องการทดสอบ'
-- หรือทดสอบ 10 คน (Test 10 persons)
-- LIMIT 10;

View file

@ -1257,7 +1257,15 @@ export class PositionController extends Controller {
) {
await new permission().PermissionUpdate(request, "SYS_ORG");
const posMaster = await this.posMasterRepository.findOne({
relations: ["positions", "orgRevision", "orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"],
relations: [
"positions",
"orgRevision",
"orgRoot",
"orgChild1",
"orgChild2",
"orgChild3",
"orgChild4",
],
where: { id: id },
});
if (!posMaster) {
@ -2403,16 +2411,16 @@ export class PositionController extends Controller {
? "posMaster.orgRootId IN (:...root)"
: "posMaster.orgRootId is null"
: "1=1",
{ root: _data.root }
{ root: _data.root },
)
.andWhere(
_data.child1 != undefined && _data.child1 != null
? _data.child1[0] != null
? "posMaster.orgChild1Id IN (:...child1)"
// : `posMaster.orgChild1Id is ${_data.privilege == "PARENT" ? "not null" : "null"}`
: `posMaster.orgChild1Id is null`
: // : `posMaster.orgChild1Id is ${_data.privilege == "PARENT" ? "not null" : "null"}`
`posMaster.orgChild1Id is null`
: "1=1",
{ child1: _data.child1 }
{ child1: _data.child1 },
)
.andWhere(
_data.child2 != undefined && _data.child2 != null
@ -2443,26 +2451,27 @@ export class PositionController extends Controller {
{
child4: _data.child4,
},
)
);
// .andWhere(checkChildConditions)
// .andWhere(typeCondition)
// .andWhere(revisionCondition);
if (body.keyword != null && body.keyword != "") {
query.orWhere(
new Brackets((qb) => {
qb.andWhere(
body.keyword != null && body.keyword != ""
? body.isAll == false
? searchShortName
: `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'`
: "1=1",
)
.andWhere(checkChildConditions)
.andWhere(typeCondition)
.andWhere(revisionCondition);
}),
)
query
.orWhere(
new Brackets((qb) => {
qb.andWhere(
body.keyword != null && body.keyword != ""
? body.isAll == false
? searchShortName
: `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'`
: "1=1",
)
.andWhere(checkChildConditions)
.andWhere(typeCondition)
.andWhere(revisionCondition);
}),
)
.orWhere(
new Brackets((qb) => {
qb.andWhere(
@ -2971,50 +2980,50 @@ export class PositionController extends Controller {
const type0LastPosMasterNo =
requestBody.type == 0
? await this.posMasterRepository.find({
where: {
orgRootId: requestBody.id,
orgChild1Id: IsNull(),
},
})
where: {
orgRootId: requestBody.id,
orgChild1Id: IsNull(),
},
})
: [];
const type1LastPosMasterNo =
requestBody.type == 1
? await this.posMasterRepository.find({
where: {
orgChild1Id: requestBody.id,
orgChild2Id: IsNull(),
},
})
where: {
orgChild1Id: requestBody.id,
orgChild2Id: IsNull(),
},
})
: [];
const type2LastPosMasterNo =
requestBody.type == 2
? await this.posMasterRepository.find({
where: {
orgChild2Id: requestBody.id,
orgChild3Id: IsNull(),
},
})
where: {
orgChild2Id: requestBody.id,
orgChild3Id: IsNull(),
},
})
: [];
const type3LastPosMasterNo =
requestBody.type == 3
? await this.posMasterRepository.find({
where: {
orgChild3Id: requestBody.id,
orgChild4Id: IsNull(),
},
})
where: {
orgChild3Id: requestBody.id,
orgChild4Id: IsNull(),
},
})
: [];
const type4LastPosMasterNo =
requestBody.type == 4
? await this.posMasterRepository.find({
where: {
orgChild4Id: requestBody.id,
},
})
where: {
orgChild4Id: requestBody.id,
},
})
: [];
const allLastPosMasterNo = [
@ -3848,14 +3857,14 @@ export class PositionController extends Controller {
_profile.posMasterNo = getPosMasterNo(dataMaster);
_profile.org = getOrgFullName(dataMaster);
// ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ
if(!dataMaster.isSit){
if (!dataMaster.isSit) {
_profile.position = _position.positionName;
_profile.posTypeId = _position.posTypeId;
_profile.posLevelId = _position.posLevelId;
_profile.positionField = _position.positionField ?? undefined;
_profile.posExecutive = _position.posExecutive?.posExecutiveName ?? undefined;
_profile.positionArea = _position.positionArea ?? undefined;
_profile.positionExecutiveField = _position.positionExecutiveField ?? undefined;
_profile.positionField = _position.positionField ?? _null;
_profile.posExecutive = _position.posExecutive?.posExecutiveName ?? _null;
_profile.positionArea = _position.positionArea ?? _null;
_profile.positionExecutiveField = _position.positionExecutiveField ?? _null;
}
await this.profileRepository.save(_profile);
setLogDataDiff(request, { before, after: _profile });
@ -5193,9 +5202,9 @@ export class PositionController extends Controller {
}
/**
* API
* API
*
* @summary ORG_070 - (ADMIN) #56
* @summary
*
*/
@Post("master/position-condition")
@ -5206,7 +5215,7 @@ export class PositionController extends Controller {
id: string;
revisionId: string;
type: number;
isAll: boolean;
isAll: boolean; // true คือเลือกเฉพาะตำแหน่งติดเงื่อนไข / false คือเลือกตำแหน่งทั้งหมด
page: number;
pageSize: number;
keyword?: string;
@ -5226,7 +5235,7 @@ export class PositionController extends Controller {
let level: any = resolveNodeLevel(orgDna);
const cannotViewRootPosMaster =
(_data.privilege === "PARENT") ||
_data.privilege === "PARENT" ||
(_data.privilege === "BROTHER" && level > 1) ||
(_data.privilege === "CHILD" && level > 0) ||
(_data.privilege === "NORMAL" && level != 0);
@ -5258,46 +5267,46 @@ export class PositionController extends Controller {
typeCondition = {
...(cannotViewRootPosMaster ? { orgRootId: null } : { orgRootId: body.id }),
};
if (!body.isAll) {
checkChildConditions = {
orgChild1Id: IsNull(),
};
searchShortName = `CONCAT(orgRoot.orgRootShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`;
} else {
}
// if (!body.isAll) {
// checkChildConditions = {
// orgChild1Id: IsNull(),
// };
// searchShortName = `CONCAT(orgRoot.orgRootShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`;
// } else {
// }
} else if (body.type === 1) {
typeCondition = {
...(cannotViewChild1PosMaster ? { orgChild1Id: null } : { orgChild1Id: body.id }),
};
if (!body.isAll) {
checkChildConditions = {
orgChild2Id: IsNull(),
};
searchShortName = `CONCAT(orgChild1.orgChild1ShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`;
} else {
}
// if (!body.isAll) {
// checkChildConditions = {
// orgChild2Id: IsNull(),
// };
// searchShortName = `CONCAT(orgChild1.orgChild1ShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`;
// } else {
// }
} else if (body.type === 2) {
typeCondition = {
...(cannotViewChild2PosMaster ? { orgChild2Id: null } : { orgChild2Id: body.id }),
};
if (!body.isAll) {
checkChildConditions = {
orgChild3Id: IsNull(),
};
searchShortName = `CONCAT(orgChild2.orgChild2ShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`;
} else {
}
// if (!body.isAll) {
// checkChildConditions = {
// orgChild3Id: IsNull(),
// };
// searchShortName = `CONCAT(orgChild2.orgChild2ShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`;
// } else {
// }
} else if (body.type === 3) {
typeCondition = {
...(cannotViewChild3PosMaster ? { orgChild3Id: null } : { orgChild3Id: body.id }),
};
if (!body.isAll) {
checkChildConditions = {
orgChild4Id: IsNull(),
};
searchShortName = `CONCAT(orgChild3.orgChild3ShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`;
} else {
}
// if (!body.isAll) {
// checkChildConditions = {
// orgChild4Id: IsNull(),
// };
// searchShortName = `CONCAT(orgChild3.orgChild3ShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`;
// } else {
// }
} else if (body.type === 4) {
typeCondition = {
...(cannotViewChild4PosMaster ? { orgChild4Id: null } : { orgChild4Id: body.id }),
@ -5370,7 +5379,7 @@ export class PositionController extends Controller {
(masterId.length > 0
? { id: In(masterId) }
: { posMasterNo: Like(`%${body.keyword}%`) })),
current_holderId: IsNull(),
...(!body.isAll && { isCondition: true }),
},
];
let [posMaster, total] = await AppDataSource.getRepository(PosMaster)
@ -5439,15 +5448,15 @@ export class PositionController extends Controller {
new Brackets((qb) => {
qb.andWhere(
body.keyword != null && body.keyword != ""
? body.isAll == false
? searchShortName
: `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'`
? `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'`
: "1=1",
)
.andWhere(checkChildConditions)
.andWhere(typeCondition)
.andWhere(revisionCondition)
.andWhere({ current_holderId: IsNull() });
.andWhere(revisionCondition);
if (!body.isAll) {
qb.andWhere({ isCondition: true });
}
}),
)
.orWhere(
@ -5457,8 +5466,10 @@ export class PositionController extends Controller {
)
.andWhere(checkChildConditions)
.andWhere(typeCondition)
.andWhere(revisionCondition)
.andWhere({ current_holderId: IsNull() });
.andWhere(revisionCondition);
if (!body.isAll) {
qb.andWhere({ isCondition: true });
}
}),
)
.orderBy("orgRoot.orgRootOrder", "ASC")

View file

@ -6,8 +6,6 @@ import HttpError from "../interfaces/http-error";
import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
import { ProfileGovernment, UpdateProfileGovernment } from "../entities/ProfileGovernment";
import { Position } from "../entities/Position";
import { PosMaster } from "../entities/PosMaster";
import {
calculateAge,
calculateGovAge,
@ -15,7 +13,6 @@ import {
setLogDataDiff,
} from "../interfaces/utils";
import permission from "../interfaces/permission";
import { OrgRevision } from "../entities/OrgRevision";
import { In } from "typeorm";
@Route("api/v1/org/profile/government")
@Tags("ProfileGovernment")
@ -23,9 +20,6 @@ import { In } from "typeorm";
export class ProfileGovernmentHistoryController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
private govRepo = AppDataSource.getRepository(ProfileGovernment);
private positionRepo = AppDataSource.getRepository(Position);
private posMasterRepo = AppDataSource.getRepository(PosMaster);
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
/**
*
* @summary
@ -33,13 +27,6 @@ export class ProfileGovernmentHistoryController extends Controller {
*/
@Get("user")
public async getGovHistoryUser(@Request() request: { user: Record<string, any> }) {
const orgRevision = await this.orgRevisionRepository.findOne({
select: ["id"],
where: {
orgRevisionIsDraft: false,
orgRevisionIsCurrent: true,
},
});
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
@ -51,79 +38,19 @@ export class ProfileGovernmentHistoryController extends Controller {
posLevel: true,
},
});
const posMaster = await this.posMasterRepo.findOne({
where: {
// orgRevision: {
// orgRevisionIsCurrent: true,
// orgRevisionIsDraft: false,
// },
orgRevisionId: orgRevision?.id,
current_holderId: profile.id,
},
order: { createdAt: "DESC" },
relations: {
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
const position = await this.positionRepo.findOne({
where: {
positionIsSelected: true,
posMaster: {
// orgRevision: {
// orgRevisionIsCurrent: true,
// orgRevisionIsDraft: false,
// },
orgRevisionId: orgRevision?.id,
current_holderId: profile.id,
},
},
order: { createdAt: "DESC" },
relations: {
posExecutive: true,
},
});
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const fullNameParts = [
posMaster == null || posMaster.orgChild4 == null ? null : posMaster.orgChild4.orgChild4Name,
posMaster == null || posMaster.orgChild3 == null ? null : posMaster.orgChild3.orgChild3Name,
posMaster == null || posMaster.orgChild2 == null ? null : posMaster.orgChild2.orgChild2Name,
posMaster == null || posMaster.orgChild1 == null ? null : posMaster.orgChild1.orgChild1Name,
posMaster == null || posMaster.orgRoot == null ? null : posMaster.orgRoot.orgRootName,
];
const org = fullNameParts.filter((part) => part !== undefined && part !== null).join("\n");
let orgShortName = "";
if (posMaster != null) {
if (posMaster.orgChild1Id === null) {
orgShortName = posMaster.orgRoot?.orgRootShortName;
} else if (posMaster.orgChild2Id === null) {
orgShortName = posMaster.orgChild1?.orgChild1ShortName;
} else if (posMaster.orgChild3Id === null) {
orgShortName = posMaster.orgChild2?.orgChild2ShortName;
} else if (posMaster.orgChild4Id === null) {
orgShortName = posMaster.orgChild3?.orgChild3ShortName;
} else {
orgShortName = posMaster.orgChild4?.orgChild4ShortName;
}
}
//posMaster?.isSit แก้ไขชั่วคราว
// ดึงข้อมูลจาก profile ที่เก็บไว้แล้ว
const data = {
org: org, //สังกัด
positionField: position == null || posMaster?.isSit ? null : position.positionField, //สายงาน
org: record.org ?? null, //สังกัด
positionField: record.positionField ?? null, //สายงาน
position: record.position, //ตำแหน่ง
posLevel: record.posLevel == null ? null : record.posLevel.posLevelName, //ระดับ
posMasterNo: posMaster == null ? null : `${orgShortName} ${posMaster.posMasterNo}`, //เลขที่ตำแหน่ง
posMasterNo: record.posMasterNo ?? null, //เลขที่ตำแหน่ง
posType: record.posType == null ? null : record.posType.posTypeName, //ประเภท
posExecutive:
position == null || position.posExecutive == null || posMaster?.isSit
? null
: position.posExecutive.posExecutiveName, //ตำแหน่งทางการบริหาร
positionArea: position == null || posMaster?.isSit ? null : position.positionArea, //ด้าน/สาขา
positionExecutiveField: position == null || posMaster?.isSit ? null : position.positionExecutiveField, //ด้านทางการบริหาร
posExecutive: record.posExecutive ?? null, //ตำแหน่งทางการบริหาร
positionArea: record.positionArea ?? null, //ด้าน/สาขา
positionExecutiveField: record.positionExecutiveField ?? null, //ด้านทางการบริหาร
dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate),
dateRetireLaw: record.dateRetireLaw ?? null,
// govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
@ -135,10 +62,10 @@ export class ProfileGovernmentHistoryController extends Controller {
govAgePlus: record.govAgePlus,
reasonSameDate: record.reasonSameDate,
};
return new HttpSuccess(data);
}
/**
*
* @summary
@ -150,25 +77,17 @@ export class ProfileGovernmentHistoryController extends Controller {
let _workflow = await new permission().Workflow(req, profileId, "SYS_REGISTRY_OFFICER");
if (_workflow == false)
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
const orgRevision = await this.orgRevisionRepository.findOne({
select: ["id"],
where: {
orgRevisionIsDraft: false,
orgRevisionIsCurrent: true,
},
});
// ค้นหา profile ก่อน
const record = await this.profileRepo.findOne({
where: { id: profileId },
relations: ["posType", "posLevel"],
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล profile");
}
// ค้นหา profileSalary แยกต่างหาก
const profileWithSalary = await this.profileRepo.findOne({
where: {
@ -201,70 +120,13 @@ export class ProfileGovernmentHistoryController extends Controller {
},
},
});
// ใช้ profileSalary จาก query ที่สอง หรือ [] ถ้าไม่เจอ
record.profileSalary = profileWithSalary?.profileSalary || [];
const posMaster = await this.posMasterRepo.findOne({
where: {
orgRevisionId: orgRevision?.id,
current_holderId: profileId,
},
order: { createdAt: "DESC" },
relations: {
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
const position = await this.positionRepo.findOne({
where: {
positionIsSelected: true,
posMaster: {
orgRevisionId: orgRevision?.id,
current_holderId: profileId,
},
},
order: { createdAt: "DESC" },
relations: {
posExecutive: true,
},
});
// if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const fullNameParts = [
posMaster == null || posMaster.orgChild4 == null ? null : posMaster.orgChild4.orgChild4Name,
posMaster == null || posMaster.orgChild3 == null ? null : posMaster.orgChild3.orgChild3Name,
posMaster == null || posMaster.orgChild2 == null ? null : posMaster.orgChild2.orgChild2Name,
posMaster == null || posMaster.orgChild1 == null ? null : posMaster.orgChild1.orgChild1Name,
posMaster == null || posMaster.orgRoot == null ? null : posMaster.orgRoot.orgRootName,
];
const org = fullNameParts.filter((part) => part !== undefined && part !== null).join("\n");
let orgShortName = "";
if (posMaster != null) {
if (posMaster.orgChild1Id === null) {
orgShortName = posMaster.orgRoot?.orgRootShortName ?? "";
} else if (posMaster.orgChild2Id === null) {
orgShortName = posMaster.orgChild1?.orgChild1ShortName ?? "";
} else if (posMaster.orgChild3Id === null) {
orgShortName = posMaster.orgChild2?.orgChild2ShortName ?? "";
} else if (posMaster.orgChild4Id === null) {
orgShortName = posMaster.orgChild3?.orgChild3ShortName ?? "";
} else {
orgShortName = posMaster.orgChild4?.orgChild4ShortName ?? "";
}
}
let _OrgLeave: any = [];
let _profileSalary: any = null;
if (record?.isLeave && record?.profileSalary.length > 0) {
// _OrgLeave = [
// record?.profileSalary[0].orgChild4 ? record?.profileSalary[0].orgChild4 : null,
// record?.profileSalary[0].orgChild3 ? record?.profileSalary[0].orgChild3 : null,
// record?.profileSalary[0].orgChild2 ? record?.profileSalary[0].orgChild2 : null,
// record?.profileSalary[0].orgChild1 ? record?.profileSalary[0].orgChild1 : null,
// record?.profileSalary[0].orgRoot ? record?.profileSalary[0].orgRoot : null,
// ];
if (record.leaveType == "RETIRE") {
_profileSalary =
record?.profileSalary.length > 1
@ -288,27 +150,23 @@ export class ProfileGovernmentHistoryController extends Controller {
}
}
const orgLeave = _OrgLeave.filter((x: any) => x !== undefined && x !== null).join("\n");
//posMaster?.isSit แก้ไขชั่วคราว
// ดึงข้อมูลจาก profile ที่เก็บไว้แล้ว
const data = {
org: record?.isLeave == false ? org : orgLeave, //สังกัด
positionField: position == null || posMaster?.isSit ? null : position.positionField, //สายงาน
org: record?.isLeave == false ? (record.org ?? null) : orgLeave, //สังกัด
positionField: record.positionField ?? null, //สายงาน
position: record?.position, //ตำแหน่ง
posLevel: record?.posLevel == null ? null : record?.posLevel.posLevelName, //ระดับ
posMasterNo:
record?.isLeave == false
? posMaster == null
? null
: `${orgShortName} ${posMaster.posMasterNo}`
? record.posMasterNo ?? null
: _profileSalary != null
? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}`
: null, //เลขที่ตำแหน่ง
posType: record?.posType == null ? null : record?.posType.posTypeName, //ประเภท
posExecutive:
position == null || position.posExecutive == null || posMaster?.isSit
? null
: position.posExecutive.posExecutiveName, //ตำแหน่งทางการบริหาร
positionArea: position == null || posMaster?.isSit ? null : position.positionArea, //ด้าน/สาขา
positionExecutiveField: position == null || posMaster?.isSit ? null : position.positionExecutiveField, //ด้านทางการบริหาร
posExecutive: record.posExecutive ?? null, //ตำแหน่งทางการบริหาร
positionArea: record.positionArea ?? null, //ด้าน/สาขา
positionExecutiveField: record.positionExecutiveField ?? null, //ด้านทางการบริหาร
dateLeave: record?.birthDate == null ? null : calculateRetireDate(record?.birthDate),
dateRetireLaw: record?.dateRetireLaw ?? null,
// govAge: record?.dateStart == null ? null : calculateAge(record?.dateStart),
@ -320,30 +178,22 @@ export class ProfileGovernmentHistoryController extends Controller {
govAgePlus: record?.govAgePlus,
reasonSameDate: record?.reasonSameDate,
};
return new HttpSuccess(data);
}
@Get("admin/{profileId}")
public async getGovHistoryAdmin(@Path() profileId: string) {
const orgRevision = await this.orgRevisionRepository.findOne({
select: ["id"],
where: {
orgRevisionIsDraft: false,
orgRevisionIsCurrent: true,
},
});
// ค้นหา profile ก่อน
const record = await this.profileRepo.findOne({
where: { id: profileId },
relations: ["posType", "posLevel"],
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล profile");
}
// ค้นหา profileSalary แยกต่างหาก
const profileWithSalary = await this.profileRepo.findOne({
where: {
@ -376,70 +226,13 @@ export class ProfileGovernmentHistoryController extends Controller {
},
},
});
// ใช้ profileSalary จาก query ที่สอง หรือ [] ถ้าไม่เจอ
record.profileSalary = profileWithSalary?.profileSalary || [];
const posMaster = await this.posMasterRepo.findOne({
where: {
orgRevisionId: orgRevision?.id,
current_holderId: profileId,
},
order: { createdAt: "DESC" },
relations: {
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
const position = await this.positionRepo.findOne({
where: {
positionIsSelected: true,
posMaster: {
orgRevisionId: orgRevision?.id,
current_holderId: profileId,
},
},
order: { createdAt: "DESC" },
relations: {
posExecutive: true,
},
});
// if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const fullNameParts = [
posMaster == null || posMaster.orgChild4 == null ? null : posMaster.orgChild4.orgChild4Name,
posMaster == null || posMaster.orgChild3 == null ? null : posMaster.orgChild3.orgChild3Name,
posMaster == null || posMaster.orgChild2 == null ? null : posMaster.orgChild2.orgChild2Name,
posMaster == null || posMaster.orgChild1 == null ? null : posMaster.orgChild1.orgChild1Name,
posMaster == null || posMaster.orgRoot == null ? null : posMaster.orgRoot.orgRootName,
];
const org = fullNameParts.filter((part) => part !== undefined && part !== null).join("\n");
let orgShortName = "";
if (posMaster != null) {
if (posMaster.orgChild1Id === null) {
orgShortName = posMaster.orgRoot?.orgRootShortName;
} else if (posMaster.orgChild2Id === null) {
orgShortName = posMaster.orgChild1?.orgChild1ShortName;
} else if (posMaster.orgChild3Id === null) {
orgShortName = posMaster.orgChild2?.orgChild2ShortName;
} else if (posMaster.orgChild4Id === null) {
orgShortName = posMaster.orgChild3?.orgChild3ShortName;
} else {
orgShortName = posMaster.orgChild4?.orgChild4ShortName;
}
}
let _OrgLeave: any = [];
let _profileSalary: any = null;
if (record?.isLeave && record?.profileSalary.length > 0) {
// _OrgLeave = [
// record?.profileSalary[0].orgChild4 ? record?.profileSalary[0].orgChild4 : null,
// record?.profileSalary[0].orgChild3 ? record?.profileSalary[0].orgChild3 : null,
// record?.profileSalary[0].orgChild2 ? record?.profileSalary[0].orgChild2 : null,
// record?.profileSalary[0].orgChild1 ? record?.profileSalary[0].orgChild1 : null,
// record?.profileSalary[0].orgRoot ? record?.profileSalary[0].orgRoot : null,
// ];
if (record.leaveType == "RETIRE") {
_profileSalary =
record?.profileSalary.length > 1
@ -463,27 +256,23 @@ export class ProfileGovernmentHistoryController extends Controller {
}
}
const orgLeave = _OrgLeave.filter((x: any) => x !== undefined && x !== null).join("\n");
//posMaster?.isSit แก้ไขชั่วคราว
// ดึงข้อมูลจาก profile ที่เก็บไว้แล้ว
const data = {
org: record?.isLeave == false ? org : orgLeave, //สังกัด
positionField: position == null || posMaster?.isSit ? null : position.positionField, //สายงาน
org: record?.isLeave == false ? (record.org ?? null) : orgLeave, //สังกัด
positionField: record.positionField ?? null, //สายงาน
position: record?.position, //ตำแหน่ง
posLevel: record?.posLevel == null ? null : record?.posLevel.posLevelName, //ระดับ
posMasterNo:
record?.isLeave == false
? posMaster == null
? null
: `${orgShortName} ${posMaster.posMasterNo}`
? record.posMasterNo ?? null
: _profileSalary != null
? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}`
: null, //เลขที่ตำแหน่ง
posType: record?.posType == null ? null : record?.posType.posTypeName, //ประเภท
posExecutive:
position == null || position.posExecutive == null || posMaster?.isSit
? null
: position.posExecutive.posExecutiveName, //ตำแหน่งทางการบริหาร
positionArea: position == null || posMaster?.isSit ? null : position.positionArea, //ด้าน/สาขา
positionExecutiveField: position == null || posMaster?.isSit ? null : position.positionExecutiveField, //ด้านทางการบริหาร
posExecutive: record.posExecutive ?? null, //ตำแหน่งทางการบริหาร
positionArea: record.positionArea ?? null, //ด้าน/สาขา
positionExecutiveField: record.positionExecutiveField ?? null, //ด้านทางการบริหาร
dateLeave: record?.birthDate == null ? null : calculateRetireDate(record?.birthDate),
dateRetireLaw: record?.dateRetireLaw ?? null,
// govAge: record?.dateStart == null ? null : calculateAge(record?.dateStart),
@ -496,10 +285,10 @@ export class ProfileGovernmentHistoryController extends Controller {
reasonSameDate: record?.reasonSameDate,
isLeave: record?.isLeave,
};
return new HttpSuccess(data);
}
/**
*
* @summary by keycloak
@ -517,7 +306,7 @@ export class ProfileGovernmentHistoryController extends Controller {
});
return new HttpSuccess(record);
}
/**
*
* @summary
@ -533,12 +322,12 @@ export class ProfileGovernmentHistoryController extends Controller {
order: { lastUpdatedAt: "DESC" },
where: { profileId: profileId },
});
// record.pop();
return new HttpSuccess(record);
}
/**
*
* @summary
@ -554,14 +343,14 @@ export class ProfileGovernmentHistoryController extends Controller {
const record = await this.profileRepo.findOne({
where: { id: profileId },
});
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const before = structuredClone(record);
const history = new ProfileGovernment();
Object.assign(record, body);
Object.assign(history, { ...record, id: undefined });
history.profileId = profileId;
record.lastUpdateUserId = req.user.sub;
record.lastUpdateFullName = req.user.name;
@ -572,13 +361,14 @@ export class ProfileGovernmentHistoryController extends Controller {
history.createdFullName = req.user.name;
history.createdAt = new Date();
history.lastUpdatedAt = new Date();
await Promise.all([
this.profileRepo.save(record, { data: req }),
setLogDataDiff(req, { before, after: record }),
this.govRepo.save(history, { data: req }),
]);
return new HttpSuccess();
}
}