แก้ไข KpiGroup

This commit is contained in:
AnandaTon 2024-04-18 10:24:18 +07:00
parent eaff0dac2c
commit c86a88d32b
5 changed files with 103 additions and 59 deletions

View file

@ -19,9 +19,9 @@ import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import { KpiGroup, creatKpiGroup, updateKpiGroup } from "../entities/kpiGrop"; import { KpiGroup, creatKpiGroup, updateKpiGroup } from "../entities/kpiGroup";
@Route("api/v1/kpi/group") @Route("api/v1/kpi/group")
@Tags("kpi") @Tags("kpiGroup")
@Security("bearerAuth") @Security("bearerAuth")
@Response( @Response(
HttpStatusCode.INTERNAL_SERVER_ERROR, HttpStatusCode.INTERNAL_SERVER_ERROR,
@ -45,14 +45,21 @@ export class kpiGroupController extends Controller {
@Body() requestBody: creatKpiGroup, @Body() requestBody: creatKpiGroup,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const kpi = Object.assign(new KpiGroup(), requestBody); const kpiGroup = Object.assign(new KpiGroup(), requestBody);
kpi.nameGroupKPI = requestBody.nameGroupKPI; const ChkkpinameGroup = await this.kpiGroupRepository.findOne({
kpi.createdUserId = request.user.sub; where: {
kpi.createdFullName = request.user.name; nameGroupKPI: requestBody.nameGroupKPI,
kpi.lastUpdateUserId = request.user.sub; },
kpi.lastUpdateFullName = request.user.name; });
await this.kpiGroupRepository.save(kpi); if (ChkkpinameGroup) {
return new HttpSuccess(kpi.id); throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว");
}
kpiGroup.createdUserId = request.user.sub;
kpiGroup.createdFullName = request.user.name;
kpiGroup.lastUpdateUserId = request.user.sub;
kpiGroup.lastUpdateFullName = request.user.name;
await this.kpiGroupRepository.save(kpiGroup);
return new HttpSuccess(kpiGroup.id);
} }
/** /**
@ -65,20 +72,27 @@ export class kpiGroupController extends Controller {
@Body() requestBody: updateKpiGroup, @Body() requestBody: updateKpiGroup,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const kpiUpdate = await this.kpiGroupRepository.findOne({ const kpiGroup = await this.kpiGroupRepository.findOne({
where: { id: id }, where: { id: id },
}); });
if (!kpiUpdate) { if (!kpiGroup) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
} }
requestBody.nameGroupKPI = requestBody.nameGroupKPI.trim().toUpperCase(); const ChkkpinameGroup = await this.kpiGroupRepository.findOne({
this.kpiGroupRepository.merge(kpiUpdate, requestBody); where: {
kpiUpdate.createdUserId = request.user.sub; nameGroupKPI: requestBody.nameGroupKPI,
kpiUpdate.createdFullName = request.user.name; },
kpiUpdate.lastUpdateUserId = request.user.sub; });
kpiUpdate.lastUpdateFullName = request.user.name; if (ChkkpinameGroup) {
await this.kpiGroupRepository.save(kpiUpdate); throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว");
}
requestBody.nameGroupKPI = requestBody.nameGroupKPI;
this.kpiGroupRepository.merge(kpiGroup, requestBody);
kpiGroup.lastUpdateUserId = request.user.sub;
kpiGroup.lastUpdateFullName = request.user.name;
await this.kpiGroupRepository.save(kpiGroup);
return new HttpSuccess(id); return new HttpSuccess(id);
} }
@ -91,14 +105,14 @@ export class kpiGroupController extends Controller {
nameGroupKPI: "string", //ชื่อกลุ่มงาน nameGroupKPI: "string", //ชื่อกลุ่มงาน
}) })
async KpiGroupById(@Path() id: string) { async KpiGroupById(@Path() id: string) {
const kpi = await this.kpiGroupRepository.findOne({ const kpiGroup = await this.kpiGroupRepository.findOne({
where: { id: id }, where: { id: id },
select: ["nameGroupKPI"], select: ["nameGroupKPI"],
}); });
if (!kpi) { if (!kpiGroup) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
} }
return new HttpSuccess(kpi); return new HttpSuccess(kpiGroup);
} }
/** /**
@ -107,14 +121,38 @@ export class kpiGroupController extends Controller {
*/ */
@Delete("{id}") @Delete("{id}")
async deleteKpiGroup(@Path() id: string) { async deleteKpiGroup(@Path() id: string) {
const chkKpiGroup = await this.kpiGroupRepository.findOne({ const kpiGroup = await this.kpiGroupRepository.findOne({
where: { id: id }, where: { id: id },
}); });
if (!chkKpiGroup) { if (!kpiGroup) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
} }
await this.kpiGroupRepository.remove(chkKpiGroup); await this.kpiGroupRepository.remove(kpiGroup);
return new HttpSuccess(); return new HttpSuccess();
} }
/**
* API list
* @param page
* @param pageSize
* @param year
* @param keyword
*/
@Get()
async listKpiGroup(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("year") year?: string,
@Query("keyword") keyword?: string,
) {
const [kpiGroup, total] = await this.kpiGroupRepository.findAndCount({
// where: {
// name: Like(`%${keyword}%`),
// },
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
});
return new HttpSuccess({ data: kpiGroup, total });
}
} }

View file

@ -19,10 +19,10 @@ import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import { KpiPeriod, createKpiPeriod, updateKpiPeriod } from "../entities/kpi"; import { KpiPeriod, createKpiPeriod, updateKpiPeriod } from "../entities/kpiPeriod";
@Route("api/v1/kpi/period") @Route("api/v1/kpi/period")
@Tags("kpi") @Tags("kpiPeriod")
@Security("bearerAuth") @Security("bearerAuth")
@Response( @Response(
HttpStatusCode.INTERNAL_SERVER_ERROR, HttpStatusCode.INTERNAL_SERVER_ERROR,
@ -46,14 +46,14 @@ export class kpiController extends Controller {
@Body() requestBody: createKpiPeriod, @Body() requestBody: createKpiPeriod,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const kpi = Object.assign(new KpiPeriod(), requestBody); const kpiPeriod = Object.assign(new KpiPeriod(), requestBody);
kpi.durationKPI = requestBody.durationKPI.trim().toUpperCase(); kpiPeriod.durationKPI = requestBody.durationKPI.trim().toUpperCase();
kpi.createdUserId = request.user.sub; kpiPeriod.createdUserId = request.user.sub;
kpi.createdFullName = request.user.name; kpiPeriod.createdFullName = request.user.name;
kpi.lastUpdateUserId = request.user.sub; kpiPeriod.lastUpdateUserId = request.user.sub;
kpi.lastUpdateFullName = request.user.name; kpiPeriod.lastUpdateFullName = request.user.name;
await this.kpiPeriodRepository.save(kpi); await this.kpiPeriodRepository.save(kpiPeriod);
return new HttpSuccess(kpi.id); return new HttpSuccess(kpiPeriod.id);
} }
/** /**
@ -68,10 +68,10 @@ export class kpiController extends Controller {
@Body() requestBody: updateKpiPeriod, @Body() requestBody: updateKpiPeriod,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const kpiUpdate = await this.kpiPeriodRepository.findOne({ const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: id }, where: { id: id },
}); });
if (!kpiUpdate) { if (!kpiPeriod) {
throw new HttpError( throw new HttpError(
HttpStatusCode.NOT_FOUND, HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
@ -79,12 +79,12 @@ export class kpiController extends Controller {
} }
requestBody.durationKPI = requestBody.durationKPI.trim().toUpperCase(); requestBody.durationKPI = requestBody.durationKPI.trim().toUpperCase();
this.kpiPeriodRepository.merge(kpiUpdate, requestBody); this.kpiPeriodRepository.merge(kpiPeriod, requestBody);
kpiUpdate.createdUserId = request.user.sub; kpiPeriod.createdUserId = request.user.sub;
kpiUpdate.createdFullName = request.user.name; kpiPeriod.createdFullName = request.user.name;
kpiUpdate.lastUpdateUserId = request.user.sub; kpiPeriod.lastUpdateUserId = request.user.sub;
kpiUpdate.lastUpdateFullName = request.user.name; kpiPeriod.lastUpdateFullName = request.user.name;
await this.kpiPeriodRepository.save(kpiUpdate); await this.kpiPeriodRepository.save(kpiPeriod);
return new HttpSuccess(id); return new HttpSuccess(id);
} }
@ -99,18 +99,18 @@ export class kpiController extends Controller {
endDate: "datetime", //วันสิ้นสุด endDate: "datetime", //วันสิ้นสุด
}) })
async CloseKpiPeriodById(@Path() id: string) { async CloseKpiPeriodById(@Path() id: string) {
const kpi = await this.kpiPeriodRepository.findOne({ const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: id }, where: { id: id },
}); });
if (!kpi) { if (!kpiPeriod) {
throw new HttpError( throw new HttpError(
HttpStatusCode.NOT_FOUND, HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
); );
} }
kpi.isActive = false; kpiPeriod.isActive = false;
await this.kpiPeriodRepository.save(kpi); await this.kpiPeriodRepository.save(kpiPeriod);
return new HttpSuccess(kpi); return new HttpSuccess(kpiPeriod);
} }
/** /**
@ -124,18 +124,18 @@ export class kpiController extends Controller {
endDate: "datetime", //วันสิ้นสุด endDate: "datetime", //วันสิ้นสุด
}) })
async OpenKpiPeriodById(@Path() id: string) { async OpenKpiPeriodById(@Path() id: string) {
const kpi = await this.kpiPeriodRepository.findOne({ const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: id }, where: { id: id },
}); });
if (!kpi) { if (!kpiPeriod) {
throw new HttpError( throw new HttpError(
HttpStatusCode.NOT_FOUND, HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
); );
} }
kpi.isActive = true; kpiPeriod.isActive = true;
await this.kpiPeriodRepository.save(kpi); await this.kpiPeriodRepository.save(kpiPeriod);
return new HttpSuccess(kpi); return new HttpSuccess(kpiPeriod);
} }
/** /**
@ -149,17 +149,17 @@ export class kpiController extends Controller {
endDate: "datetime", //วันสิ้นสุด endDate: "datetime", //วันสิ้นสุด
}) })
async GetKpiPeriodById(@Path() id: string) { async GetKpiPeriodById(@Path() id: string) {
const kpi = await this.kpiPeriodRepository.findOne({ const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: id }, where: { id: id },
select: ["durationKPI", "startDate", "endDate", "isActive"], select: ["durationKPI", "startDate", "endDate", "isActive"],
}); });
if (!kpi) { if (!kpiPeriod) {
throw new HttpError( throw new HttpError(
HttpStatusCode.NOT_FOUND, HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
); );
} }
return new HttpSuccess(kpi); return new HttpSuccess(kpiPeriod);
} }
/** /**
@ -191,17 +191,17 @@ export class kpiController extends Controller {
*/ */
@Delete("{id}") @Delete("{id}")
async deleteKpiPerriod(@Path() id: string) { async deleteKpiPerriod(@Path() id: string) {
const chkKpiPeriod = await this.kpiPeriodRepository.findOne({ const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: id }, where: { id: id },
}); });
if (!chkKpiPeriod) { if (!kpiPeriod) {
throw new HttpError( throw new HttpError(
HttpStatusCode.NOT_FOUND, HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
); );
} }
await this.kpiPeriodRepository.remove(chkKpiPeriod); await this.kpiPeriodRepository.remove(kpiPeriod);
return new HttpSuccess(); return new HttpSuccess();
} }
} }

View file

@ -28,6 +28,12 @@
"tags": [ "tags": [
{ {
"name": "Test", "description": "สำหรับทดสอบ" "name": "Test", "description": "สำหรับทดสอบ"
},
{
"name": "kpiGroup", "description": "ชื่อกลุ่มงาน"
},
{
"name": "kpiPeriod", "description": "รอบKpi"
} }
] ]
}, },