api งานที่ได้รับมอบหมายพิเศษ

This commit is contained in:
Kittapath 2024-04-23 17:55:57 +07:00
parent 5db5f445c8
commit 55ceeac3d9
4 changed files with 415 additions and 93 deletions

View file

@ -19,7 +19,12 @@ import {
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { KpiUserPlanned, CreateKpiUserPlanned, UpdateKpiUserPlanned, KpiUserPlannedDataPoint } from "../entities/kpiUserPlanned";
import {
KpiUserPlanned,
CreateKpiUserPlanned,
UpdateKpiUserPlanned,
KpiUserPlannedDataPoint,
} from "../entities/kpiUserPlanned";
import HttpError from "../interfaces/http-error";
import { Not } from "typeorm";
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
@ -51,15 +56,15 @@ export class KpiUserPlannedController extends Controller {
@Request() request: { user: Record<string, any> },
) {
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where:{id:requestBody.kpiUserEvaluationId},
})
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
const chkKpiPlan = await this.kpiPlanRepository.findOne({
where:{id:requestBody.kpiPlanId},
})
where: { id: requestBody.kpiPlanId },
});
if (!chkKpiPlan) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินตามแผน");
}
@ -94,7 +99,7 @@ export class KpiUserPlannedController extends Controller {
if (!kpiUserPlanned) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้");
}
kpiUserPlanned.lastUpdateUserId = request.user.sub;
kpiUserPlanned.lastUpdateFullName = request.user.name;
this.kpiUserPlannedRepository.merge(kpiUserPlanned, requestBody);
@ -129,14 +134,14 @@ export class KpiUserPlannedController extends Controller {
@Get("{id}")
async GetKpiUserPlannedDetail(@Path() id: string) {
const getKpiUserPlanned = await this.kpiUserPlannedRepository.findOne({
relations: ["kpiPlan","kpiUserEvaluation"],
relations: ["kpiPlan", "kpiUserEvaluation"],
where: { id: id },
});
if (!getKpiUserPlanned) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้");
}
const mapGetKpiUserPlanned = ({
const mapGetKpiUserPlanned = {
id: getKpiUserPlanned.id,
evaluationId: getKpiUserPlanned.kpiUserEvaluation.id,
kpiPlanId: getKpiUserPlanned.kpiPlan.id,
@ -147,7 +152,7 @@ export class KpiUserPlannedController extends Controller {
unit: getKpiUserPlanned.unit,
meaning: getKpiUserPlanned.meaning,
formula: getKpiUserPlanned.formula,
});
};
return new HttpSuccess(mapGetKpiUserPlanned);
}
@ -159,17 +164,15 @@ export class KpiUserPlannedController extends Controller {
*
*/
@Get()
async GetKpiUserPlanned(
@Query("id") id: string ,
) {
async GetKpiUserPlanned(@Query("id") id: string) {
const kpiUserPlanned = await this.kpiUserPlannedRepository.find({
where:{
kpiUserEvaluationId:id
where: {
kpiUserEvaluationId: id,
},
relations: ["kpiPlan","kpiUserEvaluation"],
relations: ["kpiPlan", "kpiUserEvaluation"],
order: { createdAt: "ASC" },
});
const mapKpiUserPlanned = kpiUserPlanned.map((item) => ({
id: item.id,
evaluationId: item.kpiUserEvaluation.id,
@ -181,11 +184,18 @@ export class KpiUserPlannedController extends Controller {
meaning: item.meaning,
formula: item.formula,
point: item.point,
achievement: item.point === 1 ? "ระดับ 1" :
item.point === 2 ? "ระดับ 2" :
item.point === 3 ? "ระดับ 3" :
item.point === 4 ? "ระดับ 4" :
item.point === 5 ? "ระดับ 5" : null,
achievement:
item.point === 1
? item.kpiPlan.achievement1
: item.point === 2
? item.kpiPlan.achievement2
: item.point === 3
? item.kpiPlan.achievement3
: item.point === 4
? item.kpiPlan.achievement4
: item.point === 5
? item.kpiPlan.achievement5
: null,
achievement1: item.kpiPlan.achievement1,
achievement2: item.kpiPlan.achievement2,
achievement3: item.kpiPlan.achievement3,
@ -197,29 +207,31 @@ export class KpiUserPlannedController extends Controller {
/**
* API
*
*
* @summary
*
*
*
*
*/
@Post("point")
async CreateKpiUserPlannedPoint(
@Body() requestBody: KpiUserPlannedDataPoint[],
@Request() request: { user: Record<string, any> },
){
for (const item of requestBody) {
const kpiUserPlanned = await this.kpiUserPlannedRepository.findOne({
where: { id: item.id },
});
if (!kpiUserPlanned) {
throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้: ${item.id}`);
}
this.kpiUserPlannedRepository.merge(kpiUserPlanned, item);
kpiUserPlanned.lastUpdateUserId = request.user.sub;
kpiUserPlanned.lastUpdateFullName = request.user.name;
await this.kpiUserPlannedRepository.save(kpiUserPlanned);
@Post("point")
async CreateKpiUserPlannedPoint(
@Body() requestBody: KpiUserPlannedDataPoint[],
@Request() request: { user: Record<string, any> },
) {
for (const item of requestBody) {
const kpiUserPlanned = await this.kpiUserPlannedRepository.findOne({
where: { id: item.id },
});
if (!kpiUserPlanned) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
`ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้: ${item.id}`,
);
}
return new HttpSuccess();
this.kpiUserPlannedRepository.merge(kpiUserPlanned, item);
kpiUserPlanned.lastUpdateUserId = request.user.sub;
kpiUserPlanned.lastUpdateFullName = request.user.name;
await this.kpiUserPlannedRepository.save(kpiUserPlanned);
}
return new HttpSuccess();
}
}

View file

@ -19,7 +19,12 @@ import {
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { KpiUserRole, CreateKpiUserRole, UpdateKpiUserRole, KpiUserRoleDataPoint } from "../entities/kpiUserRole";
import {
KpiUserRole,
CreateKpiUserRole,
UpdateKpiUserRole,
KpiUserRoleDataPoint,
} from "../entities/kpiUserRole";
import HttpError from "../interfaces/http-error";
import { Not } from "typeorm";
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
@ -51,17 +56,20 @@ export class KpiUserRoleController extends Controller {
@Request() request: { user: Record<string, any> },
) {
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where:{id:requestBody.kpiUserEvaluationId},
})
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
const chkKpiRole = await this.kpiRoleRepository.findOne({
where:{id:requestBody.kpiRoleId},
})
where: { id: requestBody.kpiRoleId },
});
if (!chkKpiRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก");
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก",
);
}
const kpiUserRole = Object.assign(new KpiUserRole(), requestBody);
@ -95,19 +103,22 @@ export class KpiUserRoleController extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้");
}
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where:{id:requestBody.kpiUserEvaluationId},
})
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
const chkKpiRole = await this.kpiRoleRepository.findOne({
where:{id:requestBody.kpiRoleId},
})
where: { id: requestBody.kpiRoleId },
});
if (!chkKpiRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก");
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก",
);
}
kpiUserRole.lastUpdateUserId = request.user.sub;
kpiUserRole.lastUpdateFullName = request.user.name;
this.kpiUserRoleRepository.merge(kpiUserRole, requestBody);
@ -142,14 +153,14 @@ export class KpiUserRoleController extends Controller {
@Get("{id}")
async GetKpiUserRoleDetail(@Path() id: string) {
const getKpiUserRole = await this.kpiUserRoleRepository.findOne({
relations: ["kpiRole","kpiUserEvaluation"],
relations: ["kpiRole", "kpiUserEvaluation"],
where: { id: id },
});
if (!getKpiUserRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้");
}
const mapKpiUserRole = ({
const mapKpiUserRole = {
id: getKpiUserRole.id,
evaluationId: getKpiUserRole.kpiUserEvaluation.id,
kpiRoleId: getKpiUserRole.kpiRole.id,
@ -160,7 +171,7 @@ export class KpiUserRoleController extends Controller {
unit: getKpiUserRole.unit,
meaning: getKpiUserRole.meaning,
formula: getKpiUserRole.formula,
});
};
return new HttpSuccess(mapKpiUserRole);
}
@ -172,17 +183,15 @@ export class KpiUserRoleController extends Controller {
*
*/
@Get()
async GetKpiUserRole(
@Query("id") id: string ,
) {
async GetKpiUserRole(@Query("id") id: string) {
const kpiUserRole = await this.kpiUserRoleRepository.find({
where:{
kpiUserEvaluationId:id
where: {
kpiUserEvaluationId: id,
},
relations: ["kpiRole","kpiUserEvaluation"],
relations: ["kpiRole", "kpiUserEvaluation"],
order: { createdAt: "ASC" },
});
const mapKpiUserRole = kpiUserRole.map((item) => ({
id: item.id,
evaluationId: item.kpiUserEvaluation.id,
@ -195,11 +204,18 @@ export class KpiUserRoleController extends Controller {
meaning: item.meaning,
formula: item.formula,
point: item.point,
achievement: item.point === 1 ? "ระดับ 1" :
item.point === 2 ? "ระดับ 2" :
item.point === 3 ? "ระดับ 3" :
item.point === 4 ? "ระดับ 4" :
item.point === 5 ? "ระดับ 5" : null,
achievement:
item.point === 1
? item.kpiRole.achievement1
: item.point === 2
? item.kpiRole.achievement2
: item.point === 3
? item.kpiRole.achievement3
: item.point === 4
? item.kpiRole.achievement4
: item.point === 5
? item.kpiRole.achievement5
: null,
achievement1: item.kpiRole.achievement1,
achievement2: item.kpiRole.achievement2,
achievement3: item.kpiRole.achievement3,
@ -209,31 +225,33 @@ export class KpiUserRoleController extends Controller {
return new HttpSuccess(mapKpiUserRole);
}
/**
/**
* API
*
*
* @summary
*
*
*
*
*/
@Post("point")
async CreateKpiUserRolePoint(
@Body() requestBody: KpiUserRoleDataPoint[],
@Request() request: { user: Record<string, any> },
){
for (const item of requestBody) {
const kpiUserRole = await this.kpiUserRoleRepository.findOne({
where: { id: item.id },
});
if (!kpiUserRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้: ${item.id}`);
}
this.kpiUserRoleRepository.merge(kpiUserRole, item);
kpiUserRole.lastUpdateUserId = request.user.sub;
kpiUserRole.lastUpdateFullName = request.user.name;
await this.kpiUserRoleRepository.save(kpiUserRole);
}
return new HttpSuccess();
}
@Post("point")
async CreateKpiUserRolePoint(
@Body() requestBody: KpiUserRoleDataPoint[],
@Request() request: { user: Record<string, any> },
) {
for (const item of requestBody) {
const kpiUserRole = await this.kpiUserRoleRepository.findOne({
where: { id: item.id },
});
if (!kpiUserRole) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
`ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้: ${item.id}`,
);
}
this.kpiUserRoleRepository.merge(kpiUserRole, item);
kpiUserRole.lastUpdateUserId = request.user.sub;
kpiUserRole.lastUpdateFullName = request.user.name;
await this.kpiUserRoleRepository.save(kpiUserRole);
}
return new HttpSuccess();
}
}

View file

@ -0,0 +1,229 @@
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import {
KpiUserSpecial,
CreateKpiUserSpecial,
UpdateKpiUserSpecial,
KpiUserSpecialDataPoint,
} from "../entities/kpiUserSpecial";
import HttpError from "../interfaces/http-error";
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
@Route("api/v1/kpi/user/achievement/special")
@Tags("KpiUserSpecial")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class KpiUserSpecialController extends Controller {
private kpiUserSpecialRepository = AppDataSource.getRepository(KpiUserSpecial);
private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation);
/**
* API
*
* @summary - #
*
*/
@Post()
async createKpiUserSpecial(
@Body()
requestBody: CreateKpiUserSpecial,
@Request() request: { user: Record<string, any> },
) {
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
const kpiUserSpecial = Object.assign(new KpiUserSpecial(), requestBody);
if (!kpiUserSpecial) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
kpiUserSpecial.createdUserId = request.user.sub;
kpiUserSpecial.createdFullName = request.user.name;
kpiUserSpecial.lastUpdateUserId = request.user.sub;
kpiUserSpecial.lastUpdateFullName = request.user.name;
await this.kpiUserSpecialRepository.save(kpiUserSpecial);
return new HttpSuccess(kpiUserSpecial.id);
}
/**
* API
*
* @summary - #
*
* @param {string} id Id
*/
@Put("{id}")
async editKpiUserSpecial(
@Path() id: string,
@Body() requestBody: UpdateKpiUserSpecial,
@Request() request: { user: Record<string, any> },
) {
const kpiUserSpecial = await this.kpiUserSpecialRepository.findOne({ where: { id } });
if (!kpiUserSpecial) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้");
}
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
kpiUserSpecial.lastUpdateUserId = request.user.sub;
kpiUserSpecial.lastUpdateFullName = request.user.name;
Object.assign(kpiUserSpecial, requestBody);
await this.kpiUserSpecialRepository.save(kpiUserSpecial);
return new HttpSuccess(kpiUserSpecial.id);
}
/**
* API
*
* @summary - #
*
*/
@Delete("{id}")
async deleteKpiUserSpecial(@Path() id: string) {
const delKpiUserSpecial = await this.kpiUserSpecialRepository.findOne({ where: { id } });
if (!delKpiUserSpecial) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้");
}
await this.kpiUserSpecialRepository.remove(delKpiUserSpecial);
return new HttpSuccess();
}
/**
* API
*
* @summary - #
*
* @param {string} id Id
*/
@Get("{id}")
async GetKpiUserSpecialDetail(@Path() id: string) {
const getKpiUserSpecial = await this.kpiUserSpecialRepository.findOne({
relations: ["kpiUserEvaluation"],
where: { id: id },
});
if (!getKpiUserSpecial) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้");
}
const mapKpiUserSpecial = {
id: getKpiUserSpecial.id,
evaluationId: getKpiUserSpecial.kpiUserEvaluation.id,
including: getKpiUserSpecial.including,
includingName: getKpiUserSpecial.includingName,
target: getKpiUserSpecial.target,
weight: getKpiUserSpecial.weight,
unit: getKpiUserSpecial.unit,
meaning: getKpiUserSpecial.meaning,
formula: getKpiUserSpecial.formula,
};
return new HttpSuccess(mapKpiUserSpecial);
}
/**
* API
*
* @summary - #
*
*/
@Get()
async GetKpiUserSpecial(@Query("id") id: string) {
const kpiUserSpecial = await this.kpiUserSpecialRepository.find({
where: {
kpiUserEvaluationId: id,
},
relations: ["kpiUserEvaluation"],
order: { createdAt: "ASC" },
});
const mapKpiUserSpecial = kpiUserSpecial.map((item) => ({
id: item.id,
evaluationId: item.kpiUserEvaluation.id,
including: item.including,
includingName: item.includingName,
target: item.target,
weight: item.weight,
unit: item.unit,
meaning: item.meaning,
formula: item.formula,
point: item.point,
achievement:
item.point === 1
? item.achievement1
: item.point === 2
? item.achievement2
: item.point === 3
? item.achievement3
: item.point === 4
? item.achievement4
: item.point === 5
? item.achievement5
: null,
achievement1: item.achievement1,
achievement2: item.achievement2,
achievement3: item.achievement3,
achievement4: item.achievement4,
achievement5: item.achievement5,
}));
return new HttpSuccess(mapKpiUserSpecial);
}
/**
* API
*
* @summary
*
*
*/
@Post("point")
async CreateKpiUserSpecialPoint(
@Body() requestBody: KpiUserSpecialDataPoint[],
@Request() request: { user: Record<string, any> },
) {
for (const item of requestBody) {
const kpiUserSpecial = await this.kpiUserSpecialRepository.findOne({
where: { id: item.id },
});
if (!kpiUserSpecial) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
`ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้: ${item.id}`,
);
}
this.kpiUserSpecialRepository.merge(kpiUserSpecial, item);
kpiUserSpecial.lastUpdateUserId = request.user.sub;
kpiUserSpecial.lastUpdateFullName = request.user.name;
await this.kpiUserSpecialRepository.save(kpiUserSpecial);
}
return new HttpSuccess();
}
}

View file

@ -122,3 +122,66 @@ export class KpiUserSpecial extends EntityBase {
@JoinColumn({ name: "kpiUserEvaluationId" })
kpiUserEvaluation: KpiUserEvaluation;
}
export class CreateKpiUserSpecial {
@Column()
including: string | null;
@Column()
includingName: string | null;
@Column()
achievement1: string | null;
@Column()
achievement2: string | null;
@Column()
achievement3: string | null;
@Column()
achievement4: string | null;
@Column()
achievement5: string | null;
@Column()
target: string;
@Column()
unit: number;
@Column()
weight: number;
@Column()
meaning: string;
@Column()
formula: string;
@Column("uuid")
kpiUserEvaluationId: string;
}
export class UpdateKpiUserSpecial {
@Column()
including: string | null;
@Column()
includingName: string | null;
@Column()
achievement1: string | null;
@Column()
achievement2: string | null;
@Column()
achievement3: string | null;
@Column()
achievement4: string | null;
@Column()
achievement5: string | null;
@Column()
target: string;
@Column()
unit: number;
@Column()
weight: number;
@Column()
meaning: string;
@Column()
formula: string;
@Column("uuid")
kpiUserEvaluationId: string;
}
export class KpiUserSpecialDataPoint {
id: string;
point: number;
}