start project
This commit is contained in:
commit
0703810fa3
62 changed files with 12665 additions and 0 deletions
414
src/controllers/EvaluateController.ts
Normal file
414
src/controllers/EvaluateController.ts
Normal file
|
|
@ -0,0 +1,414 @@
|
|||
import {
|
||||
Controller,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
Request,
|
||||
SuccessResponse,
|
||||
Response,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Query,
|
||||
Put,
|
||||
} from "tsoa";
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import HttpStatusCode from "../interfaces/http-status";
|
||||
import { RequestWithUser } from "../middlewares/user";
|
||||
import { findEndDate, setLogDataDiff } from "../interfaces/utils";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import { AssignDirector } from "../entities/AssignDirector";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import { Assign } from "../entities/Assign";
|
||||
import {
|
||||
CreateEvaluateCommander,
|
||||
EvaluateCommander,
|
||||
} from "../entities/EvaluateCommander";
|
||||
import { Personal } from "../entities/Personal";
|
||||
import CallAPI from "../interfaces/call-api";
|
||||
|
||||
@Route("api/v1/evaluate")
|
||||
@Tags("แบบประเมินผล (ผู้บังคับบัญชา)")
|
||||
@Security("bearerAuth")
|
||||
@Response(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง"
|
||||
)
|
||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
export class EvaluateController extends Controller {
|
||||
private assignDirectorRepository =
|
||||
AppDataSource.getRepository(AssignDirector);
|
||||
private assignRepository = AppDataSource.getRepository(Assign);
|
||||
private evaluateCommanderRepository =
|
||||
AppDataSource.getRepository(EvaluateCommander);
|
||||
private personalRepository = AppDataSource.getRepository(Personal);
|
||||
|
||||
/**
|
||||
* API ข้อมูลตอนกดสร้างแบบประเมินผล (ผู้บังคับบัญชา)
|
||||
*
|
||||
* @summary ข้อมูลตอนกดสร้างแบบประเมินผล (ผู้บังคับบัญชา)
|
||||
*
|
||||
*/
|
||||
@Get("create")
|
||||
async CreateEvaluate(
|
||||
@Query() assign_id: string,
|
||||
@Request() request: RequestWithUser
|
||||
) {
|
||||
const director = await this.assignDirectorRepository.findOne({
|
||||
select: ["personal_id"],
|
||||
where: {
|
||||
assign_id,
|
||||
role: "commander",
|
||||
},
|
||||
});
|
||||
if (!director) {
|
||||
return new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล");
|
||||
}
|
||||
const director_id = director.personal_id;
|
||||
|
||||
const assign = await this.assignRepository.findOne({
|
||||
relations: ["profile"],
|
||||
where: { id: assign_id },
|
||||
});
|
||||
if (!assign) {
|
||||
return new HttpError(
|
||||
HttpStatusCode.NOT_FOUND,
|
||||
"ไม่พบข้อมูลแบบมอบหมายงาน"
|
||||
);
|
||||
}
|
||||
|
||||
const profile = await (assign.profile
|
||||
? {
|
||||
...assign.profile,
|
||||
id: assign.profile.personal_id,
|
||||
name:
|
||||
assign.profile.prefixName +
|
||||
assign.profile.firstName +
|
||||
" " +
|
||||
assign.profile.lastName,
|
||||
Oc: assign.profile.organization,
|
||||
}
|
||||
: null);
|
||||
|
||||
const evaluate_amount = await this.evaluateCommanderRepository.count({
|
||||
where: {
|
||||
assign_id,
|
||||
director_id,
|
||||
},
|
||||
});
|
||||
const evaluate_no = await (evaluate_amount + 1);
|
||||
const start_date =
|
||||
evaluate_amount == 0
|
||||
? assign.date_start
|
||||
: findEndDate(evaluate_amount * 3, assign.date_start);
|
||||
|
||||
const commanderData = await this.assignDirectorRepository.find({
|
||||
select: [
|
||||
"personal_id",
|
||||
"dated",
|
||||
"fullname",
|
||||
"position",
|
||||
"posType",
|
||||
"posLevel",
|
||||
],
|
||||
where: { personal_id: director_id },
|
||||
});
|
||||
|
||||
const commander = await commanderData.map((element) => ({
|
||||
...element,
|
||||
name: element.fullname,
|
||||
label: `${element.fullname} (${element.position}, ${element.posType}: ${element.posLevel})`,
|
||||
}));
|
||||
|
||||
return new HttpSuccess({
|
||||
person: profile,
|
||||
assign,
|
||||
evaluate_no: evaluate_no,
|
||||
start_date: start_date,
|
||||
end_date: findEndDate(3, start_date),
|
||||
director: commander,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* API ข้อมูลแบบประเมินผล (ผู้บังคับบัญชา)
|
||||
*
|
||||
* @summary ข้อมูลแบบประเมินผล (ผู้บังคับบัญชา)
|
||||
*
|
||||
*/
|
||||
@Get("")
|
||||
async GetEvaluate(@Query() assign_id: string, @Query() evaluate_no?: string) {
|
||||
// ต้องปรับเป็น id ของคนที่ access เข้ามา
|
||||
const director = await this.assignDirectorRepository.findOne({
|
||||
select: ["personal_id"],
|
||||
where: {
|
||||
assign_id,
|
||||
role: "commander",
|
||||
},
|
||||
});
|
||||
if (!director) {
|
||||
return new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล");
|
||||
}
|
||||
const director_id = director.personal_id;
|
||||
let evaluate: any = null;
|
||||
if (evaluate_no) {
|
||||
evaluate = await this.evaluateCommanderRepository.findOne({
|
||||
where: {
|
||||
director_id,
|
||||
assign_id,
|
||||
no: evaluate_no,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
evaluate = await this.evaluateCommanderRepository.findOne({
|
||||
where: {
|
||||
director_id,
|
||||
assign_id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (!evaluate) {
|
||||
return new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมิน");
|
||||
}
|
||||
|
||||
const assign = await this.assignRepository.findOne({
|
||||
where: { id: assign_id },
|
||||
});
|
||||
if (!assign) {
|
||||
return new HttpError(
|
||||
HttpStatusCode.NOT_FOUND,
|
||||
"ไม่พบข้อมูลแบบมอบหมายงาน"
|
||||
);
|
||||
}
|
||||
|
||||
// const profile = await (assign.profile
|
||||
// ? {
|
||||
// ...assign.profile,
|
||||
// id: assign.profile.personal_id,
|
||||
// name:
|
||||
// assign.profile.prefixName +
|
||||
// assign.profile.firstName +
|
||||
// " " +
|
||||
// assign.profile.lastName,
|
||||
// Oc: assign.profile.organization,
|
||||
// }
|
||||
// : null);
|
||||
|
||||
// const evaluate_amount = await this.evaluateCommanderRepository.count({
|
||||
// where: {
|
||||
// assign_id,
|
||||
// director_id,
|
||||
// },
|
||||
// });
|
||||
// const evaluate_no = await (evaluate_amount + 1);
|
||||
// const start_date =
|
||||
// evaluate_amount == 0
|
||||
// ? assign.date_start
|
||||
// : findEndDate(evaluate_amount * 3, assign.date_start);
|
||||
|
||||
const directorData = await this.assignDirectorRepository.find({
|
||||
select: [
|
||||
"personal_id",
|
||||
"dated",
|
||||
"fullname",
|
||||
"position",
|
||||
"posType",
|
||||
"posLevel",
|
||||
],
|
||||
where: { personal_id: director_id },
|
||||
});
|
||||
|
||||
const directors = await directorData.map((element) => ({
|
||||
...element,
|
||||
name: element.fullname,
|
||||
label: `${element.fullname} (${element.position}, ${element.posType}: ${element.posLevel})`,
|
||||
}));
|
||||
|
||||
const experimenteeData = await this.personalRepository.find({
|
||||
select: [
|
||||
"personal_id",
|
||||
"prefixName",
|
||||
"firstName",
|
||||
"lastName",
|
||||
"positionName",
|
||||
"positionLevelName",
|
||||
"organization",
|
||||
],
|
||||
where: { personal_id: assign.personal_id },
|
||||
});
|
||||
|
||||
const experimentee = await experimenteeData.map((element) => ({
|
||||
...element,
|
||||
name: element.prefixName + element.firstName + " " + element.lastName,
|
||||
Oc: element.organization,
|
||||
}));
|
||||
|
||||
return new HttpSuccess({
|
||||
experimentee: experimentee,
|
||||
director: directors ? directors : null,
|
||||
assign,
|
||||
evaluate,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* API บันทึกข้อมูลแบบประเมินผล (ผู้บังคับบัญชา)
|
||||
*
|
||||
* @summary บันทึกข้อมูลแบบประเมินผล (ผู้บังคับบัญชา)
|
||||
*
|
||||
*/
|
||||
@Post("")
|
||||
async PostData(
|
||||
@Query() assign_id: string,
|
||||
@Body() requestBody: CreateEvaluateCommander,
|
||||
@Request() request: RequestWithUser
|
||||
) {
|
||||
const director = await this.assignDirectorRepository.findOne({
|
||||
select: ["personal_id"],
|
||||
where: {
|
||||
assign_id,
|
||||
role: "commander",
|
||||
},
|
||||
});
|
||||
if (!director) {
|
||||
return new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล");
|
||||
}
|
||||
|
||||
const director_id = director.personal_id;
|
||||
|
||||
const assign = await this.assignRepository.findOne({
|
||||
relations: ["profile"],
|
||||
where: { id: assign_id },
|
||||
});
|
||||
if (!assign) {
|
||||
return new HttpError(
|
||||
HttpStatusCode.NOT_FOUND,
|
||||
"ไม่พบข้อมูลแบบมอบหมายงาน"
|
||||
);
|
||||
}
|
||||
|
||||
const postData: any = await {
|
||||
assign_id,
|
||||
...requestBody,
|
||||
director_id,
|
||||
no: requestBody.evaluate_no,
|
||||
date_start: requestBody.start_date,
|
||||
personal_id: assign.personal_id,
|
||||
|
||||
achievement_other_desc: requestBody.achievement_other
|
||||
? requestBody.achievement_other.text
|
||||
: "",
|
||||
achievement_other_level: requestBody.achievement_other
|
||||
? Number(requestBody.achievement_other.level)
|
||||
: 0,
|
||||
behavior_other_desc: requestBody.behavior_orther.text,
|
||||
behavior_other_level:
|
||||
requestBody.behavior_orther.text != ""
|
||||
? Number(requestBody.behavior_orther.level)
|
||||
: 0,
|
||||
|
||||
createdUserId: request.user.sub,
|
||||
createdFullName: request.user.name,
|
||||
updateUserId: request.user.sub,
|
||||
updateFullName: request.user.name,
|
||||
};
|
||||
|
||||
await this.evaluateCommanderRepository.save(postData, {
|
||||
data: request,
|
||||
});
|
||||
setLogDataDiff(request, { before: null, after: postData });
|
||||
|
||||
if (Number(requestBody.evaluate_no) < 2) {
|
||||
// #noted cronjob
|
||||
// แจ้งผู้บังคับบัญชาเข้ามาบันทึกผลทุก 3 เดือน 2 ครั้ง
|
||||
var dateSend = await findEndDate(3, requestBody.start_date);
|
||||
const nextNo = await (Number(requestBody.evaluate_no) + 1);
|
||||
await new CallAPI()
|
||||
.PostData(request, "/placement/noti", {
|
||||
subject: `ถึงกำหนดประเมินผลการทดลองปฏิบัติหน้าที่ราชการครั้งที่ ${nextNo} ${assign.profile.prefixName}${assign.profile.firstName} ${assign.profile.lastName}`,
|
||||
body: `ถึงกำหนดประเมินผลการทดลองปฏิบัติหน้าที่ราชการ (สำหรับผู้บังคับบัญชา) ครั้งที่ ${nextNo} ${assign.profile.prefixName}${assign.profile.firstName} ${assign.profile.lastName}`,
|
||||
receiverUserId: director_id,
|
||||
payload: "",
|
||||
isSendMail: true,
|
||||
isSendInbox: true,
|
||||
receiveDate: dateSend,
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error calling API:", error);
|
||||
});
|
||||
}
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API แก้ไขข้อมูลแบบประเมินผล (ผู้บังคับบัญชา)
|
||||
*
|
||||
* @summary แก้ไขข้อมูลแบบประเมินผล (ผู้บังคับบัญชา)
|
||||
*
|
||||
*/
|
||||
@Put("")
|
||||
async UpdateData(
|
||||
@Query() assign_id: string,
|
||||
@Query() evaluate_id: string,
|
||||
@Body() requestBody: CreateEvaluateCommander,
|
||||
@Request() request: RequestWithUser
|
||||
) {
|
||||
let evaluate = await this.evaluateCommanderRepository.findOne({
|
||||
where: { id: evaluate_id },
|
||||
});
|
||||
|
||||
const before = evaluate;
|
||||
|
||||
if (!evaluate) {
|
||||
return new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการประเมิน");
|
||||
}
|
||||
|
||||
evaluate.commander_dated = requestBody.commander_dated;
|
||||
evaluate.knowledge_level = requestBody.knowledge_level;
|
||||
evaluate.skill_level = requestBody.skill_level;
|
||||
evaluate.competency_level = requestBody.competency_level;
|
||||
evaluate.learn_level = requestBody.learn_level;
|
||||
evaluate.apply_level = requestBody.apply_level;
|
||||
evaluate.success_level = requestBody.success_level;
|
||||
evaluate.achievement_other_desc = requestBody.achievement_other
|
||||
? requestBody.achievement_other.text
|
||||
: "";
|
||||
evaluate.achievement_other_level =
|
||||
requestBody.achievement_other.text != ""
|
||||
? Number(requestBody.achievement_other.level)
|
||||
: 0;
|
||||
|
||||
evaluate.conduct1_level = requestBody.conduct1_level;
|
||||
evaluate.conduct2_level = requestBody.conduct2_level;
|
||||
evaluate.conduct3_level = requestBody.conduct3_level;
|
||||
evaluate.conduct4_level = requestBody.conduct4_level;
|
||||
evaluate.moral1_level = requestBody.moral1_level;
|
||||
evaluate.moral2_level = requestBody.moral2_level;
|
||||
evaluate.moral3_level = requestBody.moral3_level;
|
||||
evaluate.discipline1_level = requestBody.discipline1_level;
|
||||
evaluate.discipline2_level = requestBody.discipline2_level;
|
||||
evaluate.discipline3_level = requestBody.discipline3_level;
|
||||
evaluate.discipline4_level = requestBody.discipline4_level;
|
||||
evaluate.discipline5_level = requestBody.discipline5_level;
|
||||
evaluate.behavior_other_desc = requestBody.behavior_orther.text;
|
||||
evaluate.behavior_other_level =
|
||||
requestBody.behavior_orther.text != ""
|
||||
? Number(requestBody.behavior_orther.level)
|
||||
: 0;
|
||||
evaluate.behavior_strength_desc = requestBody.behavior_strength_desc;
|
||||
evaluate.behavior_improve_desc = requestBody.behavior_improve_desc;
|
||||
evaluate.orientation = requestBody.orientation;
|
||||
evaluate.self_learning = requestBody.self_learning;
|
||||
evaluate.training_seminar = requestBody.training_seminar;
|
||||
evaluate.other_training = requestBody.other_training;
|
||||
evaluate.updateUserId = request.user.sub;
|
||||
evaluate.updateFullName = request.user.name;
|
||||
|
||||
await this.evaluateCommanderRepository.save(evaluate, { data: request });
|
||||
setLogDataDiff(request, { before, after: evaluate });
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue