diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 9599848f..7f76a400 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -107,7 +107,7 @@ public function showOnboarding(Request $request) $top = User::withCount('appreciationsReceived') ->orderByDesc('appreciations_received_count') - ->take(1) + ->take(2) ->get(['id', 'name', 'username', 'image_path', 'institution', 'is_verified']); $verified = User::where('is_verified', true) @@ -120,7 +120,7 @@ public function showOnboarding(Request $request) $random = User::whereNotIn('id', $excludedIds) ->inRandomOrder() - ->take(2) + ->take(1) ->get(['id', 'name', 'username', 'image_path', 'institution', 'is_verified']); $suggestedContributors = $top->concat($verified)->concat($random)->values(); diff --git a/resources/js/pages/auth/Onboarding.vue b/resources/js/pages/auth/Onboarding.vue index 4259d1b9..81acfc83 100644 --- a/resources/js/pages/auth/Onboarding.vue +++ b/resources/js/pages/auth/Onboarding.vue @@ -42,6 +42,17 @@ const flashError = computed(() => (page.props as any).flash?.error); const currentStep = ref<1 | 2>(1); +const getInitialAppreciations = (suggested: Contributor[] = []): number[] => { + if (suggested.length === 0) { + return []; + } + + const [first, ...rest] = suggested; + const randomRest = [...rest].sort(() => Math.random() - 0.5).slice(0, 2); + + return [first, ...randomRest].map((c) => c.id); +}; + const form = useForm<{ name: string; username: string; @@ -55,9 +66,7 @@ const form = useForm<{ school: '', image: null, receive_emails: true, - appreciations: (props.suggestedContributors || []) - .slice(0, 3) - .map((c) => c.id), + appreciations: getInitialAppreciations(props.suggestedContributors), }); const previewUrl = ref(null); diff --git a/tests/Feature/AuthenticationTest.php b/tests/Feature/AuthenticationTest.php index 952c47bb..c7272fa6 100644 --- a/tests/Feature/AuthenticationTest.php +++ b/tests/Feature/AuthenticationTest.php @@ -392,10 +392,15 @@ }); test('onboarding passes suggested contributors to the view according to algorithm', function () { - $topUser = User::factory()->create(['name' => 'Top Appreciator']); + $topUser1 = User::factory()->create(['name' => 'Top Appreciator 1']); + $topUser2 = User::factory()->create(['name' => 'Top Appreciator 2']); $admirer = User::factory()->create(); UserAppreciation::create([ - 'user_id' => $topUser->id, + 'user_id' => $topUser1->id, + 'appreciator_id' => $admirer->id, + ]); + UserAppreciation::create([ + 'user_id' => $topUser2->id, 'appreciator_id' => $admirer->id, ]); @@ -415,8 +420,9 @@ $response->assertInertia(fn ($page) => $page ->component('auth/Onboarding') ->has('suggestedContributors', 4) - ->where('suggestedContributors.0.id', $topUser->id) - ->where('suggestedContributors.1.id', $verifiedUser->id) + ->where('suggestedContributors.0.id', $topUser1->id) + ->where('suggestedContributors.1.id', $topUser2->id) + ->where('suggestedContributors.2.id', $verifiedUser->id) ); });