127 lines
4.4 KiB
TypeScript
127 lines
4.4 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";
|
|
@Route("api/v1/org/profile-temp/avatar")
|
|
@Tags("ProfileAvatar")
|
|
@Security("bearerAuth")
|
|
export class ProfileAvatarEmployeeTempController extends Controller {
|
|
private profileRepository = AppDataSource.getRepository(ProfileEmployee);
|
|
private avatarRepository = AppDataSource.getRepository(ProfileAvatar);
|
|
|
|
@Get("{profileEmployeeId}")
|
|
public async getAvatarEmployee(
|
|
@Path() profileEmployeeId: string,
|
|
@Request() req: RequestWithUser,
|
|
) {
|
|
await new permission().PermissionList(req, "SYS_REGISTRY_TEMP");
|
|
const lists = await this.avatarRepository.find({
|
|
where: { profileEmployeeId },
|
|
});
|
|
return new HttpSuccess(lists);
|
|
}
|
|
|
|
@Get("select/{profileEmployeeId}/{id}")
|
|
public async selectAvatarEmployee(
|
|
@Path() profileEmployeeId: string,
|
|
@Path() id: string,
|
|
@Request() req: RequestWithUser,
|
|
) {
|
|
// await new permission().PermissionGet(req, "SYS_REGISTRY_TEMP"); //ไม่แน่ใจTEMPปิดไว้ก่อน
|
|
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,
|
|
) {
|
|
await new permission().PermissionUpdate(req, "SYS_REGISTRY_TEMP");
|
|
const profile = await this.profileRepository.findOne({
|
|
where: { id: body.profileEmployeeId },
|
|
});
|
|
|
|
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.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}`;
|
|
|
|
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 deleteAvatarEmployee(@Path() avatarId: string, @Request() req: RequestWithUser) {
|
|
await new permission().PermissionDelete(req, "SYS_REGISTRY_TEMP");
|
|
const result = await this.avatarRepository.delete({ id: avatarId });
|
|
|
|
if (result.affected == undefined || result.affected <= 0) {
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
}
|
|
|
|
return new HttpSuccess();
|
|
}
|
|
}
|