Files
crewli/api/app/Services/OrganisationDashboardService.php
bert.hausmans 85815ccb16 feat(forms): add Eloquent models, observer, events, activity-log helpers
Phase 4 of S1.

Models (app/Models/FormBuilder/): FormSchema, FormSchemaSection, FormField,
FormSubmission, FormValue, FormValueOption, FormTemplate, FormFieldLibrary,
FormSchemaWebhook, FormWebhookDelivery, FormSubmissionSectionStatus,
FormSubmissionDelegation. Plus UserProfile at app/Models/ (user-universal).

OrganisationScope applied on: FormSchema, FormTemplate, FormFieldLibrary.
FormSchemaWebhook documents inherited-scope discipline (OrganisationScope's
strategies — organisation_id/event_id/festival_section_id — don't cover
form_schema_id; direct queries would leak across orgs, so must go via
$schema->webhooks()).

User::profile()/getOrCreateProfile(), Event::formSchemas() (morphMany),
Person::formSubmissions() (morphMany).

Morph map enforced in AppServiceProvider with 28 keys covering every model
that appears as activitylog subject/causer. Also updated
OrganisationDashboardService (and its test) to query activitylog via
getMorphClass() instead of FQCN.

Activity log strategy: nuanced explicit calls (logSchemaChange on FormSchema,
logFieldChange on FormField) — no LogsActivity trait. Suppression for bulk
fixtures via App\Support\ActivityLog::suppressed(fn() => ...) which flips
config('activitylog.enabled') around a callback. Both our explicit calls
and spatie's trait on Organisation respect the flag via ActivityLogger::log().

FormValueObserver (app/Observers/FormBuilder/) populates value_indexed/
value_number/value_date/value_bool on save per field.value_storage_hint,
rebuilds form_value_options pivot on multi-value filterable fields, cleans
up on delete. Memoised field cache avoids N+1. Registered in AppServiceProvider.

9 lightweight event classes (app/Events/FormBuilder/) as SerializesModels
containers — submission lifecycle signatures lock in for S2 services, no
listeners yet.

Factories for all models with Dutch fake data (fake('nl_NL')). FormSchema
factory uses defaultSubmissionMode(); FormField factory uses
recommendedValueStorageHint().

Tests: 9 new observer tests (all pass); full suite 910/910 (up from 901).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 12:35:41 +02:00

139 lines
4.6 KiB
PHP

<?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', (new Organisation)->getMorphClass())
->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();
}
}