hrms-api-probation/src/controllers/SurveyController.ts

91 lines
2.7 KiB
TypeScript
Raw Normal View History

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, "ไม่พบผลการประเมินการทดลองปฏิบัติหน้าที่ราชการนี้");
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,
@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
}