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

160 lines
4.7 KiB
TypeScript
Raw Normal View History

2024-04-17 16:53:20 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import { Like, Not } from "typeorm";
2024-04-17 16:53:20 +07:00
import HttpStatusCode from "../interfaces/http-status";
2024-04-18 10:24:18 +07:00
import { KpiGroup, creatKpiGroup, updateKpiGroup } from "../entities/kpiGroup";
2024-04-17 16:53:20 +07:00
@Route("api/v1/kpi/group")
2024-04-18 10:24:18 +07:00
@Tags("kpiGroup")
2024-04-17 16:53:20 +07:00
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class kpiGroupController extends Controller {
private kpiGroupRepository = AppDataSource.getRepository(KpiGroup);
2024-04-17 16:53:20 +07:00
/**
* API
* @param requestBody
* @returns
*/
@Post()
@Example({
nameGroupKPI: "string", //ชื่อกลุ่มงาน
})
async createKpiGroup(
@Body() requestBody: creatKpiGroup,
2024-04-17 16:53:20 +07:00
@Request() request: { user: Record<string, any> },
) {
2024-04-18 10:24:18 +07:00
const kpiGroup = Object.assign(new KpiGroup(), requestBody);
const chkkpinameGroup = await this.kpiGroupRepository.findOne({
2024-04-18 10:24:18 +07:00
where: {
nameGroupKPI: requestBody.nameGroupKPI,
},
});
if (chkkpinameGroup) {
2024-04-18 10:24:18 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว");
}
kpiGroup.createdUserId = request.user.sub;
kpiGroup.createdFullName = request.user.name;
kpiGroup.lastUpdateUserId = request.user.sub;
kpiGroup.lastUpdateFullName = request.user.name;
await this.kpiGroupRepository.save(kpiGroup);
return new HttpSuccess(kpiGroup.id);
2024-04-17 16:53:20 +07:00
}
/**
* API
* @param id
*/
@Put("{id}")
async updateKpiGroup(
2024-04-17 16:53:20 +07:00
@Path() id: string,
@Body() requestBody: updateKpiGroup,
2024-04-17 16:53:20 +07:00
@Request() request: { user: Record<string, any> },
) {
2024-04-18 10:24:18 +07:00
const kpiGroup = await this.kpiGroupRepository.findOne({
2024-04-17 16:53:20 +07:00
where: { id: id },
});
2024-04-18 10:24:18 +07:00
if (!kpiGroup) {
2024-04-17 16:53:20 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
const chkkpinameGroup = await this.kpiGroupRepository.findOne({
2024-04-18 10:24:18 +07:00
where: {
nameGroupKPI: requestBody.nameGroupKPI,
},
});
if (chkkpinameGroup) {
2024-04-18 10:24:18 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว");
}
this.kpiGroupRepository.merge(kpiGroup, requestBody);
kpiGroup.lastUpdateUserId = request.user.sub;
kpiGroup.lastUpdateFullName = request.user.name;
await this.kpiGroupRepository.save(kpiGroup);
2024-04-17 16:53:20 +07:00
return new HttpSuccess(id);
}
/**
* API
* @param id
*/
@Get("{id}")
@Example({
nameGroupKPI: "string", //ชื่อกลุ่มงาน
})
async KpiGroupById(@Path() id: string) {
2024-04-18 10:24:18 +07:00
const kpiGroup = await this.kpiGroupRepository.findOne({
2024-04-17 16:53:20 +07:00
where: { id: id },
select: ["nameGroupKPI"],
});
2024-04-18 10:24:18 +07:00
if (!kpiGroup) {
2024-04-17 16:53:20 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
2024-04-18 10:24:18 +07:00
return new HttpSuccess(kpiGroup);
2024-04-17 16:53:20 +07:00
}
/**
* API
* @param id
*/
@Delete("{id}")
async deleteKpiGroup(@Path() id: string) {
2024-04-18 10:24:18 +07:00
const kpiGroup = await this.kpiGroupRepository.findOne({
2024-04-17 16:53:20 +07:00
where: { id: id },
});
2024-04-18 10:24:18 +07:00
if (!kpiGroup) {
2024-04-17 16:53:20 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
2024-04-18 10:24:18 +07:00
await this.kpiGroupRepository.remove(kpiGroup);
2024-04-17 16:53:20 +07:00
return new HttpSuccess();
}
2024-04-18 10:24:18 +07:00
/**
* API list
* @param page
* @param pageSize
*/
@Get()
async listKpiGroup(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
) {
let whereClause: any = {};
if (keyword !== undefined && keyword !== "") {
whereClause = {
where: [{ nameGroupKPI: Like(`%${keyword}%`) }],
};
}
2024-04-18 10:24:18 +07:00
const [kpiGroup, total] = await this.kpiGroupRepository.findAndCount({
...whereClause,
2024-04-18 10:24:18 +07:00
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
});
return new HttpSuccess({ data: kpiGroup, total });
}
2024-04-17 16:53:20 +07:00
}