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";
|
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")
|
|
|
|
|
@Security("bearerAuth")
|
2024-10-31 16:23:44 +07:00
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
|
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง"
|
|
|
|
|
)
|
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) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทดลองงาน");
|
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
|
@Request() request: RequestWithUser
|
|
|
|
|
) {
|
|
|
|
|
const before = null;
|
|
|
|
|
const data = await {
|
|
|
|
|
...requestBody,
|
|
|
|
|
personal_id: request.user.sub,
|
|
|
|
|
assign_id,
|
|
|
|
|
};
|
|
|
|
|
await this.surveyRepository.save(data, { data: request });
|
|
|
|
|
setLogDataDiff(request, { before, after: data });
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
2024-09-05 13:59:43 +07:00
|
|
|
}
|