เพิ่มตำแหน่งเลือกได้หลายอัน

This commit is contained in:
Kittapath 2024-04-09 21:59:23 +07:00
parent 68c31be431
commit 166c919bbe
10 changed files with 424 additions and 115 deletions

View file

@ -14,7 +14,7 @@ import {
Example,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import { Not } from "typeorm";
import { In, Not } from "typeorm";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
@ -26,6 +26,7 @@ import { PlannedGoal } from "../entities/PlannedGoal";
import { Province } from "../entities/Province";
import { PosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
import { PlannedGoalPosition } from "../entities/PlannedGoalPosition";
@Route("api/v1/development/main")
@Tags("Development")
@ -36,6 +37,7 @@ export class DevelopmentController extends Controller {
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);
@ -68,14 +70,6 @@ export class DevelopmentController extends Controller {
);
}
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 },
@ -87,6 +81,15 @@ export class DevelopmentController extends Controller {
const development = Object.assign(new Development(), requestBody);
if (requestBody.provinceIds != null) {
const chkProvince = await this.provinceRepository.find({
where: {
id: In(requestBody.provinceIds),
},
});
development.provinces = chkProvince;
}
development.createdUserId = request.user.sub;
development.createdFullName = request.user.name;
development.lastUpdateUserId = request.user.sub;
@ -143,22 +146,6 @@ export class DevelopmentController extends Controller {
);
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;
@ -166,6 +153,34 @@ export class DevelopmentController extends Controller {
data.lastUpdateFullName = request.user.name;
data.developmentPlannedGoalId = development.id;
await this.plannedGoalRepository.save(data);
await Promise.all(
x.positions.map(async (y) => {
const _data = Object.assign(new PlannedGoalPosition(), y);
if (y.posTypePlannedId != null) {
const checkId = await this.posTypeRepository.findOne({
where: { id: y.posTypePlannedId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
}
}
if (y.posLevelPlannedId != null) {
const checkId = await this.posLevelRepository.findOne({
where: { id: y.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 = data.id;
await this.plannedGoalPositionRepository.save(_data);
}),
);
}),
);
return new HttpSuccess(development.id);
@ -196,14 +211,6 @@ export class DevelopmentController extends Controller {
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 },
@ -230,6 +237,23 @@ export class DevelopmentController extends Controller {
);
}
Object.assign(development, requestBody);
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);
}
if (requestBody.provinceIds != null) {
const chkProvince = await this.provinceRepository.find({
where: {
id: In(requestBody.provinceIds),
},
});
development.provinces = chkProvince;
}
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.developmentRepository.save(development);
@ -288,22 +312,6 @@ export class DevelopmentController extends Controller {
);
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;
@ -311,6 +319,34 @@ export class DevelopmentController extends Controller {
data.lastUpdateFullName = request.user.name;
data.developmentPlannedGoalId = development.id;
await this.plannedGoalRepository.save(data);
await Promise.all(
x.positions.map(async (y) => {
const _data = Object.assign(new PlannedGoalPosition(), y);
if (y.posTypePlannedId != null) {
const checkId = await this.posTypeRepository.findOne({
where: { id: y.posTypePlannedId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
}
}
if (y.posLevelPlannedId != null) {
const checkId = await this.posLevelRepository.findOne({
where: { id: y.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 = data.id;
await this.plannedGoalPositionRepository.save(_data);
}),
);
}),
);
return new HttpSuccess(development.id);
@ -387,6 +423,15 @@ export class DevelopmentController extends Controller {
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);
@ -437,12 +482,15 @@ export class DevelopmentController extends Controller {
async GetDevelopemtById(@Path() id: string) {
const getDevelopment = await this.developmentRepository.findOne({
where: { id: id },
relations: {
developmentActualPeoples: true,
developmentPlannedPeoples: true,
developmentActualGoals: true,
developmentPlannedGoals: true,
},
relations: [
"developmentActualPeoples",
"developmentPlannedPeoples",
"developmentActualGoals",
"developmentPlannedGoals",
"developmentPlannedGoals.plannedGoalPositions",
"provinces",
// "provinces.developmentProvinces",
],
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
@ -452,6 +500,7 @@ export class DevelopmentController extends Controller {
_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;