diff --git a/src/controllers/ProfileAvatarController.ts b/src/controllers/ProfileAvatarController.ts index 3f2c60bb..a97cb6a1 100644 --- a/src/controllers/ProfileAvatarController.ts +++ b/src/controllers/ProfileAvatarController.ts @@ -70,18 +70,6 @@ export class ProfileAvatarController extends Controller { Object.assign(data, { ...body, ...meta }); - 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); - const _profile = await this.profileRepository.findOne({ where: { id: body.profileId }, relations: ["profileAvatars"], @@ -97,6 +85,18 @@ export class ProfileAvatarController extends Controller { }), ); + 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 }); } diff --git a/src/controllers/ProfileAvatarEmployeeController.ts b/src/controllers/ProfileAvatarEmployeeController.ts index 9159b645..362902bc 100644 --- a/src/controllers/ProfileAvatarEmployeeController.ts +++ b/src/controllers/ProfileAvatarEmployeeController.ts @@ -73,6 +73,21 @@ export class ProfileAvatarEmployeeController extends Controller { 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}`; @@ -85,15 +100,6 @@ export class ProfileAvatarEmployeeController extends Controller { profile.avatarName = fileName; await this.profileRepository.save(profile); - 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 }); }