sponsor event api issue

This commit is contained in:
mohanmd 2026-06-15 09:58:43 +05:30
parent 4ba8fac200
commit 95bb24ab1e
3 changed files with 106 additions and 5 deletions

View File

@ -18,19 +18,33 @@ public function getDashboard(Request $request)
{ {
/** @var Sponsor $sponsor */ /** @var Sponsor $sponsor */
$sponsor = $request->attributes->get('sponsor'); $sponsor = $request->attributes->get('sponsor');
$sponsorId = $sponsor ? $sponsor->id : null;
try { try {
// Recent charity/update announcements for the dashboard preview // 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() ->latest()
->limit(5) ->limit(5)
->get() ->get()
->map(function ($event) { ->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 [ return [
'id' => $event->id, 'id' => $event->id,
'title' => $event->title, 'title' => $event->title,
'body' => $event->body, 'body' => $content,
'type' => $event->type, 'type' => $event->type,
'status' => $event->status ?? 'pending',
'created_at' => $event->created_at->toIso8601String(), 'created_at' => $event->created_at->toIso8601String(),
'time_ago' => $event->created_at->diffForHumans(), 'time_ago' => $event->created_at->diffForHumans(),
]; ];
@ -56,7 +70,12 @@ public function getDashboard(Request $request)
'joined_at' => $sponsor->created_at->toIso8601String(), 'joined_at' => $sponsor->created_at->toIso8601String(),
], ],
'recent_charity_events' => $recentEvents, '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' => [ 'employer_stats' => [
'total' => \App\Models\User::where('role', 'employer')->count(), 'total' => \App\Models\User::where('role', 'employer')->count(),
'active' => \App\Models\User::where('role', 'employer')->where('subscription_status', 'active')->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); $perPage = (int) $request->input('per_page', 15);
$type = $request->input('type'); // optional filter: 'charity', 'update', etc. $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) { if ($type) {
$query->where('type', $type); $query->where('type', $type);
@ -129,6 +159,8 @@ public function getCharityEvents(Request $request)
'title' => $event->title, 'title' => $event->title,
'body' => $content, 'body' => $content,
'type' => $event->type, 'type' => $event->type,
'status' => $event->status ?? 'pending',
'remarks' => $event->remarks,
'posted_by' => $postedBy, 'posted_by' => $postedBy,
'organization' => $organization, 'organization' => $organization,
'created_at' => $event->created_at->toIso8601String(), 'created_at' => $event->created_at->toIso8601String(),

View File

@ -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'));
}
} }

View File

@ -23,7 +23,7 @@ export default defineConfig({
port: 5173, port: 5173,
cors: true, cors: true,
hmr: { hmr: {
host: '192.168.29.131', host: '192.168.0.211',
}, },
watch: { watch: {
ignored: ['**/storage/framework/views/**'], ignored: ['**/storage/framework/views/**'],