From 3ea5ec3486e8a3bee9b049ef1b45522e1fa31895 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Thu, 16 May 2024 12:13:46 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=AD=E0=B8=B1=E0=B8=9E=E0=B8=A3=E0=B8=B9?= =?UTF-8?q?=E0=B8=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileAvatarController.ts | 26 +++++--- .../ProfileAvatarEmployeeController.ts | 59 ++++++++++++++++--- src/entities/ProfileEmployee.ts | 9 ++- 3 files changed, 77 insertions(+), 17 deletions(-) diff --git a/src/controllers/ProfileAvatarController.ts b/src/controllers/ProfileAvatarController.ts index 83774c49..3f2c60bb 100644 --- a/src/controllers/ProfileAvatarController.ts +++ b/src/controllers/ProfileAvatarController.ts @@ -44,6 +44,8 @@ export class ProfileAvatarController extends Controller { result.isActive = true; profile.avatar = result.avatar; profile.avatarName = result.avatarName; + await this.avatarRepository.save(result); + await this.profileRepository.save(profile); return new HttpSuccess(); } @@ -51,7 +53,6 @@ export class ProfileAvatarController extends Controller { public async newAvatar(@Request() req: RequestWithUser, @Body() body: CreateProfileAvatar) { const profile = await this.profileRepository.findOne({ where: { id: body.profileId }, - relations: ["profileAvatars"], }); if (!profile) { @@ -69,16 +70,10 @@ export class ProfileAvatarController extends Controller { Object.assign(data, { ...body, ...meta }); - // await this.avatarRepository.save(data); + await this.avatarRepository.save(data); let avatar = `ทะเบียนประวัติ/โปรไฟล์/${profile.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.avatar = avatar; data.avatarName = fileName; @@ -87,6 +82,21 @@ export class ProfileAvatarController extends Controller { profile.avatarName = fileName; 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 }); } diff --git a/src/controllers/ProfileAvatarEmployeeController.ts b/src/controllers/ProfileAvatarEmployeeController.ts index e1a7a7d0..9159b645 100644 --- a/src/controllers/ProfileAvatarEmployeeController.ts +++ b/src/controllers/ProfileAvatarEmployeeController.ts @@ -14,20 +14,49 @@ export class ProfileAvatarEmployeeController extends Controller { private profileRepository = AppDataSource.getRepository(ProfileEmployee); private avatarRepository = AppDataSource.getRepository(ProfileAvatar); - @Get("{profileId}") - public async getAvatar(@Path() profileId: string) { + @Get("{profileEmployeeId}") + public async getAvatarEmployee(@Path() profileEmployeeId: string) { const lists = await this.avatarRepository.find({ - where: { profileEmployeeId: profileId }, + 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 newAvatar( + public async newAvatarEmployee( @Request() req: RequestWithUser, @Body() body: CreateProfileEmployeeAvatar, ) { - const profile = await this.profileRepository.findOneBy({ id: body.profileEmployeeId }); + const profile = await this.profileRepository.findOne({ + where: { id: body.profileEmployeeId }, + }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); @@ -45,17 +74,31 @@ export class ProfileAvatarEmployeeController extends Controller { Object.assign(data, { ...body, ...meta }); 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.avatarName = fileName; await this.avatarRepository.save(data); profile.avatar = avatar; + profile.avatarName = fileName; 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}") - public async deleteAvatar(@Path() avatarId: string) { + public async deleteAvatarEmployee(@Path() avatarId: string) { const result = await this.avatarRepository.delete({ id: avatarId }); if (result.affected == undefined || result.affected <= 0) { diff --git a/src/entities/ProfileEmployee.ts b/src/entities/ProfileEmployee.ts index 4421a7fb..02d888d5 100644 --- a/src/entities/ProfileEmployee.ts +++ b/src/entities/ProfileEmployee.ts @@ -38,6 +38,13 @@ export class ProfileEmployee extends EntityBase { }) avatar: string; + @Column({ + nullable: true, + comment: "รูปถ่าย", + default: null, + }) + avatarName: string; + @Column({ nullable: true, comment: "ประเภทลูกจ้าง (perm->ลูกจ้างประจำ temp->ลูกจ้างชั่วคราว)", @@ -475,4 +482,4 @@ export type UpdateProfileAddressEmployee = { currentDistrictId?: string | null; currentSubDistrictId?: string | null; currentZipCode?: string | null; -}; \ No newline at end of file +};