hrms-api-org/src/controllers/ProfileAvatarEmployeeController.ts
2024-10-22 08:20:45 +07:00

165 lines
5.7 KiB
TypeScript

import { Body, Controller, Delete, Get, 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 { RequestWithUser } from "../middlewares/user";
import { CreateProfileEmployeeAvatar, ProfileAvatar } from "../entities/ProfileAvatar";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import permission from "../interfaces/permission";
import { setLogDataDiff } from "../interfaces/utils";
@Route("api/v1/org/profile-employee/avatar")
@Tags("ProfileAvatar")
@Security("bearerAuth")
export class ProfileAvatarEmployeeController extends Controller {
private profileRepository = AppDataSource.getRepository(ProfileEmployee);
private avatarRepository = AppDataSource.getRepository(ProfileAvatar);
@Get("{profileEmployeeId}")
public async getAvatarEmployee(
@Path() profileEmployeeId: string,
@Request() req: RequestWithUser,
) {
let _workflow = await new permission().Workflow(req, profileEmployeeId, "SYS_REGISTRY_EMP");
if (_workflow == false)
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_EMP", profileEmployeeId);
const lists = await this.avatarRepository.find({
where: { profileEmployeeId },
order: { createdAt: "ASC" },
});
return new HttpSuccess(lists);
}
@Get("profileEmployeeId/{id}")
async getProfile(@Path() id: string, @Request() req: RequestWithUser) {
let _workflow = await new permission().Workflow(req, id, "SYS_REGISTRY_EMP");
if (_workflow == false)
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_EMP", id);
const profile = await this.profileRepository.findOne({
where: { id },
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess(profile);
}
@Get("profileEmployeeId-admin/{id}")
async getProfileAdmin(@Path() id: string) {
const profile = await this.profileRepository.findOne({
where: { id },
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess(profile);
}
@Get("select/{profileEmployeeId}/{id}")
public async selectAvatarEmployee(
@Path() profileEmployeeId: string,
@Path() id: string,
@Request() req: RequestWithUser,
) {
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_EMP", profileEmployeeId);
const result = await this.avatarRepository.findOneBy({ id: id });
if (!result) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
const profile = await this.profileRepository.findOne({
where: { id: profileEmployeeId },
relations: ["profileAvatars"],
});
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
await Promise.all(
profile.profileAvatars.map(async (item: any) => {
item.isActive = false;
await this.avatarRepository.save(item);
}),
);
result.isActive = true;
profile.avatar = result.avatar;
profile.avatarName = result.avatarName;
await this.avatarRepository.save(result);
await this.profileRepository.save(profile);
return new HttpSuccess();
}
@Post()
public async newAvatarEmployee(
@Request() req: RequestWithUser,
@Body() body: CreateProfileEmployeeAvatar,
) {
const profile = await this.profileRepository.findOne({
where: { id: body.profileEmployeeId },
});
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id);
const data = new ProfileAvatar();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
Object.assign(data, { ...body, ...meta });
const _profile = await this.profileRepository.findOne({
where: { id: body.profileEmployeeId },
relations: ["profileAvatars"],
});
if (!_profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
await Promise.all(
_profile.profileAvatars.map(async (item: any) => {
item.isActive = false;
await this.avatarRepository.save(item);
}),
);
await this.avatarRepository.save(data);
let avatar = `ทะเบียนประวัติ/โปรไฟล์/${profile.id}`;
let fileName = `profile-${data.id}`;
const before = null;
data.isActive = true;
data.avatar = avatar;
data.avatarName = fileName;
await this.avatarRepository.save(data, { data: req });
setLogDataDiff(req, { before, after: data });
profile.avatar = avatar;
profile.avatarName = fileName;
await this.profileRepository.save(profile, { data: req });
return new HttpSuccess({ avatar: avatar, avatarName: fileName });
}
@Delete("{avatarId}")
public async deleteAvatarEmployee(@Path() avatarId: string, @Request() req: RequestWithUser) {
const _record = await this.avatarRepository.findOneBy({ id: avatarId });
if (_record) {
await new permission().PermissionOrgUserDelete(
req,
"SYS_REGISTRY_EMP",
_record.profileEmployeeId,
);
}
if (!_record) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
await this.avatarRepository.remove(_record, { data: req });
return new HttpSuccess();
}
}