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

187 lines
6.1 KiB
TypeScript
Raw Normal View History

2024-03-12 16:48:33 +07:00
import {
Body,
Controller,
Delete,
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";
2024-03-12 17:17:16 +07:00
import { Profile } from "../entities/Profile";
import permission from "../interfaces/permission";
2024-03-12 16:48:33 +07:00
@Route("api/v1/org/profile/honor")
@Tags("ProfileHonor")
@Security("bearerAuth")
export class ProfileHonorController extends Controller {
2024-03-12 17:17:16 +07:00
private profileRepo = AppDataSource.getRepository(Profile);
2024-03-12 16:48:33 +07:00
private honorRepo = AppDataSource.getRepository(ProfileHonor);
private honorHistoryRepo = AppDataSource.getRepository(ProfileHonorHistory);
2024-05-23 16:44:37 +07:00
@Get("user")
public async getHonorUser(@Request() request: { user: Record<string, any> }) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.honorRepo.find({
where: { profileId: profile.id },
2024-08-30 18:02:34 +07:00
order: { createdAt: "ASC" },
2024-05-23 16:44:37 +07:00
});
return new HttpSuccess(record);
}
2024-03-12 16:48:33 +07:00
@Get("{profileId}")
public async getHonor(@Path() profileId: string, @Request() req: RequestWithUser) {
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
2024-08-30 18:02:34 +07:00
const record = await this.honorRepo.find({
where: { profileId: profileId },
order: { createdAt: "ASC" },
});
2024-03-12 16:48:33 +07:00
return new HttpSuccess(record);
}
2024-05-28 10:20:50 +07:00
/**
*
* @summary by keycloak
*
*/
@Get("history/user")
public async honorHistoryUser(@Request() request: RequestWithUser) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.honorHistoryRepo.find({
where: {
histories: {
profileId: profile.id,
},
},
2024-08-30 18:02:34 +07:00
order: { createdAt: "DESC" },
2024-05-28 10:20:50 +07:00
});
return new HttpSuccess(record);
}
@Get("admin/history/{honorId}")
public async honorAdminHistory(@Path() honorId: string, @Request() req: RequestWithUser) {
2024-08-30 18:02:34 +07:00
const _record = await this.honorRepo.findOneBy({ id: honorId });
if (_record) {
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", _record.profileId);
}
const record = await this.honorHistoryRepo.find({
where: {
profileHonorId: honorId,
},
order: { createdAt: "DESC" },
});
return new HttpSuccess(record);
}
@Get("history/{honorId}")
public async honorHistory(@Path() honorId: string, @Request() req: RequestWithUser) {
2024-08-30 18:02:34 +07:00
const record = await this.honorHistoryRepo.find({
where: {
profileHonorId: honorId,
},
order: { createdAt: "DESC" },
2024-03-12 16:48:33 +07:00
});
2024-03-12 17:01:02 +07:00
return new HttpSuccess(record);
2024-03-12 16:48:33 +07:00
}
@Post()
public async newHonor(@Request() req: RequestWithUser, @Body() body: CreateProfileHonor) {
2024-03-12 17:17:16 +07:00
if (!body.profileId) {
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
}
2024-08-30 18:02:34 +07:00
2024-03-12 17:17:16 +07:00
const profile = await this.profileRepo.findOneBy({ id: body.profileId });
2024-08-30 18:02:34 +07:00
2024-03-12 17:17:16 +07:00
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id);
2024-03-12 17:17:16 +07:00
2024-03-12 16:48:33 +07:00
const data = new ProfileHonor();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
2024-08-30 18:02:34 +07:00
createdAt: new Date(),
lastUpdatedAt: new Date(),
2024-03-12 16:48:33 +07:00
};
Object.assign(data, { ...body, ...meta });
const history = new ProfileHonorHistory();
2024-08-13 20:01:53 +07:00
Object.assign(history, { ...data, id: undefined });
2024-03-12 16:48:33 +07:00
2024-08-13 20:01:53 +07:00
await this.honorRepo.save(data);
history.profileHonorId = data.id;
await this.honorHistoryRepo.save(history);
2024-03-12 16:48:33 +07:00
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, "ไม่พบข้อมูล");
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId);
2024-03-12 16:48:33 +07:00
const history = new ProfileHonorHistory();
Object.assign(record, body);
2024-09-01 22:44:23 +07:00
Object.assign(history, { ...record, id: undefined });
2024-03-12 16:48:33 +07:00
history.profileHonorId = honorId;
record.lastUpdateUserId = req.user.sub;
2024-03-12 16:48:33 +07:00
record.lastUpdateFullName = req.user.name;
2024-08-30 18:02:34 +07:00
record.lastUpdatedAt = new Date();
history.lastUpdateUserId = req.user.sub;
2024-03-12 16:48:33 +07:00
history.lastUpdateFullName = req.user.name;
history.createdUserId = req.user.sub;
history.createdFullName = req.user.name;
2024-08-30 18:02:34 +07:00
history.createdAt = new Date();
history.lastUpdatedAt = new Date();
2024-03-12 16:48:33 +07:00
await Promise.all([this.honorRepo.save(record), this.honorHistoryRepo.save(history)]);
return new HttpSuccess();
}
@Delete("{honorId}")
public async deleteTraning(@Path() honorId: string, @Request() req: RequestWithUser) {
const _record = await this.honorRepo.findOneBy({ id: honorId });
if (_record) {
await new permission().PermissionOrgUserDelete(req, "SYS_REGISTRY_OFFICER", _record.id);
}
2024-03-12 16:48:33 +07:00
await this.honorHistoryRepo.delete({
profileHonorId: honorId,
});
const result = await this.honorRepo.delete({ id: honorId });
if (result.affected == undefined || result.affected <= 0) {
2024-03-12 16:48:33 +07:00
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}