API ดึงรายชื่อลูกจ้างประจำตามประวัติตำแหน่ง #2385
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m7s

This commit is contained in:
harid 2026-05-20 14:35:37 +07:00
parent 0e80acfb1b
commit 5dafd13124

View file

@ -7075,66 +7075,121 @@ export class OrganizationDotnetController extends Controller {
}
}
// set เวลาเป็น 23:59:59 ของวันนั้น
const date = body.date.setHours(23, 59, 59, 999);
const date = body.date ? new Date(body.date.toISOString().slice(0, 10)) : new Date();
date.setHours(23, 59, 59, 999);
let posEmpHis = await this.posMasterEmployeeHistoryRepository.find({
where: {
...typeCondition,
createdAt: LessThanOrEqual(date),
},
select: [
"profileEmployeeId",
"prefix",
"firstName",
"lastName",
"shortName",
"posMasterNo",
"position",
"posType",
"posLevel",
"ancestorDNA",
"rootDnaId",
"child1DnaId",
"child2DnaId",
"child3DnaId",
"child4DnaId",
"createdAt",
],
order: {
firstName: "ASC",
lastName: "ASC",
createdAt: "DESC", // ให้ createdAt ล่าสุดอยู่ข้างบน
createdAt: "DESC",
},
});
// group by ancestorDNA แล้วเลือก create_at ล่าสุด
const grouped = new Map<string, PosMasterEmployeeHistory>();
// group1: group by ancestorDNA แล้วเลือก create_at ล่าสุด
const grouped1 = new Map<string, PosMasterEmployeeHistory>();
for (const item of posEmpHis) {
const key = `${item.shortName}-${item.posMasterNo}`;
if (!grouped.has(key)) {
grouped.set(key, item);
const key = `${item.ancestorDNA}`;
if (!grouped1.has(key)) {
grouped1.set(key, item);
} else {
// ถ้าเจอซ้ำ ให้เลือก createdAt ล่าสุด
const exist = grouped.get(key);
const exist = grouped1.get(key);
if (exist && item.createdAt > exist.createdAt) {
grouped.set(key, item);
grouped1.set(key, item);
}
}
}
// group2: group by shortName-posMasterNo จากค่าที่ได้จาก group1
const grouped2 = new Map<string, PosMasterEmployeeHistory>();
for (const item of Array.from(grouped1.values())) {
const key = `${item.shortName}-${item.posMasterNo}`;
if (!grouped2.has(key)) {
grouped2.set(key, item);
} else {
// ถ้าเจอซ้ำ ให้เลือก createdAt ล่าสุด
const exist = grouped2.get(key);
if (exist && item.createdAt > exist.createdAt) {
grouped2.set(key, item);
}
}
}
// group3: group by firstName-lastName จากค่าที่ได้จาก group2
const grouped3 = new Map<string, PosMasterEmployeeHistory>();
for (const item of Array.from(grouped2.values())) {
const key = `${item.firstName}-${item.lastName}`;
if (!grouped3.has(key)) {
grouped3.set(key, item);
} else {
// ถ้าเจอซ้ำ ให้เลือก createdAt ล่าสุด
const exist = grouped3.get(key);
if (exist && item.createdAt > exist.createdAt) {
grouped3.set(key, item);
}
}
}
const profile_ = await Promise.all(
Array.from(grouped.values())
.filter((x) => x.profileEmployeeId != null)
.map(async (item: PosMasterEmployeeHistory) => {
let profileEmp = await this.profileEmpRepo.findOne({
where: { id: item.profileEmployeeId },
});
const profileEmployeeIds = Array.from(grouped3.values())
.filter((x) => x.profileEmployeeId != null)
.map((x) => x.profileEmployeeId);
return {
id: profileEmp?.id,
prefix: profileEmp?.prefix,
firstName: profileEmp?.firstName,
lastName: profileEmp?.lastName,
citizenId: profileEmp?.citizenId ?? null,
dateStart: profileEmp?.dateStart ?? null,
dateAppoint: profileEmp?.dateAppoint ?? null,
keycloak: profileEmp?.keycloak ?? null,
posNo: item.shortName,
position: item.position,
positionLevel: item.posLevel,
positionType: item.posType,
orgRootId: item.rootDnaId,
orgChild1Id: item.child1DnaId,
orgChild2Id: item.child2DnaId,
orgChild3Id: item.child3DnaId,
orgChild4Id: item.child4DnaId,
};
}),
const profileEmployees = await this.profileEmpRepo.find({
where: { id: In(profileEmployeeIds) },
select: ["id", "citizenId", "dateStart", "dateAppoint", "keycloak"],
});
const profileEmployeeMap = new Map(profileEmployees.map((p) => [p.id, p]));
const profile_ = Array.from(grouped3.values())
.filter((x) => x.profileEmployeeId != null)
.map((item: PosMasterEmployeeHistory) => {
const profileEmp = profileEmployeeMap.get(item.profileEmployeeId);
return {
id: item.profileEmployeeId,
prefix: item.prefix,
firstName: item.firstName,
lastName: item.lastName,
citizenId: profileEmp?.citizenId ?? null,
dateStart: profileEmp?.dateStart ?? null,
dateAppoint: profileEmp?.dateAppoint ?? null,
keycloak: profileEmp?.keycloak ?? null,
posNo: `${item.shortName} ${item.posMasterNo}`,
position: item.position,
positionLevel: item.posLevel,
positionType: item.posType,
orgRootId: item.rootDnaId,
orgChild1Id: item.child1DnaId,
orgChild2Id: item.child2DnaId,
orgChild3Id: item.child3DnaId,
orgChild4Id: item.child4DnaId,
};
});
return new HttpSuccess(
(profile_ ?? []).sort((a, b) => a.posNo.localeCompare(b.posNo, undefined, { numeric: true })),
);
return new HttpSuccess(profile_);
}
/**