api ชื่อโครงการ/กิจกรรม/หลักสูตร

This commit is contained in:
Kittapath 2024-04-03 00:55:40 +07:00
parent 9da7f47cf6
commit c2af2a3b08
20 changed files with 1819 additions and 57 deletions

View file

@ -11,20 +11,34 @@ import {
Path,
Request,
Query,
Example
Example,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import { In, Not, MoreThan, Brackets, Like, MoreThanOrEqual, } from "typeorm";
import { Not } from "typeorm";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { Development, CreateDevelopment, UpdateDevelopment } from "../entities/Development";
import { ActualPeople } from "../entities/ActualPeople";
import { PlannedPeople } from "../entities/PlannedPeople";
import { ActualGoal } from "../entities/ActualGoal";
import { PlannedGoal } from "../entities/PlannedGoal";
import { Province } from "../entities/Province";
import { PosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
@Route("api/v1/development/main")
@Tags("Development")
@Security("bearerAuth")
export class DevelopmentController extends Controller {
private developmentRepository = AppDataSource.getRepository(Development);
private actualPeopleRepository = AppDataSource.getRepository(ActualPeople);
private plannedPeopleRepository = AppDataSource.getRepository(PlannedPeople);
private actualGoalRepository = AppDataSource.getRepository(ActualGoal);
private plannedGoalRepository = AppDataSource.getRepository(PlannedGoal);
private provinceRepository = AppDataSource.getRepository(Province);
private posTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
/**
* API /
@ -33,35 +47,127 @@ export class DevelopmentController extends Controller {
*
*/
@Post()
@Example({
name: "",
year: 2024,
})
async CreateDevelopment(
@Body() requestBody: CreateDevelopment,
@Request() request: { user: Record<string, any> },
) {
const development = Object.assign(new Development(), requestBody);
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chk_name = await this.developmentRepository.find({
where: {
name: requestBody.name,
year: requestBody.year
where: {
projectName: requestBody.projectName,
year: requestBody.year,
},
});
if (chk_name.length > 0) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"โครงการ/หลักสูตรการฝึกอบรม: " + requestBody.name + " ปีงบประมาณ: " + requestBody.year + " มีอยู่ในระบบแล้ว",
HttpStatusCode.NOT_FOUND,
"โครงการ/หลักสูตรการฝึกอบรม: " +
requestBody.projectName +
" ปีงบประมาณ: " +
requestBody.year +
" มีอยู่ในระบบแล้ว",
);
}
if (requestBody.provinceId != null) {
const checkId = await this.provinceRepository.findOne({
where: { id: requestBody.provinceId },
});
if (!checkId) {
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, "ไม่พบข้อมูลจังหวัดข้อมูลด้านวิชาการ");
}
}
const development = Object.assign(new Development(), requestBody);
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);
await Promise.all(
requestBody.actualPeoples.map(async (x) => {
const data = Object.assign(new ActualPeople(), x);
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);
}),
);
await Promise.all(
requestBody.plannedPeoples.map(async (x) => {
const data = Object.assign(new PlannedPeople(), x);
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);
}),
);
await Promise.all(
requestBody.actualGoals.map(async (x) => {
if (x.posTypeActualId != null) {
const checkId = await this.posTypeRepository.findOne({
where: { id: x.posTypeActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
}
}
if (x.posLevelActualId != null) {
const checkId = await this.posLevelRepository.findOne({
where: { id: x.posLevelActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
const data = Object.assign(new ActualGoal(), x);
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);
}),
);
await Promise.all(
requestBody.plannedGoals.map(async (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, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
const data = Object.assign(new PlannedGoal(), x);
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);
}),
);
return new HttpSuccess(development.id);
}
@ -78,27 +184,135 @@ export class DevelopmentController extends Controller {
@Body() requestBody: UpdateDevelopment,
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentRepository.findOne({ where: { id } });
const development = await this.developmentRepository.findOne({
where: { id },
relations: {
developmentActualPeoples: true,
developmentPlannedPeoples: true,
developmentActualGoals: true,
developmentPlannedGoals: true,
},
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
if (requestBody.provinceId != null) {
const checkId = await this.provinceRepository.findOne({
where: { id: requestBody.provinceId },
});
if (!checkId) {
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, "ไม่พบข้อมูลจังหวัดข้อมูลด้านวิชาการ");
}
}
const chk_name = await this.developmentRepository.find({
where: {
name: requestBody.name,
where: {
projectName: requestBody.projectName,
year: requestBody.year,
id: Not(id)
id: Not(id),
},
});
if (chk_name.length > 0) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"โครงการ/หลักสูตรการฝึกอบรม: " + requestBody.name + " ปีงบประมาณ: " + requestBody.year + " มีอยู่ในระบบแล้ว",
HttpStatusCode.NOT_FOUND,
"โครงการ/หลักสูตรการฝึกอบรม: " +
requestBody.projectName +
" ปีงบประมาณ: " +
requestBody.year +
" มีอยู่ในระบบแล้ว",
);
}
Object.assign(development, requestBody);
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
this.developmentRepository.merge(development, requestBody);
await this.developmentRepository.save(development);
await this.actualPeopleRepository.remove(development.developmentActualPeoples);
await this.plannedPeopleRepository.remove(development.developmentPlannedPeoples);
await this.actualGoalRepository.remove(development.developmentActualGoals);
await this.plannedGoalRepository.remove(development.developmentPlannedGoals);
await Promise.all(
requestBody.actualPeoples.map(async (x) => {
const data = Object.assign(new ActualPeople(), x);
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);
}),
);
await Promise.all(
requestBody.plannedPeoples.map(async (x) => {
const data = Object.assign(new PlannedPeople(), x);
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);
}),
);
await Promise.all(
requestBody.actualGoals.map(async (x) => {
if (x.posTypeActualId != null) {
const checkId = await this.posTypeRepository.findOne({
where: { id: x.posTypeActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
}
}
if (x.posLevelActualId != null) {
const checkId = await this.posLevelRepository.findOne({
where: { id: x.posLevelActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
const data = Object.assign(new ActualGoal(), x);
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);
}),
);
await Promise.all(
requestBody.plannedGoals.map(async (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, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
const data = Object.assign(new PlannedGoal(), x);
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);
}),
);
return new HttpSuccess(development.id);
}
@ -111,11 +325,24 @@ export class DevelopmentController extends Controller {
*/
@Delete("{id}")
async DeleteDevelopment(@Path() id: string) {
const delDevelopment = await this.developmentRepository.findOne({ where: { id } });
if (!delDevelopment) {
const development = await this.developmentRepository.findOne({
where: { id },
relations: {
developmentActualPeoples: true,
developmentPlannedPeoples: true,
developmentActualGoals: true,
developmentPlannedGoals: true,
},
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
await this.developmentRepository.remove(delDevelopment);
await this.actualPeopleRepository.remove(development.developmentActualPeoples);
await this.plannedPeopleRepository.remove(development.developmentPlannedPeoples);
await this.actualGoalRepository.remove(development.developmentActualGoals);
await this.plannedGoalRepository.remove(development.developmentPlannedGoals);
await this.developmentRepository.remove(development);
return new HttpSuccess();
}
@ -130,18 +357,15 @@ export class DevelopmentController extends Controller {
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
@Query("year") year: number = 2024,
@Query("year") year?: number,
) {
const [development, total] = await AppDataSource.getRepository(Development)
.createQueryBuilder("development")
.andWhere(year != 0 ? "development.year LIKE :year" : "1=1", { year: `${year}` })
.orWhere("development.name LIKE :keyword", { keyword: `${keyword}` })
.select([
"development.id",
"development.name",
"development.year",
])
.andWhere(year == null ? "development.year LIKE :year" : "1=1", { year: `${year}` })
.orWhere("development.projectName LIKE :keyword", { keyword: `${keyword}` })
.select(["development.id", "development.projectName", "development.year"])
.orderBy("development.year", "DESC")
.orderBy("development.createdAt", "DESC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
@ -159,13 +383,17 @@ export class DevelopmentController extends Controller {
@Get("{id}")
async GetDevelopemtById(@Path() id: string) {
const getDevelopment = await this.developmentRepository.findOne({
select: ["id", "name", "year"],
where: { id: id },
relations: {
developmentActualPeoples: true,
developmentPlannedPeoples: true,
developmentActualGoals: true,
developmentPlannedGoals: true,
},
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
return new HttpSuccess(getDevelopment);
}
}