Merge branch 'develop' of github.com:Frappet/bma-ehr-kpi into develop

This commit is contained in:
Kittapath 2024-05-09 11:11:49 +07:00
commit 5d9e731947
3 changed files with 258 additions and 231 deletions

View file

@ -1,68 +1,75 @@
import { import {
Controller, Controller,
Get, Get,
Post, Post,
Put, Put,
Delete, Delete,
Route, Route,
Security, Security,
Tags, Tags,
Body, Body,
Path, Path,
Request, Request,
SuccessResponse, SuccessResponse,
Response, Response,
Query, Query,
} from "tsoa"; } from "tsoa";
import { AppDataSource } from "../database/data-source"; import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import { import {
KpiUserDevelopment, KpiUserDevelopment,
CreateKpiUserDevelopment, CreateKpiUserDevelopment,
UpdateKpiUserDevelopment, UpdateKpiUserDevelopment,
KpiUserDevelopmentDataPoint, KpiUserDevelopmentDataPoint,
} from "../entities/kpiUserDevelopment"; } from "../entities/kpiUserDevelopment";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation"; import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
import { Not } from "typeorm"; import { Not, Like } from "typeorm";
@Route("api/v1/kpi/user/achievement/evelopment") @Route("api/v1/kpi/user/achievement/development")
@Tags("KpiUserDevelopment") @Tags("KpiUserDevelopment")
@Security("bearerAuth") @Security("bearerAuth")
@Response( @Response(
HttpStatusCode.INTERNAL_SERVER_ERROR, HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
) )
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") @SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class KpiUserDevelopmentController extends Controller { export class KpiUserDevelopmentController extends Controller {
private kpiUserDevelopmentRepository = AppDataSource.getRepository(KpiUserDevelopment); private kpiUserDevelopmentRepository = AppDataSource.getRepository(KpiUserDevelopment);
private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation); private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation);
/** /**
* API * API
* *
* @summary - # * @summary - #
* *
*/ */
@Post() @Post()
async createKpiUserDevelopment( async createKpiUserDevelopment(
@Body() @Body()
requestBody: CreateKpiUserDevelopment, requestBody: CreateKpiUserDevelopment,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({ const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where: { id: requestBody.kpiUserEvaluationId }, where: { id: requestBody.kpiUserEvaluationId },
}); });
if (!chkUserEvaluation) { if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
} }
const kpiUserDevelopment = Object.assign(new KpiUserDevelopment(), requestBody); const chkName = await this.kpiUserDevelopmentRepository.findOne({
if (!kpiUserDevelopment) { where: { name: requestBody.name},
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); });
} if (chkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "มีชื่อนี้ในระบบแล้ว");
}
const kpiUserDevelopment = Object.assign(new KpiUserDevelopment(), requestBody);
if (!kpiUserDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
// const chk_indicator = await this.kpiUserDevelopmentRepository.findOne({ // const chk_indicator = await this.kpiUserDevelopmentRepository.findOne({
// where: { // where: {
// kpiUserEvaluationId: requestBody.kpiUserEvaluationId, // kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
@ -77,39 +84,49 @@ import {
// "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", // "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
// ); // );
// } // }
kpiUserDevelopment.createdUserId = request.user.sub; kpiUserDevelopment.createdUserId = request.user.sub;
kpiUserDevelopment.createdFullName = request.user.name; kpiUserDevelopment.createdFullName = request.user.name;
kpiUserDevelopment.lastUpdateUserId = request.user.sub; kpiUserDevelopment.lastUpdateUserId = request.user.sub;
kpiUserDevelopment.lastUpdateFullName = request.user.name; kpiUserDevelopment.lastUpdateFullName = request.user.name;
await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment); await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment);
return new HttpSuccess(kpiUserDevelopment.id); return new HttpSuccess(kpiUserDevelopment.id);
}
/**
* API
*
* @summary - #
*
* @param {string} id Id
*/
@Put("{id}")
async editKpiUserDevelopment(
@Path() id: string,
@Body() requestBody: UpdateKpiUserDevelopment,
@Request() request: { user: Record<string, any> },
) {
const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({ where: { id } });
if (!kpiUserDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้");
}
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
const chkName = await this.kpiUserDevelopmentRepository.find({
where: {
id: Not(id),
name: requestBody.name
},
});
if (chkName && chkName.length > 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "มีชื่อนี้ในระบบแล้ว");
} }
/**
* API
*
* @summary - #
*
* @param {string} id Id
*/
@Put("{id}")
async editKpiUserDevelopment(
@Path() id: string,
@Body() requestBody: UpdateKpiUserDevelopment,
@Request() request: { user: Record<string, any> },
) {
const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({ where: { id } });
if (!kpiUserDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้");
}
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
// const chk_indicator = await this.kpiUserDevelopmentRepository.findOne({ // const chk_indicator = await this.kpiUserDevelopmentRepository.findOne({
// where: { // where: {
// id: Not(id), // id: Not(id),
@ -125,118 +142,122 @@ import {
// "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", // "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
// ); // );
// } // }
kpiUserDevelopment.lastUpdateUserId = request.user.sub; kpiUserDevelopment.lastUpdateUserId = request.user.sub;
kpiUserDevelopment.lastUpdateFullName = request.user.name; kpiUserDevelopment.lastUpdateFullName = request.user.name;
Object.assign(kpiUserDevelopment, requestBody); Object.assign(kpiUserDevelopment, requestBody);
await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment); await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment);
return new HttpSuccess(kpiUserDevelopment.id); return new HttpSuccess(kpiUserDevelopment.id);
}
/**
* API
*
* @summary - #
*
*/
@Delete("{id}")
async deleteKpiUserDevelopment(@Path() id: string) {
const delKpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({ where: { id } });
if (!delKpiUserDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้");
}
await this.kpiUserDevelopmentRepository.remove(delKpiUserDevelopment);
return new HttpSuccess();
}
/**
* API
*
* @summary - #
*
* @param {string} id Id
*/
@Get("{id}")
async GetKpiUserDevelopmentDetail(@Path() id: string) {
const getKpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({
relations: ["kpiUserEvaluation"],
where: { id: id },
});
if (!getKpiUserDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้");
}
const mapKpiUserDevelopment = {
id: getKpiUserDevelopment.id,
evaluationId: getKpiUserDevelopment.kpiUserEvaluation.id,
target: getKpiUserDevelopment.target,
summary: getKpiUserDevelopment.summary,
name: getKpiUserDevelopment.name,
achievement10: getKpiUserDevelopment.achievement10,
achievement5: getKpiUserDevelopment.achievement5,
achievement0: getKpiUserDevelopment.achievement0,
};
return new HttpSuccess(mapKpiUserDevelopment);
}
/**
* API
*
* @summary - #
*
*/
@Get()
async GetKpiUserDevelopment(@Query("id") id: string) {
const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.find({
where: {
kpiUserEvaluationId: id,
},
relations: ["kpiUserEvaluation"],
order: { createdAt: "ASC" },
});
const mapKpiUserDevelopment = kpiUserDevelopment.map((item) => ({
id: item.id,
evaluationId: item.kpiUserEvaluation.id,
target: item.target,
summary: item.summary,
name: item.name,
achievement10: item.achievement10,
achievement5: item.achievement5,
achievement0: item.achievement0,
}));
return new HttpSuccess(mapKpiUserDevelopment);
}
/**
* API
*
* @summary
*
*
*/
@Post("point")
async CreateKpiUserDevelopmentPoint(
@Body() requestBody: KpiUserDevelopmentDataPoint[],
@Request() request: { user: Record<string, any> },
) {
for (const item of requestBody) {
const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({
where: { id: item.id },
});
if (!kpiUserDevelopment) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
`ไม่พบข้อมูลพัฒนาตนเองนี้: ${item.id}`,
);
}
this.kpiUserDevelopmentRepository.merge(kpiUserDevelopment, item);
kpiUserDevelopment.lastUpdateUserId = request.user.sub;
kpiUserDevelopment.lastUpdateFullName = request.user.name;
await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment);
}
return new HttpSuccess();
}
} }
/**
* API
*
* @summary - #
*
*/
@Delete("{id}")
async deleteKpiUserDevelopment(@Path() id: string) {
const delKpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({
where: { id },
});
if (!delKpiUserDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้");
}
await this.kpiUserDevelopmentRepository.remove(delKpiUserDevelopment);
return new HttpSuccess();
}
/**
* API
*
* @summary - #
*
* @param {string} id Id
*/
@Get("{id}")
async GetKpiUserDevelopmentDetail(@Path() id: string) {
const getKpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({
relations: ["kpiUserEvaluation"],
where: { id: id },
});
if (!getKpiUserDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้");
}
const mapKpiUserDevelopment = {
id: getKpiUserDevelopment.id,
evaluationId: getKpiUserDevelopment.kpiUserEvaluation.id,
target: getKpiUserDevelopment.target,
summary: getKpiUserDevelopment.summary,
name: getKpiUserDevelopment.name,
achievement10: getKpiUserDevelopment.achievement10,
achievement5: getKpiUserDevelopment.achievement5,
achievement0: getKpiUserDevelopment.achievement0,
isDevelopment70: getKpiUserDevelopment.isDevelopment70,
isDevelopment20: getKpiUserDevelopment.isDevelopment20,
isDevelopment10: getKpiUserDevelopment.isDevelopment10,
};
return new HttpSuccess(mapKpiUserDevelopment);
}
/**
* API
*
* @summary - #
*
*/
@Get()
async GetKpiUserDevelopment(@Query("id") id: string) {
const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.find({
where: {
kpiUserEvaluationId: id,
},
relations: ["kpiUserEvaluation"],
order: { createdAt: "ASC" },
});
const mapKpiUserDevelopment = kpiUserDevelopment.map((item) => ({
id: item.id,
evaluationId: item.kpiUserEvaluation.id,
target: item.target,
summary: item.summary,
name: item.name,
achievement10: item.achievement10,
achievement5: item.achievement5,
achievement0: item.achievement0,
isDevelopment70: item.isDevelopment70,
isDevelopment20: item.isDevelopment20,
isDevelopment10: item.isDevelopment10,
}));
return new HttpSuccess(mapKpiUserDevelopment);
}
// /**
// * API กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี
// *
// * @summary กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี
// *
// *
// */
// @Post("point")
// async CreateKpiUserDevelopmentPoint(
// @Body() requestBody: KpiUserDevelopmentDataPoint[],
// @Request() request: { user: Record<string, any> },
// ) {
// for (const item of requestBody) {
// const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({
// where: { id: item.id },
// });
// if (!kpiUserDevelopment) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลพัฒนาตนเองนี้: ${item.id}`);
// }
// this.kpiUserDevelopmentRepository.merge(kpiUserDevelopment, item);
// kpiUserDevelopment.lastUpdateUserId = request.user.sub;
// kpiUserDevelopment.lastUpdateFullName = request.user.name;
// await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment);
// }
// return new HttpSuccess();
// }
}

View file

@ -240,25 +240,8 @@ export class KpiUserEvaluationController extends Controller {
@Get("{id}") @Get("{id}")
async GetKpiUserEvaluationById(@Path() id: string) { async GetKpiUserEvaluationById(@Path() id: string) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
relations:["kpiPeriod"],
where: { id: id }, where: { id: id },
select: [
"id",
"profileId",
"prefix",
"firstName",
"lastName",
"kpiPeriodId",
"evaluationStatus",
"evaluationResults",
"createdAt",
"evaluatorId",
"commanderId",
"commanderHighId",
"plannedPoint",
"rolePoint",
"specialPoint",
"capacityPoint",
],
}); });
if (!kpiUserEvaluation) { if (!kpiUserEvaluation) {
throw new HttpError( throw new HttpError(
@ -266,7 +249,27 @@ export class KpiUserEvaluationController extends Controller {
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
); );
} }
return new HttpSuccess(kpiUserEvaluation); const mapData = {
id: kpiUserEvaluation.id,
profileId: kpiUserEvaluation.profileId,
prefix: kpiUserEvaluation.prefix,
firstName: kpiUserEvaluation.firstName,
lastName: kpiUserEvaluation.lastName,
evaluationStatus: kpiUserEvaluation.evaluationStatus,
evaluationResults: kpiUserEvaluation.evaluationResults,
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,
durationKPI: kpiUserEvaluation.kpiPeriod == null ? null : kpiUserEvaluation.kpiPeriod.durationKPI,
}
return new HttpSuccess(mapData);
} }
/** /**
@ -286,6 +289,7 @@ export class KpiUserEvaluationController extends Controller {
) { ) {
const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation) const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation)
.createQueryBuilder("kpiUserEvaluation") .createQueryBuilder("kpiUserEvaluation")
.leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod")
.andWhere(kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", { .andWhere(kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", {
kpiPeriodId: kpiPeriodId, kpiPeriodId: kpiPeriodId,
}) })
@ -318,6 +322,8 @@ export class KpiUserEvaluationController extends Controller {
rolePoint: item.rolePoint, rolePoint: item.rolePoint,
specialPoint: item.specialPoint, specialPoint: item.specialPoint,
capacityPoint: item.capacityPoint, capacityPoint: item.capacityPoint,
year: item.kpiPeriod ? item.kpiPeriod.year : null,
durationKPI: item.kpiPeriod ? item.kpiPeriod.durationKPI : null,
})); }));
return new HttpSuccess({ data: mapData, total }); return new HttpSuccess({ data: mapData, total });
} }

View file

@ -87,7 +87,7 @@ export class KpiUserDevelopment extends EntityBase {
export class CreateKpiUserDevelopment { export class CreateKpiUserDevelopment {
@Column() @Column()
name: string | null; name: string;
@Column() @Column()
target: string | null; target: string | null;
@Column() @Column()
@ -110,7 +110,7 @@ export class CreateKpiUserDevelopment {
export class UpdateKpiUserDevelopment { export class UpdateKpiUserDevelopment {
@Column() @Column()
name: string | null; name: string;
@Column() @Column()
target: string | null; target: string | null;
@Column() @Column()