2024-10-31 16:23:44 +07:00
|
|
|
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";
|
2025-01-21 14:35:45 +07:00
|
|
|
import permission from "../interfaces/permission";
|
|
|
|
|
import { Brackets } from "typeorm";
|
2024-09-05 13:59:43 +07:00
|
|
|
|
2024-09-05 16:56:22 +07:00
|
|
|
@Route("api/v1/probation/survey")
|
2024-09-05 13:59:43 +07:00
|
|
|
@Tags("Survey")
|
2025-01-21 14:35:45 +07:00
|
|
|
// @Security("bearerAuth")
|
2024-10-31 16:23:44 +07:00
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
2025-01-21 14:35:45 +07:00
|
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
2024-10-31 16:23:44 +07:00
|
|
|
)
|
2024-09-05 13:59:43 +07:00
|
|
|
export class SurveyController extends Controller {
|
2024-10-31 16:23:44 +07:00
|
|
|
private surveyRepository = AppDataSource.getRepository(Survey);
|
|
|
|
|
private assignRepository = AppDataSource.getRepository(Assign);
|
2024-09-05 13:59:43 +07:00
|
|
|
|
2024-10-31 16:23:44 +07:00
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
});
|
2024-09-05 13:59:43 +07:00
|
|
|
|
2024-10-31 16:23:44 +07:00
|
|
|
const dataAssign = await this.assignRepository.findOne({
|
|
|
|
|
select: ["id"],
|
|
|
|
|
where: { personal_id: personalId },
|
|
|
|
|
order: { date_start: "DESC" },
|
|
|
|
|
});
|
|
|
|
|
if (!dataAssign) {
|
2025-01-21 14:35:45 +07:00
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatusCode.NOT_FOUND,
|
|
|
|
|
"ไม่พบผลการประเมินการทดลองปฏิบัติหน้าที่ราชการนี้",
|
|
|
|
|
);
|
2024-10-31 16:23:44 +07:00
|
|
|
}
|
|
|
|
|
const assign_id = dataAssign.id;
|
2024-09-05 13:59:43 +07:00
|
|
|
|
2024-10-31 16:23:44 +07:00
|
|
|
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,
|
2025-01-21 14:35:45 +07:00
|
|
|
@Request() request: RequestWithUser,
|
2024-10-31 16:23:44 +07:00
|
|
|
) {
|
2025-01-21 14:35:45 +07:00
|
|
|
const personalId = await new CallAPI()
|
|
|
|
|
.GetData(request, "/org/profile/keycloak")
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error("Error calling API:", error);
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-31 16:23:44 +07:00
|
|
|
const before = null;
|
|
|
|
|
const data = await {
|
|
|
|
|
...requestBody,
|
2025-01-21 14:35:45 +07:00
|
|
|
personal_id: personalId,
|
2024-10-31 16:23:44 +07:00
|
|
|
assign_id,
|
2025-01-21 14:35:45 +07:00
|
|
|
createdUserId: request.user.sub,
|
|
|
|
|
updateUserId: request.user.sub,
|
2024-10-31 16:23:44 +07:00
|
|
|
};
|
|
|
|
|
await this.surveyRepository.save(data, { data: request });
|
|
|
|
|
setLogDataDiff(request, { before, after: data });
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
2025-01-21 14:35:45 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 });
|
|
|
|
|
}
|
2024-09-05 13:59:43 +07:00
|
|
|
}
|