From 0524653f4b18a39af9e27f75ecaf72084eb3b494 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Thu, 16 May 2024 10:54:13 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B8=A3=E0=B8=B9?= =?UTF-8?q?=E0=B8=9B=E0=B9=80=E0=B8=81=E0=B9=88=E0=B8=B2=20false?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileAvatarController.ts | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/controllers/ProfileAvatarController.ts b/src/controllers/ProfileAvatarController.ts index 4b35f7b6..547782d4 100644 --- a/src/controllers/ProfileAvatarController.ts +++ b/src/controllers/ProfileAvatarController.ts @@ -28,10 +28,19 @@ export class ProfileAvatarController extends Controller { if (!result) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } - const profile = await this.profileRepository.findOneBy({ id: profileId }); + 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; @@ -40,7 +49,10 @@ export class ProfileAvatarController extends Controller { @Post() public async newAvatar(@Request() req: RequestWithUser, @Body() body: CreateProfileAvatar) { - const profile = await this.profileRepository.findOneBy({ id: body.profileId }); + const profile = await this.profileRepository.findOne({ + where: { id: body.profileId }, + relations: ["profileAvatars"], + }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); @@ -60,6 +72,13 @@ export class ProfileAvatarController extends Controller { 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;