อัพรูป

This commit is contained in:
Kittapath 2024-05-16 12:13:46 +07:00
parent 6010cc662c
commit 3ea5ec3486
3 changed files with 77 additions and 17 deletions

View file

@ -44,6 +44,8 @@ export class ProfileAvatarController extends Controller {
result.isActive = true; result.isActive = true;
profile.avatar = result.avatar; profile.avatar = result.avatar;
profile.avatarName = result.avatarName; profile.avatarName = result.avatarName;
await this.avatarRepository.save(result);
await this.profileRepository.save(profile);
return new HttpSuccess(); return new HttpSuccess();
} }
@ -51,7 +53,6 @@ export class ProfileAvatarController extends Controller {
public async newAvatar(@Request() req: RequestWithUser, @Body() body: CreateProfileAvatar) { public async newAvatar(@Request() req: RequestWithUser, @Body() body: CreateProfileAvatar) {
const profile = await this.profileRepository.findOne({ const profile = await this.profileRepository.findOne({
where: { id: body.profileId }, where: { id: body.profileId },
relations: ["profileAvatars"],
}); });
if (!profile) { if (!profile) {
@ -69,16 +70,10 @@ export class ProfileAvatarController extends Controller {
Object.assign(data, { ...body, ...meta }); Object.assign(data, { ...body, ...meta });
// await this.avatarRepository.save(data); await this.avatarRepository.save(data);
let avatar = `ทะเบียนประวัติ/โปรไฟล์/${profile.id}`; let avatar = `ทะเบียนประวัติ/โปรไฟล์/${profile.id}`;
let fileName = `profile-${data.id}`; let fileName = `profile-${data.id}`;
await Promise.all(
profile.profileAvatars.map(async (item: any) => {
item.isActive = false;
await this.avatarRepository.save(item);
}),
);
data.isActive = true; data.isActive = true;
data.avatar = avatar; data.avatar = avatar;
data.avatarName = fileName; data.avatarName = fileName;
@ -87,6 +82,21 @@ export class ProfileAvatarController extends Controller {
profile.avatarName = fileName; profile.avatarName = fileName;
await this.profileRepository.save(profile); await this.profileRepository.save(profile);
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);
}),
);
return new HttpSuccess({ avatar: avatar, avatarName: fileName }); return new HttpSuccess({ avatar: avatar, avatarName: fileName });
} }

View file

@ -14,20 +14,49 @@ export class ProfileAvatarEmployeeController extends Controller {
private profileRepository = AppDataSource.getRepository(ProfileEmployee); private profileRepository = AppDataSource.getRepository(ProfileEmployee);
private avatarRepository = AppDataSource.getRepository(ProfileAvatar); private avatarRepository = AppDataSource.getRepository(ProfileAvatar);
@Get("{profileId}") @Get("{profileEmployeeId}")
public async getAvatar(@Path() profileId: string) { public async getAvatarEmployee(@Path() profileEmployeeId: string) {
const lists = await this.avatarRepository.find({ const lists = await this.avatarRepository.find({
where: { profileEmployeeId: profileId }, where: { profileEmployeeId },
}); });
return new HttpSuccess(lists); 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() @Post()
public async newAvatar( public async newAvatarEmployee(
@Request() req: RequestWithUser, @Request() req: RequestWithUser,
@Body() body: CreateProfileEmployeeAvatar, @Body() body: CreateProfileEmployeeAvatar,
) { ) {
const profile = await this.profileRepository.findOneBy({ id: body.profileEmployeeId }); const profile = await this.profileRepository.findOne({
where: { id: body.profileEmployeeId },
});
if (!profile) { if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
@ -45,17 +74,31 @@ export class ProfileAvatarEmployeeController extends Controller {
Object.assign(data, { ...body, ...meta }); Object.assign(data, { ...body, ...meta });
await this.avatarRepository.save(data); await this.avatarRepository.save(data);
let avatar = `ทะเบียนประวัติ/โปรไฟล์/${profile.id}/profile-employee-${data.id}`; let avatar = `ทะเบียนประวัติ/โปรไฟล์/${profile.id}`;
let fileName = `profile-${data.id}`;
data.isActive = true;
data.avatar = avatar; data.avatar = avatar;
data.avatarName = fileName;
await this.avatarRepository.save(data); await this.avatarRepository.save(data);
profile.avatar = avatar; profile.avatar = avatar;
profile.avatarName = fileName;
await this.profileRepository.save(profile); await this.profileRepository.save(profile);
return new HttpSuccess(avatar); const _profile = await this.profileRepository.findOne({
where: { id: body.profileEmployeeId },
relations: ["profileAvatars"],
});
if (!_profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
return new HttpSuccess({ avatar: avatar, avatarName: fileName });
} }
@Delete("{avatarId}") @Delete("{avatarId}")
public async deleteAvatar(@Path() avatarId: string) { public async deleteAvatarEmployee(@Path() avatarId: string) {
const result = await this.avatarRepository.delete({ id: avatarId }); const result = await this.avatarRepository.delete({ id: avatarId });
if (result.affected == undefined || result.affected <= 0) { if (result.affected == undefined || result.affected <= 0) {

View file

@ -38,6 +38,13 @@ export class ProfileEmployee extends EntityBase {
}) })
avatar: string; avatar: string;
@Column({
nullable: true,
comment: "รูปถ่าย",
default: null,
})
avatarName: string;
@Column({ @Column({
nullable: true, nullable: true,
comment: "ประเภทลูกจ้าง (perm->ลูกจ้างประจำ temp->ลูกจ้างชั่วคราว)", comment: "ประเภทลูกจ้าง (perm->ลูกจ้างประจำ temp->ลูกจ้างชั่วคราว)",
@ -475,4 +482,4 @@ export type UpdateProfileAddressEmployee = {
currentDistrictId?: string | null; currentDistrictId?: string | null;
currentSubDistrictId?: string | null; currentSubDistrictId?: string | null;
currentZipCode?: string | null; currentZipCode?: string | null;
}; };