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

213 lines
7.4 KiB
TypeScript
Raw Normal View History

2024-05-14 10:29:05 +07:00
import {
Body,
Controller,
Delete,
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { ProfileChangeNameHistory } from "../entities/ProfileChangeNameHistory";
import { RequestWithUser } from "../middlewares/user";
import {
CreateProfileChangeNameEmployee,
ProfileChangeName,
UpdateProfileChangeName,
} from "../entities/ProfileChangeName";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import permission from "../interfaces/permission";
import { updateName } from "../keycloak";
import { setLogDataDiff } from "../interfaces/utils";
2024-05-14 10:29:05 +07:00
@Route("api/v1/org/profile-employee/changeName")
@Tags("ProfileChangeNameEmployee")
@Security("bearerAuth")
export class ProfileChangeNameEmployeeController extends Controller {
private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee);
private changeNameRepository = AppDataSource.getRepository(ProfileChangeName);
private changeNameHistoryRepository = AppDataSource.getRepository(ProfileChangeNameHistory);
@Get("user")
public async getChangeNameUser(@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 lists = await this.changeNameRepository.find({
where: { profileEmployeeId: profile.id },
order: { createdAt: "ASC" },
});
return new HttpSuccess(lists);
}
2024-05-14 10:29:05 +07:00
@Get("{profileEmployeeId}")
public async getChangeName(@Path() profileEmployeeId: string, @Request() req: RequestWithUser) {
2024-10-22 08:20:45 +07:00
let _workflow = await new permission().Workflow(req, profileEmployeeId, "SYS_REGISTRY_EMP");
if (_workflow == false)
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_EMP", profileEmployeeId);
2024-05-14 10:29:05 +07:00
const lists = await this.changeNameRepository.find({
where: { profileEmployeeId: profileEmployeeId },
order: { createdAt: "ASC" },
});
return new HttpSuccess(lists);
}
@Get("history/{changeNameId}")
public async changeNameHistory(@Path() changeNameId: string, @Request() req: RequestWithUser) {
2024-08-30 18:02:34 +07:00
const _record = await this.changeNameRepository.findOneBy({ id: changeNameId });
if (_record) {
2024-10-22 08:20:45 +07:00
let _workflow = await new permission().Workflow(req, changeNameId, "SYS_REGISTRY_EMP");
if (_workflow == false)
await new permission().PermissionOrgUserGet(
req,
"SYS_REGISTRY_EMP",
_record.profileEmployeeId,
);
2024-08-30 18:02:34 +07:00
}
2024-05-14 10:29:05 +07:00
const record = await this.changeNameHistoryRepository.find({
where: { profileChangeNameId: changeNameId },
order: { createdAt: "DESC" },
});
return new HttpSuccess(record);
}
@Post()
public async newChangeName(
@Request() req: RequestWithUser,
@Body() body: CreateProfileChangeNameEmployee,
) {
if (!body.profileEmployeeId) {
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId");
}
2024-08-30 18:02:34 +07:00
2024-05-14 10:29:05 +07:00
const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileEmployeeId });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id);
const before = null;
2024-05-14 10:29:05 +07:00
const data = new ProfileChangeName();
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-05-14 10:29:05 +07:00
};
Object.assign(data, { ...body, ...meta });
const history = new ProfileChangeNameHistory();
2024-08-13 20:01:53 +07:00
Object.assign(history, { ...data, id: undefined });
2024-05-14 10:29:05 +07:00
await this.changeNameRepository.save(data, { data: req });
setLogDataDiff(req, { before, after: data });
2024-08-13 20:01:53 +07:00
history.profileChangeNameId = data.id;
await this.changeNameHistoryRepository.save(history, { data: req });
//setLogDataDiff(req, { before, after: history });
2024-05-14 10:29:05 +07:00
profile.firstName = body.firstName ?? profile.firstName;
profile.lastName = body.lastName ?? profile.lastName;
profile.prefix = body.prefix ?? profile.prefix;
await this.profileEmployeeRepo.save(profile, { data: req });
setLogDataDiff(req, { before, after: profile });
2024-05-27 15:17:27 +07:00
if (profile != null && profile.keycloak != null) {
const result = await updateName(profile.keycloak, profile.firstName, profile.lastName);
if (!result) {
throw new Error(result.errorMessage);
}
}
2024-05-14 10:29:05 +07:00
return new HttpSuccess(data.id);
}
@Patch("{changeNameId}")
public async editChangeName(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileChangeName,
@Path() changeNameId: string,
) {
const record = await this.changeNameRepository.findOneBy({ id: changeNameId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
2024-08-30 18:02:34 +07:00
await new permission().PermissionOrgUserUpdate(
req,
"SYS_REGISTRY_EMP",
record.profileEmployeeId,
);
2024-05-14 10:29:05 +07:00
const history = new ProfileChangeNameHistory();
Object.assign(record, body);
2024-09-01 22:44:23 +07:00
Object.assign(history, { ...record, id: undefined });
2024-05-14 10:29:05 +07:00
history.profileChangeNameId = changeNameId;
record.lastUpdateUserId = req.user.sub;
2024-05-14 10:29:05 +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-05-14 10:29:05 +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-05-14 10:29:05 +07:00
await Promise.all([
this.changeNameRepository.save(record),
this.changeNameHistoryRepository.save(history),
]);
const chkLastRecord = await this.changeNameRepository.findOne({
2024-05-27 15:17:27 +07:00
where: {
profileEmployeeId: record.profileEmployeeId,
},
order: {
createdAt: "DESC",
},
2024-05-27 15:17:27 +07:00
});
if (!chkLastRecord) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
2024-05-27 15:17:27 +07:00
const profile = await this.profileEmployeeRepo.findOneBy({ id: record.profileEmployeeId });
2024-05-27 15:17:27 +07:00
if (profile && chkLastRecord.id === record.id) {
profile.firstName = body.firstName ?? profile.firstName;
profile.lastName = body.lastName ?? profile.lastName;
profile.prefix = body.prefix ?? profile.prefix;
await this.profileEmployeeRepo.save(profile);
}
2024-05-16 15:22:30 +07:00
return new HttpSuccess();
2024-05-14 10:29:05 +07:00
}
@Delete("{changeNameId}")
public async deleteTraning(@Path() changeNameId: string, @Request() req: RequestWithUser) {
2024-08-30 18:02:34 +07:00
const _record = await this.changeNameRepository.findOneBy({ id: changeNameId });
if (_record) {
await new permission().PermissionOrgUserDelete(
req,
"SYS_REGISTRY_EMP",
_record.profileEmployeeId,
);
}
2024-05-14 10:29:05 +07:00
await this.changeNameHistoryRepository.delete({
profileChangeNameId: changeNameId,
});
const result = await this.changeNameRepository.delete({ id: changeNameId });
if (result.affected == undefined || result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}