แก้api ลูกจ้างประจำ
This commit is contained in:
parent
ab138c2e04
commit
23e5d4f7fe
44 changed files with 7960 additions and 79 deletions
118
src/controllers/ProfileAvatarEmployeeTempController.ts
Normal file
118
src/controllers/ProfileAvatarEmployeeTempController.ts
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
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 ProfileAvatarEmployeeController extends Controller {
|
||||
private profileRepository = AppDataSource.getRepository(ProfileEmployee);
|
||||
private avatarRepository = AppDataSource.getRepository(ProfileAvatar);
|
||||
|
||||
@Get("{profileEmployeeId}")
|
||||
public async getAvatarEmployee(@Path() profileEmployeeId: string) {
|
||||
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) {
|
||||
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().PermissionCreate(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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue