#231 และ #2438 checkpoint

This commit is contained in:
adisak 2026-04-21 17:37:17 +07:00
parent 7e3982a96d
commit 194d79bf04
2 changed files with 203 additions and 259 deletions

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();
}
}