hrms-api-development/src/controllers/DevelopmentController.ts

1294 lines
50 KiB
TypeScript
Raw Normal View History

2024-04-02 17:53:45 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Query,
Example,
2024-04-02 17:53:45 +07:00
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import { In, Not } from "typeorm";
2024-04-02 17:53:45 +07:00
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
2024-04-11 11:39:56 +07:00
import {
Development,
CreateDevelopment,
UpdateDevelopment1,
UpdateDevelopment2_1,
UpdateDevelopment2_2,
UpdateDevelopment3,
UpdateDevelopment4,
UpdateDevelopment5,
} from "../entities/Development";
import { ActualPeople, CreateActualPeople } from "../entities/ActualPeople";
import { CreatePlannedPeople, PlannedPeople } from "../entities/PlannedPeople";
import { ActualGoal, CreateActualGoal } from "../entities/ActualGoal";
import { CreatePlannedGoal, PlannedGoal } from "../entities/PlannedGoal";
import { Province } from "../entities/Province";
import { PosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
import { PlannedGoalPosition } from "../entities/PlannedGoalPosition";
2024-04-11 11:39:56 +07:00
import { DevelopmentHistory } from "../entities/DevelopmentHistory";
import { DevelopmentProjectType } from "../entities/DevelopmentProjectType";
2024-04-11 16:32:44 +07:00
import {
CreateDevelopmentEvaluation,
DevelopmentEvaluation,
} from "../entities/DevelopmentEvaluation";
2024-04-11 11:39:56 +07:00
import { DevelopmentAddress } from "../entities/DevelopmentAddress";
2024-04-11 16:32:44 +07:00
import { DevelopmentProjectTechniquePlanned } from "../entities/DevelopmentProjectTechniquePlanned";
import { DevelopmentProjectTechniqueActual } from "../entities/DevelopmentProjectTechniqueActual";
2024-04-02 17:53:45 +07:00
@Route("api/v1/development/main")
@Tags("Development")
@Security("bearerAuth")
export class DevelopmentController extends Controller {
private developmentRepository = AppDataSource.getRepository(Development);
2024-04-11 11:39:56 +07:00
private developmentAddresssRepository = AppDataSource.getRepository(DevelopmentAddress);
private developmentEvaluationRepository = AppDataSource.getRepository(DevelopmentEvaluation);
private developmentProjectTypeRepository = AppDataSource.getRepository(DevelopmentProjectType);
2024-04-11 16:32:44 +07:00
private developmentProjectTechniquePlannedRepository = AppDataSource.getRepository(
DevelopmentProjectTechniquePlanned,
);
private developmentProjectTechniqueActualRepository = AppDataSource.getRepository(
DevelopmentProjectTechniqueActual,
2024-04-11 11:39:56 +07:00
);
private developmentHistoryRepository = AppDataSource.getRepository(DevelopmentHistory);
private actualPeopleRepository = AppDataSource.getRepository(ActualPeople);
private plannedPeopleRepository = AppDataSource.getRepository(PlannedPeople);
private actualGoalRepository = AppDataSource.getRepository(ActualGoal);
private plannedGoalRepository = AppDataSource.getRepository(PlannedGoal);
private plannedGoalPositionRepository = AppDataSource.getRepository(PlannedGoalPosition);
private provinceRepository = AppDataSource.getRepository(Province);
private posTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
2024-04-02 17:53:45 +07:00
/**
* API /
*
* @summary DEV_001 - /#1
*
*/
@Post()
async CreateDevelopment(
@Body() requestBody: CreateDevelopment,
@Request() request: { user: Record<string, any> },
) {
const chk_name = await this.developmentRepository.find({
where: {
projectName: requestBody.projectName,
year: requestBody.year,
2024-04-02 17:53:45 +07:00
},
});
if (chk_name.length > 0) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"โครงการ/หลักสูตรการฝึกอบรม: " +
requestBody.projectName +
" ปีงบประมาณ: " +
2024-04-03 16:14:00 +07:00
(requestBody.year + 543) +
" มีอยู่ในระบบแล้ว",
2024-04-02 17:53:45 +07:00
);
}
const development = Object.assign(new Development(), requestBody);
2024-04-02 17:53:45 +07:00
development.createdUserId = request.user.sub;
development.createdFullName = request.user.name;
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.developmentRepository.save(development);
2024-04-11 11:39:56 +07:00
return new HttpSuccess(development.id);
}
/**
* API / Tab1
*
* @summary DEV_00 - /Tab1 #
*
* @param {string} id Id
*/
@Put("tab1/{id}")
async UpdateDevelopmentTab1(
@Path() id: string,
@Body() requestBody: UpdateDevelopment1,
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
const chk_name = await this.developmentRepository.find({
where: {
projectName: requestBody.projectName,
year: requestBody.year,
id: Not(id),
},
});
if (chk_name.length > 0) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"โครงการ/หลักสูตรการฝึกอบรม: " +
requestBody.projectName +
" ปีงบประมาณ: " +
(requestBody.year + 543) +
" มีอยู่ในระบบแล้ว",
);
}
Object.assign(development, requestBody);
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.developmentRepository.save(development);
return new HttpSuccess(development.id);
}
/**
* API /tab2-1
*
* @summary DEV_00 - /tab2-1 #
*
* @param {string} id Id
*/
@Put("tab2_1_add/{id}")
async CreateDevelopmenttab2_1(
@Path() id: string,
@Body() requestBody: CreatePlannedGoal,
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
2024-04-11 16:32:44 +07:00
const data = Object.assign(new PlannedGoal(), { ...requestBody, positions: [] });
2024-04-11 11:39:56 +07:00
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentPlannedGoalId = development.id;
await this.plannedGoalRepository.save(data);
await Promise.all(
2024-04-11 11:39:56 +07:00
requestBody.positions.map(async (x) => {
const _data = Object.assign(new PlannedGoalPosition(), x);
if (x.posTypePlannedId != null) {
const checkId = await this.posTypeRepository.findOne({
2024-04-11 11:39:56 +07:00
where: { id: x.posTypePlannedId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
}
}
2024-04-11 11:39:56 +07:00
if (x.posLevelPlannedId != null) {
const checkId = await this.posLevelRepository.findOne({
2024-04-11 11:39:56 +07:00
where: { id: x.posLevelPlannedId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
2024-04-11 11:39:56 +07:00
_data.createdUserId = request.user.sub;
_data.createdFullName = request.user.name;
_data.lastUpdateUserId = request.user.sub;
_data.lastUpdateFullName = request.user.name;
_data.plannedGoalId = data.id;
await this.plannedGoalPositionRepository.save(_data);
}),
);
2024-04-11 11:39:56 +07:00
return new HttpSuccess(data.id);
}
/**
* API /tab2-2
*
* @summary DEV_00 - /tab2-2 #
*
* @param {string} id Id
*/
@Put("tab2_2_add/{id}")
async CreateDevelopmenttab2_2(
@Path() id: string,
@Body() requestBody: CreatePlannedPeople,
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
const data = Object.assign(new PlannedPeople(), requestBody);
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentPlannedPeopleId = development.id;
await this.plannedPeopleRepository.save(data);
return new HttpSuccess(data.id);
}
/**
* API /tab2-3
*
* @summary DEV_00 - /tab2-3 #
*
* @param {string} id Id
*/
@Put("tab2_3_add/{id}")
async CreateDevelopmenttab2_3(
@Path() id: string,
@Body() requestBody: CreateActualGoal,
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
if (requestBody.posTypeActualId != null) {
const checkId = await this.posTypeRepository.findOne({
where: { id: requestBody.posTypeActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
}
}
if (requestBody.posLevelActualId != null) {
const checkId = await this.posLevelRepository.findOne({
where: { id: requestBody.posLevelActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
const data = Object.assign(new ActualGoal(), requestBody);
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentActualGoalId = development.id;
await this.actualGoalRepository.save(data);
return new HttpSuccess(data.id);
}
/**
* API /tab2-4
*
* @summary DEV_00 - /tab2-4 #
*
* @param {string} id Id
*/
@Put("tab2_4_add/{id}")
async CreateDevelopmenttab2_4(
@Path() id: string,
2024-04-11 16:32:44 +07:00
@Body() requestBody: CreateActualPeople,
2024-04-11 11:39:56 +07:00
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
const data = Object.assign(new ActualPeople(), requestBody);
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentActualPeopleId = development.id;
await this.actualPeopleRepository.save(data);
return new HttpSuccess(data.id);
}
/**
* API /tab2-1
*
* @summary DEV_00 - /tab2-1 #
*
2024-04-11 16:32:44 +07:00
* @param {string} id Id
2024-04-11 11:39:56 +07:00
*/
@Put("tab2_1_edit/{id}")
async UpdateDevelopmenttab2_1(
@Path() id: string,
@Body() requestBody: CreatePlannedGoal,
@Request() request: { user: Record<string, any> },
) {
const development = await this.plannedGoalRepository.findOne({
where: { id },
relations: {
plannedGoalPositions: true,
},
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
await this.plannedGoalPositionRepository.remove(development.plannedGoalPositions);
2024-04-11 16:32:44 +07:00
Object.assign(development, { ...requestBody, positions: [] });
2024-04-11 11:39:56 +07:00
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.plannedGoalRepository.save(development);
if (requestBody.positions != null) {
await Promise.all(
requestBody.positions.map(async (x) => {
const _data = Object.assign(new PlannedGoalPosition(), x);
if (x.posTypePlannedId != null) {
const checkId = await this.posTypeRepository.findOne({
where: { id: x.posTypePlannedId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
}
}
if (x.posLevelPlannedId != null) {
const checkId = await this.posLevelRepository.findOne({
where: { id: x.posLevelPlannedId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
_data.createdUserId = request.user.sub;
_data.createdFullName = request.user.name;
_data.lastUpdateUserId = request.user.sub;
_data.lastUpdateFullName = request.user.name;
_data.plannedGoalId = development.id;
await this.plannedGoalPositionRepository.save(_data);
}),
);
}
return new HttpSuccess(development.id);
}
/**
* API /tab2-2
*
* @summary DEV_00 - /tab2-2 #
*
2024-04-11 16:32:44 +07:00
* @param {string} id Id
2024-04-11 11:39:56 +07:00
*/
@Put("tab2_2_edit/{id}")
async UpdateDevelopmenttab2_2(
@Path() id: string,
@Body() requestBody: CreatePlannedPeople,
@Request() request: { user: Record<string, any> },
) {
const development = await this.plannedPeopleRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
Object.assign(development, requestBody);
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.plannedPeopleRepository.save(development);
return new HttpSuccess(development.id);
}
/**
* API /tab2-3
*
* @summary DEV_00 - /tab2-3 #
*
2024-04-11 16:32:44 +07:00
* @param {string} id Id
2024-04-11 11:39:56 +07:00
*/
@Put("tab2_3_edit/{id}")
async UpdateDevelopmenttab2_3(
@Path() id: string,
@Body() requestBody: CreateActualGoal,
@Request() request: { user: Record<string, any> },
) {
const development = await this.actualGoalRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
if (requestBody.posTypeActualId != null) {
const checkId = await this.posTypeRepository.findOne({
where: { id: requestBody.posTypeActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
}
}
if (requestBody.posLevelActualId != null) {
const checkId = await this.posLevelRepository.findOne({
where: { id: requestBody.posLevelActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
Object.assign(development, requestBody);
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.actualGoalRepository.save(development);
return new HttpSuccess(development.id);
}
/**
* API /tab2-4
*
* @summary DEV_00 - /tab2-4 #
*
2024-04-11 16:32:44 +07:00
* @param {string} id Id
2024-04-11 11:39:56 +07:00
*/
@Put("tab2_4_edit/{id}")
async UpdateDevelopmenttab2_4(
@Path() id: string,
@Body() requestBody: CreateActualPeople,
@Request() request: { user: Record<string, any> },
) {
const development = await this.actualPeopleRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
Object.assign(development, requestBody);
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.actualPeopleRepository.save(development);
return new HttpSuccess(development.id);
}
/**
* API /tab2-1
*
* @summary DEV_00 - /tab2-1 #
*
* @param {string} id Id
*/
@Delete("tab2_1/{id}")
async DeleteDevelopmenttab2_1(@Path() id: string) {
const development = await this.plannedGoalRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มเป้าหมายตามแผน");
}
const _development = await this.plannedGoalPositionRepository.find({
where: { plannedGoalId: id },
});
await this.plannedGoalPositionRepository.remove(_development);
await this.plannedGoalRepository.remove(development);
return new HttpSuccess(development.id);
}
/**
* API /tab2-2
*
* @summary DEV_00 - /tab2-2 #
*
* @param {string} id Id
*/
@Delete("tab2_2/{id}")
async DeleteDevelopmenttab2_2(@Path() id: string) {
const development = await this.plannedPeopleRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้เกี่ยวข้องเป้าหมายตามแผน");
}
await this.plannedPeopleRepository.remove(development);
return new HttpSuccess(development.id);
}
/**
* API /tab2-3
*
* @summary DEV_00 - /tab2-3 #
*
* @param {string} id Id
*/
@Delete("tab2_3/{id}")
async DeleteDevelopmenttab2_3(@Path() id: string) {
const development = await this.actualGoalRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มเป้าหมายตามจริง");
}
await this.actualGoalRepository.remove(development);
return new HttpSuccess(development.id);
}
/**
* API /tab2-4
*
* @summary DEV_00 - /tab2-4 #
*
* @param {string} id Id
*/
@Delete("tab2_4/{id}")
async DeleteDevelopmenttab2_4(@Path() id: string) {
const development = await this.actualPeopleRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้เกี่ยวข้องเป้าหมายตามจริง");
}
await this.actualPeopleRepository.remove(development);
return new HttpSuccess(development.id);
}
/**
* API / tab3
*
* @summary DEV_00 - / tab3 #
*
* @param {string} id Id
*/
@Put("tab3/{id}")
async UpdateDevelopmentTab3(
@Path() id: string,
@Body() requestBody: UpdateDevelopment3,
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentRepository.findOne({
where: { id },
relations: {
developmentProjectTypes: true,
2024-04-11 16:32:44 +07:00
developmentProjectTechniquePlanneds: true,
developmentProjectTechniqueActuals: true,
2024-04-11 11:39:56 +07:00
},
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
2024-04-11 16:32:44 +07:00
Object.assign(development, {
...requestBody,
developmentProjectTypes: [],
developmentProjectTechniquePlanneds: [],
developmentProjectTechniqueActuals: [],
});
2024-04-11 11:39:56 +07:00
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.developmentRepository.save(development);
await this.developmentProjectTypeRepository.remove(development.developmentProjectTypes);
2024-04-11 16:32:44 +07:00
await this.developmentProjectTechniquePlannedRepository.remove(
development.developmentProjectTechniquePlanneds,
);
await this.developmentProjectTechniqueActualRepository.remove(
development.developmentProjectTechniqueActuals,
2024-04-11 11:39:56 +07:00
);
if (requestBody.developmentProjectTypes != null) {
await Promise.all(
requestBody.developmentProjectTypes.map(async (x) => {
let data = new DevelopmentProjectType();
data.name = x;
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentId = development.id;
await this.developmentProjectTypeRepository.save(data);
}),
);
}
2024-04-11 16:32:44 +07:00
if (requestBody.developmentProjectTechniquePlanneds != null) {
await Promise.all(
requestBody.developmentProjectTechniquePlanneds.map(async (x) => {
let data = new DevelopmentProjectTechniquePlanned();
data.name = x;
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentId = development.id;
await this.developmentProjectTechniquePlannedRepository.save(data);
}),
);
}
if (requestBody.developmentProjectTechniqueActuals != null) {
2024-04-11 11:39:56 +07:00
await Promise.all(
2024-04-11 16:32:44 +07:00
requestBody.developmentProjectTechniqueActuals.map(async (x) => {
let data = new DevelopmentProjectTechniquePlanned();
2024-04-11 11:39:56 +07:00
data.name = x;
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentId = development.id;
2024-04-11 16:32:44 +07:00
await this.developmentProjectTechniqueActualRepository.save(data);
2024-04-11 11:39:56 +07:00
}),
);
}
return new HttpSuccess(development.id);
}
/**
* API / tab4
*
* @summary DEV_00 - / tab4 #
*
* @param {string} id Id
*/
@Put("tab4/{id}")
async UpdateDevelopmentTab4(
@Path() id: string,
@Body() requestBody: UpdateDevelopment4,
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
Object.assign(development, requestBody);
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.developmentRepository.save(development);
2024-04-11 16:32:44 +07:00
return new HttpSuccess(development.id);
}
/**
* API /tab4-1
*
* @summary DEV_00 - /tab4-1 #
*
* @param {string} id Id
*/
@Put("tab4_1_add/{id}")
async CreateDevelopmenttab4_1(
@Path() id: string,
@Body() requestBody: CreateDevelopmentEvaluation,
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
const data = Object.assign(new DevelopmentEvaluation(), requestBody);
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentId = development.id;
await this.developmentEvaluationRepository.save(data);
return new HttpSuccess(data.id);
}
/**
* API /tab4-1
*
* @summary DEV_00 - /tab4-1 #
*
* @param {string} id Id
*/
@Delete("tab4_1/{id}")
async DeleteDevelopmenttab4_1(@Path() id: string) {
const development = await this.developmentEvaluationRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้เกี่ยวข้องเป้าหมายตามแผน");
}
await this.developmentEvaluationRepository.remove(development);
return new HttpSuccess(development.id);
}
/**
* API /tab4-1
*
* @summary DEV_00 - /tab4-1 #
*
* @param {string} id Id
*/
@Put("tab4_1_edit/{id}")
async UpdateDevelopmenttab4_1(
@Path() id: string,
@Body() requestBody: CreateDevelopmentEvaluation,
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentEvaluationRepository.findOne({
where: { id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
Object.assign(development, requestBody);
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.developmentEvaluationRepository.save(development);
2024-04-02 17:53:45 +07:00
return new HttpSuccess(development.id);
}
/**
2024-04-11 11:39:56 +07:00
* API / tab5
2024-04-02 17:53:45 +07:00
*
2024-04-11 11:39:56 +07:00
* @summary DEV_00 - / tab5 #
2024-04-02 17:53:45 +07:00
*
* @param {string} id Id
*/
2024-04-11 11:39:56 +07:00
@Put("tab5/{id}")
async UpdateDevelopmentTab5(
2024-04-02 17:53:45 +07:00
@Path() id: string,
2024-04-11 11:39:56 +07:00
@Body() requestBody: UpdateDevelopment5,
2024-04-02 17:53:45 +07:00
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentRepository.findOne({
where: { id },
2024-04-11 16:32:44 +07:00
relations: { developmentAddresss: true },
});
2024-04-02 17:53:45 +07:00
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
if (requestBody.provinceActualId != null) {
const checkId = await this.provinceRepository.findOne({
where: { id: requestBody.provinceActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลจังหวัดข้อมูลด้านวิชาการ");
}
}
2024-04-11 16:32:44 +07:00
Object.assign(development, { ...requestBody, developmentAddresss: [] });
2024-04-02 17:53:45 +07:00
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.developmentRepository.save(development);
2024-04-11 16:32:44 +07:00
2024-04-11 11:39:56 +07:00
await this.developmentAddresssRepository.remove(development.developmentAddresss);
await Promise.all(
2024-04-11 11:39:56 +07:00
requestBody.developmentAddresss.map(async (x) => {
const data = Object.assign(new DevelopmentAddress(), x);
2024-04-11 16:32:44 +07:00
const chkProvince = await this.provinceRepository.findOne({
2024-04-11 11:39:56 +07:00
where: {
id: x.provinceId,
},
});
if (chkProvince == null) return;
2024-04-11 16:32:44 +07:00
data.developmentId = development.id;
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
2024-04-11 11:39:56 +07:00
await this.developmentAddresssRepository.save(data);
}),
);
2024-04-11 16:32:44 +07:00
2024-04-02 17:53:45 +07:00
return new HttpSuccess(development.id);
}
2024-04-03 14:54:56 +07:00
/**
* API
*
* @summary DEV_00 - #
*
*/
@Get("search")
async ListDevelopemt(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query() searchField?: "year" | "projectName",
@Query() searchKeyword: string = "",
) {
let queryLike = "development.projectName LIKE :keyword";
2024-04-03 14:54:56 +07:00
if (searchField == "year") {
queryLike = "development.year LIKE :keyword";
2024-04-03 14:54:56 +07:00
}
const [record, total] = await this.developmentRepository
.createQueryBuilder("development")
2024-04-03 14:54:56 +07:00
.andWhere(
searchKeyword != undefined && searchKeyword != null && searchKeyword != ""
? queryLike
: "1=1",
{
keyword: `%${searchKeyword}%`,
},
)
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
const data = await Promise.all(
record.map((_data) => {
return {
id: _data.id,
year: _data.year,
projectName: _data.projectName,
dateStart: _data.dateStart,
dateEnd: _data.dateEnd,
totalDate: _data.totalDate,
addressAcademic: _data.addressAcademic,
topicAcademic: _data.topicAcademic,
};
}),
);
return new HttpSuccess({ data: data, total });
}
2024-04-02 17:53:45 +07:00
/**
* API /
*
* @summary DEV_003 - / #3
*
2024-04-11 16:32:44 +07:00
* @param {string} id Id
2024-04-02 17:53:45 +07:00
*/
@Delete("{id}")
async DeleteDevelopment(@Path() id: string) {
const development = await this.developmentRepository.findOne({
where: { id },
relations: {
developmentActualPeoples: true,
developmentPlannedPeoples: true,
developmentActualGoals: true,
developmentPlannedGoals: true,
2024-04-11 11:39:56 +07:00
developmentProjectTypes: true,
2024-04-11 16:32:44 +07:00
developmentProjectTechniquePlanneds: true,
developmentProjectTechniqueActuals: true,
2024-04-11 11:39:56 +07:00
developmentEvaluations: true,
developmentAddresss: true,
},
});
if (!development) {
2024-04-02 17:53:45 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
if (
development.developmentPlannedGoals != null &&
development.developmentPlannedGoals.length > 0
) {
const plannedGoalPosition = await this.plannedGoalPositionRepository.find({
where: { plannedGoalId: In(development.developmentPlannedGoals.map((x) => x.id)) },
});
await this.plannedGoalPositionRepository.remove(plannedGoalPosition);
}
await this.actualPeopleRepository.remove(development.developmentActualPeoples);
await this.plannedPeopleRepository.remove(development.developmentPlannedPeoples);
await this.actualGoalRepository.remove(development.developmentActualGoals);
await this.plannedGoalRepository.remove(development.developmentPlannedGoals);
2024-04-11 11:39:56 +07:00
await this.developmentProjectTypeRepository.remove(development.developmentProjectTypes);
2024-04-11 16:32:44 +07:00
await this.developmentProjectTechniquePlannedRepository.remove(
development.developmentProjectTechniquePlanneds,
);
await this.developmentProjectTechniqueActualRepository.remove(
development.developmentProjectTechniqueActuals,
2024-04-11 11:39:56 +07:00
);
await this.developmentEvaluationRepository.remove(development.developmentEvaluations);
await this.developmentAddresssRepository.remove(development.developmentAddresss);
await this.developmentRepository.remove(development);
2024-04-02 17:53:45 +07:00
return new HttpSuccess();
}
/**
* API /
*
* @summary DEV_004 - / #4
*
*/
@Get()
async GetDevelopmentLists(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
2024-04-03 17:32:11 +07:00
@Query("year") year: number,
2024-04-11 11:39:56 +07:00
@Query("status") status: string,
@Query("root") root?: string | null,
2024-04-02 17:53:45 +07:00
@Query("keyword") keyword?: string,
) {
const [development, total] = await AppDataSource.getRepository(Development)
.createQueryBuilder("development")
2024-04-03 17:32:11 +07:00
.andWhere(year > 0 ? "development.year LIKE :year" : "1=1", {
year: `${year.toString()}`,
})
.andWhere(root != undefined && root != null ? "development.root LIKE :root" : "1=1", {
root: `${root}`,
})
2024-04-11 11:39:56 +07:00
.andWhere(status != undefined ? "development.status LIKE :status" : "1=1", {
status: `%${status}%`,
})
2024-04-03 17:32:11 +07:00
.andWhere(keyword != undefined ? "development.projectName LIKE :keyword" : "1=1", {
keyword: `%${keyword}%`,
})
.select(["development.id", "development.projectName", "development.year", "development.root"])
2024-04-02 17:53:45 +07:00
.orderBy("development.year", "DESC")
.orderBy("development.createdAt", "DESC")
2024-04-02 17:53:45 +07:00
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
return new HttpSuccess({ data: development, total });
}
/**
2024-04-11 11:39:56 +07:00
* API
*
* @summary DEV_00 - #
*
* @param {string} id Id
*/
@Get("finish/{id}")
async FinishDevelopemtById(
@Path() id: string,
@Request() request: { user: Record<string, any> },
) {
const getDevelopment = await this.developmentRepository.findOne({
where: { id: id },
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
getDevelopment.status = "FINISH";
getDevelopment.lastUpdateUserId = request.user.sub;
getDevelopment.lastUpdateFullName = request.user.name;
await this.developmentRepository.save(getDevelopment);
return new HttpSuccess(getDevelopment);
}
/**
* API
*
* @summary DEV_00 - #
*
* @param {string} id Id
*/
@Get("status/{id}")
async StatusDevelopemtById(@Path() id: string) {
const getDevelopment = await this.developmentRepository.findOne({
where: { id: id },
select: ["id", "status"],
});
return new HttpSuccess(getDevelopment);
}
/**
* API / tab1
*
* @summary DEV_00 - /tab1 #
*
* @param {string} id Id
*/
@Get("tab1/{id}")
async GetDevelopemtTab1ById(@Path() id: string) {
const getDevelopment = await this.developmentRepository.findOne({
where: { id: id },
select: [
"id",
"year",
"projectName",
"reason",
"objective",
"root",
"rootId",
"orgRootShortName",
"orgRevisionId",
],
2024-04-11 11:39:56 +07:00
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
return new HttpSuccess(getDevelopment);
}
/**
* API / tab2
*
* @summary DEV_00 - /tab2 #
*
* @param {string} id Id
*/
@Get("tab2/{id}")
async GetDevelopemtTab2ById(@Path() id: string) {
const getDevelopment = await this.developmentRepository.findOne({
where: { id: id },
relations: [
"developmentActualPeoples",
"developmentPlannedPeoples",
"developmentActualGoals",
"developmentPlannedGoals",
"developmentPlannedGoals.plannedGoalPositions",
],
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
let _getDevelopment: any = {
id: getDevelopment.id,
2024-04-11 16:32:44 +07:00
actualPeoples:
getDevelopment.developmentActualPeoples == null
? null
: getDevelopment.developmentActualPeoples.sort((a, b) =>
(a.groupTarget == null ? "" : a.groupTarget).localeCompare(
b.groupTarget == null ? "" : b.groupTarget,
),
),
plannedPeoples:
getDevelopment.developmentPlannedPeoples == null
? null
: getDevelopment.developmentPlannedPeoples.sort((a, b) =>
(a.groupTarget == null ? "" : a.groupTarget).localeCompare(
b.groupTarget == null ? "" : b.groupTarget,
),
),
actualGoals:
getDevelopment.developmentActualGoals == null
? null
: getDevelopment.developmentActualGoals.sort((a, b) =>
(a.groupTarget == null ? "" : a.groupTarget).localeCompare(
b.groupTarget == null ? "" : b.groupTarget,
),
),
plannedGoals:
getDevelopment.developmentPlannedGoals == null
? null
: getDevelopment.developmentPlannedGoals.sort((a, b) =>
(a.groupTarget == null ? "" : a.groupTarget).localeCompare(
b.groupTarget == null ? "" : b.groupTarget,
),
),
2024-04-11 11:39:56 +07:00
};
return new HttpSuccess(_getDevelopment);
}
/**
* API / tab3
*
* @summary DEV_00 - /tab3 #
*
* @param {string} id Id
*/
@Get("tab3/{id}")
async GetDevelopemtTab3ById(@Path() id: string) {
const getDevelopment = await this.developmentRepository.findOne({
where: { id: id },
relations: [
2024-04-11 16:32:44 +07:00
"developmentProjectTypes",
"developmentProjectTechniquePlanneds",
"developmentProjectTechniqueActuals",
2024-04-11 11:39:56 +07:00
],
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
2024-04-11 16:32:44 +07:00
let _getDevelopment: any = {
developmentProjectTypes: getDevelopment.developmentProjectTypes.map((x) => x.name).sort(),
projectModal: getDevelopment.projectModal,
isBackPlanned: getDevelopment.isBackPlanned,
isHoldPlanned: getDevelopment.isHoldPlanned,
projectDayBackPlanned: getDevelopment.projectDayBackPlanned,
projectDayHoldPlanned: getDevelopment.projectDayHoldPlanned,
projectNigthHoldPlanned: getDevelopment.projectNigthHoldPlanned,
developmentProjectTechniquePlanneds: getDevelopment.developmentProjectTechniquePlanneds
.map((x) => x.name)
.sort(),
isBackActual: getDevelopment.isBackActual,
isHoldActual: getDevelopment.isHoldActual,
projectDayBackActual: getDevelopment.projectDayBackActual,
projectDayHoldActual: getDevelopment.projectDayHoldActual,
projectNigthHoldActual: getDevelopment.projectNigthHoldActual,
developmentProjectTechniqueActuals: getDevelopment.developmentProjectTechniqueActuals
.map((x) => x.name)
.sort(),
};
2024-04-11 11:39:56 +07:00
return new HttpSuccess(_getDevelopment);
}
/**
* API / tab4
*
* @summary DEV_00 - /tab4 #
*
* @param {string} id Id
*/
@Get("tab4/{id}")
async GetDevelopemtTab4ById(@Path() id: string) {
const getDevelopment = await this.developmentRepository.findOne({
where: { id: id },
2024-04-11 16:32:44 +07:00
relations: ["developmentEvaluations"],
2024-04-11 11:39:56 +07:00
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
2024-04-11 16:32:44 +07:00
let _getDevelopment = {
developmentEvaluations:
getDevelopment.developmentEvaluations == null
? null
: getDevelopment.developmentEvaluations.sort((a, b) =>
(a.indicators == null ? "" : a.indicators).localeCompare(
b.indicators == null ? "" : b.indicators,
),
),
project: getDevelopment.project,
isPassAllocate: getDevelopment.isPassAllocate,
isPassNoAllocate: getDevelopment.isPassNoAllocate,
isNoPass: getDevelopment.isNoPass,
isBudget: getDevelopment.isBudget,
isOutBudget: getDevelopment.isOutBudget,
};
2024-04-11 11:39:56 +07:00
return new HttpSuccess(_getDevelopment);
}
/**
* API / tab5
*
* @summary DEV_00 - /tab5 #
*
* @param {string} id Id
*/
@Get("tab5/{id}")
async GetDevelopemtTab5ById(@Path() id: string) {
const getDevelopment = await this.developmentRepository.findOne({
where: { id: id },
2024-04-11 16:32:44 +07:00
relations: ["developmentAddresss"],
2024-04-11 11:39:56 +07:00
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
2024-04-11 16:32:44 +07:00
let _getDevelopment = {
dateStart: getDevelopment.dateStart,
dateEnd: getDevelopment.dateEnd,
totalDate: getDevelopment.totalDate,
developmentAddresss:
getDevelopment.developmentAddresss == null
? null
: getDevelopment.developmentAddresss.sort((a, b) =>
(a.address == null ? "" : a.address).localeCompare(
b.address == null ? "" : b.address,
),
),
budget: getDevelopment.budget,
budgetSub: getDevelopment.budgetSub,
accept: getDevelopment.accept,
receive: getDevelopment.receive,
approved: getDevelopment.approved,
budgetPay: getDevelopment.budgetPay,
issues: getDevelopment.issues,
chance: getDevelopment.chance,
effects: getDevelopment.effects,
riskLevel: getDevelopment.riskLevel,
riskManagement: getDevelopment.riskManagement,
expect: getDevelopment.expect,
topicAcademic: getDevelopment.topicAcademic,
addressAcademic: getDevelopment.addressAcademic,
provinceActualId: getDevelopment.provinceActualId,
};
2024-04-11 11:39:56 +07:00
return new HttpSuccess(_getDevelopment);
}
/**
* API / tab6
2024-04-02 17:53:45 +07:00
*
2024-04-11 11:39:56 +07:00
* @summary DEV_00 - /tab6 #
2024-04-02 17:53:45 +07:00
*
* @param {string} id Id
*/
2024-04-11 11:39:56 +07:00
@Get("tab6/{id}")
async GetDevelopemtTab6ById(@Path() id: string) {
2024-04-02 17:53:45 +07:00
const getDevelopment = await this.developmentRepository.findOne({
where: { id: id },
relations: [
"developmentActualPeoples",
"developmentPlannedPeoples",
"developmentActualGoals",
"developmentPlannedGoals",
"developmentPlannedGoals.plannedGoalPositions",
"provinces",
// "provinces.developmentProvinces",
],
2024-04-02 17:53:45 +07:00
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
2024-04-03 16:14:00 +07:00
let _getDevelopment: any = getDevelopment;
_getDevelopment.actualPeoples = getDevelopment.developmentActualPeoples;
_getDevelopment.plannedPeoples = getDevelopment.developmentPlannedPeoples;
_getDevelopment.actualGoals = getDevelopment.developmentActualGoals;
_getDevelopment.plannedGoals = getDevelopment.developmentPlannedGoals;
// _getDevelopment.provinces = getDevelopment.provinces.map(x=>x.developmentProvinces);
2024-04-03 16:14:00 +07:00
delete _getDevelopment.developmentActualPeoples;
delete _getDevelopment.developmentPlannedPeoples;
delete _getDevelopment.developmentActualGoals;
delete _getDevelopment.developmentPlannedGoals;
return new HttpSuccess(_getDevelopment);
2024-04-02 17:53:45 +07:00
}
2024-04-11 11:39:56 +07:00
/**
* API list
*
* @summary DEV_00 - list #
*
*/
@Get("org/root")
async GetOrgDevelopemt(@Request() request: { user: Record<string, any> }) {
const getOrg = await this.developmentRepository
.createQueryBuilder("development")
.select("development.root")
.groupBy("development.root")
.getRawMany();
if (getOrg.length > 0) {
return new HttpSuccess(getOrg.map((x) => x.development_root));
}
return new HttpSuccess(getOrg);
}
2024-04-11 11:39:56 +07:00
/**
* API upload User
*
* @summary DEV_0 - upload User #
*
* @param {string} id Id
*/
@Get("zxczxc/{id}")
async UploadUserDevelopemtById(
@Path() id: string,
@Request() request: { user: Record<string, any> },
) {
const getDevelopment = await this.developmentRepository.findOne({
where: { id: id },
relations: ["developmentHistory"],
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
await this.developmentHistoryRepository.remove(getDevelopment.developmentHistorys);
// await Promise.all(
// positions.map(async (x) => {
// const data = Object.assign(new DevelopmentHistory(), x);
// if (x.posTypeId != null) {
// const checkId = await this.posTypeRepository.findOne({
// where: { id: x.posTypeId },
// });
// if (!checkId) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
// }
// }
// if (x.posLevelId != null) {
// const checkId = await this.posLevelRepository.findOne({
// where: { id: x.posLevelId },
// });
// if (!checkId) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
// }
// }
// data.developmentId = getDevelopment.id;
// data.createdUserId = request.user.sub;
// data.createdFullName = request.user.name;
// data.lastUpdateUserId = request.user.sub;
// data.lastUpdateFullName = request.user.name;
// await this.plannedGoalPositionRepository.save(data);
// }),
// );
return new HttpSuccess();
}
2024-04-02 17:53:45 +07:00
}