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

213 lines
7.4 KiB
TypeScript
Raw Normal View History

2024-03-12 16:48:15 +07:00
import {
Body,
Controller,
Delete,
2024-03-12 17:11:42 +07:00
Example,
2024-03-12 16:48:15 +07:00
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import {
CreateProfileCertificate,
ProfileCertificate,
UpdateProfileCertificate,
} from "../entities/ProfileCertificate";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory";
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:15 +07:00
@Route("api/v1/org/profile/certificate")
@Tags("ProfileCertificate")
@Security("bearerAuth")
export class ProfileCertificateController extends Controller {
2024-03-12 17:17:16 +07:00
private profileRepo = AppDataSource.getRepository(Profile);
2024-03-12 16:48:15 +07:00
private certificateRepo = AppDataSource.getRepository(ProfileCertificate);
private certificateHistoryRepo = AppDataSource.getRepository(ProfileCertificateHistory);
@Get("user")
2024-05-23 16:44:37 +07:00
public async getCertificateUser(@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.certificateRepo.find({
where: { profileId: profile.id },
});
return new HttpSuccess(record);
}
2024-03-12 16:48:15 +07:00
@Get("{profileId}")
2024-03-12 17:11:42 +07:00
@Example({
status: 200,
message: "สำเร็จ",
result: [
{
id: "e1ef9c3d-079a-40d8-8332-664c3e9a5a70",
createdAt: "2024-03-12T03:02:27.532Z",
createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
lastUpdatedAt: "2024-03-12T03:02:27.532Z",
lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
createdFullName: "สาวิตรี ศรีสมัย",
lastUpdateFullName: "สาวิตรี ศรีสมัย",
profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
expireDate: "2024-03-12T10:01:48.000Z",
issueDate: "2024-03-12T10:01:48.000Z",
certificateNo: "string",
certificateType: "string",
issuer: "string",
},
],
})
public async getCertificate(@Path() profileId: string, @Request() req: RequestWithUser) {
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
2024-03-12 16:48:15 +07:00
const record = await this.certificateRepo.findBy({ profileId });
return new HttpSuccess(record);
}
@Get("history/{certificateId}")
2024-03-12 17:11:42 +07:00
@Example({
status: 200,
message: "สำเร็จ",
result: [
{
id: "c0ecf986-b290-44ca-b45e-f4448cdd34dd",
createdAt: "2024-03-12T03:03:30.169Z",
createdUserId: "00000000-0000-0000-0000-000000000000",
lastUpdatedAt: "2024-03-12T03:03:30.169Z",
lastUpdateUserId: "00000000-0000-0000-0000-000000000000",
createdFullName: "string",
lastUpdateFullName: "สาวิตรี ศรีสมัย",
expireDate: "2024-03-12T10:03:05.000Z",
issueDate: "2024-03-12T10:03:05.000Z",
certificateNo: "no",
certificateType: "type",
issuer: "issuer",
profileCertificateId: "e1ef9c3d-079a-40d8-8332-664c3e9a5a70",
},
{
id: "dc4c2800-5fc5-4ec3-b19a-c4a27beac35f",
createdAt: "2024-03-12T03:02:27.583Z",
createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
lastUpdatedAt: "2024-03-12T03:02:27.583Z",
lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
createdFullName: "สาวิตรี ศรีสมัย",
lastUpdateFullName: "สาวิตรี ศรีสมัย",
expireDate: "2024-03-12T10:01:48.000Z",
issueDate: "2024-03-12T10:01:48.000Z",
certificateNo: "string",
certificateType: "string",
issuer: "string",
profileCertificateId: "e1ef9c3d-079a-40d8-8332-664c3e9a5a70",
},
],
})
public async certificateHistory(@Path() certificateId: string, @Request() req: RequestWithUser) {
const _record = await this.certificateRepo.findOneBy({ id: certificateId });
if (_record) {
await new permission().PermissionOrgUserList(req, "SYS_REGISTRY_OFFICER", _record.profileId);
}
2024-03-12 16:48:15 +07:00
const record = await this.certificateHistoryRepo.findBy({
profileCertificateId: certificateId,
});
2024-03-12 17:01:02 +07:00
return new HttpSuccess(record);
2024-03-12 16:48:15 +07:00
}
@Post()
public async newCertificate(
@Request() req: RequestWithUser,
@Body() body: CreateProfileCertificate,
) {
2024-03-12 17:17:16 +07:00
if (!body.profileId) {
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
}
2024-03-12 17:17:16 +07:00
const profile = await this.profileRepo.findOneBy({ id: body.profileId });
2024-03-12 17:17:16 +07:00
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
await new permission().PermissionOrgUserCreate(req, "SYS_REGISTRY_OFFICER", profile.id);
2024-03-12 17:17:16 +07:00
2024-03-12 16:48:15 +07:00
const data = new ProfileCertificate();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
Object.assign(data, { ...body, ...meta });
const history = new ProfileCertificateHistory();
2024-08-13 20:01:53 +07:00
Object.assign(history, { ...data, id: undefined });
2024-03-12 16:48:15 +07:00
2024-08-13 20:01:53 +07:00
await this.certificateRepo.save(data);
history.profileCertificateId = data.id;
await this.certificateHistoryRepo.save(history);
2024-03-12 16:48:15 +07:00
return new HttpSuccess();
}
@Patch("{certificateId}")
public async editCertificate(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileCertificate,
@Path() certificateId: string,
) {
const record = await this.certificateRepo.findOneBy({ id: certificateId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId);
2024-03-12 16:48:15 +07:00
const history = new ProfileCertificateHistory();
Object.assign(record, body);
Object.assign(history, body);
2024-03-12 16:48:15 +07:00
history.profileCertificateId = certificateId;
record.lastUpdateUserId = req.user.sub;
2024-03-12 16:48:15 +07:00
record.lastUpdateFullName = req.user.name;
history.lastUpdateUserId = req.user.sub;
2024-03-12 16:48:15 +07:00
history.lastUpdateFullName = req.user.name;
history.createdUserId = req.user.sub;
history.createdFullName = req.user.name;
2024-03-12 16:48:15 +07:00
await Promise.all([
this.certificateRepo.save(record),
this.certificateHistoryRepo.save(history),
]);
return new HttpSuccess();
}
@Delete("{certificateId}")
public async deleteCertificate(@Path() certificateId: string, @Request() req: RequestWithUser) {
const _record = await this.certificateRepo.findOneBy({ id: certificateId });
if (_record) {
await new permission().PermissionOrgUserDelete(req, "SYS_REGISTRY_OFFICER", _record.profileId);
}
2024-03-12 16:48:15 +07:00
await this.certificateHistoryRepo.delete({
profileCertificateId: certificateId,
});
const certificateResult = await this.certificateRepo.delete({
id: certificateId,
});
if (certificateResult.affected && certificateResult.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}