161 lines
6.2 KiB
PHP
161 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Announcement;
|
|
use App\Models\Sponsor;
|
|
use Illuminate\Http\Request;
|
|
|
|
class SponsorController extends Controller
|
|
{
|
|
/**
|
|
* GET /api/sponsors/dashboard
|
|
*
|
|
* Returns the sponsor's profile summary and unread charity event count.
|
|
*/
|
|
public function getDashboard(Request $request)
|
|
{
|
|
/** @var Sponsor $sponsor */
|
|
$sponsor = $request->attributes->get('sponsor');
|
|
|
|
try {
|
|
// Recent charity/update announcements for the dashboard preview
|
|
$recentEvents = Announcement::latest()
|
|
->limit(5)
|
|
->get()
|
|
->map(function ($event) {
|
|
return [
|
|
'id' => $event->id,
|
|
'title' => $event->title,
|
|
'body' => $event->body,
|
|
'type' => $event->type,
|
|
'created_at' => $event->created_at->toIso8601String(),
|
|
'time_ago' => $event->created_at->diffForHumans(),
|
|
];
|
|
});
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'sponsor' => [
|
|
'id' => $sponsor->id,
|
|
'full_name' => $sponsor->full_name,
|
|
'organization_name' => $sponsor->organization_name,
|
|
'mobile' => $sponsor->mobile,
|
|
'email' => $sponsor->email,
|
|
'city' => $sponsor->city,
|
|
'nationality' => $sponsor->nationality,
|
|
'is_verified' => $sponsor->is_verified,
|
|
'status' => $sponsor->status,
|
|
'license_file' => $sponsor->license_file ? asset($sponsor->license_file) : null,
|
|
'joined_at' => $sponsor->created_at->toIso8601String(),
|
|
],
|
|
'recent_charity_events' => $recentEvents,
|
|
'total_events' => Announcement::count(),
|
|
]
|
|
], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
logger()->error('Sponsor Dashboard API Failure: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'An error occurred while loading your dashboard.',
|
|
'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error'
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GET /api/sponsors/charity-events
|
|
*
|
|
* Returns a paginated list of all charity/announcement events.
|
|
*/
|
|
public function getCharityEvents(Request $request)
|
|
{
|
|
try {
|
|
$page = (int) $request->input('page', 1);
|
|
$perPage = (int) $request->input('per_page', 15);
|
|
$type = $request->input('type'); // optional filter: 'charity', 'update', etc.
|
|
|
|
$query = Announcement::with('employer.employerProfile')->latest();
|
|
|
|
if ($type) {
|
|
$query->where('type', $type);
|
|
}
|
|
|
|
$total = $query->count();
|
|
$offset = ($page - 1) * $perPage;
|
|
|
|
$events = $query->skip($offset)->take($perPage)->get()
|
|
->map(function ($event) {
|
|
return [
|
|
'id' => $event->id,
|
|
'title' => $event->title,
|
|
'body' => $event->body,
|
|
'type' => $event->type,
|
|
'posted_by' => $event->employer->name ?? 'System',
|
|
'organization' => optional($event->employer)->employerProfile->company_name ?? 'Migrant Support',
|
|
'created_at' => $event->created_at->toIso8601String(),
|
|
'time_ago' => $event->created_at->diffForHumans(),
|
|
];
|
|
});
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'events' => $events,
|
|
'pagination' => [
|
|
'total' => $total,
|
|
'per_page' => $perPage,
|
|
'current_page' => $page,
|
|
'last_page' => max(1, (int) ceil($total / $perPage)),
|
|
],
|
|
]
|
|
], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
logger()->error('Sponsor Charity Events API Failure: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'An error occurred while fetching charity events.',
|
|
'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error'
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GET /api/sponsors/profile
|
|
*
|
|
* Returns the authenticated sponsor's full profile.
|
|
*/
|
|
public function getProfile(Request $request)
|
|
{
|
|
/** @var Sponsor $sponsor */
|
|
$sponsor = $request->attributes->get('sponsor');
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'sponsor' => [
|
|
'id' => $sponsor->id,
|
|
'full_name' => $sponsor->full_name,
|
|
'organization_name' => $sponsor->organization_name,
|
|
'mobile' => $sponsor->mobile,
|
|
'email' => $sponsor->email,
|
|
'nationality' => $sponsor->nationality,
|
|
'city' => $sponsor->city,
|
|
'address' => $sponsor->address,
|
|
'country_code' => $sponsor->country_code,
|
|
'is_verified' => $sponsor->is_verified,
|
|
'status' => $sponsor->status,
|
|
'license_file' => $sponsor->license_file ? asset($sponsor->license_file) : null,
|
|
'joined_at' => $sponsor->created_at->toIso8601String(),
|
|
]
|
|
]
|
|
], 200);
|
|
}
|
|
}
|