report group posno and dna
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m3s

This commit is contained in:
mamoss 2026-01-11 16:33:20 +07:00
parent b110575136
commit e040409fa5

View file

@ -6565,4 +6565,242 @@ export class OrganizationDotnetController extends Controller {
return new HttpSuccess(profile_);
}
/**
* . admin
*
* @summary . admin
*
*/
@Post("officer-by-admin-rolev4")
async GetOfficersByAdminRoleV4(
@Request() req: RequestWithUser,
@Body()
body: {
node: number;
nodeId: string;
role: string;
isRetirement?: boolean;
reqNode?: number;
reqNodeId?: string;
date?: Date;
},
) {
let typeCondition: any = {};
if (body.role === "CHILD" || body.role === "PARENT" || body.role === "BROTHER") {
if (body.role === "CHILD") {
switch (body.node) {
case 0:
typeCondition = {
rootDnaId: body.nodeId,
};
break;
case 1:
typeCondition = {
child1DnaId: body.nodeId,
};
break;
case 2:
typeCondition = {
child2DnaId: body.nodeId,
};
break;
case 3:
typeCondition = {
child3DnaId: body.nodeId,
};
break;
case 4:
typeCondition = {
child4DnaId: body.nodeId,
};
break;
default:
typeCondition = {};
break;
}
} else if (body.role === "BROTHER") {
switch (body.node) {
case 0:
typeCondition = {
rootDnaId: body.nodeId,
};
break;
case 1:
typeCondition = {
rootDnaId: body.nodeId,
};
break;
case 2:
typeCondition = {
child1DnaId: body.nodeId,
};
break;
case 3:
typeCondition = {
child2DnaId: body.nodeId,
};
break;
case 4:
typeCondition = {
child3DnaId: body.nodeId,
};
break;
default:
typeCondition = {};
break;
}
} else if (body.role === "PARENT") {
typeCondition = {
rootDnaId: body.nodeId,
child1DnaId: Not(IsNull()),
};
}
} else if (body.role === "OWNER" || body.role === "ROOT") {
switch (body.reqNode) {
case 0:
typeCondition = {
rootDnaId: body.reqNodeId,
};
break;
case 1:
typeCondition = {
child1DnaId: body.reqNodeId,
};
break;
case 2:
typeCondition = {
child2DnaId: body.reqNodeId,
};
break;
case 3:
typeCondition = {
child3DnaId: body.reqNodeId,
};
break;
case 4:
typeCondition = {
child4DnaId: body.reqNodeId,
};
break;
default:
typeCondition = {};
break;
}
} else if (body.role === "NORMAL") {
switch (body.node) {
case 0:
typeCondition = {
rootDnaId: body.nodeId,
child1DnaId: IsNull(),
};
break;
case 1:
typeCondition = {
child1DnaId: body.nodeId,
child2DnaId: IsNull(),
};
break;
case 2:
typeCondition = {
child2DnaId: body.nodeId,
child3DnaId: IsNull(),
};
break;
case 3:
typeCondition = {
child3DnaId: body.nodeId,
child4DnaId: IsNull(),
};
break;
case 4:
typeCondition = {
child4DnaId: body.nodeId,
};
break;
default:
typeCondition = {};
break;
}
}
const date = body.date ? new Date(body.date) : new Date();
// set เวลาเป็น 23:59:59 ของวันนั้น
date.setHours(23, 59, 59, 999);
let profile = await this.posMasterHistoryRepository.find({
where: {
...typeCondition,
createdAt: LessThanOrEqual(date),
// firstName: Not("") && Not(IsNull()),
// lastName: Not("") && Not(IsNull()),
},
order: {
firstName: "ASC",
lastName: "ASC",
createdAt: "DESC", // ให้ createdAt ล่าสุดอยู่ข้างบน
},
});
// group1: group by ancestorDNA แล้วเลือก create_at ล่าสุด
const grouped1 = new Map<string, PosMasterHistory>();
for (const item of profile) {
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<string, PosMasterHistory>();
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);
}
}
}
const profile_ = await Promise.all(
Array.from(grouped2.values())
.filter((x) => x.profileId != null)
.map(async (item: PosMasterHistory) => {
let profile = await this.profileRepo.findOne({
where: { id: item.profileId },
});
return {
id: item.profileId,
prefix: item.prefix,
firstName: item.firstName,
lastName: item.lastName,
citizenId: profile?.citizenId ?? null,
dateStart: profile?.dateStart ?? null,
dateAppoint: profile?.dateAppoint ?? null,
keycloak: profile?.keycloak ?? null,
posNo: item.shortName,
position: item.position,
positionLevel: item.posLevel,
positionType: item.posType,
// oc: Oc,
orgRootId: item.rootDnaId,
orgChild1Id: item.child1DnaId,
orgChild2Id: item.child2DnaId,
orgChild3Id: item.child3DnaId,
orgChild4Id: item.child4DnaId,
};
}),
);
return new HttpSuccess(profile_);
}
}