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

This commit is contained in:
Kittapath 2024-04-19 17:36:02 +07:00
commit e29c7c127f
5 changed files with 312 additions and 171 deletions

View file

@ -70,27 +70,23 @@ async updateKpiEvaluations(
*/ */
@Get() @Get()
async listKpiEvaluation( async listKpiEvaluation(
// @Query("page") page: number = 1, @Query("page") page: number = 1,
// @Query("pageSize") pageSize: number = 10, @Query("pageSize") pageSize: number = 10,
// @Query("keyword") keyword?: string, @Query("keyword") keyword?: string,
) { ) {
let whereClause: any = {}; let whereClause: any = {};
// if (keyword !== undefined && keyword !== "") { if (keyword !== undefined && keyword !== "") {
// whereClause = { whereClause = {
// where: [{ description: Like(`%${keyword}%`) }], where: [{ description: Like(`%${keyword}%`) }],
// }; };
// whereClause.where.push({ level: Like(`%${keyword}%`) }); whereClause.where.push({ level: Like(`%${keyword}%`) });
// } }
// const [kpiEvaluation, total] = await this.kpiEvaluationRepository.findAndCount({ const [kpiEvaluation, total] = await this.kpiEvaluationRepository.findAndCount({
// ...whereClause, ...whereClause,
// ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
// order:{
// level: "DESC"}
// });
const kpiEvaluation = await this.kpiEvaluationRepository.find({
order:{ order:{
level: "DESC"} level: "DESC"}
}); });
@ -100,6 +96,6 @@ async updateKpiEvaluations(
level: item.level, level: item.level,
description: item.description description: item.description
})); }));
return new HttpSuccess(formatted); return new HttpSuccess({ data: formatted, total });
} }
} }

View file

@ -1,159 +1,285 @@
// import { import {
// Controller, Controller,
// Get, Get,
// Post, Post,
// Put, Put,
// Delete, Delete,
// Route, Route,
// Security, Security,
// Tags, Tags,
// Body, Body,
// Path, Path,
// Request, Request,
// Example, Example,
// 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 HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
// import { Like, Not } from "typeorm"; import { Like, Not, In, Brackets } from "typeorm";
// import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
// import { KpiLink, createKpiLink, updateKpiLink } from "../entities/kpiLink"; import { KpiLink, createKpiLink, updateKpiLink } from "../entities/kpiLink";
// @Route("api/v1/kpi/link") import { KpiGroup } from "../entities/kpiGroup";
// @Tags("kpiLink") import { KpiCapacity } from "../entities/kpiCapacity";
// @Security("bearerAuth") import { Position } from "../entities/position";
// @Response( @Route("api/v1/kpi/link")
// HttpStatusCode.INTERNAL_SERVER_ERROR, @Tags("kpiLink")
// "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", @Security("bearerAuth")
// ) @Response(
// @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") HttpStatusCode.INTERNAL_SERVER_ERROR,
// export class kpiLinkController extends Controller { "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
// private kpiLinkRepository = AppDataSource.getRepository(KpiLink); )
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class kpiLinkController extends Controller {
private kpiGroupRepository = AppDataSource.getRepository(KpiGroup);
private kpiLinkRepository = AppDataSource.getRepository(KpiLink);
private kpiCapacityRepository = AppDataSource.getRepository(KpiCapacity);
private positionRepository = AppDataSource.getRepository(Position);
// /** /**
// * API สร้างเชื่อมโยง * API
// * @param requestBody * @param requestBody
// * @returns * @returns
// */ */
// @Post() @Post()
// @Example({ async createKpiLink(
// nameLinkKPI: "string", //ชื่อเชื่อมโยง @Body() requestBody: createKpiLink,
// }) @Request() request: { user: Record<string, any> },
// async createKpiLink( ) {
// @Body() requestBody: createKpiLink, const chkkpiGroup = await this.kpiGroupRepository.findOne({
// @Request() request: { user: Record<string, any> }, where: {
// ) { id: requestBody.kpiGroupId,
// const kpiLink = Object.assign(new KpiLink(), requestBody); },
// const chkkpinameLink = await this.kpiLinkRepository.findOne({ });
// where: {
// nameLinkKPI: requestBody.nameLinkKPI,
// },
// });
// if (chkkpinameLink) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อเชื่อมโยงนี้มีอยู่ในระบบแล้ว");
// }
// kpiLink.createdUserId = request.user.sub;
// kpiLink.createdFullName = request.user.name;
// kpiLink.lastUpdateUserId = request.user.sub;
// kpiLink.lastUpdateFullName = request.user.name;
// await this.kpiLinkRepository.save(kpiLink);
// return new HttpSuccess(kpiLink.id);
// }
// /** if (!chkkpiGroup) {
// * API แก้ไขชื่อเชื่อมโยง throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
// * @param id ไอดีของเชื่อมโยง }
// */
// @Put("{id}")
// async updateKpiLink(
// @Path() id: string,
// @Body() requestBody: updateKpiLink,
// @Request() request: { user: Record<string, any> },
// ) {
// const kpiLink = await this.kpiLinkRepository.findOne({
// where: { id: id },
// });
// if (!kpiLink) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงนี้");
// }
// const chkkpinameLink = await this.kpiLinkRepository.findOne({ const kpiLink = Object.assign(new KpiLink(), requestBody, {
// where: { createdUserId: request.user.sub,
// nameLinkKPI: requestBody.nameLinkKPI, createdFullName: request.user.name,
// }, lastUpdateUserId: request.user.sub,
// }); lastUpdateFullName: request.user.name,
// if (chkkpinameLink) { kpiGroup: chkkpiGroup,
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อเชื่อมโยงนี้มีอยู่ในระบบแล้ว"); });
// } await this.kpiLinkRepository.save(kpiLink);
// this.kpiLinkRepository.merge(kpiLink, requestBody);
// kpiLink.lastUpdateUserId = request.user.sub;
// kpiLink.lastUpdateFullName = request.user.name;
// await this.kpiLinkRepository.save(kpiLink);
// return new HttpSuccess(id);
// }
// /** if (requestBody.positions != null) {
// * API ชื่อเชื่อมโยง Promise.all(
// * @param id requestBody.positions.map(async (positionName) => {
// */ let position = new Position();
// @Get("{id}") position.name = positionName;
// @Example({ position.kpiLinkId = kpiLink.id;
// nameLinkKPI: "string", //ชื่อเชื่อมโยง position.createdUserId = request.user.sub;
// }) position.createdFullName = request.user.name;
// async KpiLinkById(@Path() id: string) { position.lastUpdateUserId = request.user.sub;
// const kpiLink = await this.kpiLinkRepository.findOne({ position.lastUpdateFullName = request.user.name;
// where: { id: id }, await this.positionRepository.save(position);
// select: ["nameLinkKPI"], }),
// }); );
// if (!kpiLink) { }
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงนี้");
// }
// return new HttpSuccess(kpiLink);
// }
// /** const chkCapacity = await this.kpiCapacityRepository.find({
// * API ลบเชื่อมโยง where: {
// * @param id id: In(requestBody.kpiCapacityIds),
// */ },
// @Delete("{id}") });
// async deleteKpiLink(@Path() id: string) { if (!chkCapacity) {
// const kpiLink = await this.kpiLinkRepository.findOne({ throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
// where: { id: id }, }
// }); kpiLink.kpiCapacitys = chkCapacity;
// if (!kpiLink) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงนี้");
// }
// await this.kpiLinkRepository.remove(kpiLink); await this.kpiLinkRepository.save(kpiLink);
// return new HttpSuccess();
// }
// /** return new HttpSuccess(kpiLink.id);
// * API list เชื่อมโยง }
// * @param page
// * @param pageSize
// */
// @Get()
// async listKpiLink(
// @Query("page") page: number = 1,
// @Query("pageSize") pageSize: number = 10,
// @Query("keyword") keyword?: string,
// ) {
// let whereClause: any = {};
// if (keyword !== undefined && keyword !== "") {
// whereClause = {
// where: [{ nameLinkKPI: Like(`%${keyword}%`) }],
// };
// }
// const [kpiLink, total] = await this.kpiLinkRepository.findAndCount({ /**
// ...whereClause, * API
// ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), * @param id
// }); */
@Put("{id}")
async updateKpiLink(
@Path() id: string,
@Body() requestBody: createKpiLink,
@Request() request: { user: Record<string, any> },
) {
const chkKpiLink = await this.kpiLinkRepository.findOne({
where: {
id: id,
},
relations: {
positions: true,
kpiCapacitys: true,
},
})
if (!chkKpiLink) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
}
// return new HttpSuccess({ data: kpiLink, total }); await this.positionRepository.remove(chkKpiLink.positions);
// } Object.assign(chkKpiLink, {
// } ...requestBody,
kpiCapacitys: [],
});
chkKpiLink.kpiGroupId = requestBody.kpiGroupId,
chkKpiLink.lastUpdateUserId = request.user.sub;
chkKpiLink.lastUpdateFullName = request.user.name;
if (requestBody.positions != null) {
Promise.all(
requestBody.positions.map(async (positionName) => {
let position = new Position();
position.name = positionName;
position.kpiLinkId = chkKpiLink.id;
position.createdUserId = request.user.sub;
position.createdFullName = request.user.name;
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
await this.positionRepository.save(position);
}),
);
}
const chkCapacity = await this.kpiCapacityRepository.find({
where: {
id: In(requestBody.kpiCapacityIds),
},
});
if (!chkCapacity) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
}
chkKpiLink.kpiCapacitys = chkCapacity;
await this.kpiLinkRepository.save(chkKpiLink);
return new HttpSuccess(chkKpiLink.id);
}
/**
* API
* @param id
*/
@Get("{id}")
async KpiLinkById(@Path() id: string) {
const kpiLink = await this.kpiLinkRepository.findOne({
where: { id: id },
relations:["positions","kpiCapacitys","kpiGroup"],
order:{
createdAt: "ASC"
}
});
if (!kpiLink) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
}
const formattedResponse = {
id: kpiLink.id,
groupName: kpiLink.kpiGroup.nameGroupKPI,
positions: kpiLink.positions.map(position => ({
id: position.id,
name: position.name
})),
capacitys: kpiLink.kpiCapacitys.map(capacity => ({
id: capacity.id ,
name: capacity.name,
type: capacity.type,
description: capacity.description
}))
};
return new HttpSuccess(formattedResponse);
}
/**
* API
* @param id
*/
@Delete("{id}")
async deleteKpiLink(@Path() id: string) {
const kpiLink = await this.kpiLinkRepository.findOne({
where: { id: id },
relations: ["kpiCapacitys"],
});
if (!kpiLink) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
}
kpiLink.kpiCapacitys = [];
await this.kpiLinkRepository.save(kpiLink);
await this.positionRepository.delete({ kpiLinkId: id });
await this.kpiLinkRepository.delete({ id: id });
return new HttpSuccess();
}
/**
* API list
* @param page
* @param pageSize
*/
@Get()
async listKpiLink(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
) {
const [kpiLink , total] = await AppDataSource.getRepository(KpiLink)
.createQueryBuilder("kpiLink")
.leftJoinAndSelect("kpiLink.kpiGroup", "kpiGroup")
.leftJoinAndSelect("kpiLink.positions", "positions")
.leftJoinAndSelect("kpiLink.kpiCapacitys", "kpiCapacitys")
.andWhere(
new Brackets((qb) => {
qb.where(
keyword != null && keyword != ""
? "kpiGroup.nameGroupKPI LIKE :keyword"
: "1=1",
{
keyword: `%${keyword}%`,
},
)
.orWhere(
keyword != null && keyword != ""
? "positions.name LIKE :keyword"
: "1=1",
{
keyword: `%${keyword}%`,
},
)
.orWhere(
keyword != null && keyword != ""
? "kpiCapacitys.name LIKE :keyword"
: "1=1",
{
keyword: `%${keyword}%`,
},
)
}),
)
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
if (!kpiLink) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
}
const formattedResponse = kpiLink.map((item) => ({
id: item.id,
groupName: item.kpiGroup.nameGroupKPI,
positions: item.positions.map(position => ({
id: position.id,
name: position.name
})),
capacitys: item.kpiCapacitys.map(capacity => ({
id: capacity.id ,
name: capacity.name,
type: capacity.type,
description: capacity.description
}))
}));
return new HttpSuccess({ data: formattedResponse, total });
}
}

View file

@ -274,9 +274,6 @@ export class kpiPlanController extends Controller {
round: `${round?.trim().toUpperCase()}`, round: `${round?.trim().toUpperCase()}`,
}, },
) )
.andWhere(keyword != undefined ? "kpiPlan.projectName LIKE :keyword" : "1=1", {
keyword: `%${keyword}%`,
})
.select([ .select([
"kpiPlan.id", "kpiPlan.id",
"kpiPlan.year", "kpiPlan.year",

View file

@ -193,6 +193,7 @@ export class kpiRoleController extends Controller {
@Query("nodeId") nodeId?: string | null, @Query("nodeId") nodeId?: string | null,
@Query("node") node?: number | null, @Query("node") node?: number | null,
@Query("keyword") keyword?: string, @Query("keyword") keyword?: string,
@Query("position") position?: string,
) { ) {
const [kpiRole, total] = await AppDataSource.getRepository(KpiRole) const [kpiRole, total] = await AppDataSource.getRepository(KpiRole)
.createQueryBuilder("kpiRole") .createQueryBuilder("kpiRole")
@ -218,8 +219,8 @@ export class kpiRoleController extends Controller {
round: `${round?.trim().toUpperCase()}`, round: `${round?.trim().toUpperCase()}`,
}, },
) )
.andWhere(keyword != undefined ? "kpiRole.projectName LIKE :keyword" : "1=1", { .andWhere(position != undefined ? "kpiRole.position LIKE :position" : "1=1", {
keyword: `%${keyword}%`, position: `%${position}%`,
}) })
.select([ .select([
"kpiRole.id", "kpiRole.id",

View file

@ -23,3 +23,24 @@ export class KpiLink extends EntityBase {
@OneToMany(() => Position, (position) => position.kpiLink) @OneToMany(() => Position, (position) => position.kpiLink)
positions: Position[]; positions: Position[];
} }
export class createKpiLink {
@Column()
kpiGroupId: string;
@Column()
positions: string[];
@Column()
kpiCapacityIds: string[];
}
export class updateKpiLink {
@Column()
kpiGroupId: string;
@Column()
positions: string[];
@Column()
kpiCapacityIds: string[];
}