er ฝั่ง user
This commit is contained in:
parent
27da57f35e
commit
fae8d38013
16 changed files with 870 additions and 72 deletions
|
|
@ -14,14 +14,18 @@ import {
|
|||
SuccessResponse,
|
||||
Response,
|
||||
Query,
|
||||
ArrayValidator
|
||||
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 { KpiCapacity, createKpiCapacity, updateKpiCapacity } from "../entities/kpiCapacity";
|
||||
import { KpiCapacityDetail, createKpiCapacityDetail, updateKpiCapacityDetail } from "../entities/kpiCapacityDetail";
|
||||
import {
|
||||
KpiCapacityDetail,
|
||||
createKpiCapacityDetail,
|
||||
updateKpiCapacityDetail,
|
||||
} from "../entities/kpiCapacityDetail";
|
||||
import { Like, In } from "typeorm";
|
||||
|
||||
@Route("api/v1/kpi/capacity")
|
||||
|
|
@ -38,36 +42,39 @@ export class kpiCapacityController extends Controller {
|
|||
|
||||
/**
|
||||
* API สร้างรายการสมรรถนะ
|
||||
*
|
||||
*
|
||||
* @summary สร้างรายการสมรรถนะ
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Post()
|
||||
@Example({
|
||||
type: "HEAD",
|
||||
name: "ชื่อสมรรถนะ",
|
||||
description: "คำจำกัดความ",
|
||||
kpiCapacityDetails: [{
|
||||
level: "ระดับ",
|
||||
description: "คำอธิบายระดับ"
|
||||
}]
|
||||
kpiCapacityDetails: [
|
||||
{
|
||||
level: "ระดับ",
|
||||
description: "คำอธิบายระดับ",
|
||||
},
|
||||
],
|
||||
})
|
||||
async createKpiCapacity(
|
||||
@Body() requestBody: {
|
||||
type: string
|
||||
name: string
|
||||
description: string
|
||||
@Body()
|
||||
requestBody: {
|
||||
type: string;
|
||||
name: string;
|
||||
description: string;
|
||||
capacityDetails: {
|
||||
level: string;
|
||||
description: string;
|
||||
}[];
|
||||
},
|
||||
@Request() request: { user: Record<string, any> },
|
||||
){
|
||||
) {
|
||||
const kpiCapacity = Object.assign(new KpiCapacity(), {
|
||||
type: requestBody.type,
|
||||
name: requestBody.name,
|
||||
description: requestBody.description
|
||||
description: requestBody.description,
|
||||
});
|
||||
kpiCapacity.createdUserId = request.user.sub;
|
||||
kpiCapacity.createdFullName = request.user.name;
|
||||
|
|
@ -77,12 +84,13 @@ export class kpiCapacityController extends Controller {
|
|||
|
||||
let idx: number = 0;
|
||||
for (const data of requestBody.capacityDetails) {
|
||||
idx += 1
|
||||
let _level = (kpiCapacity.type === "HEAD" || kpiCapacity.type === "GROUP") ? idx.toString() : data.level;
|
||||
idx += 1;
|
||||
let _level =
|
||||
kpiCapacity.type === "HEAD" || kpiCapacity.type === "GROUP" ? idx.toString() : data.level;
|
||||
const kpiCapacityDetail = Object.assign(new KpiCapacityDetail(), {
|
||||
level: _level,
|
||||
description: data.description,
|
||||
kpiCapacityId: kpiCapacity.id
|
||||
kpiCapacityId: kpiCapacity.id,
|
||||
});
|
||||
kpiCapacityDetail.createdUserId = request.user.sub;
|
||||
kpiCapacityDetail.createdFullName = request.user.name;
|
||||
|
|
@ -91,14 +99,14 @@ export class kpiCapacityController extends Controller {
|
|||
await this.kpiCapacityDetailRepository.save(kpiCapacityDetail);
|
||||
}
|
||||
|
||||
return new HttpSuccess(kpiCapacity.id)
|
||||
return new HttpSuccess(kpiCapacity.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* API แก้ไขรายการสมรรถนะ
|
||||
*
|
||||
*
|
||||
* @summary แก้ไขรายการสมรรถนะ
|
||||
*
|
||||
*
|
||||
* @param {string} id Guid, *Id รายการสมรรถนะ
|
||||
*/
|
||||
@Put("{id}")
|
||||
|
|
@ -106,19 +114,22 @@ export class kpiCapacityController extends Controller {
|
|||
type: "HEAD",
|
||||
name: "ชื่อสมรรถนะ",
|
||||
description: "คำจำกัดความ",
|
||||
kpiCapacityDetails: [{
|
||||
level: "ระดับ",
|
||||
description: "คำอธิบายระดับ"
|
||||
}]
|
||||
kpiCapacityDetails: [
|
||||
{
|
||||
level: "ระดับ",
|
||||
description: "คำอธิบายระดับ",
|
||||
},
|
||||
],
|
||||
})
|
||||
async updateKpiCapacity(
|
||||
@Path() id: string,
|
||||
@Body() requestBody: {
|
||||
type: string
|
||||
name: string
|
||||
description: string
|
||||
@Body()
|
||||
requestBody: {
|
||||
type: string;
|
||||
name: string;
|
||||
description: string;
|
||||
capacityDetails: {
|
||||
level: string
|
||||
level: string;
|
||||
description: string;
|
||||
}[];
|
||||
},
|
||||
|
|
@ -128,15 +139,12 @@ export class kpiCapacityController extends Controller {
|
|||
where: { id: id },
|
||||
});
|
||||
if (!kpiCapacity) {
|
||||
throw new HttpError(
|
||||
HttpStatusCode.NOT_FOUND,
|
||||
"ไม่พบข้อมูลรายการสมรรถนะนี้",
|
||||
);
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการสมรรถนะนี้");
|
||||
}
|
||||
const _kpiCapacity = Object.assign(new KpiCapacity(), {
|
||||
type: requestBody.type,
|
||||
name: requestBody.name,
|
||||
description: requestBody.description
|
||||
description: requestBody.description,
|
||||
});
|
||||
kpiCapacity.lastUpdateUserId = request.user.sub;
|
||||
kpiCapacity.lastUpdateFullName = request.user.name;
|
||||
|
|
@ -150,12 +158,13 @@ export class kpiCapacityController extends Controller {
|
|||
|
||||
let idx: number = 0;
|
||||
for (const data of requestBody.capacityDetails) {
|
||||
idx += 1
|
||||
let _level = (kpiCapacity.type === "HEAD" || kpiCapacity.type === "GROUP") ? idx.toString() : data.level;
|
||||
idx += 1;
|
||||
let _level =
|
||||
kpiCapacity.type === "HEAD" || kpiCapacity.type === "GROUP" ? idx.toString() : data.level;
|
||||
const kpiCapacityDetail = Object.assign(new KpiCapacityDetail(), {
|
||||
level: _level,
|
||||
description: data.description,
|
||||
kpiCapacityId: kpiCapacity.id
|
||||
kpiCapacityId: kpiCapacity.id,
|
||||
});
|
||||
kpiCapacityDetail.createdUserId = request.user.sub;
|
||||
kpiCapacityDetail.createdFullName = request.user.name;
|
||||
|
|
@ -163,8 +172,8 @@ export class kpiCapacityController extends Controller {
|
|||
kpiCapacityDetail.lastUpdateFullName = request.user.name;
|
||||
await this.kpiCapacityDetailRepository.save(kpiCapacityDetail);
|
||||
}
|
||||
|
||||
return new HttpSuccess(kpiCapacity.id)
|
||||
|
||||
return new HttpSuccess(kpiCapacity.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -179,33 +188,32 @@ export class kpiCapacityController extends Controller {
|
|||
type: "HEAD",
|
||||
name: "ชื่อสมรรถนะ",
|
||||
description: "คำจำกัดความ",
|
||||
kpiCapacityDetails: [{
|
||||
level: 1,
|
||||
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,
|
||||
"ไม่พบข้อมูลรายการสมรรถนะนี้",
|
||||
);
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการสมรรถนะนี้");
|
||||
}
|
||||
const kpiCapacityDetails = await this.kpiCapacityDetailRepository.find({
|
||||
where: { kpiCapacityId: id },
|
||||
select: ["level", "description"],
|
||||
order: { "level": "ASC" }
|
||||
})
|
||||
order: { level: "ASC" },
|
||||
});
|
||||
const mapData = {
|
||||
type: kpiCapacity.type,
|
||||
name: kpiCapacity.name,
|
||||
description: kpiCapacity.description,
|
||||
capacityDetails: kpiCapacityDetails
|
||||
}
|
||||
capacityDetails: kpiCapacityDetails,
|
||||
};
|
||||
return new HttpSuccess(mapData);
|
||||
}
|
||||
|
||||
|
|
@ -222,17 +230,13 @@ export class kpiCapacityController extends Controller {
|
|||
@Query("type") type?: string,
|
||||
@Query("keyword") keyword?: string,
|
||||
) {
|
||||
|
||||
const [kpiCapacity, total] = await AppDataSource.getRepository(KpiCapacity)
|
||||
.createQueryBuilder("kpiCapacity")
|
||||
.leftJoinAndSelect("kpiCapacity.KpiCapacityDetails", "kpiCapacityDetail")
|
||||
.leftJoinAndSelect("kpiCapacity.kpiCapacityDetails", "kpiCapacityDetail")
|
||||
.andWhere(
|
||||
keyword == undefined
|
||||
? "1=1"
|
||||
: [
|
||||
{ name: Like(`%${keyword}%`) },
|
||||
{ description: Like(`%${keyword}%`) },
|
||||
],
|
||||
: [{ name: Like(`%${keyword}%`) }, { description: Like(`%${keyword}%`) }],
|
||||
)
|
||||
.andWhere(type == undefined ? "1=1" : { type: type })
|
||||
.orderBy("kpiCapacityDetail.level", "ASC")
|
||||
|
|
@ -245,23 +249,23 @@ export class kpiCapacityController extends Controller {
|
|||
type: item.type,
|
||||
name: item.name,
|
||||
description: item.description,
|
||||
capacityDetails: item.KpiCapacityDetails.map(detail => {
|
||||
capacityDetails: item.kpiCapacityDetails.map((detail) => {
|
||||
return {
|
||||
id: detail.id,
|
||||
description: detail.description,
|
||||
level: detail.level,
|
||||
capacityId: detail.kpiCapacityId
|
||||
capacityId: detail.kpiCapacityId,
|
||||
};
|
||||
})
|
||||
}),
|
||||
}));
|
||||
return new HttpSuccess({ data: mapFormula, total });
|
||||
}
|
||||
|
||||
/**
|
||||
* API ลบรายการสมรรถนะ
|
||||
*
|
||||
*
|
||||
* @summary ลบรายการสมรรถนะ
|
||||
*
|
||||
*
|
||||
* @param {string} id Guid, *Id รายการสมรรถนะ
|
||||
*/
|
||||
@Delete("{id}")
|
||||
|
|
@ -270,10 +274,7 @@ export class kpiCapacityController extends Controller {
|
|||
where: { id: id },
|
||||
});
|
||||
if (!kpiCapacity) {
|
||||
throw new HttpError(
|
||||
HttpStatusCode.NOT_FOUND,
|
||||
"ไม่พบข้อมูลรายการสมรรถนะนี้",
|
||||
);
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการสมรรถนะนี้");
|
||||
}
|
||||
const kpiCapacityDetails = await this.kpiCapacityDetailRepository.find({
|
||||
where: { kpiCapacityId: id },
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue