diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 2f9ecc7..7ff4ebd 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -4,6 +4,7 @@ use App\Models\User; use App\Models\UserAppreciation; +use App\Notifications\UserAppreciationNotification; use App\Notifications\WelcomeNotification; use App\Rules\CleanText; use App\Services\ChatProfanityFilter; @@ -114,15 +115,23 @@ public function showOnboarding(Request $request) $top = User::withCount('appreciationsReceived') ->orderByDesc('appreciations_received_count') - ->take(2) + ->take(1) ->get(['id', 'name', 'username', 'image_path', 'institution', 'is_verified']); - $random = User::whereNotIn('id', $top->pluck('id')) + $verified = User::where('is_verified', true) + ->whereNotIn('id', $top->pluck('id')) + ->inRandomOrder() + ->take(1) + ->get(['id', 'name', 'username', 'image_path', 'institution', 'is_verified']); + + $excludedIds = $top->pluck('id')->merge($verified->pluck('id')); + + $random = User::whereNotIn('id', $excludedIds) ->inRandomOrder() ->take(2) ->get(['id', 'name', 'username', 'image_path', 'institution', 'is_verified']); - $suggestedContributors = $top->concat($random)->values(); + $suggestedContributors = $top->concat($verified)->concat($random)->values(); return Inertia::render('auth/Onboarding', [ 'user' => $request->session()->get('onboarding_user'), @@ -200,10 +209,17 @@ public function completeOnboarding(Request $request) if (! empty($validated['appreciations'])) { foreach ($validated['appreciations'] as $targetUserId) { if ((int) $targetUserId !== (int) $user->id) { - UserAppreciation::firstOrCreate([ - 'user_id' => $targetUserId, - 'appreciator_id' => $user->id, - ]); + $targetUser = User::find($targetUserId); + + if ($targetUser) { + UserAppreciation::create([ + 'user_id' => $targetUser->id, + 'appreciator_id' => $user->id, + ]); + + $totalAppreciations = $targetUser->appreciationsReceived()->count(); + $targetUser->notify(new UserAppreciationNotification($user, $totalAppreciations)); + } } } } diff --git a/resources/js/pages/auth/Onboarding.vue b/resources/js/pages/auth/Onboarding.vue index 289b6d0..e32cc34 100644 --- a/resources/js/pages/auth/Onboarding.vue +++ b/resources/js/pages/auth/Onboarding.vue @@ -54,7 +54,7 @@ const form = useForm<{ school: '', image: null, appreciations: (props.suggestedContributors || []) - .slice(0, 2) + .filter((_, index) => index === 0 || index === 2) .map((c) => c.id), }); diff --git a/tests/Feature/AuthenticationTest.php b/tests/Feature/AuthenticationTest.php index f737096..6fd6d41 100644 --- a/tests/Feature/AuthenticationTest.php +++ b/tests/Feature/AuthenticationTest.php @@ -1,6 +1,7 @@ assertRedirect(route('profile.edit')); }); -test('onboarding passes suggested contributors to the view', function () { - $topUsers = User::factory()->count(4)->create(); - $randomUsers = User::factory()->count(2)->create(); +test('onboarding passes suggested contributors to the view according to algorithm', function () { + $topUser = User::factory()->create(['name' => 'Top Appreciator']); + $admirer = User::factory()->create(); + UserAppreciation::create([ + 'user_id' => $topUser->id, + 'appreciator_id' => $admirer->id, + ]); + + $verifiedUser = User::factory()->create(['name' => 'Verified User', 'is_verified' => true]); + $randomUsers = User::factory()->count(5)->create(['is_verified' => false]); $response = $this->withSession([ 'onboarding_user' => [ @@ -352,7 +360,9 @@ $response->assertStatus(200); $response->assertInertia(fn ($page) => $page ->component('auth/Onboarding') - ->has('suggestedContributors') + ->has('suggestedContributors', 4) + ->where('suggestedContributors.0.id', $topUser->id) + ->where('suggestedContributors.1.id', $verifiedUser->id) ); });