diff --git a/app/Http/Controllers/Api/SponsorController.php b/app/Http/Controllers/Api/SponsorController.php index 5e59704..d0d90ba 100644 --- a/app/Http/Controllers/Api/SponsorController.php +++ b/app/Http/Controllers/Api/SponsorController.php @@ -18,19 +18,33 @@ public function getDashboard(Request $request) { /** @var Sponsor $sponsor */ $sponsor = $request->attributes->get('sponsor'); + $sponsorId = $sponsor ? $sponsor->id : null; try { // Recent charity/update announcements for the dashboard preview - $recentEvents = Announcement::where('status', 'approved') + $recentEvents = Announcement::where(function ($q) use ($sponsorId) { + $q->where('status', 'approved'); + if ($sponsorId) { + $q->orWhere('sponsor_id', $sponsorId); + } + }) ->latest() ->limit(5) ->get() ->map(function ($event) { + $content = $event->body; + if (strpos($event->body, '{"type":"Charity"') === 0) { + $decoded = json_decode($event->body, true); + if ($decoded) { + $content = $decoded['content'] ?? $event->body; + } + } return [ 'id' => $event->id, 'title' => $event->title, - 'body' => $event->body, + 'body' => $content, 'type' => $event->type, + 'status' => $event->status ?? 'pending', 'created_at' => $event->created_at->toIso8601String(), 'time_ago' => $event->created_at->diffForHumans(), ]; @@ -56,7 +70,12 @@ public function getDashboard(Request $request) 'joined_at' => $sponsor->created_at->toIso8601String(), ], 'recent_charity_events' => $recentEvents, - 'total_events' => Announcement::where('status', 'approved')->count(), + 'total_events' => Announcement::where(function ($q) use ($sponsorId) { + $q->where('status', 'approved'); + if ($sponsorId) { + $q->orWhere('sponsor_id', $sponsorId); + } + })->count(), 'employer_stats' => [ 'total' => \App\Models\User::where('role', 'employer')->count(), 'active' => \App\Models\User::where('role', 'employer')->where('subscription_status', 'active')->count(), @@ -91,7 +110,18 @@ public function getCharityEvents(Request $request) $perPage = (int) $request->input('per_page', 15); $type = $request->input('type'); // optional filter: 'charity', 'update', etc. - $query = Announcement::with(['employer.employerProfile', 'sponsor'])->where('status', 'approved')->latest(); + /** @var Sponsor $sponsor */ + $sponsor = $request->attributes->get('sponsor'); + $sponsorId = $sponsor ? $sponsor->id : null; + + $query = Announcement::with(['employer.employerProfile', 'sponsor']) + ->where(function ($q) use ($sponsorId) { + $q->where('status', 'approved'); + if ($sponsorId) { + $q->orWhere('sponsor_id', $sponsorId); + } + }) + ->latest(); if ($type) { $query->where('type', $type); @@ -129,6 +159,8 @@ public function getCharityEvents(Request $request) 'title' => $event->title, 'body' => $content, 'type' => $event->type, + 'status' => $event->status ?? 'pending', + 'remarks' => $event->remarks, 'posted_by' => $postedBy, 'organization' => $organization, 'created_at' => $event->created_at->toIso8601String(), diff --git a/tests/Feature/SponsorAuthApiTest.php b/tests/Feature/SponsorAuthApiTest.php index 30f791c..599f775 100644 --- a/tests/Feature/SponsorAuthApiTest.php +++ b/tests/Feature/SponsorAuthApiTest.php @@ -434,4 +434,73 @@ public function test_sponsor_dashboard_returns_stats() ] ]); } + + /** + * Test that sponsor can retrieve their own pending charity events. + */ + public function test_sponsor_can_list_own_pending_charity_event() + { + $sponsor = Sponsor::create([ + 'full_name' => 'Test Sponsor', + 'organization_name' => 'Save the Children', + 'email' => 'tsponsor@example.com', + 'mobile' => '+971509994444', + 'password' => Hash::make('password123'), + 'api_token' => 'sponsor_token_xyz', + 'status' => 'active', + ]); + + // Post a sponsor announcement (pending) + \App\Models\Announcement::create([ + 'title' => 'Sponsor Pending Event', + 'body' => 'Body of sponsor event', + 'type' => 'charity', + 'sponsor_id' => $sponsor->id, + 'status' => 'pending', + ]); + + // Post another sponsor announcement (approved) + \App\Models\Announcement::create([ + 'title' => 'Sponsor Approved Event', + 'body' => 'Body of sponsor event 2', + 'type' => 'charity', + 'sponsor_id' => $sponsor->id, + 'status' => 'approved', + ]); + + // Post another sponsor announcement (pending but from different sponsor - should NOT be returned) + $otherSponsor = Sponsor::create([ + 'full_name' => 'Other Sponsor', + 'organization_name' => 'Other Org', + 'email' => 'other@example.com', + 'mobile' => '+971509990000', + 'password' => Hash::make('password123'), + 'status' => 'active', + ]); + \App\Models\Announcement::create([ + 'title' => 'Other Sponsor Pending Event', + 'body' => 'Body of other sponsor event', + 'type' => 'charity', + 'sponsor_id' => $otherSponsor->id, + 'status' => 'pending', + ]); + + $response = $this->withHeaders([ + 'Authorization' => 'Bearer sponsor_token_xyz', + ])->getJson('/api/sponsors/charity-events'); + + $response->assertStatus(200) + ->assertJson([ + 'success' => true, + ]); + + $events = $response->json('data.events'); + $this->assertCount(2, $events); + + // Sort or check for titles + $titles = collect($events)->pluck('title'); + $this->assertTrue($titles->contains('Sponsor Pending Event')); + $this->assertTrue($titles->contains('Sponsor Approved Event')); + $this->assertFalse($titles->contains('Other Sponsor Pending Event')); + } } diff --git a/vite.config.js b/vite.config.js index fa54835..85e9101 100644 --- a/vite.config.js +++ b/vite.config.js @@ -23,7 +23,7 @@ export default defineConfig({ port: 5173, cors: true, hmr: { - host: '192.168.29.131', + host: '192.168.0.211', }, watch: { ignored: ['**/storage/framework/views/**'],