API ลบข้อมูลการฝึกอบรม/ดูงาน และ การพัฒนารายบุคคล IDP รายบุคคล
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m17s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m17s
This commit is contained in:
parent
5ef5f7d4ed
commit
6b3d1dc67b
2 changed files with 121 additions and 0 deletions
|
|
@ -23,6 +23,7 @@ import HttpError from "../interfaces/http-error";
|
|||
import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory";
|
||||
import { RequestWithUser } from "../middlewares/user";
|
||||
import { Profile } from "../entities/Profile";
|
||||
import { ProfileEmployee } from "../entities/ProfileEmployee";
|
||||
import permission from "../interfaces/permission";
|
||||
import { setLogDataDiff } from "../interfaces/utils";
|
||||
import { ProfileDevelopment } from "../entities/ProfileDevelopment";
|
||||
|
|
@ -33,11 +34,13 @@ import { In } from "typeorm";
|
|||
@Security("bearerAuth")
|
||||
export class ProfileTrainingController extends Controller {
|
||||
private profileRepo = AppDataSource.getRepository(Profile);
|
||||
private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee);
|
||||
private trainingRepo = AppDataSource.getRepository(ProfileTraining);
|
||||
private trainingHistoryRepo = AppDataSource.getRepository(ProfileTrainingHistory);
|
||||
private developmentRepo = AppDataSource.getRepository(ProfileDevelopment);
|
||||
private developmentHistoryRepo = AppDataSource.getRepository(ProfileDevelopmentHistory);
|
||||
|
||||
|
||||
@Get("user")
|
||||
public async getTrainingUser(@Request() request: { user: Record<string, any> }) {
|
||||
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
|
||||
|
|
@ -256,4 +259,83 @@ export class ProfileTrainingController extends Controller {
|
|||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API ลบข้อมูลการฝึกอบรม/ดูงาน และ การพัฒนารายบุคคล IDP รายบุคคล
|
||||
* @summary API ลบข้อมูลการฝึกอบรม/ดูงาน และ การพัฒนารายบุคคล IDP รายบุคคล
|
||||
*/
|
||||
@Post("delete-byId")
|
||||
public async deleteById(
|
||||
@Body() reqBody: {
|
||||
type: string;
|
||||
profileId: string;
|
||||
developmentId: string;
|
||||
},
|
||||
@Request() req: RequestWithUser
|
||||
) {
|
||||
|
||||
const type = reqBody.type?.trim().toUpperCase();
|
||||
// 1. validate profile
|
||||
if (type === "OFFICER") {
|
||||
const profile = await this.profileRepo.findOneBy({ id: reqBody.profileId });
|
||||
if (!profile) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
||||
}
|
||||
} else {
|
||||
const profile = await this.profileEmployeeRepo.findOneBy({ id: reqBody.profileId });
|
||||
if (!profile) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
||||
}
|
||||
}
|
||||
|
||||
const profileField = type === "OFFICER" ? "profileId" : "profileEmployeeId";
|
||||
|
||||
// 2. หา ProfileTraining
|
||||
const trainings = await this.trainingRepo.find({
|
||||
select: { id: true },
|
||||
where: {
|
||||
developmentId: reqBody.developmentId,
|
||||
[profileField]: reqBody.profileId,
|
||||
},
|
||||
});
|
||||
|
||||
if (trainings.length > 0) {
|
||||
const trainingIds = trainings.map(x => x.id);
|
||||
|
||||
// 3. ลบ TrainingHistory
|
||||
await this.trainingHistoryRepo.delete({
|
||||
profileTrainingId: In(trainingIds),
|
||||
});
|
||||
|
||||
// 4. ลบ ProfileTraining
|
||||
await this.trainingRepo.delete({
|
||||
id: In(trainingIds),
|
||||
});
|
||||
}
|
||||
|
||||
// 5. หา ProfileDevelopment
|
||||
const developments = await this.developmentRepo.find({
|
||||
select: { id: true },
|
||||
where: {
|
||||
kpiDevelopmentId: reqBody.developmentId,
|
||||
[profileField]: reqBody.profileId,
|
||||
},
|
||||
});
|
||||
|
||||
if (developments.length > 0) {
|
||||
const devIds = developments.map(x => x.id);
|
||||
|
||||
// 6. ลบ DevelopmentHistory
|
||||
await this.developmentHistoryRepo.delete({
|
||||
kpiDevelopmentId: In(devIds),
|
||||
});
|
||||
|
||||
// 7. ลบ ProfileDevelopment
|
||||
await this.developmentRepo.delete({
|
||||
id: In(devIds),
|
||||
});
|
||||
}
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue