hrms-api-org/src/controllers/ProfileHonorEmployeeController.ts

204 lines
6.7 KiB
TypeScript

import {
Body,
Controller,
Delete,
Example,
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import { CreateProfileEmployeeHonor, ProfileHonor, UpdateProfileHonor } from "../entities/ProfileHonor";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { ProfileHonorHistory } from "../entities/ProfileHonorHistory";
import { RequestWithUser } from "../middlewares/user";
import { ProfileEmployee } from "../entities/ProfileEmployee";
@Route("api/v1/org/profile-employee/honor")
@Tags("ProfileEmployeeHonor")
@Security("bearerAuth")
export class ProfileHonorEmployeeController extends Controller {
private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee);
private honorRepo = AppDataSource.getRepository(ProfileHonor);
private honorHistoryRepo = AppDataSource.getRepository(ProfileHonorHistory);
@Get("user")
public async getHonorUser(@Request() request: { user: Record<string, any> }) {
const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.honorRepo.find({
where: { profileEmployeeId: profile.id },
});
return new HttpSuccess(record);
}
@Get("{profileEmployeeId}")
@Example({
status: 200,
message: "สำเร็จ",
result: [
{
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
createdAt: "2024-03-12T03:10:05.594Z",
createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
lastUpdatedAt: "2024-03-12T03:10:05.594Z",
lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
createdFullName: "สาวิตรี ศรีสมัย",
lastUpdateFullName: "สาวิตรี ศรีสมัย",
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
detail: "string",
issueDate: "2024-03-12T10:09:47.000Z",
issuer: "string",
refCommandDate: "2024-03-12T10:09:47.000Z",
refCommandNo: "string",
isDate: true,
},
],
})
public async getHonor(@Path() profileEmployeeId: string) {
const record = await this.honorRepo.findBy({ profileEmployeeId });
return new HttpSuccess(record);
}
/**
*
* @summary ประวัติแก้ไขเกียรติประวัติ by keycloak
*
*/
@Get("history/user")
public async honorHistoryUser(@Request() request: RequestWithUser) {
const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.honorHistoryRepo.find({
where: {
histories: {
profileEmployeeId: profile.id,
},
},
});
return new HttpSuccess(record);
}
@Get("history/{honorId}")
@Example({
status: 200,
message: "สำเร็จ",
result: [
{
id: "3bedb365-4a41-4df5-8f47-b6e143221d2c",
createdAt: "2024-03-12T03:11:01.395Z",
createdUserId: "00000000-0000-0000-0000-000000000000",
lastUpdatedAt: "2024-03-12T03:11:01.395Z",
lastUpdateUserId: "00000000-0000-0000-0000-000000000000",
createdFullName: "string",
lastUpdateFullName: "สาวิตรี ศรีสมัย",
detail: "detail",
issueDate: "2024-03-12T10:10:31.000Z",
issuer: "issuer",
refCommandDate: "2024-03-12T10:10:31.000Z",
refCommandNo: "refCommandNo",
isDate: true,
profileHonorId: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
},
{
id: "ba0e2f82-014e-46c6-8b82-a7c28eb5325f",
createdAt: "2024-03-12T03:10:05.657Z",
createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
lastUpdatedAt: "2024-03-12T03:10:05.657Z",
lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
createdFullName: "สาวิตรี ศรีสมัย",
lastUpdateFullName: "สาวิตรี ศรีสมัย",
detail: "string",
issueDate: "2024-03-12T10:09:47.000Z",
issuer: "string",
refCommandDate: "2024-03-12T10:09:47.000Z",
refCommandNo: "string",
isDate: true,
profileHonorId: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
},
],
})
public async honorHistory(@Path() honorId: string) {
const record = await this.honorHistoryRepo.findBy({
profileHonorId: honorId,
});
return new HttpSuccess(record);
}
@Post()
public async newHonor(@Request() req: RequestWithUser, @Body() body: CreateProfileEmployeeHonor) {
if (!body.profileEmployeeId) {
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId");
}
const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileEmployeeId });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const data = new ProfileHonor();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
Object.assign(data, { ...body, ...meta });
await this.honorRepo.save(data);
return new HttpSuccess();
}
@Patch("{honorId}")
public async editHonor(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileHonor,
@Path() honorId: string,
) {
const record = await this.honorRepo.findOneBy({ id: honorId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileHonorHistory();
Object.assign(history, { ...record, id: undefined });
Object.assign(record, body);
history.profileHonorId = honorId;
record.lastUpdateFullName = req.user.name;
history.lastUpdateFullName = req.user.name;
await Promise.all([this.honorRepo.save(record), this.honorHistoryRepo.save(history)]);
return new HttpSuccess();
}
@Delete("{honorId}")
public async deleteTraning(@Path() honorId: string) {
await this.honorHistoryRepo.delete({
profileHonorId: honorId,
});
const result = await this.honorRepo.delete({ id: honorId });
if (result.affected == undefined || result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}