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

This commit is contained in:
harid 2026-05-20 10:57:18 +07:00
parent 458c9b1042
commit e04d1ad7d3
4 changed files with 280 additions and 260 deletions

View file

@ -26,6 +26,7 @@ import { OrgRoot } from "../entities/OrgRoot";
import { Position } from "../entities/Position"; import { Position } from "../entities/Position";
import { PosMaster } from "../entities/PosMaster"; import { PosMaster } from "../entities/PosMaster";
import { PosMasterHistory } from "../entities/PosMasterHistory"; import { PosMasterHistory } from "../entities/PosMasterHistory";
import { PosMasterEmployeeHistory } from "../entities/PosMasterEmployeeHistory";
import { Profile } from "../entities/Profile"; import { Profile } from "../entities/Profile";
import { ProfileEducation } from "../entities/ProfileEducation"; import { ProfileEducation } from "../entities/ProfileEducation";
import { ProfileEmployee } from "../entities/ProfileEmployee"; import { ProfileEmployee } from "../entities/ProfileEmployee";
@ -57,6 +58,7 @@ export class OrganizationDotnetController extends Controller {
private positionRepository = AppDataSource.getRepository(Position); private positionRepository = AppDataSource.getRepository(Position);
private posMasterRepository = AppDataSource.getRepository(PosMaster); private posMasterRepository = AppDataSource.getRepository(PosMaster);
private posMasterHistoryRepository = AppDataSource.getRepository(PosMasterHistory); private posMasterHistoryRepository = AppDataSource.getRepository(PosMasterHistory);
private posMasterEmployeeHistoryRepository = AppDataSource.getRepository(PosMasterEmployeeHistory);
private empPosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); private empPosMasterRepository = AppDataSource.getRepository(EmployeePosMaster);
private insigniaRepo = AppDataSource.getRepository(ProfileInsignia); private insigniaRepo = AppDataSource.getRepository(ProfileInsignia);
private employeePosDictRepository = AppDataSource.getRepository(EmployeePosDict); private employeePosDictRepository = AppDataSource.getRepository(EmployeePosDict);
@ -6922,229 +6924,218 @@ export class OrganizationDotnetController extends Controller {
return new HttpSuccess(profile_); return new HttpSuccess(profile_);
} }
// /** /**
// * รายชื่อขรก. ตามสิทธิ์ admin * admin
// * * @summary admin
// * @summary รายชื่อขรก. ตามสิทธิ์ admin */
// * @Post("employee-by-admin-rolev2")
// */ async GetEmployeesByAdminRoleV2(
// @Post("employee-by-admin-rolev2") @Request() req: RequestWithUser,
// async GetEmployeesByAdminRoleV2( @Body()
// @Request() req: RequestWithUser, body: {
// @Body() node: number;
// body: { nodeId: string;
// node: number; role: string;
// nodeId: string; isRetirement?: boolean;
// role: string; reqNode?: number;
// isRetirement?: boolean; reqNodeId?: string;
// reqNode?: number; date: Date;
// reqNodeId?: string; },
// date?: Date; ) {
// }, let typeCondition: any = {};
// ) { if (body.role === "CHILD" || body.role === "BROTHER") {
// let typeCondition: any = {}; if (body.role === "CHILD") {
// if (body.role === "CHILD" || body.role === "PARENT" || body.role === "BROTHER") { switch (body.node) {
// if (body.role === "CHILD") { case 0:
// switch (body.node) { typeCondition = {
// case 0: rootDnaId: body.nodeId,
// typeCondition = { };
// rootDnaId: body.nodeId, break;
// }; case 1:
// break; typeCondition = {
// case 1: child1DnaId: body.nodeId,
// typeCondition = { };
// child1DnaId: body.nodeId, break;
// }; case 2:
// break; typeCondition = {
// case 2: child2DnaId: body.nodeId,
// typeCondition = { };
// child2DnaId: body.nodeId, break;
// }; case 3:
// break; typeCondition = {
// case 3: child3DnaId: body.nodeId,
// typeCondition = { };
// child3DnaId: body.nodeId, break;
// }; case 4:
// break; typeCondition = {
// case 4: child4DnaId: body.nodeId,
// typeCondition = { };
// child4DnaId: body.nodeId, break;
// }; default:
// break; typeCondition = {};
// default: break;
// typeCondition = {}; }
// break; } else if (body.role === "BROTHER") {
// } switch (body.node) {
// } else if (body.role === "BROTHER") { case 0:
// switch (body.node) { typeCondition = {
// case 0: rootDnaId: body.nodeId,
// typeCondition = { };
// rootDnaId: body.nodeId, break;
// }; case 1:
// break; typeCondition = {
// case 1: rootDnaId: body.nodeId,
// typeCondition = { };
// rootDnaId: body.nodeId, break;
// }; case 2:
// break; typeCondition = {
// case 2: child1DnaId: body.nodeId,
// typeCondition = { };
// child1DnaId: body.nodeId, break;
// }; case 3:
// break; typeCondition = {
// case 3: child2DnaId: body.nodeId,
// typeCondition = { };
// child2DnaId: body.nodeId, break;
// }; case 4:
// break; typeCondition = {
// case 4: child3DnaId: body.nodeId,
// typeCondition = { };
// child3DnaId: body.nodeId, break;
// }; default:
// break; typeCondition = {};
// default: break;
// typeCondition = {}; }
// break; }
// } } else if (body.role === "OWNER" || body.role === "ROOT" || body.role === "PARENT") {
// } else if (body.role === "PARENT") { switch (body.reqNode) {
// typeCondition = { case 0:
// rootDnaId: body.nodeId, typeCondition = {
// child1DnaId: Not(IsNull()), rootDnaId: body.reqNodeId,
// }; };
// } break;
// } else if (body.role === "OWNER" || body.role === "ROOT") { case 1:
// switch (body.reqNode) { typeCondition = {
// case 0: child1DnaId: body.reqNodeId,
// typeCondition = { };
// rootDnaId: body.reqNodeId, break;
// }; case 2:
// break; typeCondition = {
// case 1: child2DnaId: body.reqNodeId,
// typeCondition = { };
// child1DnaId: body.reqNodeId, break;
// }; case 3:
// break; typeCondition = {
// case 2: child3DnaId: body.reqNodeId,
// typeCondition = { };
// child2DnaId: body.reqNodeId, break;
// }; case 4:
// break; typeCondition = {
// case 3: child4DnaId: body.reqNodeId,
// typeCondition = { };
// child3DnaId: body.reqNodeId, break;
// }; default:
// break; typeCondition = {};
// case 4: break;
// typeCondition = { }
// child4DnaId: body.reqNodeId, } else if (body.role === "NORMAL") {
// }; switch (body.node) {
// break; case 0:
// default: typeCondition = {
// typeCondition = {}; rootDnaId: body.nodeId,
// break; child1DnaId: IsNull(),
// } };
// } else if (body.role === "NORMAL") { break;
// switch (body.node) { case 1:
// case 0: typeCondition = {
// typeCondition = { child1DnaId: body.nodeId,
// rootDnaId: body.nodeId, child2DnaId: IsNull(),
// child1DnaId: IsNull(), };
// }; break;
// break; case 2:
// case 1: typeCondition = {
// typeCondition = { child2DnaId: body.nodeId,
// child1DnaId: body.nodeId, child3DnaId: IsNull(),
// child2DnaId: IsNull(), };
// }; break;
// break; case 3:
// case 2: typeCondition = {
// typeCondition = { child3DnaId: body.nodeId,
// child2DnaId: body.nodeId, child4DnaId: IsNull(),
// child3DnaId: IsNull(), };
// }; break;
// break; case 4:
// case 3: typeCondition = {
// typeCondition = { child4DnaId: body.nodeId,
// child3DnaId: body.nodeId, };
// child4DnaId: IsNull(), break;
// }; default:
// break; typeCondition = {};
// case 4: break;
// typeCondition = { }
// child4DnaId: body.nodeId, }
// }; // set เวลาเป็น 23:59:59 ของวันนั้น
// break; const date = body.date.setHours(23, 59, 59, 999);
// 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.posMasterEmployeeHistoryRepository.find({ let posEmpHis = await this.posMasterEmployeeHistoryRepository.find({
// where: { where: {
// ...typeCondition, ...typeCondition,
// createdAt: LessThanOrEqual(date), createdAt: LessThanOrEqual(date),
// // firstName: Not("") && Not(IsNull()), },
// // lastName: Not("") && Not(IsNull()), order: {
// }, firstName: "ASC",
// order: { lastName: "ASC",
// firstName: "ASC", createdAt: "DESC", // ให้ createdAt ล่าสุดอยู่ข้างบน
// lastName: "ASC", },
// createdAt: "DESC", // ให้ createdAt ล่าสุดอยู่ข้างบน });
// },
// });
// // group by ancestorDNA แล้วเลือก create_at ล่าสุด // group by ancestorDNA แล้วเลือก create_at ล่าสุด
// const grouped = new Map<string, PosMasterEmployeeHistory>(); const grouped = new Map<string, PosMasterEmployeeHistory>();
// for (const item of profile) { for (const item of posEmpHis) {
// const key = `${item.shortName}-${item.posMasterNo}`; const key = `${item.shortName}-${item.posMasterNo}`;
// if (!grouped.has(key)) { if (!grouped.has(key)) {
// grouped.set(key, item); grouped.set(key, item);
// } else { } else {
// // ถ้าเจอซ้ำ ให้เลือก createdAt ล่าสุด // ถ้าเจอซ้ำ ให้เลือก createdAt ล่าสุด
// const exist = grouped.get(key); const exist = grouped.get(key);
// if (exist && item.createdAt > exist.createdAt) { if (exist && item.createdAt > exist.createdAt) {
// grouped.set(key, item); grouped.set(key, item);
// } }
// } }
// } }
// const profile_ = await Promise.all( const profile_ = await Promise.all(
// Array.from(grouped.values()) Array.from(grouped.values())
// .filter((x) => x.profileId != null) .filter((x) => x.profileEmployeeId != null)
// .map(async (item: PosMasterEmployeeHistory) => { .map(async (item: PosMasterEmployeeHistory) => {
// let profile = await this.profileRepo.findOne({ let profileEmp = await this.profileEmpRepo.findOne({
// where: { id: item.profileId }, where: { id: item.profileEmployeeId },
// }); });
// return { return {
// id: item.profileId, id: profileEmp?.id,
// prefix: item.prefix, prefix: profileEmp?.prefix,
// firstName: item.firstName, firstName: profileEmp?.firstName,
// lastName: item.lastName, lastName: profileEmp?.lastName,
// citizenId: profile?.citizenId ?? null, citizenId: profileEmp?.citizenId ?? null,
// dateStart: profile?.dateStart ?? null, dateStart: profileEmp?.dateStart ?? null,
// dateAppoint: profile?.dateAppoint ?? null, dateAppoint: profileEmp?.dateAppoint ?? null,
// keycloak: profile?.keycloak ?? null, keycloak: profileEmp?.keycloak ?? null,
// posNo: item.shortName, posNo: item.shortName,
// position: item.position, position: item.position,
// positionLevel: item.posLevel, positionLevel: item.posLevel,
// positionType: item.posType, positionType: item.posType,
// // oc: Oc, orgRootId: item.rootDnaId,
// orgRootId: item.rootDnaId, orgChild1Id: item.child1DnaId,
// orgChild1Id: item.child1DnaId, orgChild2Id: item.child2DnaId,
// orgChild2Id: item.child2DnaId, orgChild3Id: item.child3DnaId,
// orgChild3Id: item.child3DnaId, orgChild4Id: item.child4DnaId,
// orgChild4Id: item.child4DnaId, };
// }; }),
// }), );
// );
// return new HttpSuccess(profile_); return new HttpSuccess(profile_);
// } }
/** /**
* 4. API Update profile * 4. API Update profile

View file

@ -99,51 +99,51 @@ export class PosMasterEmployeeHistory extends EntityBase {
}) })
ancestorDNA: string; ancestorDNA: string;
// @Column({ @Column({
// nullable: true, nullable: true,
// length: 40, length: 40,
// comment: "คีย์นอก(FK)ของตาราง profile", comment: "คีย์นอก(FK)ของตาราง profileEmployee",
// default: null, default: null,
// }) })
// profileId: string; profileEmployeeId: string;
// @Column({ @Column({
// nullable: true, nullable: true,
// length: 40, length: 40,
// comment: "dna ของตาราง orgRoot", comment: "dna ของตาราง orgRoot",
// default: null, default: null,
// }) })
// rootDnaId: string; rootDnaId: string;
// @Column({ @Column({
// nullable: true, nullable: true,
// length: 40, length: 40,
// comment: "dna ของตาราง orgChild1", comment: "dna ของตาราง orgChild1",
// default: null, default: null,
// }) })
// child1DnaId: string; child1DnaId: string;
// @Column({ @Column({
// nullable: true, nullable: true,
// length: 40, length: 40,
// comment: "dna ของตาราง orgChild2", comment: "dna ของตาราง orgChild2",
// default: null, default: null,
// }) })
// child2DnaId: string; child2DnaId: string;
// @Column({ @Column({
// nullable: true, nullable: true,
// length: 40, length: 40,
// comment: "dna ของตาราง orgChild3", comment: "dna ของตาราง orgChild3",
// default: null, default: null,
// }) })
// child3DnaId: string; child3DnaId: string;
// @Column({ @Column({
// nullable: true, nullable: true,
// length: 40, length: 40,
// comment: "dna ของตาราง orgChild4", comment: "dna ของตาราง orgChild4",
// default: null, default: null,
// }) })
// child4DnaId: string; child4DnaId: string;
} }

View file

@ -0,0 +1,23 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class UpdatePosMasterEmpHisAddDna1779244154610 implements MigrationInterface {
name = 'UpdatePosMasterEmpHisAddDna1779244154610'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`posMasterEmployeeHistory\` ADD \`profileEmployeeId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง profileEmployee'`);
await queryRunner.query(`ALTER TABLE \`posMasterEmployeeHistory\` ADD \`rootDnaId\` varchar(40) NULL COMMENT 'dna ของตาราง orgRoot'`);
await queryRunner.query(`ALTER TABLE \`posMasterEmployeeHistory\` ADD \`child1DnaId\` varchar(40) NULL COMMENT 'dna ของตาราง orgChild1'`);
await queryRunner.query(`ALTER TABLE \`posMasterEmployeeHistory\` ADD \`child2DnaId\` varchar(40) NULL COMMENT 'dna ของตาราง orgChild2'`);
await queryRunner.query(`ALTER TABLE \`posMasterEmployeeHistory\` ADD \`child3DnaId\` varchar(40) NULL COMMENT 'dna ของตาราง orgChild3'`);
await queryRunner.query(`ALTER TABLE \`posMasterEmployeeHistory\` ADD \`child4DnaId\` varchar(40) NULL COMMENT 'dna ของตาราง orgChild4'`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`posMasterEmployeeHistory\` DROP COLUMN \`child4DnaId\``);
await queryRunner.query(`ALTER TABLE \`posMasterEmployeeHistory\` DROP COLUMN \`child3DnaId\``);
await queryRunner.query(`ALTER TABLE \`posMasterEmployeeHistory\` DROP COLUMN \`child2DnaId\``);
await queryRunner.query(`ALTER TABLE \`posMasterEmployeeHistory\` DROP COLUMN \`child1DnaId\``);
await queryRunner.query(`ALTER TABLE \`posMasterEmployeeHistory\` DROP COLUMN \`rootDnaId\``);
await queryRunner.query(`ALTER TABLE \`posMasterEmployeeHistory\` DROP COLUMN \`profileEmployeeId\``);
}
}

View file

@ -230,6 +230,7 @@ export async function CreatePosMasterHistoryEmployee(
: null; : null;
h.ancestorDNA = pm.ancestorDNA; h.ancestorDNA = pm.ancestorDNA;
if (!type || type != "DELETE") { if (!type || type != "DELETE") {
h.profileEmployeeId = pm.current_holder?.id || _null;
h.prefix = pm.current_holder?.prefix || _null; h.prefix = pm.current_holder?.prefix || _null;
h.firstName = pm.current_holder?.firstName || _null; h.firstName = pm.current_holder?.firstName || _null;
h.lastName = pm.current_holder?.lastName || _null; h.lastName = pm.current_holder?.lastName || _null;
@ -237,6 +238,11 @@ export async function CreatePosMasterHistoryEmployee(
h.posType = selectedPosition?.posType?.posTypeName ?? _null; h.posType = selectedPosition?.posType?.posTypeName ?? _null;
h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null; h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null;
} }
h.rootDnaId = pm.orgRoot?.ancestorDNA || _null;
h.child1DnaId = pm.orgChild1?.ancestorDNA || _null;
h.child2DnaId = pm.orgChild2?.ancestorDNA || _null;
h.child3DnaId = pm.orgChild3?.ancestorDNA || _null;
h.child4DnaId = pm.orgChild4?.ancestorDNA || _null;
h.posMasterNoPrefix = pm.posMasterNoPrefix ?? _null; h.posMasterNoPrefix = pm.posMasterNoPrefix ?? _null;
h.posMasterNo = pm.posMasterNo ?? _null; h.posMasterNo = pm.posMasterNo ?? _null;
h.posMasterNoSuffix = pm.posMasterNoSuffix ?? _null; h.posMasterNoSuffix = pm.posMasterNoSuffix ?? _null;