แก้ไข 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 HttpError from "../interfaces/http-error";
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")
@Tags("kpi")
@Tags("kpiGroup")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
@ -45,14 +45,21 @@ export class kpiGroupController extends Controller {
@Body() requestBody: creatKpiGroup,
@Request() request: { user: Record<string, any> },
) {
const kpi = Object.assign(new KpiGroup(), requestBody);
kpi.nameGroupKPI = requestBody.nameGroupKPI;
kpi.createdUserId = request.user.sub;
kpi.createdFullName = request.user.name;
kpi.lastUpdateUserId = request.user.sub;
kpi.lastUpdateFullName = request.user.name;
await this.kpiGroupRepository.save(kpi);
return new HttpSuccess(kpi.id);
const kpiGroup = Object.assign(new KpiGroup(), requestBody);
const ChkkpinameGroup = await this.kpiGroupRepository.findOne({
where: {
nameGroupKPI: requestBody.nameGroupKPI,
},
});
if (ChkkpinameGroup) {
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,
@Request() request: { user: Record<string, any> },
) {
const kpiUpdate = await this.kpiGroupRepository.findOne({
const kpiGroup = await this.kpiGroupRepository.findOne({
where: { id: id },
});
if (!kpiUpdate) {
if (!kpiGroup) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
requestBody.nameGroupKPI = requestBody.nameGroupKPI.trim().toUpperCase();
this.kpiGroupRepository.merge(kpiUpdate, requestBody);
kpiUpdate.createdUserId = request.user.sub;
kpiUpdate.createdFullName = request.user.name;
kpiUpdate.lastUpdateUserId = request.user.sub;
kpiUpdate.lastUpdateFullName = request.user.name;
await this.kpiGroupRepository.save(kpiUpdate);
const ChkkpinameGroup = await this.kpiGroupRepository.findOne({
where: {
nameGroupKPI: requestBody.nameGroupKPI,
},
});
if (ChkkpinameGroup) {
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);
}
@ -91,14 +105,14 @@ export class kpiGroupController extends Controller {
nameGroupKPI: "string", //ชื่อกลุ่มงาน
})
async KpiGroupById(@Path() id: string) {
const kpi = await this.kpiGroupRepository.findOne({
const kpiGroup = await this.kpiGroupRepository.findOne({
where: { id: id },
select: ["nameGroupKPI"],
});
if (!kpi) {
if (!kpiGroup) {
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}")
async deleteKpiGroup(@Path() id: string) {
const chkKpiGroup = await this.kpiGroupRepository.findOne({
const kpiGroup = await this.kpiGroupRepository.findOne({
where: { id: id },
});
if (!chkKpiGroup) {
if (!kpiGroup) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
await this.kpiGroupRepository.remove(chkKpiGroup);
await this.kpiGroupRepository.remove(kpiGroup);
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 HttpError from "../interfaces/http-error";
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")
@Tags("kpi")
@Tags("kpiPeriod")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
@ -46,14 +46,14 @@ export class kpiController extends Controller {
@Body() requestBody: createKpiPeriod,
@Request() request: { user: Record<string, any> },
) {
const kpi = Object.assign(new KpiPeriod(), requestBody);
kpi.durationKPI = requestBody.durationKPI.trim().toUpperCase();
kpi.createdUserId = request.user.sub;
kpi.createdFullName = request.user.name;
kpi.lastUpdateUserId = request.user.sub;
kpi.lastUpdateFullName = request.user.name;
await this.kpiPeriodRepository.save(kpi);
return new HttpSuccess(kpi.id);
const kpiPeriod = Object.assign(new KpiPeriod(), requestBody);
kpiPeriod.durationKPI = requestBody.durationKPI.trim().toUpperCase();
kpiPeriod.createdUserId = request.user.sub;
kpiPeriod.createdFullName = request.user.name;
kpiPeriod.lastUpdateUserId = request.user.sub;
kpiPeriod.lastUpdateFullName = request.user.name;
await this.kpiPeriodRepository.save(kpiPeriod);
return new HttpSuccess(kpiPeriod.id);
}
/**
@ -68,10 +68,10 @@ export class kpiController extends Controller {
@Body() requestBody: updateKpiPeriod,
@Request() request: { user: Record<string, any> },
) {
const kpiUpdate = await this.kpiPeriodRepository.findOne({
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: id },
});
if (!kpiUpdate) {
if (!kpiPeriod) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
@ -79,12 +79,12 @@ export class kpiController extends Controller {
}
requestBody.durationKPI = requestBody.durationKPI.trim().toUpperCase();
this.kpiPeriodRepository.merge(kpiUpdate, requestBody);
kpiUpdate.createdUserId = request.user.sub;
kpiUpdate.createdFullName = request.user.name;
kpiUpdate.lastUpdateUserId = request.user.sub;
kpiUpdate.lastUpdateFullName = request.user.name;
await this.kpiPeriodRepository.save(kpiUpdate);
this.kpiPeriodRepository.merge(kpiPeriod, requestBody);
kpiPeriod.createdUserId = request.user.sub;
kpiPeriod.createdFullName = request.user.name;
kpiPeriod.lastUpdateUserId = request.user.sub;
kpiPeriod.lastUpdateFullName = request.user.name;
await this.kpiPeriodRepository.save(kpiPeriod);
return new HttpSuccess(id);
}
@ -99,18 +99,18 @@ export class kpiController extends Controller {
endDate: "datetime", //วันสิ้นสุด
})
async CloseKpiPeriodById(@Path() id: string) {
const kpi = await this.kpiPeriodRepository.findOne({
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: id },
});
if (!kpi) {
if (!kpiPeriod) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
);
}
kpi.isActive = false;
await this.kpiPeriodRepository.save(kpi);
return new HttpSuccess(kpi);
kpiPeriod.isActive = false;
await this.kpiPeriodRepository.save(kpiPeriod);
return new HttpSuccess(kpiPeriod);
}
/**
@ -124,18 +124,18 @@ export class kpiController extends Controller {
endDate: "datetime", //วันสิ้นสุด
})
async OpenKpiPeriodById(@Path() id: string) {
const kpi = await this.kpiPeriodRepository.findOne({
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: id },
});
if (!kpi) {
if (!kpiPeriod) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
);
}
kpi.isActive = true;
await this.kpiPeriodRepository.save(kpi);
return new HttpSuccess(kpi);
kpiPeriod.isActive = true;
await this.kpiPeriodRepository.save(kpiPeriod);
return new HttpSuccess(kpiPeriod);
}
/**
@ -149,17 +149,17 @@ export class kpiController extends Controller {
endDate: "datetime", //วันสิ้นสุด
})
async GetKpiPeriodById(@Path() id: string) {
const kpi = await this.kpiPeriodRepository.findOne({
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: id },
select: ["durationKPI", "startDate", "endDate", "isActive"],
});
if (!kpi) {
if (!kpiPeriod) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
);
}
return new HttpSuccess(kpi);
return new HttpSuccess(kpiPeriod);
}
/**
@ -191,17 +191,17 @@ export class kpiController extends Controller {
*/
@Delete("{id}")
async deleteKpiPerriod(@Path() id: string) {
const chkKpiPeriod = await this.kpiPeriodRepository.findOne({
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: id },
});
if (!chkKpiPeriod) {
if (!kpiPeriod) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
);
}
await this.kpiPeriodRepository.remove(chkKpiPeriod);
await this.kpiPeriodRepository.remove(kpiPeriod);
return new HttpSuccess();
}
}

View file

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