This commit is contained in:
DESKTOP-2S5P7D1\Windows 10 2025-02-03 13:38:46 +07:00
parent fba2938169
commit a9c26b4377

View file

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