checkpoint

This commit is contained in:
AdisakKanthawilang 2024-04-19 16:19:09 +07:00
parent b70f447174
commit 85f39a55a6
3 changed files with 260 additions and 166 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,236 @@
// 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 } 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 (!chkkpiGroup) {
// }, throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
// }); }
// if (chkkpinameLink) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อเชื่อมโยงนี้มีอยู่ในระบบแล้ว"); const kpiLink = Object.assign(new KpiLink(), requestBody, {
// } createdUserId: request.user.sub,
// kpiLink.createdUserId = request.user.sub; createdFullName: request.user.name,
// kpiLink.createdFullName = request.user.name; lastUpdateUserId: request.user.sub,
// kpiLink.lastUpdateUserId = request.user.sub; lastUpdateFullName: request.user.name,
// kpiLink.lastUpdateFullName = request.user.name; kpiGroup: chkkpiGroup,
// await this.kpiLinkRepository.save(kpiLink); });
// return new HttpSuccess(kpiLink.id); await this.kpiLinkRepository.save(kpiLink);
// }
if (requestBody.positions != null) {
Promise.all(
requestBody.positions.map(async (positionName) => {
let position = new Position();
position.name = positionName;
position.kpiLinkId = kpiLink.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({
// * API แก้ไขชื่อเชื่อมโยง where: {
// * @param id ไอดีของเชื่อมโยง id: In(requestBody.kpiCapacityIds),
// */ },
// @Put("{id}") });
// async updateKpiLink( if (!chkCapacity) {
// @Path() id: string, throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
// @Body() requestBody: updateKpiLink, }
// @Request() request: { user: Record<string, any> }, kpiLink.kpiCapacitys = chkCapacity;
// ) {
// const kpiLink = await this.kpiLinkRepository.findOne({ await this.kpiLinkRepository.save(kpiLink);
// where: { id: id },
// });
// if (!kpiLink) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงนี้");
// }
// const chkkpinameLink = await this.kpiLinkRepository.findOne({ return new HttpSuccess(kpiLink.id);
// where: { }
// nameLinkKPI: requestBody.nameLinkKPI,
// },
// });
// if (chkkpinameLink) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อเชื่อมโยงนี้มีอยู่ในระบบแล้ว");
// }
// this.kpiLinkRepository.merge(kpiLink, requestBody);
// kpiLink.lastUpdateUserId = request.user.sub;
// kpiLink.lastUpdateFullName = request.user.name;
// await this.kpiLinkRepository.save(kpiLink);
// return new HttpSuccess(id);
// }
// /** /**
// * API ชื่อเชื่อมโยง * API
// * @param id * @param id
// */ */
// @Get("{id}") @Put("{id}")
// @Example({ async updateKpiLink(
// nameLinkKPI: "string", //ชื่อเชื่อมโยง @Path() id: string,
// }) @Body() requestBody: createKpiLink,
// async KpiLinkById(@Path() id: string) { @Request() request: { user: Record<string, any> },
// const kpiLink = await this.kpiLinkRepository.findOne({ ) {
// where: { id: id }, const chkKpiLink = await this.kpiLinkRepository.findOne({
// select: ["nameLinkKPI"], where: {
// }); id: id,
// if (!kpiLink) { },
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงนี้"); relations: {
// } positions: true,
// return new HttpSuccess(kpiLink); kpiCapacitys: true,
// } },
})
if (!chkKpiLink) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
}
// /** await this.positionRepository.remove(chkKpiLink.positions);
// * API ลบเชื่อมโยง Object.assign(chkKpiLink, {
// * @param id ...requestBody,
// */ positions: [],
// @Delete("{id}") kpiCapacitys: [],
// async deleteKpiLink(@Path() id: string) { });
// const kpiLink = await this.kpiLinkRepository.findOne({ chkKpiLink.kpiGroupId = requestBody.kpiGroupId,
// where: { id: id }, chkKpiLink.lastUpdateUserId = request.user.sub;
// }); chkKpiLink.lastUpdateFullName = request.user.name;
// if (!kpiLink) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงนี้"); 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);
}),
);
}
// await this.kpiLinkRepository.remove(kpiLink); const chkCapacity = await this.kpiCapacityRepository.find({
// return new HttpSuccess(); 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 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 = { * API
// where: [{ nameLinkKPI: Like(`%${keyword}%`) }], * @param id
// }; */
// } @Get("{id}")
async KpiLinkById(@Path() id: string) {
const kpiLink = await this.kpiLinkRepository.findOne({
where: { id: id },
relations:["positions","kpiCapacitys"]
});
if (!kpiLink) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
}
const formattedResponse = {
id: kpiLink.id,
positions: kpiLink.positions.map(position => ({
id: position.id,
name: position.name
})),
kpiCapacitys: kpiLink.kpiCapacitys.map(capacity => ({
id: capacity.id ,
name: capacity.name,
type: capacity.type,
description: capacity.description
}))
};
return new HttpSuccess(kpiLink);
}
// const [kpiLink, total] = await this.kpiLinkRepository.findAndCount({ // /**
// ...whereClause, // * API ลบเชื่อมโยง
// ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), // * @param id
// }); // */
// @Delete("{id}")
// async deleteKpiLink(@Path() id: string) {
// const kpiLink = await this.kpiLinkRepository.findOne({
// where: { id: id },
// });
// if (!kpiLink) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
// }
// return new HttpSuccess({ data: kpiLink, total }); // await this.kpiLinkRepository.remove(kpiLink);
// } // 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,
// ) {
// let whereClause: any = {};
// if (keyword !== undefined && keyword !== "") {
// whereClause = {
// where: [{ nameLinkKPI: Like(`%${keyword}%`) }],
// };
// }
// const [kpiLink, total] = await this.kpiLinkRepository.findAndCount({
// ...whereClause,
// ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
// });
// return new HttpSuccess({ data: kpiLink, total });
// }
}

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[];
}