hrms-api-probation/src/controllers/DataOptionsController.ts

409 lines
13 KiB
TypeScript
Raw Normal View History

2025-03-26 14:35:09 +07:00
import {
Controller,
Route,
Security,
Tags,
Request,
SuccessResponse,
Response,
Get,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { RequestWithUser } from "../middlewares/user";
import { Knowledge, TypeKnowledge } from "../entities/Knowledge";
import { Skill, TypeSkill } from "../entities/Skill";
import { MapKnowledgeSkill } from "../entities/MapKnowledgeSkill";
import { Personal } from "../entities/Personal";
import { Law } from "../entities/Law";
import { Assign } from "../entities/Assign";
import permission from "../interfaces/permission";
2024-09-05 13:59:43 +07:00
2024-09-05 16:56:22 +07:00
@Route("api/v1/probation/data-options")
2024-09-05 13:59:43 +07:00
@Tags("Data Options")
@Security("bearerAuth")
2025-03-26 14:35:09 +07:00
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
2025-04-25 09:15:26 +07:00
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
2025-03-26 14:35:09 +07:00
)
2024-09-05 13:59:43 +07:00
export class DataOptionController extends Controller {
2025-03-26 14:35:09 +07:00
private personalRepository = AppDataSource.getRepository(Personal);
private knowledgeRepository = AppDataSource.getRepository(Knowledge);
2025-04-25 09:15:26 +07:00
private mapKnowledgeSkillRepository = AppDataSource.getRepository(MapKnowledgeSkill);
2025-03-26 14:35:09 +07:00
private skillRepository = AppDataSource.getRepository(Skill);
private lawRepository = AppDataSource.getRepository(Law);
private assignRepository = AppDataSource.getRepository(Assign);
/**
* API list
*
* @summary options
*
*/
@Get("knowledge")
async GetKnowledge(@Query() personal_id: string) {
try {
const person = await this.personalRepository.findOne({
where: { personal_id },
});
if (!person) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคล");
}
let result = await this.mapKnowledgeSkillRepository.findOne({
where: {
positionName: person.positionName,
positionLevelName: person.positionLevelName,
active: 1,
},
});
if (!result) {
// ถ้าตำแหน่งไม่ตรงหาจากระดับตำแหน่งแทนเพื่อให้ฟอร์มสามารถกรอกต่อได้
result = await this.mapKnowledgeSkillRepository.findOne({
where: {
positionLevelName: person.positionLevelName,
active: 1,
},
});
}
if (!result) {
2025-04-25 09:15:26 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลความรู้ที่ตรงกับตำแหน่ง");
2025-03-26 14:35:09 +07:00
}
const knowledges = await this.knowledgeRepository.find({
where: { type: TypeKnowledge.PERFORMANCE, active: 1 },
});
const knowledge = knowledges.map((knowledge) => ({
id: knowledge.id,
title: knowledge.title,
description:
result.knowlage_performance_level == 1
? knowledge.level1
: result.knowlage_performance_level == 2
? knowledge.level2
: result.knowlage_performance_level == 3
? knowledge.level3
: result.knowlage_performance_level == 4
? knowledge.level4
: knowledge.level5,
level: result.knowlage_performance_level,
}));
return new HttpSuccess(knowledge);
} catch (error: any) {
if (error instanceof HttpError) {
throw error;
2025-04-25 09:15:26 +07:00
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
2025-03-26 14:35:09 +07:00
}
}
/**
* API
*
* @summary options
*
*/
@Get("skill")
async GetSkill(@Query() personal_id: string) {
try {
const person = await this.personalRepository.findOne({
where: { personal_id },
});
if (!person) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
let result = await this.mapKnowledgeSkillRepository.findOne({
select: [
"skill_computer_level",
"skill_english_level",
"skill_information_level",
"skill_resourse_level",
],
where: {
positionName: person.positionName,
positionLevelName: person.positionLevelName,
active: 1,
},
});
if (!result) {
// ถ้าตำแหน่งไม่ตรงหาจากระดับตำแหน่งแทนเพื่อให้ฟอร์มสามารถกรอกต่อได้
result = await this.mapKnowledgeSkillRepository.findOne({
select: [
"skill_computer_level",
"skill_english_level",
"skill_information_level",
"skill_resourse_level",
],
where: {
positionLevelName: person.positionLevelName,
active: 1,
},
});
}
if (!result) {
2025-04-25 09:15:26 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทักษะที่ตรงกับตำแหน่ง");
2025-03-26 14:35:09 +07:00
}
const computerData = await this.skillRepository.findOne({
where: { type: TypeSkill.COMPUTER, active: 1 },
});
if (!computerData) {
2025-04-25 09:15:26 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทักษะคอมพิวเตอร์");
2025-03-26 14:35:09 +07:00
}
const computer = await {
id: computerData.id,
title: computerData.title,
level_description:
result.skill_computer_level == 1
? computerData.level1
: result.skill_computer_level == 2
? computerData.level2
: result.skill_computer_level == 3
? computerData.level3
: result.skill_computer_level == 4
? computerData.level4
: computerData.level5,
level: result.skill_computer_level,
};
const englishData = await this.skillRepository.findOne({
where: { type: TypeSkill.ENG, active: 1 },
});
if (!englishData) {
2025-04-25 09:15:26 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทักษะภาษาอังกฤษ");
2025-03-26 14:35:09 +07:00
}
const english = await {
id: englishData.id,
title: englishData.title,
level_description:
result.skill_english_level == 1
? englishData.level1
: result.skill_english_level == 2
? englishData.level2
: result.skill_english_level == 3
? englishData.level3
: result.skill_english_level == 4
? englishData.level4
: englishData.level5,
level: result.skill_english_level,
};
const informationData = await this.skillRepository.findOne({
where: { type: TypeSkill.INFORMATION, active: 1 },
});
if (!informationData) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const information = await {
id: informationData.id,
title: informationData.title,
level_description:
result.skill_information_level == 1
? informationData.level1
: result.skill_information_level == 2
? informationData.level2
: result.skill_information_level == 3
? informationData.level3
: result.skill_information_level == 4
? informationData.level4
: informationData.level5,
level: result.skill_information_level,
};
const resourseData = await this.skillRepository.findOne({
where: { type: TypeSkill.RESOURSE, active: 1 },
});
if (!resourseData) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const resourse = await {
id: resourseData.id,
title: resourseData.title,
level_description:
result.skill_resourse_level == 1
? resourseData.level1
: result.skill_resourse_level == 2
? resourseData.level2
: result.skill_resourse_level == 3
? resourseData.level3
: result.skill_resourse_level == 4
? resourseData.level4
: resourseData.level5,
level: result.skill_resourse_level,
};
return new HttpSuccess({
computer,
english,
information,
resourse,
});
} catch (error: any) {
if (error instanceof HttpError) {
throw error;
2025-04-25 09:15:26 +07:00
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
2025-03-26 14:35:09 +07:00
}
}
/**
* API list
*
* @summary options
*
*/
@Get("law")
async GetLaw(@Query() personal_id: string) {
try {
const results = await this.lawRepository.find({
select: ["id", "parent_id", "description", "status_select"],
where: {
active: 1,
},
});
if (!results) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const result = await results.map((v) => ({
...v,
checked: 0,
}));
return new HttpSuccess(result);
} catch (error: any) {
if (error instanceof HttpError) {
throw error;
2025-04-25 09:15:26 +07:00
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
2025-03-26 14:35:09 +07:00
}
}
/**
* API
*
* @summary
*
*/
@Get("new-assign")
2025-04-25 09:15:26 +07:00
async NewAssign(@Query() personal_id: string, @Request() request: RequestWithUser) {
2025-03-26 14:35:09 +07:00
try {
2025-04-25 09:15:26 +07:00
let _workflow = await new permission().Workflow(request, personal_id, "SYS_PROBATION");
if (_workflow == false) await new permission().PermissionGet(request, "SYS_PROBATION");
2025-03-26 14:35:09 +07:00
const person = await this.personalRepository.findOne({
select: [
"personal_id",
"prefixName",
"firstName",
"lastName",
"posNo",
"positionName",
"positionLevelName",
"positionLineName",
"isProbation",
"orgRootName",
"organization",
"createdAt",
"updatedAt",
],
where: { personal_id },
});
if (!person) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const assign = await this.assignRepository.count({
where: {
personal_id,
},
});
const responsePerson = {
id: person.personal_id,
...person,
};
return new HttpSuccess({
person: responsePerson,
assign_no: assign + 1,
assign_month: 6,
});
} catch (error: any) {
if (error instanceof HttpError) {
throw error;
2025-04-25 09:15:26 +07:00
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
2025-03-26 14:35:09 +07:00
}
}
/**
* API (USER)
*
* @summary (USER)
*
*/
@Get("new-assign-user")
2025-04-25 09:15:26 +07:00
async NewAssignUser(@Query() personal_id: string, @Request() request: RequestWithUser) {
2025-03-26 14:35:09 +07:00
try {
const person = await this.personalRepository.findOne({
select: [
"personal_id",
"prefixName",
"firstName",
"lastName",
"posNo",
"positionName",
"positionLevelName",
"positionLineName",
"isProbation",
"orgRootName",
"organization",
"createdAt",
"updatedAt",
],
where: { personal_id },
});
if (!person) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const assign = await this.assignRepository.count({
where: {
personal_id,
},
});
const responsePerson = {
id: person.personal_id,
...person,
};
return new HttpSuccess({
person: responsePerson,
assign_no: assign + 1,
assign_month: 6,
});
} catch (error: any) {
if (error instanceof HttpError) {
throw error;
2025-04-25 09:15:26 +07:00
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
2025-03-26 14:35:09 +07:00
}
}
2024-09-05 13:59:43 +07:00
}