ประวัติอัตรากำลัง

This commit is contained in:
Kittapath 2024-02-01 20:05:26 +07:00
parent c574726522
commit e90e3a27c6
2 changed files with 279 additions and 189 deletions

View file

@ -24,7 +24,7 @@ import { PosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
import { CreatePosDict, PosDict } from "../entities/PosDict";
import HttpError from "../interfaces/http-error";
import { ILike, In, IsNull, Like, Not } from "typeorm";
import { Equal, ILike, In, IsNull, Like, Not } from "typeorm";
import { CreatePosMaster, PosMaster } from "../entities/PosMaster";
import { OrgRevision } from "../entities/OrgRevision";
import { OrgRoot } from "../entities/OrgRoot";
@ -546,11 +546,11 @@ export class PositionController extends Controller {
if (!posMaster) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลอัตรากำลัง");
}
posMaster.orgRootId = "";
posMaster.orgChild1Id = "";
posMaster.orgChild2Id = "";
posMaster.orgChild3Id = "";
posMaster.orgChild4Id = "";
posMaster.orgRootId = null;
posMaster.orgChild1Id = null;
posMaster.orgChild2Id = null;
posMaster.orgChild3Id = null;
posMaster.orgChild4Id = null;
let orgRoot: any = null;
if (requestBody.orgRootId != null)
@ -619,11 +619,6 @@ export class PositionController extends Controller {
posMaster.lastUpdateUserId = request.user.sub;
posMaster.lastUpdateFullName = request.user.name;
await this.posMasterRepository.save(posMaster);
const positions = await this.positionRepository.find({
where: [{ posMasterId: posMaster.id }],
});
await this.positionRepository.remove(positions);
await this.positionRepository.delete({ posMasterId: posMaster.id });
requestBody.positions.forEach(async (x: any) => {
const position = Object.assign(new Position());
@ -737,182 +732,145 @@ export class PositionController extends Controller {
try {
let typeCondition: any = {};
let checkChildConditions: any = {};
//******** ของตั้ม(เก่า) *********//
// if (body.type === 0) {
// typeCondition = {
// orgRootId: body.id,
// };
// if (!body.isAll) {
// checkChildConditions = {
// orgChild1Id: IsNull(),
// };
if (body.type === 0) {
typeCondition = {
orgRootId: body.id,
};
if (!body.isAll) {
checkChildConditions = {
orgChild1Id: IsNull(),
};
}
} else if (body.type === 1) {
typeCondition = {
orgChild1Id: body.id,
};
if (!body.isAll) {
checkChildConditions = {
orgChild2Id: IsNull(),
};
}
} else if (body.type === 2) {
typeCondition = {
orgChild2Id: body.id,
};
if (!body.isAll) {
checkChildConditions = {
orgChild3Id: IsNull(),
};
}
} else if (body.type === 3) {
typeCondition = {
orgChild3Id: body.id,
};
if (!body.isAll) {
checkChildConditions = {
orgChild4Id: IsNull(),
};
}
} else if (body.type === 4) {
typeCondition = {
orgChild4Id: body.id,
};
}
let findPosition: any;
let masterId = new Array();
const findTypes: PosType[] = await this.posTypeRepository.find({
where: { posTypeName: Like(`%${body.keyword}%`) },
select: ["id"],
});
findPosition = await this.positionRepository.find({
where: { posTypeId: In(findTypes.map((x) => x.id)) },
select: ["posMasterId"],
});
masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId));
const findLevel: PosLevel[] = await this.posLevelRepository.find({
where: { posLevelName: Like(`%${body.keyword}%`) },
select: ["id"],
});
findPosition = await this.positionRepository.find({
where: { posLevelId: In(findLevel.map((x) => x.id)) },
select: ["posMasterId"],
});
masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId));
const findExecutive: PosExecutive[] = await this.posExecutiveRepository.find({
where: { posExecutiveName: Like(`%${body.keyword}%`) },
select: ["id"],
});
findPosition = await this.positionRepository.find({
where: { posExecutiveId: In(findExecutive.map((x) => x.id)) },
select: ["posMasterId"],
});
masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId));
findPosition = await this.positionRepository.find({
where: { positionName: Like(`%${body.keyword}%`) },
select: ["posMasterId"],
});
masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId));
// let keywordAsInt: any;
// keywordAsInt = body.keyword == null ? null : parseInt(body.keyword, 10);
// if (isNaN(keywordAsInt)) {
// keywordAsInt = "";
// }
// } else if (body.type === 1) {
// typeCondition = {
// orgChild1Id: body.id,
// };
// if (!body.isAll) {
// checkChildConditions = {
// orgChild2Id: IsNull(),
// };
// }
// } else if (body.type === 2) {
// typeCondition = {
// orgChild2Id: body.id,
// };
// if (!body.isAll) {
// checkChildConditions = {
// orgChild3Id: IsNull(),
// };
// }
// } else if (body.type === 3) {
// typeCondition = {
// orgChild3Id: body.id,
// };
// if (!body.isAll) {
// checkChildConditions = {
// orgChild4Id: IsNull(),
// };
// }
// } else if (body.type === 4) {
// typeCondition = {
// orgChild4Id: body.id,
// };
// }
const posMaster = await this.posMasterRepository.find({
where: {
...typeCondition,
...checkChildConditions,
// posMasterNo: Like(`%${keywordAsInt}%`),
id: In(masterId),
},
order: { posMasterOrder: "ASC" },
relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"],
skip: (body.page - 1) * body.pageSize,
take: body.pageSize,
});
const total = posMaster.length;
// const keywordPosMasterCondition = body.keyword
// ? {
// posMasterNoPrefix: Like(`%${body.keyword}%`),
// positionName: Like(`%${body.keyword}%`),
// posTypeName: Like(`%${body.keyword}%`),
// posLevelName: Like(`%${body.keyword}%`),
// }
// : {};
//******** จบของตั้ม(เก่า) *********//
const formattedData = await Promise.all(
posMaster.map(async (posMaster) => {
const positions = await this.positionRepository.find({
where: {
posMasterId: posMaster.id,
},
relations: ["posLevel", "posType", "posExecutive"],
});
//******** ของมอส *********//
// return {
// id: posMaster.id,
// posMasterNoPrefix: posMaster.posMasterNoPrefix,
// posMasterNo: posMaster.posMasterNo,
// posMasterNoSuffix: posMaster.posMasterNoSuffix,
// orgShortname:
// body.type === 0
// ? posMaster.orgRoot.orgRootShortName
// : body.type === 1
// ? posMaster.orgChild1.orgChild1ShortName
// : body.type === 2
// ? posMaster.orgChild2.orgChild2ShortName
// : body.type === 3
// ? posMaster.orgChild3.orgChild3ShortName
// : body.type === 4
// ? posMaster.orgChild4.orgChild4ShortName
// : "-",
// profileIdCurrentHolder: posMaster.profileIdCurrentHolder,
// profileIdNextHolder: posMaster.profileIdNextHolder,
// positions: positions.map((position) => ({
// id: position.id,
// positionName: position.positionName,
// positionField: position.positionField,
// posTypeId: position.posTypeId,
// posTypeName: position.posType == null ? null : position.posType.posTypeName,
// posLevelId: position.posLevelId,
// posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName,
// posExecutiveId: position.posExecutiveId,
// posExecutiveName:
// position.posExecutive == null ? null : position.posExecutive.posExecutiveName,
// positionExecutiveField: position.positionExecutiveField,
// positionArea: position.positionArea,
// positionIsSelected: position.positionIsSelected,
// })),
// };
// }),
// );
// return new HttpSuccess({ data: formattedData, total });
//******** จบของมอส *********//
let data = AppDataSource.getRepository(PosMaster)
.createQueryBuilder("posMaster")
.leftJoin("posMaster.positions", "position")
.leftJoin("position.posType", "posType")
.leftJoin("position.posLevel", "posLevel")
.andWhere("posMaster.posMasterNoPrefix LIKE :keyword", { keyword: `%${body.keyword}%` })
.orWhere("position.positionName LIKE :keyword", { keyword: `%${body.keyword}%` })
.orWhere("posType.posTypeName LIKE :keyword", { keyword: `%${body.keyword}%` })
.orWhere("posLevel.posLevelName LIKE :keyword", { keyword: `%${body.keyword}%` })
.orderBy("posMaster.posMasterOrder", "ASC")
.skip((body.page - 1) * body.pageSize)
.take(body.pageSize);
if (body.type === 0) {
data = data.andWhere("posMaster.orgRootId = :orgRootId", { orgRootId: body.id });
if (!body.isAll) {
data = data.andWhere("posMaster.orgChild1Id IS NULL");
}
} else if (body.type === 1) {
data = data.andWhere("posMaster.orgChild1Id = :orgChild1Id", { orgChild1Id: body.id });
if (!body.isAll) {
data = data.andWhere("posMaster.orgChild2Id IS NULL");
}
} else if (body.type === 2) {
data = data.andWhere("posMaster.orgChild2Id = :orgChild2Id", { orgChild2Id: body.id });
if (!body.isAll) {
data = data.andWhere("posMaster.orgChild3Id IS NULL");
}
} else if (body.type === 3) {
data = data.andWhere("posMaster.orgChild3Id = :orgChild3Id", { orgChild3Id: body.id });
if (!body.isAll) {
data = data.andWhere("posMaster.orgChild4Id IS NULL");
}
} else if (body.type === 4) {
data = data.andWhere("posMaster.orgChild4Id = :orgChild4Id", { orgChild4Id: body.id });
}
const [posMaster, total] = await data.getManyAndCount();
// const posMaster = await this.posMasterRepository.find({
// where: {
// ...typeCondition,
// ...checkChildConditions,
// },
// order: { posMasterOrder: "ASC" },
// skip: (body.page - 1) * body.pageSize,
// take: body.pageSize,
// });
// const total = posMaster.length;
const formattedData = await Promise.all(
posMaster.map(async (posMaster) => {
const positions = await this.positionRepository.find({
where: {
posMasterId: posMaster.id,
},
relations: ["posLevel", "posType", "posExecutive"],
});
return {
id: posMaster.id,
posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix,
positions: positions.map((position) => ({
id: position.id,
positionName: position.positionName,
positionField: position.positionField,
posTypeId: position.posTypeId,
posTypeName: position.posType == null ? null : position.posType.posTypeName,
posLevelId: position.posLevelId,
posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName,
posExecutiveId: position.posExecutiveId,
posExecutiveName:
position.posExecutive == null ? null : position.posExecutive.posExecutiveName,
positionExecutiveField: position.positionExecutiveField,
positionArea: position.positionArea,
positionIsSelected: position.positionIsSelected,
})),
};
}),
);
return new HttpSuccess({ data: formattedData, total });
return {
id: posMaster.id,
posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix,
orgShortname:
body.type === 0
? posMaster.orgRoot.orgRootShortName
: body.type === 1
? posMaster.orgChild1.orgChild1ShortName
: body.type === 2
? posMaster.orgChild2.orgChild2ShortName
: body.type === 3
? posMaster.orgChild3.orgChild3ShortName
: body.type === 4
? posMaster.orgChild4.orgChild4ShortName
: "-",
positions: positions.map((position) => ({
id: position.id,
positionName: position.positionName,
positionField: position.positionField,
posTypeId: position.posTypeId,
posTypeName: position.posType == null ? null : position.posType.posTypeName,
posLevelId: position.posLevelId,
posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName,
posExecutiveId: position.posExecutiveId,
posExecutiveName:
position.posExecutive == null ? null : position.posExecutive.posExecutiveName,
positionExecutiveField: position.positionExecutiveField,
positionArea: position.positionArea,
positionIsSelected: position.positionIsSelected,
})),
};
}),
);
return new HttpSuccess({ data: formattedData, total });
} catch (error) {
return error;
}
@ -1156,4 +1114,136 @@ export class PositionController extends Controller {
return error;
}
}
/**
* API
*
* @summary ORG_054 - (ADMIN) #58
*
* @param {string} id Id
*/
@Get("history/{id}")
async getHistoryPosMater(@Path() id: string) {
const posMaster = await this.posMasterRepository.findOne({
where: { id },
});
if (!posMaster) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id);
}
const posMasters = await this.posMasterRepository.find({
where: { ancestorDNA: posMaster.ancestorDNA },
order: { lastUpdatedAt: "DESC" },
relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"],
});
const _data = posMasters.map((item) => ({
id: item.id,
orgShortName:
item.orgRoot == null
? "-"
: item.orgChild1 == null
? item.orgRoot
: item.orgChild2 == null
? item.orgChild1.orgChild1ShortName
: item.orgChild3 == null
? item.orgChild2.orgChild2ShortName
: item.orgChild4 == null
? item.orgChild3.orgChild3ShortName
: item.orgChild4.orgChild4ShortName,
lastUpdatedAt: item.lastUpdatedAt,
posMasterNoPrefix: item.posMasterNoPrefix,
posMasterNo: item.posMasterNo,
posMasterNoSuffix: item.posMasterNoSuffix,
}));
return new HttpSuccess(_data);
}
/**
* API
*
* @summary ORG_054 - (ADMIN) #59
*
*/
@Post("move")
async movePosMaster(
@Body() requestBody: { id: string; type: number; positionMaster: string[] },
@Request() request: { user: Record<string, any> },
) {
try {
const posMasters = await this.posMasterRepository.find({
where: { id: In(requestBody.positionMaster) },
});
posMasters.forEach(async (posMaster: any) => {
posMaster.orgRootId = null;
posMaster.orgChild1Id = null;
posMaster.orgChild2Id = null;
posMaster.orgChild3Id = null;
posMaster.orgChild4Id = null;
if (requestBody.type == 0) {
const org = await this.orgRootRepository.findOne({
where: { id: requestBody.id },
});
if (org != null) {
posMaster.orgRootId = org.id;
posMaster.orgRevisionId = org.orgRevisionId;
}
}
if (requestBody.type == 1) {
const org = await this.child1Repository.findOne({
where: { id: requestBody.id },
});
if (org != null) {
posMaster.orgRootId = org.orgRootId;
posMaster.orgChild1Id = org.id;
posMaster.orgRevisionId = org.orgRevisionId;
}
}
if (requestBody.type == 2) {
const org = await this.child2Repository.findOne({
where: { id: requestBody.id },
});
if (org != null) {
posMaster.orgRootId = org.orgRootId;
posMaster.orgChild1Id = org.orgChild1Id;
posMaster.orgChild2Id = org.id;
posMaster.orgRevisionId = org.orgRevisionId;
}
}
if (requestBody.type == 3) {
const org = await this.child3Repository.findOne({
where: { id: requestBody.id },
});
if (org != null) {
posMaster.orgRootId = org.orgRootId;
posMaster.orgChild1Id = org.orgChild1Id;
posMaster.orgChild2Id = org.orgChild2Id;
posMaster.orgChild3Id = org.id;
posMaster.orgRevisionId = org.orgRevisionId;
}
}
if (requestBody.type == 4) {
const org = await this.child4Repository.findOne({
where: { id: requestBody.id },
});
if (org != null) {
posMaster.orgRootId = org.orgRootId;
posMaster.orgChild1Id = org.orgChild1Id;
posMaster.orgChild2Id = org.orgChild2Id;
posMaster.orgChild3Id = org.orgChild3Id;
posMaster.orgChild4Id = org.id;
posMaster.orgRevisionId = org.orgRevisionId;
}
}
posMaster.createdUserId = request.user.sub;
posMaster.createdFullName = request.user.name;
posMaster.lastUpdateUserId = request.user.sub;
posMaster.lastUpdateFullName = request.user.name;
await this.posMasterRepository.save(posMaster);
});
return new HttpSuccess();
} catch (error) {
return error;
}
}
}