diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index fce9bb98..91e5c9d2 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -7075,121 +7075,66 @@ export class OrganizationDotnetController extends Controller { } } // set เวลาเป็น 23:59:59 ของวันนั้น - const date = body.date ? new Date(body.date.toISOString().slice(0, 10)) : new Date(); - date.setHours(23, 59, 59, 999); + const date = body.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: "DESC", // ให้ createdAt ล่าสุดอยู่ข้างบน }, }); - // group1: group by ancestorDNA แล้วเลือก create_at ล่าสุด - const grouped1 = new Map(); + // group by ancestorDNA แล้วเลือก create_at ล่าสุด + const grouped = new Map(); for (const item of posEmpHis) { - const key = `${item.ancestorDNA}`; - if (!grouped1.has(key)) { - grouped1.set(key, item); - } else { - // ถ้าเจอซ้ำ ให้เลือก createdAt ล่าสุด - const exist = grouped1.get(key); - if (exist && item.createdAt > exist.createdAt) { - grouped1.set(key, item); - } - } - } - // group2: group by shortName-posMasterNo จากค่าที่ได้จาก group1 - const grouped2 = new Map(); - for (const item of Array.from(grouped1.values())) { const key = `${item.shortName}-${item.posMasterNo}`; - if (!grouped2.has(key)) { - grouped2.set(key, item); + if (!grouped.has(key)) { + grouped.set(key, item); } else { // ถ้าเจอซ้ำ ให้เลือก createdAt ล่าสุด - const exist = grouped2.get(key); + const exist = grouped.get(key); if (exist && item.createdAt > exist.createdAt) { - grouped2.set(key, item); - } - } - } - // group3: group by firstName-lastName จากค่าที่ได้จาก group2 - const grouped3 = new Map(); - 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); + grouped.set(key, item); } } } - const profileEmployeeIds = Array.from(grouped3.values()) - .filter((x) => x.profileEmployeeId != null) - .map((x) => x.profileEmployeeId); + 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 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 { + 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, + }; + }), ); + + return new HttpSuccess(profile_); } /** diff --git a/src/services/webSocket.ts b/src/services/webSocket.ts index 7d464655..36359cd8 100644 --- a/src/services/webSocket.ts +++ b/src/services/webSocket.ts @@ -22,7 +22,7 @@ export function initWebSocket() { }); io.on("connection", (ws) => { - // console.log("✅ Client connected to WebSocket"); + console.log("✅ Client connected to WebSocket"); ws.on("close", () => { console.log("❌ Client disconnected"); @@ -46,7 +46,7 @@ export async function sendWebSocket( ) { if (!io) initWebSocket(); // console.log( `🔔 :`,data.message); - + for (let [id, session] of io.of("/").sockets) { const user: { sub: string;