hrms-api-probation/src/controllers/SurveyController.ts
DESKTOP-2S5P7D1\Windows 10 a9c26b4377 add auth
2025-02-03 13:38:46 +07:00

132 lines
4.7 KiB
TypeScript

import { Controller, Route, Security, Tags, Request, SuccessResponse, Response, Get, Post, Body, Query } from "tsoa"
import HttpSuccess from "../interfaces/http-success"
import HttpStatusCode from "../interfaces/http-status"
import HttpError from "../interfaces/http-error"
import { RequestWithUser } from "../middlewares/user"
import { setLogDataDiff } from "../interfaces/utils"
import { Survey } from "../entities/Survey"
import { Assign } from "../entities/Assign"
import { AppDataSource } from "../database/data-source"
import CallAPI from "../interfaces/call-api"
import permission from "../interfaces/permission"
import { Brackets } from "typeorm"
@Route("api/v1/probation/survey")
@Tags("Survey")
@Security("bearerAuth")
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง")
export class SurveyController extends Controller {
private surveyRepository = AppDataSource.getRepository(Survey)
private assignRepository = AppDataSource.getRepository(Assign)
/**
* API แบบสำรวจความคิดเห็น
*
* @summary แบบสำรวจความคิดเห็น
*
*/
@Get("")
async GetSurvey(@Request() request: RequestWithUser) {
const personalId = await new CallAPI().GetData(request, "/org/profile/keycloak").catch(error => {
console.error("Error calling API:", error)
})
const dataAssign = await this.assignRepository.findOne({
select: ["id"],
where: { personal_id: personalId },
order: { date_start: "DESC" },
})
if (!dataAssign) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบผลการประเมินการทดลองปฏิบัติหน้าที่ราชการนี้")
}
const assign_id = dataAssign.id
const data = await this.surveyRepository.findOne({
where: {
assign_id,
},
})
return new HttpSuccess({ data: data, assignId: assign_id })
}
/**
* API บันทึกแบบสำรวจความคิดเห็น
*
* @summary บันทึกแบบสำรวจความคิดเห็น
*
*/
@Post("")
async PostSurvey(@Query() assign_id: string, @Body() requestBody: any, @Request() request: RequestWithUser) {
const personalId = await new CallAPI().GetData(request, "/org/profile/keycloak").catch(error => {
console.error("Error calling API:", error)
})
const before = null
const data = await {
...requestBody,
personal_id: personalId,
assign_id,
createdUserId: request.user.sub,
updateUserId: request.user.sub,
}
await this.surveyRepository.save(data, { data: request })
setLogDataDiff(request, { before, after: data })
return new HttpSuccess()
}
/**
* API รายการผลสำรวจความคิดเห็นของ Admin
*
* @summary ผลสำรวจความคิดเห็นของ Admin
*
*/
@Get("/admin")
async GetSurveyAdmin(
@Query() year: number = new Date().getFullYear(),
@Query() keyword: string = "",
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Request() request: RequestWithUser
) {
// await new permission().PermissionUpdate(request, "SYS_PROBATION");
const start = new Date("01-01-" + year)
const end = new Date("12-31-" + year)
const searchKeyword = await (keyword ? keyword.trim() : null)
const [lists, total] = await AppDataSource.getRepository(Survey)
.createQueryBuilder("survey")
.leftJoinAndSelect("survey.personal", "personal")
.where(`survey.createdAt BETWEEN '${start.toISOString()}' AND '${end.toISOString()}'`)
.andWhere(
new Brackets(qb => {
qb.orWhere(searchKeyword ? `CONCAT(personal.prefixName, personal.firstName," ",personal.lastName) like '%${keyword}%'` : "1=1", {
keyword: `%${searchKeyword}%`,
})
qb.orWhere(searchKeyword ? `CONCAT(personal.positionName, personal.positionLevelName) like '%${keyword}%'` : "1=1", {
keyword: `%${searchKeyword}%`,
})
})
)
.orderBy("survey.createdAt", "DESC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount()
const data = lists.map(item => {
return {
createdAt: item.createdAt,
personal_id: item.personal_id,
assign_id: item.assign_id,
answer1: item.answer1,
answer2: item.answer2,
answer3: item.answer3,
fullname: item.personal ? `${item.personal.prefixName}${item.personal.firstName} ${item.personal.lastName}` : "",
position: item.personal ? `${item.personal.positionName}${item.personal.positionLevelName}` : "",
}
})
return new HttpSuccess({ data, total: total })
}
}