From f4066e9120c50303a917cec5655b713b6619960c Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Tue, 21 Jan 2025 14:35:45 +0700 Subject: [PATCH] add survey admin --- src/controllers/SurveyController.ts | 92 ++++++++++++++++++- src/entities/Personal.ts | 8 +- src/entities/Survey.ts | 13 ++- .../1737442569342-updateRelationSurvey.ts | 14 +++ .../1737443725676-updateRelationSurvey.ts | 16 ++++ 5 files changed, 128 insertions(+), 15 deletions(-) create mode 100644 src/migration/1737442569342-updateRelationSurvey.ts create mode 100644 src/migration/1737443725676-updateRelationSurvey.ts diff --git a/src/controllers/SurveyController.ts b/src/controllers/SurveyController.ts index f5836e5..fa06db4 100644 --- a/src/controllers/SurveyController.ts +++ b/src/controllers/SurveyController.ts @@ -20,13 +20,15 @@ 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") +// @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, - "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง" + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) export class SurveyController extends Controller { private surveyRepository = AppDataSource.getRepository(Survey); @@ -52,7 +54,10 @@ export class SurveyController extends Controller { order: { date_start: "DESC" }, }); if (!dataAssign) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบผลการประเมินการทดลองปฏิบัติหน้าที่ราชการนี้"); + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบผลการประเมินการทดลองปฏิบัติหน้าที่ราชการนี้", + ); } const assign_id = dataAssign.id; @@ -74,17 +79,94 @@ export class SurveyController extends Controller { async PostSurvey( @Query() assign_id: string, @Body() requestBody: any, - @Request() request: RequestWithUser + @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: request.user.sub, + 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(); } + + /** + * 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 [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}` + : "", + }; + }); + + return new HttpSuccess({ data, total: total }); + } } diff --git a/src/entities/Personal.ts b/src/entities/Personal.ts index a4946b7..d1dd69a 100644 --- a/src/entities/Personal.ts +++ b/src/entities/Personal.ts @@ -2,6 +2,7 @@ import { Entity, Column, PrimaryGeneratedColumn, OneToOne, JoinColumn, OneToMany import { EntityBase } from "./base/Base"; import { Assign } from "./Assign"; import { Appoint } from "./Appoint"; +import { Survey } from "./Survey"; @Entity("personal") export class Personal extends EntityBase { @@ -124,9 +125,10 @@ export class Personal extends EntityBase { @OneToOne(() => Appoint, (appoint) => appoint.personal) @JoinColumn() appoint: Appoint; - // @OneToOne(() => Appoint, (appoint: Appoint) => appoint.profileId) - // @JoinColumn({ name: "personal_id" }) - // appoint: Appoint + + @OneToOne(() => Survey, (survey) => survey.personal) + @JoinColumn({ name: "personal_id" }) + survey: Survey; } export class CreatePersonal { diff --git a/src/entities/Survey.ts b/src/entities/Survey.ts index a6ca2c2..293844c 100644 --- a/src/entities/Survey.ts +++ b/src/entities/Survey.ts @@ -1,12 +1,7 @@ -import { - Entity, - Column, - PrimaryGeneratedColumn, - ManyToOne, - JoinColumn, -} from "typeorm"; +import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn, OneToOne } from "typeorm"; import { EntityBase } from "./base/Base"; import { Assign } from "./Assign"; +import { Personal } from "./Personal"; @Entity("survey") export class Survey extends EntityBase { @@ -48,6 +43,10 @@ export class Survey extends EntityBase { @ManyToOne(() => Assign, (assign: Assign) => assign.surveys) @JoinColumn({ name: "assign_id" }) assign: Assign; + + @OneToOne(() => Personal, (person) => person.survey, { cascade: true }) + @JoinColumn({ name: "personal_id" }) + personal: Personal; } export class CreateSurvel { diff --git a/src/migration/1737442569342-updateRelationSurvey.ts b/src/migration/1737442569342-updateRelationSurvey.ts new file mode 100644 index 0000000..55cc9ab --- /dev/null +++ b/src/migration/1737442569342-updateRelationSurvey.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateRelationSurvey1737442569342 implements MigrationInterface { + name = 'UpdateRelationSurvey1737442569342' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX \`IDX_5c42dcafb9ff2c3785b9bf447a\` ON \`personal\``); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE UNIQUE INDEX \`IDX_5c42dcafb9ff2c3785b9bf447a\` ON \`personal\` (\`appointId\`)`); + } + +} diff --git a/src/migration/1737443725676-updateRelationSurvey.ts b/src/migration/1737443725676-updateRelationSurvey.ts new file mode 100644 index 0000000..eef90a2 --- /dev/null +++ b/src/migration/1737443725676-updateRelationSurvey.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateRelationSurvey1737443725676 implements MigrationInterface { + name = 'UpdateRelationSurvey1737443725676' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`personal\` ADD CONSTRAINT \`FK_53ffbd1e99fe0da682dab5c77ff\` FOREIGN KEY (\`personal_id\`, \`personal_id\`) REFERENCES \`survey\`(\`personal_id\`,\`assign_id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`survey\` ADD CONSTRAINT \`FK_9bae89d4bb00502e057fb619a70\` FOREIGN KEY (\`personal_id\`) REFERENCES \`personal\`(\`personal_id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`survey\` DROP FOREIGN KEY \`FK_9bae89d4bb00502e057fb619a70\``); + await queryRunner.query(`ALTER TABLE \`personal\` DROP FOREIGN KEY \`FK_53ffbd1e99fe0da682dab5c77ff\``); + } + +}