Assign Survey

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2024-10-31 16:23:44 +07:00
parent 51a6453f39
commit 114a27c5e6
2 changed files with 82 additions and 39 deletions

View file

@ -1,47 +1,90 @@
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 { RequestWithUser } from "../middlewares/user"
import { setLogDataDiff } from "../interfaces/utils"
import { Survey } from "../entities/Survey"
import { AppDataSource } from "../database/data-source"
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";
@Route("api/v1/probation/survey")
@Tags("Survey")
@Security("bearerAuth")
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง"
)
export class SurveyController extends Controller {
private surveyRepository = AppDataSource.getRepository(Survey)
private surveyRepository = AppDataSource.getRepository(Survey);
private assignRepository = AppDataSource.getRepository(Assign);
/**
* API
*
* @summary
*
*/
@Get("")
async GetSurvey(@Query() assign_id: string) {
const data = await this.surveyRepository.findOne({
where: {
assign_id,
},
})
return new HttpSuccess(data)
}
/**
* 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);
});
/**
* 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 })
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;
return new HttpSuccess()
}
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();
}
}