feat(organisation): add dashboard-stats endpoint
GET /organisations/{organisation}/dashboard-stats returns members,
events (with status breakdown + active count), persons, the first five
members sorted by join date, and the five most recent activity log
entries. Business logic lives in OrganisationDashboardService; access
follows OrganisationPolicy@view.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -7,14 +7,20 @@ namespace App\Http\Controllers\Api\V1;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\StoreOrganisationRequest;
|
||||
use App\Http\Requests\Api\V1\UpdateOrganisationRequest;
|
||||
use App\Http\Resources\Api\V1\OrganisationDashboardStatsResource;
|
||||
use App\Http\Resources\Api\V1\OrganisationResource;
|
||||
use App\Models\Organisation;
|
||||
use App\Services\OrganisationDashboardService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
final class OrganisationController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly OrganisationDashboardService $dashboardService,
|
||||
) {}
|
||||
|
||||
public function index(): AnonymousResourceCollection
|
||||
{
|
||||
Gate::authorize('viewAny', Organisation::class);
|
||||
@@ -54,4 +60,13 @@ final class OrganisationController extends Controller
|
||||
|
||||
return $this->success(new OrganisationResource($organisation->fresh()));
|
||||
}
|
||||
|
||||
public function dashboardStats(Organisation $organisation): JsonResponse
|
||||
{
|
||||
Gate::authorize('view', $organisation);
|
||||
|
||||
return $this->success(new OrganisationDashboardStatsResource(
|
||||
$this->dashboardService->statsFor($organisation),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources\Api\V1;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/**
|
||||
* @property-read array<string, mixed> $resource
|
||||
*/
|
||||
final class OrganisationDashboardStatsResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'members_count' => $this->resource['members_count'],
|
||||
'events_count' => $this->resource['events_count'],
|
||||
'events_by_status' => $this->resource['events_by_status'],
|
||||
'active_events_count' => $this->resource['active_events_count'],
|
||||
'persons_count' => $this->resource['persons_count'],
|
||||
'top_members' => $this->resource['top_members'],
|
||||
'recent_activity' => $this->resource['recent_activity'],
|
||||
];
|
||||
}
|
||||
}
|
||||
138
api/app/Services/OrganisationDashboardService.php
Normal file
138
api/app/Services/OrganisationDashboardService.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Event;
|
||||
use App\Models\Organisation;
|
||||
use App\Models\Person;
|
||||
use App\Models\Scopes\OrganisationScope;
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\Activitylog\Models\Activity;
|
||||
|
||||
final class OrganisationDashboardService
|
||||
{
|
||||
private const ACTIVE_STATUSES = ['published', 'registration_open', 'buildup', 'showday'];
|
||||
|
||||
private const STATUSES = [
|
||||
'draft',
|
||||
'published',
|
||||
'registration_open',
|
||||
'buildup',
|
||||
'showday',
|
||||
'teardown',
|
||||
'closed',
|
||||
];
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function statsFor(Organisation $organisation): array
|
||||
{
|
||||
return [
|
||||
'members_count' => $this->membersCount($organisation),
|
||||
'events_count' => $this->eventsCount($organisation),
|
||||
'events_by_status' => $this->eventsByStatus($organisation),
|
||||
'active_events_count' => $this->activeEventsCount($organisation),
|
||||
'persons_count' => $this->personsCount($organisation),
|
||||
'top_members' => $this->topMembers($organisation),
|
||||
'recent_activity' => $this->recentActivity($organisation),
|
||||
];
|
||||
}
|
||||
|
||||
private function membersCount(Organisation $organisation): int
|
||||
{
|
||||
return $organisation->users()->count();
|
||||
}
|
||||
|
||||
private function eventsCount(Organisation $organisation): int
|
||||
{
|
||||
return Event::withoutGlobalScope(OrganisationScope::class)
|
||||
->where('organisation_id', $organisation->id)
|
||||
->count();
|
||||
}
|
||||
|
||||
/** @return array<string, int> */
|
||||
private function eventsByStatus(Organisation $organisation): array
|
||||
{
|
||||
$counts = Event::withoutGlobalScope(OrganisationScope::class)
|
||||
->where('organisation_id', $organisation->id)
|
||||
->selectRaw('status, COUNT(*) as total')
|
||||
->groupBy('status')
|
||||
->pluck('total', 'status')
|
||||
->map(fn ($count) => (int) $count)
|
||||
->toArray();
|
||||
|
||||
$result = [];
|
||||
foreach (self::STATUSES as $status) {
|
||||
$result[$status] = $counts[$status] ?? 0;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function activeEventsCount(Organisation $organisation): int
|
||||
{
|
||||
return Event::withoutGlobalScope(OrganisationScope::class)
|
||||
->where('organisation_id', $organisation->id)
|
||||
->whereIn('status', self::ACTIVE_STATUSES)
|
||||
->count();
|
||||
}
|
||||
|
||||
private function personsCount(Organisation $organisation): int
|
||||
{
|
||||
return Person::withoutGlobalScope(OrganisationScope::class)
|
||||
->whereIn(
|
||||
'event_id',
|
||||
Event::withoutGlobalScope(OrganisationScope::class)
|
||||
->where('organisation_id', $organisation->id)
|
||||
->select('id')
|
||||
)
|
||||
->count();
|
||||
}
|
||||
|
||||
/** @return array<int, array<string, mixed>> */
|
||||
private function topMembers(Organisation $organisation): array
|
||||
{
|
||||
return $organisation->users()
|
||||
->orderBy('organisation_user.created_at', 'asc')
|
||||
->limit(5)
|
||||
->get()
|
||||
->map(fn ($user) => [
|
||||
'id' => $user->id,
|
||||
'name' => $user->full_name,
|
||||
'email' => $user->email,
|
||||
'avatar_url' => $user->avatar,
|
||||
'role' => $user->pivot?->role,
|
||||
'joined_at' => $user->pivot?->created_at?->toIso8601String(),
|
||||
])
|
||||
->all();
|
||||
}
|
||||
|
||||
/** @return array<int, array<string, mixed>> */
|
||||
private function recentActivity(Organisation $organisation): array
|
||||
{
|
||||
return Activity::query()
|
||||
->where('subject_type', Organisation::class)
|
||||
->where('subject_id', $organisation->id)
|
||||
->with('causer')
|
||||
->latest()
|
||||
->limit(5)
|
||||
->get()
|
||||
->map(function (Activity $activity): array {
|
||||
/** @var \App\Models\User|null $causer */
|
||||
$causer = $activity->causer;
|
||||
|
||||
return [
|
||||
'id' => $activity->id,
|
||||
'description' => $activity->description,
|
||||
'causer_name' => $causer?->full_name,
|
||||
'causer_avatar_url' => $causer?->avatar,
|
||||
'subject_type' => $activity->subject_type ? class_basename($activity->subject_type) : null,
|
||||
'subject_id' => $activity->subject_id,
|
||||
'properties' => $activity->attribute_changes?->toArray() ?? [],
|
||||
'created_at' => $activity->created_at?->toIso8601String(),
|
||||
];
|
||||
})
|
||||
->all();
|
||||
}
|
||||
}
|
||||
@@ -168,6 +168,7 @@ Route::middleware(['auth:sanctum', 'impersonation'])->group(function () {
|
||||
// Organisations
|
||||
Route::apiResource('organisations', OrganisationController::class)
|
||||
->only(['index', 'show', 'store', 'update']);
|
||||
Route::get('organisations/{organisation}/dashboard-stats', [OrganisationController::class, 'dashboardStats']);
|
||||
|
||||
// Events (nested under organisations)
|
||||
Route::apiResource('organisations.events', EventController::class)
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\Organisation;
|
||||
|
||||
use App\Models\Event;
|
||||
use App\Models\Organisation;
|
||||
use App\Models\Person;
|
||||
use App\Models\User;
|
||||
use Database\Seeders\RoleSeeder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Spatie\Activitylog\Models\Activity;
|
||||
use Tests\TestCase;
|
||||
|
||||
class OrganisationDashboardStatsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->seed(RoleSeeder::class);
|
||||
}
|
||||
|
||||
public function test_unauthenticated_request_returns_401(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
|
||||
$response = $this->getJson("/api/v1/organisations/{$org->id}/dashboard-stats");
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
|
||||
public function test_member_from_other_org_returns_403(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$otherOrg = Organisation::factory()->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson("/api/v1/organisations/{$otherOrg->id}/dashboard-stats");
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_authenticated_member_can_fetch_dashboard_stats(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$org = Organisation::factory()->create();
|
||||
$org->users()->attach($user, ['role' => 'org_member']);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson("/api/v1/organisations/{$org->id}/dashboard-stats");
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonStructure([
|
||||
'data' => [
|
||||
'members_count',
|
||||
'events_count',
|
||||
'events_by_status',
|
||||
'active_events_count',
|
||||
'persons_count',
|
||||
'top_members',
|
||||
'recent_activity',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_stats_return_correct_counts(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$org = Organisation::factory()->create();
|
||||
$org->users()->attach($user, ['role' => 'org_admin']);
|
||||
|
||||
// 2 extra members -> 3 total
|
||||
$org->users()->attach(User::factory()->create(), ['role' => 'org_member']);
|
||||
$org->users()->attach(User::factory()->create(), ['role' => 'org_member']);
|
||||
|
||||
// Events: 2 draft, 1 published, 1 buildup, 1 closed = 5 total, 2 active (published + buildup)
|
||||
Event::factory()->count(2)->create(['organisation_id' => $org->id, 'status' => 'draft']);
|
||||
$published = Event::factory()->create(['organisation_id' => $org->id, 'status' => 'published']);
|
||||
Event::factory()->create(['organisation_id' => $org->id, 'status' => 'buildup']);
|
||||
Event::factory()->create(['organisation_id' => $org->id, 'status' => 'closed']);
|
||||
|
||||
// 3 persons in the published event
|
||||
Person::factory()->count(3)->create(['event_id' => $published->id]);
|
||||
|
||||
// Noise: another org with its own data — should be filtered out
|
||||
$otherOrg = Organisation::factory()->create();
|
||||
$otherOrg->users()->attach(User::factory()->create(), ['role' => 'org_admin']);
|
||||
$otherEvent = Event::factory()->create(['organisation_id' => $otherOrg->id, 'status' => 'published']);
|
||||
Person::factory()->create(['event_id' => $otherEvent->id]);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson("/api/v1/organisations/{$org->id}/dashboard-stats");
|
||||
|
||||
$response->assertOk();
|
||||
$data = $response->json('data');
|
||||
|
||||
$this->assertSame(3, $data['members_count']);
|
||||
$this->assertSame(5, $data['events_count']);
|
||||
$this->assertSame(2, $data['active_events_count']);
|
||||
$this->assertSame(3, $data['persons_count']);
|
||||
$this->assertSame(2, $data['events_by_status']['draft']);
|
||||
$this->assertSame(1, $data['events_by_status']['published']);
|
||||
$this->assertSame(1, $data['events_by_status']['buildup']);
|
||||
$this->assertSame(1, $data['events_by_status']['closed']);
|
||||
$this->assertSame(0, $data['events_by_status']['registration_open']);
|
||||
}
|
||||
|
||||
public function test_top_members_limited_to_five_and_ordered_by_joined_at(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$org = Organisation::factory()->create();
|
||||
|
||||
// Attach user first with the earliest join timestamp
|
||||
$org->users()->attach($user, ['role' => 'org_admin']);
|
||||
|
||||
// Attach 6 more members (total 7)
|
||||
for ($i = 0; $i < 6; $i++) {
|
||||
$org->users()->attach(User::factory()->create(), ['role' => 'org_member']);
|
||||
}
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson("/api/v1/organisations/{$org->id}/dashboard-stats");
|
||||
|
||||
$response->assertOk();
|
||||
$members = $response->json('data.top_members');
|
||||
|
||||
$this->assertCount(5, $members);
|
||||
$this->assertSame($user->id, $members[0]['id']);
|
||||
$this->assertSame('org_admin', $members[0]['role']);
|
||||
}
|
||||
|
||||
public function test_recent_activity_limited_to_five_entries(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$org = Organisation::factory()->create();
|
||||
$org->users()->attach($user, ['role' => 'org_admin']);
|
||||
|
||||
Activity::query()->delete();
|
||||
|
||||
// Trigger 7 activity log entries on the organisation
|
||||
for ($i = 0; $i < 7; $i++) {
|
||||
$org->update(['name' => "Changed {$i}"]);
|
||||
}
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson("/api/v1/organisations/{$org->id}/dashboard-stats");
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertCount(5, $response->json('data.recent_activity'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user