From a9c26b43779778632df86ca08c3a56397bf1437c Mon Sep 17 00:00:00 2001 From: "DESKTOP-2S5P7D1\\Windows 10" Date: Mon, 3 Feb 2025 13:38:46 +0700 Subject: [PATCH] add auth --- src/controllers/SurveyController.ts | 272 ++++++++++++---------------- 1 file changed, 116 insertions(+), 156 deletions(-) diff --git a/src/controllers/SurveyController.ts b/src/controllers/SurveyController.ts index fa06db4..1e2ec98 100644 --- a/src/controllers/SurveyController.ts +++ b/src/controllers/SurveyController.ts @@ -1,172 +1,132 @@ -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"; -import permission from "../interfaces/permission"; -import { Brackets } from "typeorm"; +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" +import permission from "../interfaces/permission" +import { Brackets } from "typeorm" @Route("api/v1/probation/survey") @Tags("Survey") -// @Security("bearerAuth") -@Response( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", -) +@Security("bearerAuth") +@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง") export class SurveyController extends Controller { - private surveyRepository = AppDataSource.getRepository(Survey); - private assignRepository = AppDataSource.getRepository(Assign); + private surveyRepository = AppDataSource.getRepository(Survey) + private assignRepository = AppDataSource.getRepository(Assign) - /** - * 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 แบบสำรวจความคิดเห็น + * + */ + @Get("") + async GetSurvey(@Request() request: RequestWithUser) { + const personalId = await new CallAPI().GetData(request, "/org/profile/keycloak").catch(error => { + console.error("Error calling API:", error) + }) - 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; + 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 - const data = await this.surveyRepository.findOne({ - where: { - assign_id, - }, - }); - return new HttpSuccess({ data: data, assignId: assign_id }); - } + 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 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 personalId = await new CallAPI().GetData(request, "/org/profile/keycloak").catch(error => { + console.error("Error calling API:", error) + }) - const before = null; - const data = await { - ...requestBody, - personal_id: personalId, - assign_id, - createdUserId: request.user.sub, - updateUserId: request.user.sub, - }; - await this.surveyRepository.save(data, { data: request }); - setLogDataDiff(request, { before, after: data }); + const before = null + const data = await { + ...requestBody, + personal_id: personalId, + assign_id, + createdUserId: request.user.sub, + updateUserId: request.user.sub, + } + await this.surveyRepository.save(data, { data: request }) + setLogDataDiff(request, { before, after: data }) - return new HttpSuccess(); - } + return new HttpSuccess() + } - /** - * 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); + /** + * 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 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 [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}` - : "", - }; - }); + 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 }); - } + return new HttpSuccess({ data, total: total }) + } }