diff --git a/app/Services/Account/ProfileService.php b/app/Services/Account/ProfileService.php index 0fc395d..c437692 100644 --- a/app/Services/Account/ProfileService.php +++ b/app/Services/Account/ProfileService.php @@ -3,27 +3,29 @@ namespace App\Services\Account; use App\Models\User; +use App\Services\Concerns\RunsInTransaction; +use App\Services\Concerns\SyncsPhotos; use App\Services\Media\MediaService; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; -use Illuminate\Support\Facades\Log; -use Illuminate\Validation\ValidationException; class ProfileService { + use RunsInTransaction, SyncsPhotos; + public function __construct( private readonly MediaService $mediaService, ) {} public function update(array $validated, User $user): void { - try { - DB::transaction(function () use ($user, $validated): void { + $this->runInTransaction( + function () use ($user, $validated): void { $user->update([ 'email' => $validated['email'], 'username' => $validated['username'], ]); + /** @var \App\Models\UserProfile $profile */ $profile = $user->profile()->updateOrCreate( ['user_id' => $user->id], [ @@ -35,29 +37,20 @@ public function update(array $validated, User $user): void ], ); - $s3Keys = ! empty($validated['profile_s3_key']) ? [$validated['profile_s3_key']] : null; - $removeIds = $validated['remove_profile_photo_ids'] ?? null; - - $this->mediaService->syncCollection( + $this->syncPhotos( $profile, - 'profile_photo', - null, - $removeIds, - 1, - s3Keys: $s3Keys, + [ + 'photos' => $validated['profile_photo'] ?? null, + 'remove_media_ids' => $validated['remove_profile_photo_ids'] ?? null, + 's3_keys' => ! empty($validated['profile_s3_key']) ? [$validated['profile_s3_key']] : null, + ], + maxPhotos: 1, + required: false, + collection: 'profile_photo', ); - }); - } catch (ValidationException $e) { - throw $e; - } catch (\Throwable $e) { - Log::error('Gagal memperbarui profil: '.$e->getMessage(), [ - 'trace' => $e->getTraceAsString(), - ]); - - throw ValidationException::withMessages([ - 'system' => 'Terjadi kesalahan pada server. Silakan laporkan masalah ini ke pihak terkait.', - ]); - } + }, + 'Gagal memperbarui profil', + ); } public function updatePassword(User $user, string $password): void diff --git a/resources/js/pages/admin/account/Profile.vue b/resources/js/pages/admin/account/Profile.vue index b72ef85..1f3043e 100644 --- a/resources/js/pages/admin/account/Profile.vue +++ b/resources/js/pages/admin/account/Profile.vue @@ -1,7 +1,7 @@