From 114a27c5e62ec88582f129905b9becbcff9879cf Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Thu, 31 Oct 2024 16:23:44 +0700 Subject: [PATCH] Assign Survey --- .../AssignPermissionsController.ts | 2 +- src/controllers/SurveyController.ts | 119 ++++++++++++------ 2 files changed, 82 insertions(+), 39 deletions(-) diff --git a/src/controllers/AssignPermissionsController.ts b/src/controllers/AssignPermissionsController.ts index 32a05a6..0f2c05f 100644 --- a/src/controllers/AssignPermissionsController.ts +++ b/src/controllers/AssignPermissionsController.ts @@ -29,7 +29,7 @@ export class AssignPermissionsController extends Controller { console.error("Error calling API:", error); }); - const role = director.find((e: any) => e.role === personalId)?.role; + const role = director.find((e: any) => e.personal_id === personalId)?.role; const reportPersonId = await this.assignRepository.findOneBy({ reportPersonId: personalId, diff --git a/src/controllers/SurveyController.ts b/src/controllers/SurveyController.ts index 1b7933b..a3fa57a 100644 --- a/src/controllers/SurveyController.ts +++ b/src/controllers/SurveyController.ts @@ -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(); + } }