api Capacity
This commit is contained in:
parent
4e7315b44d
commit
00a69ebd31
1 changed files with 215 additions and 106 deletions
|
|
@ -22,7 +22,7 @@ import HttpError from "../interfaces/http-error";
|
||||||
import HttpStatusCode from "../interfaces/http-status";
|
import HttpStatusCode from "../interfaces/http-status";
|
||||||
import { KpiCapacity, createKpiCapacity, updateKpiCapacity } from "../entities/kpiCapacity";
|
import { KpiCapacity, createKpiCapacity, updateKpiCapacity } from "../entities/kpiCapacity";
|
||||||
import { KpiCapacityDetail, createKpiCapacityDetail, updateKpiCapacityDetail } from "../entities/kpiCapacityDetail";
|
import { KpiCapacityDetail, createKpiCapacityDetail, updateKpiCapacityDetail } from "../entities/kpiCapacityDetail";
|
||||||
import { Like } from "typeorm/browser";
|
import { Like, In } from "typeorm";
|
||||||
|
|
||||||
@Route("api/v1/kpi/capacity")
|
@Route("api/v1/kpi/capacity")
|
||||||
@Tags("kpiCapacity")
|
@Tags("kpiCapacity")
|
||||||
|
|
@ -37,18 +37,18 @@ export class kpiCapacityController extends Controller {
|
||||||
private kpiCapacityDetailRepository = AppDataSource.getRepository(KpiCapacityDetail);
|
private kpiCapacityDetailRepository = AppDataSource.getRepository(KpiCapacityDetail);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* สร้างรายการสมรรถนะ
|
* API สร้างรายการสมรรถนะ
|
||||||
* @param requestBody
|
*
|
||||||
* @param request
|
* @summary สร้างรายการสมรรถนะ
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
@Post()
|
@Post()
|
||||||
@Example({
|
@Example({
|
||||||
type: "HEAD", //ประเภทสมรรถนะ
|
type: "HEAD",
|
||||||
name: "รายการสมรรถะ 1", //ชื่อสมรรถนะ
|
name: "ชื่อสมรรถนะ",
|
||||||
description: "รายละเอียดรายการสมรรถะ 1", //คำจำกัดความ
|
description: "คำจำกัดความ",
|
||||||
kpiCapacityDetails: [{
|
kpiCapacityDetails: [{
|
||||||
level: 1, //ระดับ
|
description: "คำอธิบายระดับ"
|
||||||
description: "คำอธิบายระดับ" //คำอธิบายระดับ
|
|
||||||
}]
|
}]
|
||||||
})
|
})
|
||||||
async createKpiCapacity(
|
async createKpiCapacity(
|
||||||
|
|
@ -57,118 +57,227 @@ export class kpiCapacityController extends Controller {
|
||||||
type: string
|
type: string
|
||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
kpiCapacityDetails: {
|
capacityDetails: {
|
||||||
level: number;
|
// level: number;
|
||||||
description: string;
|
description: string;
|
||||||
}[];
|
}[];
|
||||||
},
|
},
|
||||||
@Request() request: { user: Record<string, any> },
|
@Request() request: { user: Record<string, any> },
|
||||||
) {
|
){
|
||||||
const kpiCapacity = Object.assign(new KpiCapacity(), requestBody);
|
const kpiCapacity = Object.assign(new KpiCapacity(), {
|
||||||
|
type: requestBody.type,
|
||||||
|
name: requestBody.name,
|
||||||
|
description: requestBody.description
|
||||||
|
});
|
||||||
kpiCapacity.createdUserId = request.user.sub;
|
kpiCapacity.createdUserId = request.user.sub;
|
||||||
kpiCapacity.createdFullName = request.user.name;
|
kpiCapacity.createdFullName = request.user.name;
|
||||||
kpiCapacity.lastUpdateUserId = request.user.sub;
|
kpiCapacity.lastUpdateUserId = request.user.sub;
|
||||||
kpiCapacity.lastUpdateFullName = request.user.name;
|
kpiCapacity.lastUpdateFullName = request.user.name;
|
||||||
await this.kpiCapacityRepository.save(kpiCapacity);
|
await this.kpiCapacityRepository.save(kpiCapacity);
|
||||||
return new HttpSuccess(kpiCapacity.id);
|
|
||||||
|
let idx: number = 0;
|
||||||
|
for (const data of requestBody.capacityDetails) {
|
||||||
|
idx += 1
|
||||||
|
const kpiCapacityDetail = Object.assign(new KpiCapacityDetail(), {
|
||||||
|
level: idx,
|
||||||
|
description: data.description,
|
||||||
|
kpiCapacityId: kpiCapacity.id
|
||||||
|
});
|
||||||
|
kpiCapacityDetail.createdUserId = request.user.sub;
|
||||||
|
kpiCapacityDetail.createdFullName = request.user.name;
|
||||||
|
kpiCapacityDetail.lastUpdateUserId = request.user.sub;
|
||||||
|
kpiCapacityDetail.lastUpdateFullName = request.user.name;
|
||||||
|
await this.kpiCapacityDetailRepository.save(kpiCapacityDetail);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HttpSuccess(kpiCapacity.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * API แก้ไขรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
* API แก้ไขรายการสมรรถนะ
|
||||||
// * @param id
|
*
|
||||||
// * @param requestBody
|
* @summary แก้ไขรายการสมรรถนะ
|
||||||
// * @param request
|
*
|
||||||
// */
|
* @param {string} id Guid, *Id รายการสมรรถนะ
|
||||||
// @Put("{id}")
|
*/
|
||||||
// async updateKpiPeriod(
|
@Put("{id}")
|
||||||
// @Path() id: string,
|
@Example({
|
||||||
// @Body() requestBody: updateKpiPeriod,
|
type: "HEAD",
|
||||||
// @Request() request: { user: Record<string, any> },
|
name: "ชื่อสมรรถนะ",
|
||||||
// ) {
|
description: "คำจำกัดความ",
|
||||||
// const kpiPeriod = await this.kpiPeriodRepository.findOne({
|
kpiCapacityDetails: [{
|
||||||
// where: { id: id },
|
description: "คำอธิบายระดับ"
|
||||||
// });
|
}]
|
||||||
// if (!kpiPeriod) {
|
})
|
||||||
// throw new HttpError(
|
async updateKpiCapacity(
|
||||||
// HttpStatusCode.NOT_FOUND,
|
@Path() id: string,
|
||||||
// "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
@Body() requestBody: {
|
||||||
// );
|
type: string
|
||||||
// }
|
name: string
|
||||||
|
description: string
|
||||||
|
capacityDetails: {
|
||||||
|
description: string;
|
||||||
|
}[];
|
||||||
|
},
|
||||||
|
@Request() request: { user: Record<string, any> },
|
||||||
|
) {
|
||||||
|
const kpiCapacity = await this.kpiCapacityRepository.findOne({
|
||||||
|
where: { id: id },
|
||||||
|
});
|
||||||
|
if (!kpiCapacity) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatusCode.NOT_FOUND,
|
||||||
|
"ไม่พบข้อมูลรายการสมรรถนะนี้",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const _kpiCapacity = Object.assign(new KpiCapacity(), {
|
||||||
|
type: requestBody.type,
|
||||||
|
name: requestBody.name,
|
||||||
|
description: requestBody.description
|
||||||
|
});
|
||||||
|
kpiCapacity.lastUpdateUserId = request.user.sub;
|
||||||
|
kpiCapacity.lastUpdateFullName = request.user.name;
|
||||||
|
this.kpiCapacityRepository.merge(kpiCapacity, _kpiCapacity);
|
||||||
|
await this.kpiCapacityRepository.save(kpiCapacity);
|
||||||
|
|
||||||
// requestBody.durationKPI = requestBody.durationKPI.trim().toUpperCase();
|
const _kpiCapacityDetailsOld = await this.kpiCapacityDetailRepository.find({
|
||||||
// this.kpiPeriodRepository.merge(kpiPeriod, requestBody);
|
where: { kpiCapacityId: kpiCapacity.id },
|
||||||
// kpiPeriod.createdUserId = request.user.sub;
|
});
|
||||||
// kpiPeriod.createdFullName = request.user.name;
|
await this.kpiCapacityDetailRepository.remove(_kpiCapacityDetailsOld);
|
||||||
// kpiPeriod.lastUpdateUserId = request.user.sub;
|
|
||||||
// kpiPeriod.lastUpdateFullName = request.user.name;
|
|
||||||
// await this.kpiPeriodRepository.save(kpiPeriod);
|
|
||||||
// return new HttpSuccess(id);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /**
|
let idx: number = 0;
|
||||||
// * API รอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
for (const data of requestBody.capacityDetails) {
|
||||||
// * @param id Guid, *Id รอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
idx += 1
|
||||||
// */
|
const kpiCapacityDetail = Object.assign(new KpiCapacityDetail(), {
|
||||||
// @Get("{id}")
|
level: idx,
|
||||||
// @Example({
|
description: data.description,
|
||||||
// durationKPI: "string", //รอบเดือนที่สร้าง
|
kpiCapacityId: kpiCapacity.id
|
||||||
// startDate: "datetime", //วันเริ่มต้น
|
});
|
||||||
// endDate: "datetime", //วันสิ้นสุด
|
kpiCapacityDetail.createdUserId = request.user.sub;
|
||||||
// })
|
kpiCapacityDetail.createdFullName = request.user.name;
|
||||||
// async GetKpiPeriodById(@Path() id: string) {
|
kpiCapacityDetail.lastUpdateUserId = request.user.sub;
|
||||||
// const kpiPeriod = await this.kpiPeriodRepository.findOne({
|
kpiCapacityDetail.lastUpdateFullName = request.user.name;
|
||||||
// where: { id: id },
|
await this.kpiCapacityDetailRepository.save(kpiCapacityDetail);
|
||||||
// select: ["durationKPI", "startDate", "endDate", "isActive"],
|
}
|
||||||
// });
|
|
||||||
// if (!kpiPeriod) {
|
|
||||||
// throw new HttpError(
|
|
||||||
// HttpStatusCode.NOT_FOUND,
|
|
||||||
// "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// return new HttpSuccess(kpiPeriod);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /**
|
return new HttpSuccess(kpiCapacity.id)
|
||||||
// * API list รอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
}
|
||||||
// * @param page
|
|
||||||
// * @param pageSize
|
|
||||||
// * @param keyword
|
|
||||||
// */
|
|
||||||
// @Get()
|
|
||||||
// async listKpiPeriod(
|
|
||||||
// @Query("page") page: number = 1,
|
|
||||||
// @Query("pageSize") pageSize: number = 10,
|
|
||||||
// @Query("year") year?: string,
|
|
||||||
// @Query("keyword") keyword?: string,
|
|
||||||
// ) {
|
|
||||||
// const [kpiPeriod, total] = await this.kpiPeriodRepository.findAndCount({
|
|
||||||
// // where: {
|
|
||||||
// // durationKPI: Like(`%${keyword}%`),
|
|
||||||
// // },
|
|
||||||
// ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
|
|
||||||
// });
|
|
||||||
|
|
||||||
// return new HttpSuccess({ data: kpiPeriod, total });
|
/**
|
||||||
// }
|
* API รายละเอียดรายการสมรรถนะ
|
||||||
|
*
|
||||||
|
* @summary รายละเอียดดรายการสมรรถนะ
|
||||||
|
*
|
||||||
|
* @param {string} id Guid, *Id รายการสมรรถนะ
|
||||||
|
*/
|
||||||
|
@Get("{id}")
|
||||||
|
@Example({
|
||||||
|
type: "HEAD",
|
||||||
|
name: "ชื่อสมรรถนะ",
|
||||||
|
description: "คำจำกัดความ",
|
||||||
|
kpiCapacityDetails: [{
|
||||||
|
level: 1,
|
||||||
|
description: "คำอธิบายระดับ"
|
||||||
|
}]
|
||||||
|
})
|
||||||
|
async GetKpiCapacityById(@Path() id: string) {
|
||||||
|
const kpiCapacity = await this.kpiCapacityRepository.findOne({
|
||||||
|
where: { id: id },
|
||||||
|
select: ["type", "name", "description"],
|
||||||
|
})
|
||||||
|
if (!kpiCapacity) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatusCode.NOT_FOUND,
|
||||||
|
"ไม่พบข้อมูลรายการสมรรถนะนี้",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const kpiCapacityDetails = await this.kpiCapacityDetailRepository.find({
|
||||||
|
where: { kpiCapacityId: id },
|
||||||
|
select: ["level", "description"],
|
||||||
|
order: { "level": "ASC" }
|
||||||
|
})
|
||||||
|
const mapData = {
|
||||||
|
type: kpiCapacity.type,
|
||||||
|
name: kpiCapacity.name,
|
||||||
|
description: kpiCapacity.description,
|
||||||
|
capacityDetails: kpiCapacityDetails
|
||||||
|
}
|
||||||
|
return new HttpSuccess(mapData);
|
||||||
|
}
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * API ลบรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
* API รายการสมรรถนะ
|
||||||
// * @param id
|
*
|
||||||
// */
|
* @summary รายการสมรรถนะ
|
||||||
// @Delete("{id}")
|
*
|
||||||
// async deleteKpiPerriod(@Path() id: string) {
|
*/
|
||||||
// const kpiPeriod = await this.kpiPeriodRepository.findOne({
|
@Get()
|
||||||
// where: { id: id },
|
async listKpiCapacity(
|
||||||
// });
|
@Query("page") page: number = 1,
|
||||||
// if (!kpiPeriod) {
|
@Query("pageSize") pageSize: number = 10,
|
||||||
// throw new HttpError(
|
@Query("type") type?: string,
|
||||||
// HttpStatusCode.NOT_FOUND,
|
@Query("keyword") keyword?: string,
|
||||||
// "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
) {
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
// await this.kpiPeriodRepository.remove(kpiPeriod);
|
const [kpiCapacity, total] = await AppDataSource.getRepository(KpiCapacity)
|
||||||
// return new HttpSuccess();
|
.createQueryBuilder("kpiCapacity")
|
||||||
// }
|
.leftJoinAndSelect("kpiCapacity.KpiCapacityDetails", "kpiCapacityDetail")
|
||||||
|
.andWhere(
|
||||||
|
keyword == undefined
|
||||||
|
? "1=1"
|
||||||
|
: [
|
||||||
|
{ "kpiCapacity.name": Like(`%${keyword}%`) },
|
||||||
|
{ "kpiCapacity.description": Like(`%${keyword}%`) },
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.andWhere(type == undefined ? "1=1" : { "kpiCapacity.type": type })
|
||||||
|
.orderBy("kpiCapacityDetail.level", "ASC")
|
||||||
|
.skip((page - 1) * pageSize)
|
||||||
|
.take(pageSize)
|
||||||
|
.getManyAndCount();
|
||||||
|
|
||||||
|
const mapFormula = kpiCapacity.map((item) => ({
|
||||||
|
id: item.id,
|
||||||
|
type: item.type,
|
||||||
|
name: item.name,
|
||||||
|
description: item.description,
|
||||||
|
capacityDetails: item.KpiCapacityDetails.map(detail => {
|
||||||
|
return {
|
||||||
|
id: detail.id,
|
||||||
|
description: detail.description,
|
||||||
|
level: detail.level,
|
||||||
|
capacityId: detail.kpiCapacityId
|
||||||
|
};
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
return new HttpSuccess({ data: mapFormula, total });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API ลบรายการสมรรถนะ
|
||||||
|
*
|
||||||
|
* @summary ลบรายการสมรรถนะ
|
||||||
|
*
|
||||||
|
* @param {string} id Guid, *Id รายการสมรรถนะ
|
||||||
|
*/
|
||||||
|
@Delete("{id}")
|
||||||
|
async deleteKpiCapacity(@Path() id: string) {
|
||||||
|
const kpiCapacity = await this.kpiCapacityRepository.findOne({
|
||||||
|
where: { id: id },
|
||||||
|
});
|
||||||
|
if (!kpiCapacity) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatusCode.NOT_FOUND,
|
||||||
|
"ไม่พบข้อมูลรายการสมรรถนะนี้",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const kpiCapacityDetails = await this.kpiCapacityDetailRepository.find({
|
||||||
|
where: { kpiCapacityId: id },
|
||||||
|
});
|
||||||
|
if (kpiCapacityDetails.length > 0) {
|
||||||
|
await this.kpiCapacityDetailRepository.remove(kpiCapacityDetails);
|
||||||
|
}
|
||||||
|
await this.kpiCapacityRepository.remove(kpiCapacity);
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue