Linear Flow (PlacementService) #224
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m10s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m10s
This commit is contained in:
parent
2f17c10050
commit
616ccf9e64
6 changed files with 1160 additions and 777 deletions
|
|
@ -104,7 +104,10 @@ import { LeaveType } from "../entities/LeaveType";
|
|||
import { KeycloakAttributeService } from "../services/KeycloakAttributeService";
|
||||
import { reOrderCommandRecivesAndDelete } from "../services/CommandService";
|
||||
import { RetirementService } from "../services/RetirementService";
|
||||
import { OfficerProfileService } from "../services/OfficerProfileService";
|
||||
import { ExecuteOfficerProfileService } from "../services/ExecuteOfficerProfileService";
|
||||
import { ExecuteSalaryService } from "../services/ExecuteSalaryService";
|
||||
import { ExecuteSalaryCurrentService } from "../services/ExecuteSalaryCurrentService";
|
||||
import { ExecuteSalaryEmployeeCurrentService } from "../services/ExecuteSalaryEmployeeCurrentService";
|
||||
import { promisify } from "util";
|
||||
const REDIS_HOST = process.env.REDIS_HOST;
|
||||
const REDIS_PORT = process.env.REDIS_PORT;
|
||||
|
|
@ -3648,6 +3651,14 @@ export class CommandController extends Controller {
|
|||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API สร้าง ProfileSalary ข้าราชการ + อัปเดตตำแหน่งปัจจุบัน (เปลี่ยนตำแหน่ง)
|
||||
*
|
||||
* Thin wrapper — เรียก ExecuteSalaryCurrentService (C-PM-03, 04, 05, 06, 07, 39, 47)
|
||||
* ทั้ง endpoint นี้และ consumer ใน rabbitmq ใช้ service ตัวเดียวกัน (Linear Flow)
|
||||
*
|
||||
* @summary API สร้าง ProfileSalary ข้าราชการ + เปลี่ยนตำแหน่ง
|
||||
*/
|
||||
@Post("excexute/salary-current")
|
||||
public async newSalaryAndUpdateCurrent(
|
||||
@Request() req: RequestWithUser,
|
||||
|
|
@ -3689,326 +3700,10 @@ export class CommandController extends Controller {
|
|||
}[];
|
||||
},
|
||||
) {
|
||||
let _posNumCodeSit: string = "";
|
||||
let _posNumCodeSitAbb: string = "";
|
||||
const _command = await this.commandRepository.findOne({
|
||||
where: { id: body.data.find((x) => x.commandId)?.commandId ?? "" },
|
||||
await new ExecuteSalaryCurrentService().executeSalaryCurrent(body.data, {
|
||||
user: { sub: req.user.sub, name: req.user.name },
|
||||
req,
|
||||
});
|
||||
if (_command) {
|
||||
if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
|
||||
const orgRootDeputy = await this.orgRootRepository.findOne({
|
||||
where: {
|
||||
isDeputy: true,
|
||||
orgRevision: {
|
||||
orgRevisionIsCurrent: true,
|
||||
orgRevisionIsDraft: false,
|
||||
},
|
||||
},
|
||||
relations: ["orgRevision"],
|
||||
});
|
||||
_posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
|
||||
_posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป.";
|
||||
} else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
|
||||
_posNumCodeSit = "กรุงเทพมหานคร";
|
||||
_posNumCodeSitAbb = "กทม.";
|
||||
} else {
|
||||
let _profileAdmin = await this.profileRepository.findOne({
|
||||
where: {
|
||||
keycloak: _command?.createdUserId.toString(),
|
||||
current_holders: {
|
||||
orgRevision: {
|
||||
orgRevisionIsCurrent: true,
|
||||
orgRevisionIsDraft: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
|
||||
});
|
||||
_posNumCodeSit =
|
||||
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
|
||||
"";
|
||||
_posNumCodeSitAbb =
|
||||
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
|
||||
.orgRootShortName ?? "";
|
||||
}
|
||||
}
|
||||
await Promise.all(
|
||||
body.data.map(async (item) => {
|
||||
const profile: any = await this.profileRepository.findOneBy({ id: item.profileId });
|
||||
if (!profile) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้");
|
||||
}
|
||||
let _null: any = null;
|
||||
const dest_item = await this.salaryRepo.findOne({
|
||||
where: { profileId: item.profileId },
|
||||
order: { order: "DESC" },
|
||||
});
|
||||
const before = null;
|
||||
const data = new ProfileSalary();
|
||||
|
||||
const meta = {
|
||||
order: dest_item == null ? 1 : dest_item.order + 1,
|
||||
createdUserId: req.user.sub,
|
||||
createdFullName: req.user.name,
|
||||
lastUpdateUserId: req.user.sub,
|
||||
lastUpdateFullName: req.user.name,
|
||||
createdAt: new Date(),
|
||||
lastUpdatedAt: new Date(),
|
||||
};
|
||||
data.posNumCodeSit = _posNumCodeSit;
|
||||
data.posNumCodeSitAbb = _posNumCodeSitAbb;
|
||||
Object.assign(data, { ...item, ...meta });
|
||||
const history = new ProfileSalaryHistory();
|
||||
Object.assign(history, { ...data, id: undefined });
|
||||
await this.salaryRepo.save(data, { data: req });
|
||||
setLogDataDiff(req, { before, after: data });
|
||||
history.commandId = item.commandId ?? _null;
|
||||
history.profileSalaryId = data.id;
|
||||
await this.salaryHistoryRepo.save(history, { data: req });
|
||||
|
||||
// STEP 1: หา posMaster ที่จะใช้งานตาม id ที่ส่งมา
|
||||
let posMaster = await this.posMasterRepository.findOne({
|
||||
where: { id: item.posmasterId },
|
||||
relations: {
|
||||
orgRevision: true,
|
||||
orgRoot: true,
|
||||
orgChild1: true,
|
||||
orgChild2: true,
|
||||
orgChild3: true,
|
||||
orgChild4: true,
|
||||
},
|
||||
});
|
||||
|
||||
// เช็คว่า posMaster ที่หามาอยู่ในโครงสร้างปัจจุบันหรือไม่
|
||||
const isCurrent =
|
||||
posMaster?.orgRevision?.orgRevisionIsCurrent === true &&
|
||||
posMaster?.orgRevision?.orgRevisionIsDraft === false;
|
||||
|
||||
// ถ้าไม่อยู่ในโครงสร้างปัจจุบัน ให้หาตัวใหม่จาก ancestorDNA
|
||||
if (!isCurrent && posMaster?.ancestorDNA) {
|
||||
posMaster = await this.posMasterRepository.findOne({
|
||||
where: {
|
||||
ancestorDNA: posMaster.ancestorDNA,
|
||||
orgRevision: {
|
||||
orgRevisionIsCurrent: true,
|
||||
orgRevisionIsDraft: false,
|
||||
},
|
||||
},
|
||||
relations: {
|
||||
orgRevision: true,
|
||||
orgRoot: true,
|
||||
orgChild1: true,
|
||||
orgChild2: true,
|
||||
orgChild3: true,
|
||||
orgChild4: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (posMaster == null) {
|
||||
console.error(
|
||||
`[CommandController] PosMaster not found - posMasterId: ${item.posmasterId}, `
|
||||
);
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
|
||||
}
|
||||
|
||||
const posMasterOld = await this.posMasterRepository.findOne({
|
||||
where: {
|
||||
current_holderId: item.profileId,
|
||||
orgRevisionId: posMaster.orgRevisionId,
|
||||
},
|
||||
});
|
||||
if (posMasterOld != null) {
|
||||
posMasterOld.current_holderId = null;
|
||||
posMasterOld.lastUpdatedAt = new Date();
|
||||
}
|
||||
|
||||
const positionOld = await this.positionRepository.findOne({
|
||||
where: {
|
||||
posMasterId: posMasterOld?.id,
|
||||
positionIsSelected: true,
|
||||
},
|
||||
});
|
||||
if (positionOld != null) {
|
||||
logPositionIsSelectedChange(positionOld.id, positionOld.positionIsSelected, false, {
|
||||
posMasterId: posMasterOld?.id,
|
||||
userId: req.user.sub,
|
||||
endpoint: "updateMaster",
|
||||
action: "command_change_reset_old_position",
|
||||
});
|
||||
|
||||
positionOld.positionIsSelected = false;
|
||||
await this.positionRepository.save(positionOld);
|
||||
}
|
||||
|
||||
const checkPosition = await this.positionRepository.find({
|
||||
where: {
|
||||
posMasterId: posMaster!.id, // ใช้ posMaster ตัวใหม่ (ที่อาจจะเปลี่ยนจาก ancestorDNA)
|
||||
positionIsSelected: true,
|
||||
},
|
||||
});
|
||||
if (checkPosition.length > 0) {
|
||||
console.log(
|
||||
`[positionIsSelected-DEBUG] Command change: clearing ${checkPosition.length} positions (posMasterId: ${posMaster!.id}, userId: ${req.user.sub}, endpoint: updateMaster)`
|
||||
);
|
||||
|
||||
const clearPosition = checkPosition.map((positions) => {
|
||||
logPositionIsSelectedChange(positions.id, positions.positionIsSelected, false, {
|
||||
posMasterId: posMaster!.id,
|
||||
userId: req.user.sub,
|
||||
endpoint: "updateMaster",
|
||||
action: "command_change_clear_positions",
|
||||
});
|
||||
|
||||
return {
|
||||
...positions,
|
||||
positionIsSelected: false,
|
||||
};
|
||||
});
|
||||
await this.positionRepository.save(clearPosition);
|
||||
}
|
||||
|
||||
posMaster.current_holderId = item.profileId;
|
||||
posMaster.lastUpdatedAt = new Date();
|
||||
// posMaster.conditionReason = _null;
|
||||
// posMaster.isCondition = false;
|
||||
if (posMasterOld != null) {
|
||||
await this.posMasterRepository.save(posMasterOld);
|
||||
await CreatePosMasterHistoryOfficer(posMasterOld.id, req);
|
||||
}
|
||||
await this.posMasterRepository.save(posMaster);
|
||||
|
||||
// STEP 2: กำหนด position ใหม่
|
||||
// Match position ตามลำดับ priority:
|
||||
// Condition 1: match จาก positionId
|
||||
// Condition 2: match 7 ฟิลด์ (positionName, posTypeId, posLevelId, positionField, positionArea, positionExecutiveField, posExecutiveId)
|
||||
// Condition 3: match 3 ฟิลด์ (positionName, posTypeId, posLevelId)
|
||||
// Fallback: เลือก position แรกใน posMaster
|
||||
|
||||
let positionNew: Position | null = null;
|
||||
|
||||
// Resolve ID: ใช้ positionTypeId/positionLevelId ก่อน ถ้าไม่มี fallback เป็น positionType/positionLevel
|
||||
const posTypeId = item.positionTypeId || item.positionType;
|
||||
const posLevelId = item.positionLevelId || item.positionLevel;
|
||||
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
// CONDITION 1: เช็คจาก positionId ตรง
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
if (item.positionId) {
|
||||
const positionById = await this.positionRepository.findOne({
|
||||
where: {
|
||||
id: item.positionId,
|
||||
posMasterId: posMaster.id, // ต้องอยู่ใน posMaster ที่ถูกต้อง
|
||||
},
|
||||
relations: ["posExecutive"],
|
||||
});
|
||||
|
||||
if (positionById) {
|
||||
positionNew = positionById;
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
// CONDITION 2: Match 7 ฟิลด์ (ถ้า Condition 1 ไม่ match)
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
if (!positionNew && item.positionName && posTypeId && posLevelId) {
|
||||
// สร้าง where clause แบบ dynamic - ใส่เฉพาะฟิลด์ที่มีค่า
|
||||
const whereCondition: any = {
|
||||
posMasterId: posMaster.id,
|
||||
positionName: item.positionName,
|
||||
posTypeId: posTypeId,
|
||||
posLevelId: posLevelId,
|
||||
};
|
||||
|
||||
// เพิ่มเฉพาะฟิลด์ที่มีค่า (ไม่ใช่ null, undefined, หรือ string ว่าง)
|
||||
if (item.positionField) {
|
||||
whereCondition.positionField = item.positionField;
|
||||
}
|
||||
if (item.posExecutiveId) {
|
||||
whereCondition.posExecutiveId = item.posExecutiveId;
|
||||
}
|
||||
if (item.positionExecutiveField) {
|
||||
whereCondition.positionExecutiveField = item.positionExecutiveField;
|
||||
}
|
||||
if (item.positionArea) {
|
||||
whereCondition.positionArea = item.positionArea;
|
||||
}
|
||||
|
||||
const positionBy7Fields = await this.positionRepository.findOne({
|
||||
where: whereCondition,
|
||||
relations: ["posExecutive"],
|
||||
order: { orderNo: "ASC" }
|
||||
});
|
||||
|
||||
if (positionBy7Fields) {
|
||||
positionNew = positionBy7Fields;
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
// CONDITION 3: Match 3 ฟิลด์ (ถ้า Condition 2 ไม่ match)
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
if (!positionNew && item.positionName && posTypeId && posLevelId) {
|
||||
const positionBy3Fields = await this.positionRepository.findOne({
|
||||
where: {
|
||||
posMasterId: posMaster.id,
|
||||
positionName: item.positionName,
|
||||
posTypeId: posTypeId,
|
||||
posLevelId: posLevelId,
|
||||
},
|
||||
relations: ["posExecutive"],
|
||||
order: { orderNo: "ASC" }
|
||||
});
|
||||
|
||||
if (positionBy3Fields) {
|
||||
positionNew = positionBy3Fields;
|
||||
}
|
||||
}
|
||||
|
||||
// // ═══════════════════════════════════════════════════════════
|
||||
// // FALLBACK: ถ้าทั้ง 3 ไม่ match ให้เลือก position แรกใน posMaster
|
||||
// // ═══════════════════════════════════════════════════════════
|
||||
// if (!positionNew) {
|
||||
// const fallbackPositions = await this.positionRepository.find({
|
||||
// where: {
|
||||
// posMasterId: posMaster.id,
|
||||
// },
|
||||
// relations: ["posExecutive"],
|
||||
// order: {
|
||||
// orderNo: "ASC",
|
||||
// },
|
||||
// take: 1,
|
||||
// });
|
||||
|
||||
// if (fallbackPositions.length > 0) {
|
||||
// positionNew = fallbackPositions[0];
|
||||
// }
|
||||
// }
|
||||
|
||||
// ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ
|
||||
if (positionNew != null) {
|
||||
positionNew.positionIsSelected = true;
|
||||
// อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit
|
||||
profile.posMasterNo = getPosMasterNo(posMaster);
|
||||
profile.org = getOrgFullName(posMaster);
|
||||
if (!posMaster.isSit) {
|
||||
profile.posLevelId = positionNew.posLevelId;
|
||||
profile.posTypeId = positionNew.posTypeId;
|
||||
profile.position = positionNew.positionName;
|
||||
profile.positionField = positionNew.positionField ?? null;
|
||||
profile.posExecutive = positionNew.posExecutive?.posExecutiveName ?? null;
|
||||
profile.positionArea = positionNew.positionArea ?? null;
|
||||
profile.positionExecutiveField = positionNew.positionExecutiveField ?? null;
|
||||
}
|
||||
profile.amount = item.amount ?? null;
|
||||
profile.amountSpecial = item.amountSpecial ?? null;
|
||||
await this.profileRepository.save(profile);
|
||||
await this.positionRepository.save(positionNew);
|
||||
}
|
||||
await CreatePosMasterHistoryOfficer(posMaster.id, req);
|
||||
}),
|
||||
);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
|
|
@ -4016,7 +3711,7 @@ export class CommandController extends Controller {
|
|||
public async newSalaryEmployeeAndUpdateCurrent(
|
||||
@Request() req: RequestWithUser,
|
||||
@Body()
|
||||
body: {
|
||||
body: {
|
||||
data: {
|
||||
profileId: string;
|
||||
amount?: Double | null;
|
||||
|
|
@ -4046,162 +3741,10 @@ export class CommandController extends Controller {
|
|||
}[];
|
||||
},
|
||||
) {
|
||||
let _posNumCodeSit: string = "";
|
||||
let _posNumCodeSitAbb: string = "";
|
||||
const _command = await this.commandRepository.findOne({
|
||||
where: { id: body.data.find((x) => x.commandId)?.commandId ?? "" },
|
||||
await new ExecuteSalaryEmployeeCurrentService().executeSalaryEmployeeCurrent(body.data, {
|
||||
user: { sub: req.user.sub, name: req.user.name },
|
||||
req,
|
||||
});
|
||||
if (_command) {
|
||||
if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
|
||||
const orgRootDeputy = await this.orgRootRepository.findOne({
|
||||
where: {
|
||||
isDeputy: true,
|
||||
orgRevision: {
|
||||
orgRevisionIsCurrent: true,
|
||||
orgRevisionIsDraft: false,
|
||||
},
|
||||
},
|
||||
relations: ["orgRevision"],
|
||||
});
|
||||
_posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
|
||||
_posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป.";
|
||||
} else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
|
||||
_posNumCodeSit = "กรุงเทพมหานคร";
|
||||
_posNumCodeSitAbb = "กทม.";
|
||||
} else {
|
||||
let _profileAdmin = await this.profileRepository.findOne({
|
||||
where: {
|
||||
keycloak: _command?.createdUserId.toString(),
|
||||
current_holders: {
|
||||
orgRevision: {
|
||||
orgRevisionIsCurrent: true,
|
||||
orgRevisionIsDraft: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
|
||||
});
|
||||
_posNumCodeSit =
|
||||
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
|
||||
"";
|
||||
_posNumCodeSitAbb =
|
||||
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
|
||||
.orgRootShortName ?? "";
|
||||
}
|
||||
}
|
||||
await Promise.all(
|
||||
body.data.map(async (item) => {
|
||||
const profile: any = await this.profileEmployeeRepository.findOneBy({ id: item.profileId });
|
||||
if (!profile) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
||||
}
|
||||
|
||||
const dest_item = await this.salaryRepo.findOne({
|
||||
where: { profileEmployeeId: item.profileId },
|
||||
order: { order: "DESC" },
|
||||
});
|
||||
const before = null;
|
||||
const data = new ProfileSalary();
|
||||
data.posNumCodeSit = _posNumCodeSit;
|
||||
data.posNumCodeSitAbb = _posNumCodeSitAbb;
|
||||
const meta = {
|
||||
order: dest_item == null ? 1 : dest_item.order + 1,
|
||||
createdUserId: req.user.sub,
|
||||
createdFullName: req.user.name,
|
||||
lastUpdateUserId: req.user.sub,
|
||||
lastUpdateFullName: req.user.name,
|
||||
createdAt: new Date(),
|
||||
lastUpdatedAt: new Date(),
|
||||
};
|
||||
|
||||
Object.assign(data, {
|
||||
...item,
|
||||
...meta,
|
||||
profileEmployeeId: item.profileId,
|
||||
profileId: undefined,
|
||||
});
|
||||
const history = new ProfileSalaryHistory();
|
||||
Object.assign(history, { ...data, id: undefined });
|
||||
|
||||
await this.salaryRepo.save(data, { data: req });
|
||||
setLogDataDiff(req, { before, after: data });
|
||||
history.profileSalaryId = data.id;
|
||||
await this.salaryHistoryRepo.save(history, { data: req });
|
||||
|
||||
const posMaster = await this.employeePosMasterRepository.findOne({
|
||||
where: { id: item.posmasterId },
|
||||
relations: ["orgRoot"],
|
||||
});
|
||||
if (posMaster == null)
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
|
||||
|
||||
const posMasterOld = await this.employeePosMasterRepository.findOne({
|
||||
where: {
|
||||
current_holderId: item.profileId,
|
||||
orgRevisionId: posMaster.orgRevisionId,
|
||||
},
|
||||
});
|
||||
if (posMasterOld != null) {
|
||||
posMasterOld.current_holderId = null;
|
||||
posMasterOld.lastUpdatedAt = new Date();
|
||||
}
|
||||
// if (posMasterOld != null) posMasterOld.next_holderId = null;
|
||||
|
||||
const positionOld = await this.employeePositionRepository.findOne({
|
||||
where: {
|
||||
posMasterId: posMasterOld?.id,
|
||||
positionIsSelected: true,
|
||||
},
|
||||
});
|
||||
if (positionOld != null) {
|
||||
positionOld.positionIsSelected = false;
|
||||
await this.employeePositionRepository.save(positionOld);
|
||||
}
|
||||
|
||||
const checkPosition = await this.employeePositionRepository.find({
|
||||
where: {
|
||||
posMasterId: item.posmasterId,
|
||||
positionIsSelected: true,
|
||||
},
|
||||
});
|
||||
if (checkPosition.length > 0) {
|
||||
const clearPosition = checkPosition.map((positions) => ({
|
||||
...positions,
|
||||
positionIsSelected: false,
|
||||
}));
|
||||
await this.employeePositionRepository.save(clearPosition);
|
||||
}
|
||||
|
||||
posMaster.current_holderId = item.profileId;
|
||||
posMaster.lastUpdatedAt = new Date();
|
||||
posMaster.next_holderId = null;
|
||||
if (posMasterOld != null) {
|
||||
await this.employeePosMasterRepository.save(posMasterOld);
|
||||
await CreatePosMasterHistoryEmployee(posMasterOld.id, req);
|
||||
}
|
||||
await this.employeePosMasterRepository.save(posMaster);
|
||||
const positionNew = await this.employeePositionRepository.findOne({
|
||||
where: {
|
||||
id: item.positionId,
|
||||
posMasterId: item.posmasterId,
|
||||
},
|
||||
});
|
||||
if (positionNew != null) {
|
||||
positionNew.positionIsSelected = true;
|
||||
profile.posLevelId = positionNew.posLevelId;
|
||||
profile.posTypeId = positionNew.posTypeId;
|
||||
profile.position = positionNew.positionName;
|
||||
profile.employeeOc = posMaster?.orgRoot?.orgRootName ?? null;
|
||||
profile.positionEmployeePositionId = positionNew.positionName;
|
||||
profile.amount = item.amount ?? null;
|
||||
profile.amountSpecial = item.amountSpecial ?? null;
|
||||
await this.profileEmployeeRepository.save(profile);
|
||||
await this.employeePositionRepository.save(positionNew);
|
||||
}
|
||||
await CreatePosMasterHistoryEmployee(posMaster.id, req);
|
||||
}),
|
||||
);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
|
|
@ -5003,6 +4546,14 @@ export class CommandController extends Controller {
|
|||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API สร้าง ProfileSalary ข้าราชการ + handle leave/ออกจากราชการ/ช่วยราชการ
|
||||
*
|
||||
* Thin wrapper — เรียก ExecuteSalaryService (C-PM-13, 15, 16)
|
||||
* ทั้ง endpoint นี้และ consumer ใน rabbitmq ใช้ service ตัวเดียวกัน (Linear Flow)
|
||||
*
|
||||
* @summary API สร้าง ProfileSalary ข้าราชการ (leave/โอน/ช่วยราชการ)
|
||||
*/
|
||||
@Post("excexute/salary")
|
||||
public async newSalaryAndUpdate(
|
||||
@Request() req: RequestWithUser,
|
||||
|
|
@ -5045,221 +4596,10 @@ export class CommandController extends Controller {
|
|||
}[];
|
||||
},
|
||||
) {
|
||||
let _posNumCodeSit: string = "";
|
||||
let _posNumCodeSitAbb: string = "";
|
||||
const _command = await this.commandRepository.findOne({
|
||||
relations: ["commandType"],
|
||||
where: { id: body.data.find((x) => x.commandId)?.commandId ?? "" },
|
||||
await new ExecuteSalaryService().executeSalary(body.data, {
|
||||
user: { sub: req.user.sub, name: req.user.name },
|
||||
req,
|
||||
});
|
||||
if (_command) {
|
||||
if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") {
|
||||
const orgRootDeputy = await this.orgRootRepository.findOne({
|
||||
where: {
|
||||
isDeputy: true,
|
||||
orgRevision: {
|
||||
orgRevisionIsCurrent: true,
|
||||
orgRevisionIsDraft: false,
|
||||
},
|
||||
},
|
||||
relations: ["orgRevision"],
|
||||
});
|
||||
_posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร";
|
||||
_posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป.";
|
||||
} else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") {
|
||||
_posNumCodeSit = "กรุงเทพมหานคร";
|
||||
_posNumCodeSitAbb = "กทม.";
|
||||
} else {
|
||||
let _profileAdmin = await this.profileRepository.findOne({
|
||||
where: {
|
||||
keycloak: _command?.createdUserId.toString(),
|
||||
current_holders: {
|
||||
orgRevision: {
|
||||
orgRevisionIsCurrent: true,
|
||||
orgRevisionIsDraft: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"],
|
||||
});
|
||||
_posNumCodeSit =
|
||||
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ??
|
||||
"";
|
||||
_posNumCodeSitAbb =
|
||||
_profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot
|
||||
.orgRootShortName ?? "";
|
||||
}
|
||||
}
|
||||
await Promise.all(
|
||||
body.data.map(async (item) => {
|
||||
const profile: any = await this.profileRepository.findOne({
|
||||
where: { id: item.profileId },
|
||||
// relations: ["roleKeycloaks"],
|
||||
relations: {
|
||||
roleKeycloaks: true,
|
||||
posType: true,
|
||||
posLevel: true,
|
||||
},
|
||||
});
|
||||
if (!profile) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้");
|
||||
}
|
||||
const posMaster: any = await this.posMasterRepository.findOne({
|
||||
where: {
|
||||
current_holderId: item.profileId,
|
||||
orgRevision: {
|
||||
orgRevisionIsCurrent: true,
|
||||
orgRevisionIsDraft: false,
|
||||
},
|
||||
},
|
||||
relations: {
|
||||
orgRevision: true,
|
||||
orgRoot: true,
|
||||
orgChild1: true,
|
||||
orgChild2: true,
|
||||
orgChild3: true,
|
||||
orgChild4: true,
|
||||
},
|
||||
});
|
||||
|
||||
const orgRevisionRef = posMaster ? posMaster.id : null;
|
||||
const orgRootRef = orgRevisionRef?.orgRoot ?? null;
|
||||
const orgChild1Ref = orgRevisionRef?.orgChild1 ?? null;
|
||||
const orgChild2Ref = orgRevisionRef?.orgChild2 ?? null;
|
||||
const orgChild3Ref = orgRevisionRef?.orgChild3 ?? null;
|
||||
const orgChild4Ref = orgRevisionRef?.orgChild4 ?? null;
|
||||
|
||||
//ลบตำแหน่งที่รักษาการแทน
|
||||
const code = _command?.commandType?.code;
|
||||
if (code && ["C-PM-13"].includes(code)) {
|
||||
removePostMasterAct(profile.id);
|
||||
}
|
||||
|
||||
let _commandYear = item.commandYear;
|
||||
if (item.commandYear) {
|
||||
_commandYear = item.commandYear > 2500 ? item.commandYear : item.commandYear + 543;
|
||||
}
|
||||
const dest_item = await this.salaryRepo.findOne({
|
||||
where: { profileId: item.profileId },
|
||||
order: { order: "DESC" },
|
||||
});
|
||||
const before = null;
|
||||
const data = new ProfileSalary();
|
||||
data.posNumCodeSit = _posNumCodeSit;
|
||||
data.posNumCodeSitAbb = _posNumCodeSitAbb;
|
||||
const meta = {
|
||||
order: dest_item == null ? 1 : dest_item.order + 1,
|
||||
createdUserId: req.user.sub,
|
||||
createdFullName: req.user.name,
|
||||
lastUpdateUserId: req.user.sub,
|
||||
lastUpdateFullName: req.user.name,
|
||||
createdAt: new Date(),
|
||||
lastUpdatedAt: new Date(),
|
||||
};
|
||||
if (item.isLeave != undefined && item.isLeave == true) {
|
||||
await CreatePosMasterHistoryOfficer(orgRevisionRef, req, "DELETE");
|
||||
await removeProfileInOrganize(profile.id, "OFFICER");
|
||||
}
|
||||
const clearProfile = await checkCommandType(String(item.commandId));
|
||||
const _null: any = null;
|
||||
if (clearProfile.status) {
|
||||
if (profile.keycloak != null && profile.keycloak != "" && profile.isDelete === false) {
|
||||
const delUserKeycloak = await deleteUser(profile.keycloak);
|
||||
if (delUserKeycloak) {
|
||||
// Task #228
|
||||
// profile.keycloak = _null;
|
||||
profile.roleKeycloaks = [];
|
||||
profile.isActive = false;
|
||||
profile.isDelete = true;
|
||||
}
|
||||
}
|
||||
profile.isLeave = item.isLeave;
|
||||
profile.leaveCommandId = item.commandId ?? _null;
|
||||
profile.leaveCommandNo = `${item.commandNo}/${_commandYear}`;
|
||||
profile.leaveRemark = clearProfile.leaveRemark ?? _null;
|
||||
profile.leaveDate = item.commandDateAffect ?? _null;
|
||||
profile.leaveType = clearProfile.LeaveType ?? _null;
|
||||
//ออกจากราชการ ไม่ต้องลบตำแหน่งในทะเบียน (issue #1516)
|
||||
// profile.position = _null;
|
||||
// profile.posTypeId = _null;
|
||||
// profile.posLevelId = _null;
|
||||
profile.leaveReason = item.leaveReason ?? _null;
|
||||
profile.dateLeave = item.dateLeave ?? _null;
|
||||
profile.amount = item.amount ?? _null;
|
||||
profile.amountSpecial = item.amountSpecial ?? _null;
|
||||
await this.profileRepository.save(profile, { data: req });
|
||||
|
||||
// if (profile.id) {
|
||||
// await this.keycloakAttributeService.clearOrgDnaAttributes(
|
||||
// [profile.id],
|
||||
// "PROFILE",
|
||||
// );
|
||||
// }
|
||||
}
|
||||
Object.assign(data, { ...item, ...meta });
|
||||
const history = new ProfileSalaryHistory();
|
||||
Object.assign(history, { ...data, id: undefined });
|
||||
|
||||
await this.salaryRepo.save(data, { data: req });
|
||||
setLogDataDiff(req, { before, after: data });
|
||||
history.profileSalaryId = data.id;
|
||||
await this.salaryHistoryRepo.save(history, { data: req });
|
||||
|
||||
if (_command) {
|
||||
/*
|
||||
const command = await this.commandRepository.findOne({
|
||||
where: { id: item.commandId },
|
||||
relations: ["commandType"],
|
||||
});
|
||||
*/
|
||||
if (["C-PM-15", "C-PM-16"].includes(_command.commandType.code)) {
|
||||
// ประวัติคำสั่งให้ช่วยราชการ
|
||||
const dataAssis = new ProfileAssistance();
|
||||
|
||||
const metaAssis = {
|
||||
profileId: item.profileId,
|
||||
agency: item.officerOrg,
|
||||
dateStart: item.dateStart,
|
||||
dateEnd: item.dateEnd,
|
||||
commandNo: `${item.commandNo}/${_commandYear}`,
|
||||
commandName: item.commandName,
|
||||
refId: item.refId,
|
||||
refCommandDate: new Date(),
|
||||
commandId: item.commandId,
|
||||
createdUserId: req.user.sub,
|
||||
createdFullName: req.user.name,
|
||||
lastUpdateUserId: req.user.sub,
|
||||
lastUpdateFullName: req.user.name,
|
||||
createdAt: new Date(),
|
||||
lastUpdatedAt: new Date(),
|
||||
status: _command.commandType.code == "C-PM-15" ? "PENDING" : "DONE",
|
||||
};
|
||||
|
||||
Object.assign(dataAssis, metaAssis);
|
||||
const historyAssis = new ProfileAssistanceHistory();
|
||||
Object.assign(historyAssis, { ...dataAssis, id: undefined });
|
||||
|
||||
await this.assistanceRepository.save(dataAssis);
|
||||
historyAssis.profileAssistanceId = dataAssis.id;
|
||||
await this.assistanceHistoryRepository.save(historyAssis);
|
||||
}
|
||||
// Task #2190
|
||||
else if (_command.commandType.code == "C-PM-13") {
|
||||
let organizeName = "";
|
||||
if (orgRootRef) {
|
||||
const names = [
|
||||
orgChild4Ref?.orgChild4Name,
|
||||
orgChild3Ref?.orgChild3Name,
|
||||
orgChild2Ref?.orgChild2Name,
|
||||
orgChild1Ref?.orgChild1Name,
|
||||
orgRootRef?.orgRootName,
|
||||
].filter(Boolean);
|
||||
organizeName = names.join(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
|
|
@ -6712,7 +6052,7 @@ export class CommandController extends Controller {
|
|||
}[];
|
||||
},
|
||||
) {
|
||||
await new OfficerProfileService().executeCreateOfficerProfile(body.data, {
|
||||
await new ExecuteOfficerProfileService().executeCreateOfficerProfile(body.data, {
|
||||
user: { sub: req.user.sub, name: req.user.name },
|
||||
req,
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue