api userEvaluation

This commit is contained in:
Bright 2024-04-22 13:23:31 +07:00
parent 22117c4d24
commit 1c7d62e149
3 changed files with 228 additions and 0 deletions

View file

@ -0,0 +1,209 @@
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
ArrayValidator
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { KpiPeriod } from "../entities/kpiPeriod";
import { KpiUserEvaluation, createKpiUserEvaluation, updateKpiUserEvaluation } from "../entities/kpiUserEvaluation";
import { Like, In } from "typeorm";
import CallAPI from "../interfaces/call-api";
import { Any } from "typeorm/browser";
@Route("api/v1/kpi/user/evaluation")
@Tags("kpiUserEvaluation")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class KpiUserEvaluationController extends Controller {
private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod);
private kpiUserEvalutionRepository = AppDataSource.getRepository(KpiUserEvaluation);
/**
* API (USER)
*
* @summary (USER)
*
*
*/
@Post()
async CreateKpiUserEvaluation(
@Body() requestBody: createKpiUserEvaluation,
@Request() request: { user: Record<string, any> },
){
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: requestBody.kpiPeriodId },
});
if (!kpiPeriod) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
);
}
const kpiUserEvaluation = Object.assign(new KpiUserEvaluation(), requestBody);
await new CallAPI()
.GetData(request, "org/profile/keycloak/position")
.then((x) => {
kpiUserEvaluation.profileId = x.profileId
kpiUserEvaluation.prefix = x.prefix
kpiUserEvaluation.firstName = x.firstName
kpiUserEvaluation.lastName = x.lastName
})
.catch((x) => {});
kpiUserEvaluation.createdUserId = request.user.sub;
kpiUserEvaluation.createdFullName = request.user.name;
kpiUserEvaluation.lastUpdateUserId = request.user.sub;
kpiUserEvaluation.lastUpdateFullName = request.user.name;
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id);
}
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Put("{id}")
async updateKpiUserEvaluation(
@Path() id: string,
@Body() requestBody: updateKpiUserEvaluation,
@Request() request: { user: Record<string, any> },
) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
});
if (!KpiUserEvaluation) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: requestBody.kpiPeriodId },
});
if (!kpiPeriod) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
);
}
if (kpiUserEvaluation) {
this.kpiUserEvalutionRepository.merge(kpiUserEvaluation, requestBody);
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id)
}
}
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Get("{id}")
async GetKpiUserEvaluationById(@Path() id: string) {
const KpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
select: ["id", "profileId", "prefix", "firstName", "lastName", "kpiPeriodId"],
})
if (!KpiUserEvaluation) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
return new HttpSuccess(KpiUserEvaluation);
}
/**
* API (USER)
*
* @summary (USER)
*
*/
@Get()
async listKpiUserEvaluation(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("period") period?: string,
@Query("keyword") keyword?: string,
) {
const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation)
.createQueryBuilder("kpiUserEvaluation")
.leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod")
.andWhere(
keyword == undefined
? "1=1"
: [
{ prefix: Like(`%${keyword}%`) },
{ firstName: Like(`%${keyword}%`) },
{ lastName: Like(`%${keyword}%`) },
],
)
.andWhere(period == undefined && period == null && period == ""
? "1=1"
: { "kpiPeriod.durationKPI" : In([period]) })
.orderBy("kpiUserEvaluation.createdAt", "ASC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
const mapData = kpiUserEvaluation.map((item) => ({
id: item.id,
profileId: item.profileId,
prefix: item.prefix,
firstname: item.firstName,
lastname: item.lastName,
kpiPeriodId: item.kpiPeriodId,
}));
return new HttpSuccess({ data: mapData, total });
}
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Delete("{id}")
async deleteKpiUserEvaluation(@Path() id: string) {
const KpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
});
if (!KpiUserEvaluation) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
await this.kpiUserEvalutionRepository.remove(KpiUserEvaluation);
return new HttpSuccess();
}
}

View file

@ -0,0 +1,16 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class AddTableKpiUserEvaluation1713757864608 implements MigrationInterface {
name = 'AddTableKpiUserEvaluation1713757864608'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE \`kpiUserEvaluation\` (\`id\` varchar(36) NOT NULL, \`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`lastUpdatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`lastUpdateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'string', \`lastUpdateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'string', \`prefix\` varchar(255) NULL COMMENT 'คำนำหน้า', \`firstName\` varchar(255) NULL COMMENT 'ชื่อ', \`lastName\` varchar(255) NULL COMMENT 'สกุล', \`kpiPeriodId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง kpiPeriodId', \`profileId\` varchar(40) NULL COMMENT 'ไอดีโปรไฟล์', PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
await queryRunner.query(`ALTER TABLE \`kpiUserEvaluation\` ADD CONSTRAINT \`FK_ec9dcf722db1e7d96b21b6b85aa\` FOREIGN KEY (\`kpiPeriodId\`) REFERENCES \`kpiPeriod\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`kpiUserEvaluation\` DROP FOREIGN KEY \`FK_ec9dcf722db1e7d96b21b6b85aa\``);
await queryRunner.query(`DROP TABLE \`kpiUserEvaluation\``);
}
}

View file

@ -40,6 +40,9 @@
},
{
"name": "kpiCapacity", "description": "สมรรถนะ"
},
{
"name": "kpiUserEvaluation", "description": "รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)"
}
]
},