hrms-api-org/src/controllers/ProfileHonorController.ts
2024-03-18 16:02:30 +07:00

174 lines
5.5 KiB
TypeScript

import {
Body,
Controller,
Delete,
Example,
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import { CreateProfileHonor, 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 { Profile } from "../entities/Profile";
@Route("api/v1/org/profile/honor")
@Tags("ProfileHonor")
@Security("bearerAuth")
export class ProfileHonorController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
private honorRepo = AppDataSource.getRepository(ProfileHonor);
private honorHistoryRepo = AppDataSource.getRepository(ProfileHonorHistory);
@Get("{profileId}")
@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: "สาวิตรี ศรีสมัย",
profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
isActive: true,
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() profileId: string) {
const record = await this.honorRepo.findBy({ profileId });
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: "สาวิตรี ศรีสมัย",
isActive: true,
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: "สาวิตรี ศรีสมัย",
isActive: true,
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: CreateProfileHonor) {
if (!body.profileId) {
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
}
const profile = await this.profileRepo.findOneBy({ id: body.profileId });
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 && result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}