hrms-api-kpi/src/controllers/KpiPlanController.ts

512 lines
18 KiB
TypeScript
Raw Normal View History

2024-04-19 09:44:46 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
} 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 { KpiPlan, createKpiPlan, updateKpiPlan } from "../entities/kpiPlan";
import CallAPI from "../interfaces/call-api";
2024-04-19 17:45:56 +07:00
import { KpiPeriod } from "../entities/kpiPeriod";
2024-05-07 11:37:15 +07:00
import { Brackets, IsNull, Not } from "typeorm";
import { KpiPlanHistory } from "../entities/kpiPlanHistory";
2024-04-19 09:44:46 +07:00
@Route("api/v1/kpi/plan")
@Tags("kpiPlan")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class kpiPlanController extends Controller {
private kpiPlanRepository = AppDataSource.getRepository(KpiPlan);
2024-05-07 11:37:15 +07:00
private kpiPlanHistoryRepository = AppDataSource.getRepository(KpiPlanHistory);
2024-04-19 17:45:56 +07:00
private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod);
2024-04-19 09:44:46 +07:00
/**
*
* @param requestBody
* @param request
*/
@Post()
async createKpiPlan(
@Body() requestBody: createKpiPlan,
@Request() request: { user: Record<string, any> },
) {
const kpiPlan = Object.assign(new KpiPlan(), requestBody);
2024-04-19 17:45:56 +07:00
if (requestBody.kpiPeriodId != null) {
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: requestBody.kpiPeriodId },
});
if (!kpiPeriod) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้");
}
}
2024-04-19 09:44:46 +07:00
await new CallAPI()
.PostData(request, "org/find/all", {
node: requestBody.node,
nodeId: requestBody.nodeId,
})
.then((x) => {
kpiPlan.root = x.root;
kpiPlan.rootId = x.rootId;
kpiPlan.rootShortName = x.rootShortName;
kpiPlan.child1 = requestBody.node <= 0 ? null : x.child1;
kpiPlan.child1Id = requestBody.node <= 0 ? null : x.child1Id;
kpiPlan.child1ShortName = requestBody.node <= 0 ? null : x.child1ShortName;
kpiPlan.child2 = requestBody.node <= 1 ? null : x.child2;
kpiPlan.child2Id = requestBody.node <= 1 ? null : x.child2Id;
kpiPlan.child2ShortName = requestBody.node <= 1 ? null : x.child2ShortName;
kpiPlan.child3 = requestBody.node <= 2 ? null : x.child3;
kpiPlan.child3Id = requestBody.node <= 2 ? null : x.child3Id;
kpiPlan.child3ShortName = requestBody.node <= 2 ? null : x.child3ShortName;
kpiPlan.child4 = requestBody.node <= 3 ? null : x.child4;
kpiPlan.child4Id = requestBody.node <= 3 ? null : x.child4Id;
kpiPlan.child4ShortName = requestBody.node <= 3 ? null : x.child4ShortName;
})
.catch((x) => {});
await new CallAPI()
.PostData(request, "development/strategy/find/all", {
strategy: requestBody.strategy,
strategyId: requestBody.strategyId,
})
.then((x) => {
kpiPlan.strategyChild1 = x.strategyChild1;
kpiPlan.strategyChild1Id = x.strategyChild1Id;
kpiPlan.strategyChild2 = requestBody.strategy <= 1 ? null : x.strategyChild2;
kpiPlan.strategyChild2Id = requestBody.strategy <= 1 ? null : x.strategyChild2Id;
kpiPlan.strategyChild3 = requestBody.strategy <= 2 ? null : x.strategyChild3;
kpiPlan.strategyChild3Id = requestBody.strategy <= 2 ? null : x.strategyChild3Id;
kpiPlan.strategyChild4 = requestBody.strategy <= 3 ? null : x.strategyChild4;
kpiPlan.strategyChild4Id = requestBody.strategy <= 3 ? null : x.strategyChild4Id;
kpiPlan.strategyChild5 = requestBody.strategy <= 4 ? null : x.strategyChild5;
kpiPlan.strategyChild5Id = requestBody.strategy <= 4 ? null : x.strategyChild5Id;
})
.catch((x) => {});
2024-04-19 17:35:42 +07:00
2024-05-07 11:37:15 +07:00
let maxIncludingPlan: any;
if (requestBody.node == 0) {
maxIncludingPlan = await this.kpiPlanRepository
.createQueryBuilder("kpiPlan")
.select("MAX(kpiPlan.including)", "maxIncluding")
.where("kpiPlan.rootId = :rootId AND kpiPlan.child1Id IS NULL", {
rootId: requestBody.nodeId,
})
.andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
} else if (requestBody.node == 1) {
maxIncludingPlan = await this.kpiPlanRepository
.createQueryBuilder("kpiPlan")
.select("MAX(kpiPlan.including)", "maxIncluding")
.where("kpiPlan.child1Id = :child1Id AND kpiPlan.child2Id IS NULL", {
child1Id: requestBody.nodeId,
})
.andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
} else if (requestBody.node == 2) {
maxIncludingPlan = await this.kpiPlanRepository
.createQueryBuilder("kpiPlan")
.select("MAX(kpiPlan.including)", "maxIncluding")
.where("kpiPlan.child2Id = :child2Id AND kpiPlan.child3Id IS NULL", {
child2Id: requestBody.nodeId,
})
.andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
} else if (requestBody.node == 3) {
maxIncludingPlan = await this.kpiPlanRepository
.createQueryBuilder("kpiPlan")
.select("MAX(kpiPlan.including)", "maxIncluding")
.where("kpiPlan.child3Id = :child3Id AND kpiPlan.child4Id IS NULL", {
child3Id: requestBody.nodeId,
})
.andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
} else if (requestBody.node == 4) {
maxIncludingPlan = await this.kpiPlanRepository
.createQueryBuilder("kpiPlan")
.select("MAX(kpiPlan.including)", "maxIncluding")
.where("kpiPlan.child4Id = :child4Id", {
child4Id: requestBody.nodeId,
})
.andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
}
kpiPlan.including = maxIncludingPlan.maxIncluding + 1;
2024-04-19 09:44:46 +07:00
kpiPlan.createdUserId = request.user.sub;
kpiPlan.createdFullName = request.user.name;
kpiPlan.lastUpdateUserId = request.user.sub;
kpiPlan.lastUpdateFullName = request.user.name;
await this.kpiPlanRepository.save(kpiPlan);
2024-05-07 11:37:15 +07:00
const history = new KpiPlanHistory();
history.kpiPlanId = kpiPlan.id;
history.createdUserId = request.user.sub;
history.createdFullName = request.user.name;
history.lastUpdateUserId = request.user.sub;
history.lastUpdateFullName = request.user.name;
await this.kpiPlanHistoryRepository.save(history);
2024-04-19 09:44:46 +07:00
return new HttpSuccess(kpiPlan.id);
}
/**
* API
* @param id
* @param requestBody
* @param request
*/
@Put("{id}")
async updateKpiPlan(
@Path() id: string,
@Body() requestBody: updateKpiPlan,
@Request() request: { user: Record<string, any> },
) {
const kpiPlan = await this.kpiPlanRepository.findOne({
where: { id: id },
});
if (!kpiPlan) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้");
}
2024-04-19 17:45:56 +07:00
if (requestBody.kpiPeriodId != null) {
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: requestBody.kpiPeriodId },
});
if (!kpiPeriod) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้");
}
}
2024-04-19 09:44:46 +07:00
Object.assign(kpiPlan, requestBody);
await new CallAPI()
.PostData(request, "org/find/all", {
node: requestBody.node,
nodeId: requestBody.nodeId,
})
.then((x) => {
kpiPlan.root = x.root;
kpiPlan.rootId = x.rootId;
kpiPlan.rootShortName = x.rootShortName;
kpiPlan.child1 = requestBody.node <= 0 ? null : x.child1;
kpiPlan.child1Id = requestBody.node <= 0 ? null : x.child1Id;
kpiPlan.child1ShortName = requestBody.node <= 0 ? null : x.child1ShortName;
kpiPlan.child2 = requestBody.node <= 1 ? null : x.child2;
kpiPlan.child2Id = requestBody.node <= 1 ? null : x.child2Id;
kpiPlan.child2ShortName = requestBody.node <= 1 ? null : x.child2ShortName;
kpiPlan.child3 = requestBody.node <= 2 ? null : x.child3;
kpiPlan.child3Id = requestBody.node <= 2 ? null : x.child3Id;
kpiPlan.child3ShortName = requestBody.node <= 2 ? null : x.child3ShortName;
kpiPlan.child4 = requestBody.node <= 3 ? null : x.child4;
kpiPlan.child4Id = requestBody.node <= 3 ? null : x.child4Id;
kpiPlan.child4ShortName = requestBody.node <= 3 ? null : x.child4ShortName;
})
.catch((x) => {});
await new CallAPI()
.PostData(request, "development/strategy/find/all", {
strategy: requestBody.strategy,
strategyId: requestBody.strategyId,
})
.then((x) => {
kpiPlan.strategyChild1 = x.strategyChild1;
kpiPlan.strategyChild1Id = x.strategyChild1Id;
kpiPlan.strategyChild2 = requestBody.strategy <= 1 ? null : x.strategyChild2;
kpiPlan.strategyChild2Id = requestBody.strategy <= 1 ? null : x.strategyChild2Id;
kpiPlan.strategyChild3 = requestBody.strategy <= 2 ? null : x.strategyChild3;
kpiPlan.strategyChild3Id = requestBody.strategy <= 2 ? null : x.strategyChild3Id;
kpiPlan.strategyChild4 = requestBody.strategy <= 3 ? null : x.strategyChild4;
kpiPlan.strategyChild4Id = requestBody.strategy <= 3 ? null : x.strategyChild4Id;
kpiPlan.strategyChild5 = requestBody.strategy <= 4 ? null : x.strategyChild5;
kpiPlan.strategyChild5Id = requestBody.strategy <= 4 ? null : x.strategyChild5Id;
})
.catch((x) => {});
kpiPlan.createdUserId = request.user.sub;
kpiPlan.createdFullName = request.user.name;
kpiPlan.lastUpdateUserId = request.user.sub;
kpiPlan.lastUpdateFullName = request.user.name;
await this.kpiPlanRepository.save(kpiPlan);
2024-05-07 11:37:15 +07:00
const history = new KpiPlanHistory();
history.kpiPlanId = kpiPlan.id;
history.createdUserId = request.user.sub;
history.createdFullName = request.user.name;
history.lastUpdateUserId = request.user.sub;
history.lastUpdateFullName = request.user.name;
await this.kpiPlanHistoryRepository.save(history);
2024-04-19 09:44:46 +07:00
return new HttpSuccess(id);
}
/**
* API
* @param id Guid, *Id
*/
@Get("{id}")
async GetKpiPlanById(@Path() id: string) {
const kpiPlan = await this.kpiPlanRepository.findOne({
where: { id: id },
2024-04-19 17:45:56 +07:00
relations: { kpiPeriod: true },
2024-04-19 09:44:46 +07:00
});
if (!kpiPlan) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้");
}
let node = null;
let nodeId = null;
let nodeName = null;
2024-04-19 09:44:46 +07:00
if (kpiPlan.child4Id != null) {
node = 4;
nodeId = kpiPlan.child4Id;
nodeName = kpiPlan.child4;
2024-04-19 09:44:46 +07:00
} else if (kpiPlan.child3Id != null) {
node = 3;
nodeId = kpiPlan.child3Id;
nodeName = kpiPlan.child3;
2024-04-19 09:44:46 +07:00
} else if (kpiPlan.child2Id != null) {
node = 2;
nodeId = kpiPlan.child2Id;
nodeName = kpiPlan.child2;
2024-04-19 09:44:46 +07:00
} else if (kpiPlan.child1Id != null) {
node = 1;
nodeId = kpiPlan.child1Id;
nodeName = kpiPlan.child1;
2024-04-19 09:44:46 +07:00
} else if (kpiPlan.rootId != null) {
node = 0;
nodeId = kpiPlan.rootId;
nodeName = kpiPlan.root;
2024-04-19 09:44:46 +07:00
}
let strategy = null;
let strategyId = null;
let strategyName = null;
2024-04-19 09:44:46 +07:00
if (kpiPlan.strategyChild5Id != null) {
strategy = 5;
strategyId = kpiPlan.strategyChild5Id;
} else if (kpiPlan.strategyChild4Id != null) {
strategy = 4;
strategyId = kpiPlan.strategyChild4Id;
strategyName = kpiPlan.strategyChild4;
2024-04-19 09:44:46 +07:00
} else if (kpiPlan.strategyChild3Id != null) {
strategy = 3;
strategyId = kpiPlan.strategyChild3Id;
strategyName = kpiPlan.strategyChild3;
2024-04-19 09:44:46 +07:00
} else if (kpiPlan.strategyChild2Id != null) {
strategy = 2;
strategyId = kpiPlan.strategyChild2Id;
strategyName = kpiPlan.strategyChild2;
2024-04-19 09:44:46 +07:00
} else if (kpiPlan.strategyChild1Id != null) {
strategy = 1;
strategyId = kpiPlan.strategyChild1Id;
strategyName = kpiPlan.strategyChild1;
2024-04-19 09:44:46 +07:00
}
const formattedData = {
id: kpiPlan.id,
2024-04-19 17:45:56 +07:00
year: kpiPlan.kpiPeriod == null ? null : kpiPlan.kpiPeriod.year,
round: kpiPlan.kpiPeriod == null ? null : kpiPlan.kpiPeriod.durationKPI,
kpiPeriodId: kpiPlan.kpiPeriodId,
2024-04-19 09:44:46 +07:00
including: kpiPlan.including,
includingName: kpiPlan.includingName,
target: kpiPlan.target,
unit: kpiPlan.unit,
weight: kpiPlan.weight,
achievement1: kpiPlan.achievement1,
achievement2: kpiPlan.achievement2,
achievement3: kpiPlan.achievement3,
achievement4: kpiPlan.achievement4,
achievement5: kpiPlan.achievement5,
meaning: kpiPlan.meaning,
formula: kpiPlan.formula,
2024-04-26 10:18:00 +07:00
root: kpiPlan.rootId,
child1: kpiPlan.child1Id,
child2: kpiPlan.child2Id,
child3: kpiPlan.child3Id,
child4: kpiPlan.child4Id,
2024-04-19 09:44:46 +07:00
node: node,
nodeId: nodeId,
nodeName: nodeName,
2024-04-19 09:44:46 +07:00
orgRevisionId: kpiPlan.orgRevisionId,
strategy: strategy,
strategyId: strategyId,
strategyName: strategyName,
2024-04-26 10:18:00 +07:00
strategyChild1: kpiPlan.strategyChild1Id,
strategyChild2: kpiPlan.strategyChild2Id,
strategyChild3: kpiPlan.strategyChild3Id,
strategyChild4: kpiPlan.strategyChild4Id,
strategyChild5: kpiPlan.strategyChild5Id,
2024-04-19 09:44:46 +07:00
};
return new HttpSuccess(formattedData);
}
/**
* API list
* @param page
* @param pageSize
* @param keyword
*/
@Get()
async listKpiPlan(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
2024-04-20 12:51:44 +07:00
@Query("kpiPeriodId") kpiPeriodId?: string,
2024-04-19 09:44:46 +07:00
@Query("nodeId") nodeId?: string | null,
@Query("node") node?: number | null,
@Query("keyword") keyword?: string,
) {
const [kpiPlan, total] = await AppDataSource.getRepository(KpiPlan)
.createQueryBuilder("kpiPlan")
2024-04-20 12:51:44 +07:00
.leftJoinAndSelect("kpiPlan.kpiPeriod", "kpiPeriod")
2024-04-19 09:44:46 +07:00
.andWhere(
node != undefined && node != null
? node == 4
? "kpiPlan.child4Id LIKE :nodeId"
: node == 3
? "kpiPlan.child3Id LIKE :nodeId"
: node == 2
? "kpiPlan.child2Id LIKE :nodeId"
: node == 1
? "kpiPlan.child1Id LIKE :nodeId"
: "kpiPlan.rootId LIKE :nodeId"
: "1=1",
{
2024-04-26 12:40:43 +07:00
nodeId: nodeId,
2024-04-19 09:44:46 +07:00
},
)
.andWhere(
2024-04-20 12:51:44 +07:00
kpiPeriodId != undefined && kpiPeriodId != null && kpiPeriodId != ""
? "kpiPlan.kpiPeriodId LIKE :kpiPeriodId"
2024-04-19 17:45:56 +07:00
: "1=1",
2024-04-19 09:44:46 +07:00
{
2024-04-26 12:40:43 +07:00
kpiPeriodId: kpiPeriodId,
2024-04-19 09:44:46 +07:00
},
)
.andWhere(
new Brackets((qb) => {
qb.orWhere("kpiPlan.including LIKE :keyword", { keyword: `%${keyword}%` }).orWhere(
"kpiPlan.includingName LIKE :keyword",
{ keyword: `%${keyword}%` },
);
}),
)
2024-04-19 09:44:46 +07:00
.select([
"kpiPlan.id",
2024-04-20 12:51:44 +07:00
"kpiPeriod.year",
"kpiPeriod.durationKPI",
2024-04-19 09:44:46 +07:00
"kpiPlan.including",
"kpiPlan.includingName",
2024-04-22 09:53:59 +07:00
"kpiPlan.createdAt",
2024-04-19 09:44:46 +07:00
])
.orderBy("kpiPlan.createdAt", "DESC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
return new HttpSuccess({ data: kpiPlan, total });
}
/**
* API
* @param id
*/
@Delete("{id}")
async deleteKpiPlan(@Path() id: string) {
const kpiPlan = await this.kpiPlanRepository.findOne({
where: { id: id },
});
if (!kpiPlan) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้");
}
2024-05-07 11:37:15 +07:00
let type = 0;
if (kpiPlan.child1Id != null) {
type = 1;
} else if (kpiPlan.child2Id != null) {
type = 2;
} else if (kpiPlan.child3Id != null) {
type = 3;
} else if (kpiPlan.child4Id != null) {
type = 4;
}
await this.kpiPlanHistoryRepository.delete({ kpiPlanId: id });
2024-04-19 09:44:46 +07:00
await this.kpiPlanRepository.remove(kpiPlan);
2024-05-07 11:37:15 +07:00
if (kpiPlan) {
let remainingKpiPlans: any;
if (type == 0) {
remainingKpiPlans = await this.kpiPlanRepository.find({
where: {
rootId: kpiPlan.rootId,
child1Id: IsNull(),
},
order: {
including: "ASC",
},
});
} else if (type == 1) {
remainingKpiPlans = await this.kpiPlanRepository.find({
where: {
child1Id: kpiPlan.child1Id,
child2Id: IsNull(),
},
order: {
including: "ASC",
},
});
} else if (type == 2) {
remainingKpiPlans = await this.kpiPlanRepository.find({
where: {
child2Id: kpiPlan.child2Id,
child3Id: IsNull(),
},
order: {
including: "ASC",
},
});
} else if (type == 3) {
remainingKpiPlans = await this.kpiPlanRepository.find({
where: {
child3Id: kpiPlan.child3Id,
child4Id: IsNull(),
},
order: {
including: "ASC",
},
});
} else if (type == 4) {
remainingKpiPlans = await this.kpiPlanRepository.find({
where: {
child4Id: kpiPlan.child4Id,
},
order: {
including: "ASC",
},
});
}
remainingKpiPlans.forEach((kpiPlan: any, index: any) => {
kpiPlan.including = index + 1;
});
await this.kpiPlanRepository.save(remainingKpiPlans);
}
2024-04-19 09:44:46 +07:00
return new HttpSuccess();
}
}