hrms-api-kpi/src/controllers/KpiUserEvaluationController.ts

758 lines
28 KiB
TypeScript
Raw Normal View History

2024-04-22 13:23:31 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
2024-04-26 10:18:00 +07:00
ArrayValidator,
2024-04-22 13:23:31 +07:00
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { KpiPeriod } from "../entities/kpiPeriod";
2024-04-26 10:18:00 +07:00
import {
KpiUserEvaluation,
createKpiUserEvaluation,
updateKpiUserCheckEvaluation,
2024-04-26 10:18:00 +07:00
updateKpiUserEvaluation,
updateKpiUserPointEvaluation,
updateKpiUserStatusEvaluation,
updateKpiUserReqEditEvaluation,
updateKpiUserResultEvaluation,
2024-04-26 10:18:00 +07:00
} from "../entities/kpiUserEvaluation";
import { Like, In, Brackets } from "typeorm";
2024-04-22 13:23:31 +07:00
import CallAPI from "../interfaces/call-api";
@Route("api/v1/kpi/user/evaluation")
@Tags("kpiUserEvaluation")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class KpiUserEvaluationController extends Controller {
private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod);
private kpiUserEvalutionRepository = AppDataSource.getRepository(KpiUserEvaluation);
2024-04-26 10:18:00 +07:00
/**
* API
*
* @summary
*
*/
2024-05-09 16:08:21 +07:00
@Post("admin")
2024-04-26 10:18:00 +07:00
async listKpiAdminEvaluation(
@Request() request: { user: Record<string, any> },
2024-05-09 16:08:21 +07:00
@Body()
requestBody: {
page: number;
pageSize: number;
kpiPeriodId?: string;
keyword?: string;
status?: string | null;
results?: string | null;
reqedit?: string | null;
},
2024-04-26 10:18:00 +07:00
) {
let profileId: any = null;
await new CallAPI()
.GetData(request, "org/profile/keycloak/position")
.then((x) => {
profileId = x.profileId;
})
.catch((x) => {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ");
});
2024-05-13 18:14:08 +07:00
// let role = "EVALUATOR";
// if (profileId == item.commanderId) {
// role = "COMMANDER";
// } else if (profileId == item.commanderHighId) {
// role = "COMMANDERHIGH";
// }
2024-04-26 10:18:00 +07:00
const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation)
.createQueryBuilder("kpiUserEvaluation")
2024-05-09 16:08:21 +07:00
.andWhere(requestBody.kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", {
kpiPeriodId: requestBody.kpiPeriodId,
2024-05-09 15:20:49 +07:00
})
.andWhere(
2024-05-09 16:08:21 +07:00
requestBody.status != null && requestBody.status != undefined
2024-05-13 18:14:08 +07:00
? requestBody.status.trim().toUpperCase() == "NEW"
? `evaluationStatus LIKE CASE WHEN evaluatorId = "${profileId}" THEN "NEW_EVALUATOR" WHEN commanderId = "${profileId}" THEN "NEW_COMMANDER" WHEN commanderHighId = "${profileId}" THEN "NEW_COMMANDER_HIGH" ELSE "${requestBody.status.trim().toUpperCase()}" END`
: requestBody.status.trim().toUpperCase() == "EVALUATING"
? `evaluationStatus LIKE CASE WHEN evaluatorId = "${profileId}" THEN "EVALUATING_EVALUATOR" WHEN commanderId = "${profileId}" THEN "EVALUATING_COMMANDER" WHEN commanderHighId = "${profileId}" THEN "EVALUATING_COMMANDER_HIGH" ELSE "${requestBody.status.trim().toUpperCase()}" END`
: "evaluationStatus LIKE :status"
2024-05-09 16:08:21 +07:00
: "1=1",
{
2024-05-13 18:14:08 +07:00
status:
requestBody.status == null || requestBody.status == undefined
? null
: requestBody.status.trim().toUpperCase(),
2024-05-09 16:08:21 +07:00
},
)
.andWhere(
requestBody.results != null && requestBody.results != undefined
? "evaluationResults LIKE :results"
: "1=1",
2024-05-09 15:20:49 +07:00
{
2024-05-13 18:14:08 +07:00
results:
requestBody.results == null || requestBody.results == undefined
? null
: requestBody.results.trim().toUpperCase(),
2024-05-09 15:20:49 +07:00
},
)
.andWhere(
2024-05-09 16:08:21 +07:00
requestBody.reqedit != null && requestBody.reqedit != undefined
2024-05-13 18:14:08 +07:00
? requestBody.reqedit.trim().toUpperCase() == "NEW"
? `evaluationReqEdit LIKE CASE WHEN evaluatorId = "${profileId}" THEN "EVALUATOR" WHEN commanderId = "${profileId}" THEN "COMMANDER" WHEN commanderHighId = "${profileId}" THEN "COMMANDER_HIGH" ELSE "${requestBody.reqedit.trim().toUpperCase()}" END`
: "evaluationReqEdit LIKE :reqedit"
2024-05-09 16:08:21 +07:00
: "1=1",
2024-05-09 15:20:49 +07:00
{
2024-05-13 18:14:08 +07:00
reqedit:
requestBody.reqedit == null || requestBody.reqedit == undefined
? null
: requestBody.reqedit.trim().toUpperCase(),
2024-05-09 15:20:49 +07:00
},
)
.andWhere(
new Brackets((qb) => {
qb.orWhere("kpiUserEvaluation.evaluatorId LIKE :profileId", {
2024-05-13 18:14:08 +07:00
profileId: `${profileId}`,
})
.orWhere("kpiUserEvaluation.commanderId LIKE :profileId", {
2024-05-13 18:14:08 +07:00
profileId: `${profileId}`,
})
.orWhere("kpiUserEvaluation.commanderHighId LIKE :profileId", {
2024-05-13 18:14:08 +07:00
profileId: `${profileId}`,
});
}),
)
.andWhere(
new Brackets((qb) => {
2024-05-09 16:08:21 +07:00
qb.orWhere("kpiUserEvaluation.prefix LIKE :keyword", {
keyword: `%${requestBody.keyword}%`,
})
.orWhere("kpiUserEvaluation.firstName LIKE :keyword", {
keyword: `%${requestBody.keyword}%`,
})
.orWhere("kpiUserEvaluation.lastName LIKE :keyword", {
keyword: `%${requestBody.keyword}%`,
});
}),
)
2024-04-26 10:18:00 +07:00
.orderBy("kpiUserEvaluation.createdAt", "ASC")
2024-05-09 16:08:21 +07:00
.skip((requestBody.page - 1) * requestBody.pageSize)
.take(requestBody.pageSize)
2024-04-26 10:18:00 +07:00
.getManyAndCount();
const mapData = kpiUserEvaluation.map((item) => ({
id: item.id,
profileId: item.profileId,
prefix: item.prefix,
firstname: item.firstName,
lastname: item.lastName,
kpiPeriodId: item.kpiPeriodId,
evaluationStatus: item.evaluationStatus,
evaluationResults: item.evaluationResults,
createdAt: item.createdAt,
2024-04-26 13:17:25 +07:00
evaluatorId: item.evaluatorId,
commanderId: item.commanderId,
commanderHighId: item.commanderHighId,
2024-04-26 10:18:00 +07:00
}));
return new HttpSuccess({ data: mapData, total });
}
2024-04-22 13:23:31 +07:00
/**
* API (USER)
2024-04-26 10:18:00 +07:00
*
2024-04-22 13:23:31 +07:00
* @summary (USER)
2024-04-26 10:18:00 +07:00
*
*
2024-04-22 13:23:31 +07:00
*/
@Post()
async CreateKpiUserEvaluation(
@Body() requestBody: createKpiUserEvaluation,
@Request() request: { user: Record<string, any> },
2024-04-26 10:18:00 +07:00
) {
2024-04-22 13:23:31 +07:00
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: requestBody.kpiPeriodId },
});
if (!kpiPeriod) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
);
}
2024-04-26 10:18:00 +07:00
2024-04-22 13:23:31 +07:00
const kpiUserEvaluation = Object.assign(new KpiUserEvaluation(), requestBody);
await new CallAPI()
.GetData(request, "org/profile/keycloak/position")
.then((x) => {
2024-04-26 10:18:00 +07:00
kpiUserEvaluation.profileId = x.profileId;
kpiUserEvaluation.prefix = x.prefix;
kpiUserEvaluation.firstName = x.firstName;
kpiUserEvaluation.lastName = x.lastName;
2024-04-22 13:23:31 +07:00
})
.catch((x) => {});
2024-05-08 14:03:13 +07:00
kpiUserEvaluation.evaluationStatus = "NEW";
2024-04-26 10:18:00 +07:00
kpiUserEvaluation.evaluationResults = "PENDING";
kpiUserEvaluation.createdUserId = request.user.sub;
kpiUserEvaluation.createdFullName = request.user.name;
kpiUserEvaluation.lastUpdateUserId = request.user.sub;
kpiUserEvaluation.lastUpdateFullName = request.user.name;
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id);
2024-04-22 13:23:31 +07:00
}
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Put("check/{id}")
async updateKpiUserCheckEvaluation(
@Path() id: string,
@Body() requestBody: updateKpiUserCheckEvaluation,
@Request() request: { user: Record<string, any> },
) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
});
if (!kpiUserEvaluation) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
kpiUserEvaluation.lastUpdateUserId = request.user.sub;
kpiUserEvaluation.lastUpdateFullName = request.user.name;
Object.assign(kpiUserEvaluation, requestBody);
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id);
}
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Put("point/{id}")
async updateKpiUserPointEvaluation(
@Path() id: string,
@Body() requestBody: updateKpiUserPointEvaluation,
@Request() request: { user: Record<string, any> },
) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
});
if (!kpiUserEvaluation) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
kpiUserEvaluation.lastUpdateUserId = request.user.sub;
kpiUserEvaluation.lastUpdateFullName = request.user.name;
Object.assign(kpiUserEvaluation, requestBody);
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id);
}
2024-04-22 13:23:31 +07:00
/**
* API (USER)
2024-04-26 10:18:00 +07:00
*
2024-04-22 13:23:31 +07:00
* @summary (USER)
2024-04-26 10:18:00 +07:00
*
2024-04-22 13:23:31 +07:00
* @param {string} id Guid, *Id (USER)
*/
@Put("{id}")
async updateKpiUserEvaluation(
@Path() id: string,
@Body() requestBody: updateKpiUserEvaluation,
@Request() request: { user: Record<string, any> },
) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
});
if (!kpiUserEvaluation) {
2024-04-22 13:23:31 +07:00
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: requestBody.kpiPeriodId },
});
if (!kpiPeriod) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
);
}
kpiUserEvaluation.lastUpdateUserId = request.user.sub;
kpiUserEvaluation.lastUpdateFullName = request.user.name;
this.kpiUserEvalutionRepository.merge(kpiUserEvaluation, requestBody);
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id);
2024-04-22 13:23:31 +07:00
}
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Put("edit/{id}")
async updateKpiUserReqEditEvaluation(
@Path() id: string,
@Body() requestBody: updateKpiUserReqEditEvaluation,
@Request() request: { user: Record<string, any> },
) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
});
if (!kpiUserEvaluation) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
kpiUserEvaluation.evaluationReqEdit = requestBody.status.trim().toUpperCase();
kpiUserEvaluation.lastUpdateUserId = request.user.sub;
kpiUserEvaluation.lastUpdateFullName = request.user.name;
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id);
}
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Put("result/{id}")
async updateKpiUserResultEvaluation(
@Path() id: string,
@Body() requestBody: updateKpiUserResultEvaluation,
@Request() request: { user: Record<string, any> },
) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
});
if (!kpiUserEvaluation) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
kpiUserEvaluation.evaluationResults = requestBody.status.trim().toUpperCase();
kpiUserEvaluation.lastUpdateUserId = request.user.sub;
kpiUserEvaluation.lastUpdateFullName = request.user.name;
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id);
}
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Put("status/{id}")
async updateKpiUserStatusEvaluation(
@Path() id: string,
@Body() requestBody: updateKpiUserStatusEvaluation,
@Request() request: { user: Record<string, any> },
) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
});
if (!kpiUserEvaluation) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
kpiUserEvaluation.evaluationStatus = requestBody.status.trim().toUpperCase();
kpiUserEvaluation.lastUpdateUserId = request.user.sub;
kpiUserEvaluation.lastUpdateFullName = request.user.name;
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id);
}
2024-04-22 13:23:31 +07:00
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Get("{id}")
async GetKpiUserEvaluationById(@Path() id: string) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
2024-05-09 11:25:08 +07:00
relations: ["kpiPeriod"],
2024-04-22 13:23:31 +07:00
where: { id: id },
2024-04-26 10:18:00 +07:00
});
if (!kpiUserEvaluation) {
2024-04-22 13:23:31 +07:00
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
const mapData = {
id: kpiUserEvaluation.id,
profileId: kpiUserEvaluation.profileId,
prefix: kpiUserEvaluation.prefix,
firstName: kpiUserEvaluation.firstName,
lastName: kpiUserEvaluation.lastName,
evaluationStatus: kpiUserEvaluation.evaluationStatus,
evaluationResults: kpiUserEvaluation.evaluationResults,
2024-05-09 11:25:08 +07:00
evaluationReqEdit: kpiUserEvaluation.evaluationReqEdit,
createdAt: kpiUserEvaluation.createdAt,
evaluatorId: kpiUserEvaluation.evaluatorId,
commanderId: kpiUserEvaluation.commanderId,
commanderHighId: kpiUserEvaluation.commanderHighId,
plannedPoint: kpiUserEvaluation.plannedPoint,
rolePoint: kpiUserEvaluation.rolePoint,
specialPoint: kpiUserEvaluation.specialPoint,
capacityPoint: kpiUserEvaluation.capacityPoint,
kpiPeriodId: kpiUserEvaluation.kpiPeriodId,
year: kpiUserEvaluation.kpiPeriod == null ? null : kpiUserEvaluation.kpiPeriod.year,
2024-05-09 11:25:08 +07:00
durationKPI:
kpiUserEvaluation.kpiPeriod == null ? null : kpiUserEvaluation.kpiPeriod.durationKPI,
};
return new HttpSuccess(mapData);
2024-04-22 13:23:31 +07:00
}
/**
* API (USER)
*
* @summary (USER)
*
*/
@Get()
async listKpiUserEvaluation(
2024-05-08 16:17:07 +07:00
@Request() request: { user: Record<string, any> },
2024-04-22 13:23:31 +07:00
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
2024-04-22 15:24:44 +07:00
@Query("kpiPeriodId") kpiPeriodId?: string,
2024-05-08 16:17:07 +07:00
@Query("keyword") keyword?: string,
@Query("status") status?: string,
2024-04-22 13:23:31 +07:00
) {
const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation)
.createQueryBuilder("kpiUserEvaluation")
2024-05-09 11:25:08 +07:00
.leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod")
2024-04-26 10:18:00 +07:00
.andWhere(kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", {
2024-04-26 12:40:43 +07:00
kpiPeriodId: kpiPeriodId,
2024-04-26 10:18:00 +07:00
})
2024-05-08 16:17:07 +07:00
.andWhere({ createdUserId: request.user.sub })
.andWhere(
status == null || status == undefined ? "1=1" : "evaluationStatus LIKE :evaluationStatus",
{
evaluationStatus: status == undefined ? "" : status.trim().toUpperCase(),
},
)
2024-04-22 13:23:31 +07:00
.orderBy("kpiUserEvaluation.createdAt", "ASC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
const mapData = kpiUserEvaluation.map((item) => ({
id: item.id,
profileId: item.profileId,
prefix: item.prefix,
firstname: item.firstName,
lastname: item.lastName,
kpiPeriodId: item.kpiPeriodId,
2024-04-22 15:24:44 +07:00
evaluationStatus: item.evaluationStatus,
evaluationResults: item.evaluationResults,
2024-04-26 13:17:25 +07:00
evaluatorId: item.evaluatorId,
commanderId: item.commanderId,
commanderHighId: item.commanderHighId,
2024-04-22 15:24:44 +07:00
createdAt: item.createdAt,
plannedPoint: item.plannedPoint,
rolePoint: item.rolePoint,
specialPoint: item.specialPoint,
capacityPoint: item.capacityPoint,
year: item.kpiPeriod ? item.kpiPeriod.year : null,
durationKPI: item.kpiPeriod ? item.kpiPeriod.durationKPI : null,
2024-04-22 13:23:31 +07:00
}));
return new HttpSuccess({ data: mapData, total });
}
/**
* API (USER)
2024-04-26 10:18:00 +07:00
*
2024-04-22 13:23:31 +07:00
* @summary (USER)
2024-04-26 10:18:00 +07:00
*
2024-04-22 13:23:31 +07:00
* @param {string} id Guid, *Id (USER)
*/
@Delete("{id}")
async deleteKpiUserEvaluation(@Path() id: string) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
2024-04-22 13:23:31 +07:00
where: { id: id },
});
if (!kpiUserEvaluation) {
2024-04-22 13:23:31 +07:00
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
await this.kpiUserEvalutionRepository.remove(kpiUserEvaluation);
2024-04-22 13:23:31 +07:00
return new HttpSuccess();
}
2024-05-13 11:13:02 +07:00
/**
* API
*
* @summary
*
*
*/
@Post("admin/change-status")
async ChangeStatus(
@Request() request: { user: Record<string, any> },
@Body()
requestBody: {
status: string;
id: string[];
},
) {
2024-05-13 16:19:46 +07:00
let profileId: any = null;
await new CallAPI()
.GetData(request, "org/profile/keycloak/position")
.then((x) => {
profileId = x.profileId;
})
.catch((x) => {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ");
});
2024-05-13 11:13:02 +07:00
const list = await this.kpiUserEvalutionRepository.find({
where: { id: In(requestBody.id) },
});
if (!list || list.length === 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const hasAllData = requestBody.id.every((id) => list.some((item) => item.id === id));
if (!hasAllData) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "พบบางไอดีผู้ประเมินที่ไม่มีข้อมูล");
}
list.forEach(async (item) => {
2024-05-13 16:19:46 +07:00
let role = "EVALUATOR";
if (profileId == item.commanderId) {
role = "COMMANDER";
} else if (profileId == item.commanderHighId) {
role = "COMMANDERHIGH";
}
if (requestBody.status.trim().toUpperCase() == "APPROVE") {
if (role == "EVALUATOR") {
if (item.evaluationStatus == "NEW_EVALUATOR") {
if (item.commanderId == null || item.commanderId == "") {
2024-05-13 16:19:46 +07:00
item.evaluationStatus = "APPROVE";
} else {
item.evaluationStatus = "NEW_COMMANDER";
}
}
} else if (role == "COMMANDER") {
if (item.evaluationStatus == "NEW_COMMANDER") {
if (item.commanderHighId == null || item.commanderHighId == "") {
2024-05-13 16:19:46 +07:00
item.evaluationStatus = "APPROVE";
} else {
item.evaluationStatus = "NEW_COMMANDER_HIGH";
}
}
} else {
item.evaluationStatus = requestBody.status.trim().toUpperCase();
}
} else {
item.evaluationStatus = requestBody.status.trim().toUpperCase();
}
2024-05-13 11:13:02 +07:00
item.lastUpdateUserId = request.user.sub;
item.lastUpdateFullName = request.user.name;
await this.kpiUserEvalutionRepository.save(item);
});
return new HttpSuccess();
}
/**
* API
*
* @summary
*
*
*/
@Post("admin/req-edit")
async RequestEdit(
@Request() request: { user: Record<string, any> },
@Body()
requestBody: {
status: string;
id: string[];
},
) {
2024-05-13 16:19:46 +07:00
let profileId: any = null;
await new CallAPI()
.GetData(request, "org/profile/keycloak/position")
.then((x) => {
profileId = x.profileId;
})
.catch((x) => {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ");
});
2024-05-13 11:13:02 +07:00
const list = await this.kpiUserEvalutionRepository.find({
where: { id: In(requestBody.id) },
});
if (!list || list.length === 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const hasAllData = requestBody.id.every((id) => list.some((item) => item.id === id));
if (!hasAllData) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "พบบางไอดีผู้ประเมินที่ไม่มีข้อมูล");
}
2024-05-13 16:19:46 +07:00
2024-05-13 11:13:02 +07:00
list.forEach(async (item) => {
2024-05-13 16:19:46 +07:00
let role = "EVALUATOR";
if (profileId == item.commanderId) {
role = "COMMANDER";
} else if (profileId == item.commanderHighId) {
role = "COMMANDERHIGH";
}
if (requestBody.status.trim().toUpperCase() == "DONE") {
if (role == "EVALUATOR") {
if (item.evaluationReqEdit == "EVALUATOR") {
if (item.commanderId == null || item.commanderId == "") {
2024-05-13 16:19:46 +07:00
item.evaluationReqEdit = "DONE";
} else {
item.evaluationReqEdit = "COMMANDER";
}
}
} else if (role == "COMMANDER") {
if (item.evaluationReqEdit == "COMMANDER") {
if (item.commanderHighId == null || item.commanderHighId == "") {
2024-05-13 16:19:46 +07:00
item.evaluationReqEdit = "DONE";
} else {
item.evaluationReqEdit = "COMMANDER_HIGH";
}
}
} else {
item.evaluationReqEdit = requestBody.status.trim().toUpperCase();
}
} else {
item.evaluationReqEdit = requestBody.status.trim().toUpperCase();
}
2024-05-13 11:13:02 +07:00
item.lastUpdateUserId = request.user.sub;
item.lastUpdateFullName = request.user.name;
await this.kpiUserEvalutionRepository.save(item);
});
return new HttpSuccess();
}
2024-05-13 11:37:18 +07:00
2024-05-13 16:19:46 +07:00
/**
2024-05-13 11:37:18 +07:00
* API
*
* @summary
*
*
*/
2024-05-13 16:19:46 +07:00
@Post("admin/result-status")
async ResultStatus(
@Request() request: { user: Record<string, any> },
@Body()
requestBody: {
status: string;
id: string[];
},
) {
let profileId: any = null;
await new CallAPI()
.GetData(request, "org/profile/keycloak/position")
.then((x) => {
profileId = x.profileId;
})
.catch((x) => {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ");
});
const list = await this.kpiUserEvalutionRepository.find({
where: { id: In(requestBody.id) },
});
if (!list || list.length === 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const hasAllData = requestBody.id.every((id) => list.some((item) => item.id === id));
if (!hasAllData) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "พบบางไอดีผู้ประเมินที่ไม่มีข้อมูล");
}
list.forEach(async (item) => {
let role = "EVALUATOR";
if (profileId == item.commanderId) {
role = "COMMANDER";
} else if (profileId == item.commanderHighId) {
role = "COMMANDERHIGH";
}
if (requestBody.status.trim().toUpperCase() == "DONE") {
if (role == "EVALUATOR") {
if (item.evaluationStatus == "EVALUATING_EVALUATOR") {
if (item.commanderId == null || item.commanderId == "") {
2024-05-13 16:19:46 +07:00
item.evaluationStatus = "COMPLETE";
} else {
item.evaluationStatus = "EVALUATING_COMMANDER";
}
item.evaluationResults = requestBody.status.trim().toUpperCase();
}
} else if (role == "COMMANDER") {
if (item.evaluationStatus == "EVALUATING_COMMANDER") {
if (item.commanderHighId == null || item.commanderHighId == "") {
2024-05-13 16:19:46 +07:00
item.evaluationStatus = "COMPLETE";
} else {
item.evaluationStatus = "EVALUATING_COMMANDER_HIGH";
}
}
} else {
item.evaluationStatus = "COMPLETE";
}
} else {
// item.evaluationStatus = requestBody.status.trim().toUpperCase();
}
item.lastUpdateUserId = request.user.sub;
item.lastUpdateFullName = request.user.name;
await this.kpiUserEvalutionRepository.save(item);
});
return new HttpSuccess();
}
2024-04-22 13:23:31 +07:00
}