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
|
|
@ -27,6 +27,7 @@ import {
|
|||
import permission from "../interfaces/permission";
|
||||
import { DevelopmentProject } from "../entities/DevelopmentProject";
|
||||
import { In, Brackets } from "typeorm";
|
||||
import { setLogDataDiff } from "../interfaces/utils";
|
||||
@Route("api/v1/org/profile-employee/development")
|
||||
@Tags("ProfileDevelopment")
|
||||
@Security("bearerAuth")
|
||||
|
|
@ -273,6 +274,44 @@ export class ProfileDevelopmentEmployeeController extends Controller {
|
|||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API ลบข้อมูลการพัฒนารายบุคคล IDP
|
||||
* @summary API ลบข้อมูลการพัฒนารายบุคคล IDP
|
||||
* @param developmentId คีย์การพัฒนารายบุคคล IDP
|
||||
*/
|
||||
@Patch("update-delete/{developmentId}")
|
||||
public async updateIsDeletedTraining(
|
||||
@Request() req: RequestWithUser,
|
||||
@Path() developmentId: string,
|
||||
) {
|
||||
const record = await this.developmentRepository.findOneBy({ id: developmentId });
|
||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
if (record.isDeleted === true) {
|
||||
return new HttpSuccess();
|
||||
}
|
||||
await new permission().PermissionOrgUserDelete(req, "SYS_REGISTRY_EMP", record.profileEmployeeId);
|
||||
const before = structuredClone(record);
|
||||
const history = new ProfileDevelopmentHistory();
|
||||
const now = new Date();
|
||||
record.isDeleted = true;
|
||||
record.lastUpdateUserId = req.user.sub;
|
||||
record.lastUpdateFullName = req.user.name;
|
||||
record.lastUpdatedAt = now;
|
||||
|
||||
Object.assign(history, { ...record, id: undefined });
|
||||
history.createdUserId = req.user.sub;
|
||||
history.createdFullName = req.user.name;
|
||||
history.createdAt = now;
|
||||
|
||||
await Promise.all([
|
||||
this.developmentRepository.save(record, { data: req }),
|
||||
setLogDataDiff(req, { before, after: record }),
|
||||
this.developmentHistoryRepository.save(history, { data: req }),
|
||||
]);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
@Delete("{developmentId}")
|
||||
public async deleteDevelopment(@Path() developmentId: string, @Request() req: RequestWithUser) {
|
||||
const _record = await this.developmentRepository.findOneBy({ id: developmentId });
|
||||
|
|
|
|||
|
|
@ -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