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

1126 lines
44 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";
import { DevelopmentProjectTechnique } from "../entities/DevelopmentProjectTechnique";
import { DevelopmentEvaluation } from "../entities/DevelopmentEvaluation";
import { DevelopmentAddress } from "../entities/DevelopmentAddress";
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);
private developmentProjectTechniqueRepository = AppDataSource.getRepository(
DevelopmentProjectTechnique,
);
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, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
const data = Object.assign(new PlannedGoal(), requestBody);
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,
@Body() requestBody: CreateActualPeople[],
@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 #
*
* @param {string} id Id
*/
@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);
const _requestBody: any = requestBody;
delete _requestBody.positions;
Object.assign(development, requestBody);
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 #
*
* @param {string} id Id
*/
@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 #
*
* @param {string} id Id
*/
@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 #
*
* @param {string} id Id
*/
@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,
developmentProjectTechniques: true,
},
});
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);
await this.developmentProjectTypeRepository.remove(development.developmentProjectTypes);
await this.developmentProjectTechniqueRepository.remove(
development.developmentProjectTechniques,
);
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);
}),
);
}
if (requestBody.developmentProjectTechniques != null) {
await Promise.all(
requestBody.developmentProjectTechniques.map(async (x) => {
let data = new DevelopmentProjectTechnique();
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.developmentProjectTechniqueRepository.save(data);
}),
);
}
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 },
relations: {
developmentEvaluations: true,
},
});
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);
await this.developmentEvaluationRepository.remove(development.developmentEvaluations);
await Promise.all(
2024-04-11 11:39:56 +07:00
requestBody.developmentEvaluations.map(async (x) => {
const data = Object.assign(new DevelopmentEvaluation(), x);
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
data.developmentId = development.id;
await this.developmentEvaluationRepository.save(data);
}),
);
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 },
relations: {
2024-04-11 11:39:56 +07:00
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, "ไม่พบข้อมูลจังหวัดข้อมูลด้านวิชาการ");
}
}
Object.assign(development, requestBody);
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 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);
const chkProvince = await this.provinceRepository.find({
where: {
id: x.provinceId,
},
});
if (chkProvince == null) return;
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
data.developmentId = development.id;
await this.developmentAddresssRepository.save(data);
}),
);
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 - #
*
* @param {string} id Id
*/
@Get("search")
async ListDevelopemt(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query() searchField?: "year" | "projectName",
@Query() searchKeyword: string = "",
) {
let queryLike = "developer.projectName LIKE :keyword";
if (searchField == "year") {
queryLike = "developer.year LIKE :keyword";
}
const [record, total] = await this.developmentRepository
.createQueryBuilder("developer")
.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
*
* @param {string} id Id
*/
@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,
developmentProjectTechniques: true,
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);
await this.developmentProjectTechniqueRepository.remove(
development.developmentProjectTechniques,
);
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,
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()}`,
})
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"])
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"],
});
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,
actualPeoples: getDevelopment.developmentActualPeoples,
plannedPeoples: getDevelopment.developmentPlannedPeoples,
actualGoals: getDevelopment.developmentActualGoals,
plannedGoals: getDevelopment.developmentPlannedGoals,
};
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: [
"developmentActualPeoples",
"developmentPlannedPeoples",
"developmentActualGoals",
"developmentPlannedGoals",
"developmentPlannedGoals.plannedGoalPositions",
"provinces",
// "provinces.developmentProvinces",
],
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
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);
delete _getDevelopment.developmentActualPeoples;
delete _getDevelopment.developmentPlannedPeoples;
delete _getDevelopment.developmentActualGoals;
delete _getDevelopment.developmentPlannedGoals;
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 },
relations: [
"developmentActualPeoples",
"developmentPlannedPeoples",
"developmentActualGoals",
"developmentPlannedGoals",
"developmentPlannedGoals.plannedGoalPositions",
"provinces",
// "provinces.developmentProvinces",
],
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
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);
delete _getDevelopment.developmentActualPeoples;
delete _getDevelopment.developmentPlannedPeoples;
delete _getDevelopment.developmentActualGoals;
delete _getDevelopment.developmentPlannedGoals;
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 },
relations: [
"developmentActualPeoples",
"developmentPlannedPeoples",
"developmentActualGoals",
"developmentPlannedGoals",
"developmentPlannedGoals.plannedGoalPositions",
"provinces",
// "provinces.developmentProvinces",
],
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
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);
delete _getDevelopment.developmentActualPeoples;
delete _getDevelopment.developmentPlannedPeoples;
delete _getDevelopment.developmentActualGoals;
delete _getDevelopment.developmentPlannedGoals;
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 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
}