migrate add column isDeleted + API ลบข้อมูลฝึกอบรม/ดูงาน + การพัฒนารายบุคคล idp + รักษาการ Task #2276, #2279, #2278
All checks were successful
Build & Deploy on Dev / build (push) Successful in 50s

This commit is contained in:
harid 2026-02-03 17:44:30 +07:00
parent bb18fed9ae
commit 30bf5ad9e3
12 changed files with 317 additions and 9 deletions

View file

@ -25,6 +25,9 @@ import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
import permission from "../interfaces/permission";
import { setLogDataDiff } from "../interfaces/utils";
import { ProfileDevelopment } from "../entities/ProfileDevelopment";
import { ProfileDevelopmentHistory } from "../entities/ProfileDevelopmentHistory";
import { In } from "typeorm";
@Route("api/v1/org/profile/training")
@Tags("ProfileTraining")
@Security("bearerAuth")
@ -32,6 +35,8 @@ export class ProfileTrainingController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
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> }) {
@ -40,7 +45,7 @@ export class ProfileTrainingController extends Controller {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.trainingRepo.find({
where: { profileId: profile.id },
where: { profileId: profile.id, isDeleted: false },
order: { createdAt: "ASC" },
});
return new HttpSuccess(record);
@ -52,7 +57,7 @@ export class ProfileTrainingController extends Controller {
if (_workflow == false)
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
const record = await this.trainingRepo.find({
where: { profileId },
where: { profileId: profileId, isDeleted: false },
order: { createdAt: "ASC" },
});
return new HttpSuccess(record);
@ -178,4 +183,77 @@ export class ProfileTrainingController extends Controller {
return new HttpSuccess();
}
/**
* API /
* @summary API /
* @param trainingId /
*/
@Patch("update-delete/{trainingId}")
public async updateIsDeletedTraining(
@Request() req: RequestWithUser,
@Path() trainingId: string,
) {
const record = await this.trainingRepo.findOneBy({ id: trainingId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
if (record.isDeleted === true) {
return new HttpSuccess();
}
await new permission().PermissionOrgUserDelete(req, "SYS_REGISTRY_OFFICER", record.profileId);
const before = structuredClone(record);
const history = new ProfileTrainingHistory();
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.profileTrainingId = trainingId;
history.createdUserId = req.user.sub;
history.createdFullName = req.user.name;
history.createdAt = now;
await Promise.all([
this.trainingRepo.save(record, { data: req }),
setLogDataDiff(req, { before, after: record }),
this.trainingHistoryRepo.save(history, { data: req }),
]);
return new HttpSuccess();
}
/**
* API / IDP
* @summary API / IDP
*/
@Post("delete-all")
public async deleteAllTraining(
@Body() reqBody: { developmentId: string },
@Request() req: RequestWithUser
) {
const trainings = await this.trainingRepo.find({
select: { id: true },
where: { developmentId: reqBody.developmentId },
});
if (trainings.length > 0) {
const trainingIds = trainings.map((x) => x.id);
await this.trainingHistoryRepo.delete({
profileTrainingId: In(trainingIds),
});
await this.trainingRepo.delete({
developmentId: reqBody.developmentId,
});
}
await this.developmentHistoryRepo.delete({
kpiDevelopmentId: reqBody.developmentId,
});
await this.developmentRepo.delete({
kpiDevelopmentId: reqBody.developmentId
});
return new HttpSuccess();
}
}