hrms-api-org/src/controllers/ProfileAvatarController.ts
2024-08-13 10:25:58 +07:00

127 lines
4.3 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 { Profile } from "../entities/Profile";
import { CreateProfileAvatar, ProfileAvatar } from "../entities/ProfileAvatar";
import permission from "../interfaces/permission";
@Route("api/v1/org/profile/avatar")
@Tags("ProfileAvatar")
@Security("bearerAuth")
export class ProfileAvatarController extends Controller {
private profileRepository = AppDataSource.getRepository(Profile);
private avatarRepository = AppDataSource.getRepository(ProfileAvatar);
@Get("{profileId}")
public async getAvatar(@Path() profileId: string) {
const lists = await this.avatarRepository.find({
where: { profileId: profileId },
});
return new HttpSuccess(lists);
}
@Get("profileId/{id}")
async getProfile(@Path() id: string) {
const profile = await this.profileRepository.findOne({
select: ["id", "avatar", "avatarName"],
where: { id },
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess(profile);
}
@Get("select/{profileId}/{id}")
public async selectAvatar(@Path() profileId: string, @Path() id: string) {
const result = await this.avatarRepository.findOneBy({ id: id });
if (!result) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
const profile = await this.profileRepository.findOne({
where: { id: profileId },
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 newAvatar(@Request() req: RequestWithUser, @Body() body: CreateProfileAvatar) {
await new permission().PermissionCreate(req, "SYS_REGISTRY_OFFICER");
const profile = await this.profileRepository.findOne({
where: { id: body.profileId },
});
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const data = new ProfileAvatar();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
Object.assign(data, { ...body, ...meta });
const _profile = await this.profileRepository.findOne({
where: { id: body.profileId },
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}`;
data.isActive = true;
data.avatar = avatar;
data.avatarName = fileName;
await this.avatarRepository.save(data);
profile.avatar = avatar;
profile.avatarName = fileName;
await this.profileRepository.save(profile);
return new HttpSuccess({ avatar: avatar, avatarName: fileName });
}
@Delete("{avatarId}")
public async deleteAvatar(@Path() avatarId: string, @Request() req: RequestWithUser) {
await new permission().PermissionDelete(req, "SYS_REGISTRY_OFFICER");
const result = await this.avatarRepository.delete({ id: avatarId });
if (result.affected == undefined || result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}