2024-03-12 16:48:54 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Patch,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Request,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
import { AppDataSource } from "../database/data-source";
|
|
|
|
|
import {
|
|
|
|
|
CreateProfileTraining,
|
|
|
|
|
ProfileTraining,
|
|
|
|
|
UpdateProfileTraining,
|
|
|
|
|
} from "../entities/ProfileTraining";
|
|
|
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
|
|
|
import HttpStatus from "../interfaces/http-status";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory";
|
|
|
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
|
|
|
|
|
|
|
|
@Route("api/v1/org/profile/training")
|
|
|
|
|
@Tags("ProfileTraining")
|
|
|
|
|
@Security("bearerAuth")
|
|
|
|
|
export class ProfileTrainingController extends Controller {
|
|
|
|
|
private trainingRepo = AppDataSource.getRepository(ProfileTraining);
|
|
|
|
|
private trainingHistoryRepo = AppDataSource.getRepository(ProfileTrainingHistory);
|
|
|
|
|
|
|
|
|
|
@Get("{profileId}")
|
|
|
|
|
public async getTraining(@Path() profileId: string) {
|
|
|
|
|
const record = await this.trainingRepo.findBy({ profileId });
|
|
|
|
|
return new HttpSuccess(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("history/{trainingId}")
|
|
|
|
|
public async trainingHistory(@Path() trainingId: string) {
|
|
|
|
|
const record = await this.trainingHistoryRepo.findBy({
|
|
|
|
|
profileTrainingId: trainingId,
|
|
|
|
|
});
|
2024-03-12 17:01:02 +07:00
|
|
|
return new HttpSuccess(record);
|
2024-03-12 16:48:54 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post()
|
|
|
|
|
public async newTraining(@Request() req: RequestWithUser, @Body() body: CreateProfileTraining) {
|
|
|
|
|
const data = new ProfileTraining();
|
|
|
|
|
const history = new ProfileTrainingHistory();
|
|
|
|
|
|
|
|
|
|
const meta = {
|
|
|
|
|
createdUserId: req.user.sub,
|
|
|
|
|
createdFullName: req.user.name,
|
|
|
|
|
lastUpdateUserId: req.user.sub,
|
|
|
|
|
lastUpdateFullName: req.user.name,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Object.assign(data, { ...body, ...meta });
|
|
|
|
|
Object.assign(history, { ...body, ...meta });
|
|
|
|
|
|
|
|
|
|
const result = await this.trainingRepo.save(data);
|
|
|
|
|
|
|
|
|
|
history.profileTrainingId = result.id;
|
|
|
|
|
|
|
|
|
|
await this.trainingHistoryRepo.save(history);
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch("{trainingId}")
|
|
|
|
|
public async editTraining(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Body() body: UpdateProfileTraining,
|
|
|
|
|
@Path() trainingId: string,
|
|
|
|
|
) {
|
|
|
|
|
const record = await this.trainingRepo.findOneBy({ id: trainingId });
|
|
|
|
|
|
|
|
|
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
|
|
|
|
|
const history = new ProfileTrainingHistory();
|
|
|
|
|
|
|
|
|
|
Object.assign(record, body);
|
|
|
|
|
Object.assign(history, { ...body, id: undefined });
|
|
|
|
|
history.profileTrainingId = trainingId;
|
|
|
|
|
record.lastUpdateFullName = req.user.name;
|
|
|
|
|
history.lastUpdateFullName = req.user.name;
|
|
|
|
|
|
|
|
|
|
await Promise.all([this.trainingRepo.save(record), this.trainingHistoryRepo.save(history)]);
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("{trainingId}")
|
|
|
|
|
public async deleteTraining(@Path() trainingId: string) {
|
|
|
|
|
await this.trainingHistoryRepo.delete({
|
|
|
|
|
profileTrainingId: trainingId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await this.trainingRepo.delete({ id: trainingId });
|
|
|
|
|
|
|
|
|
|
if (result.affected && result.affected <= 0) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
}
|