add school calendar logic

This commit is contained in:
root
2026-03-10 16:55:50 -04:00
parent 311bb93977
commit abebe0d9c0
25 changed files with 1358 additions and 750 deletions
@@ -0,0 +1,154 @@
<?php
namespace App\Http\Controllers\Api\Settings;
use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\Settings\SchoolCalendarIndexRequest;
use App\Http\Requests\Settings\SchoolCalendarOptionsRequest;
use App\Http\Requests\Settings\SchoolCalendarStoreRequest;
use App\Http\Requests\Settings\SchoolCalendarUpdateRequest;
use App\Http\Resources\Settings\SchoolCalendarEventDetailResource;
use App\Http\Resources\Settings\SchoolCalendarEventResource;
use App\Services\Settings\SchoolCalendar\SchoolCalendarContextService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarFormatterService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarMeetingService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarMutationService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarQueryService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;
class SchoolCalendarController extends BaseApiController
{
public function __construct(
private SchoolCalendarContextService $contextService,
private SchoolCalendarQueryService $queryService,
private SchoolCalendarMeetingService $meetingService,
private SchoolCalendarFormatterService $formatterService,
private SchoolCalendarMutationService $mutationService
) {
parent::__construct();
}
public function options(SchoolCalendarOptionsRequest $request): JsonResponse
{
return $this->success([
'event_types' => $this->contextService->eventTypes(),
'defaults' => [
'school_year' => $this->contextService->defaultSchoolYear(),
'semester' => $this->contextService->defaultSemester(),
],
]);
}
public function index(SchoolCalendarIndexRequest $request): JsonResponse
{
$filters = $request->validated();
$audience = $filters['audience'] ?? null;
$includeMeetings = array_key_exists('include_meetings', $filters)
? (bool) $filters['include_meetings']
: true;
if (empty($filters['school_year'])) {
$filters['school_year'] = $this->contextService->defaultSchoolYear();
}
$events = $this->queryService->listEvents($filters);
$events = $this->queryService->filterEventsForAudience($events, $audience);
$formatted = $events->map(function ($event) use ($audience) {
return $this->formatterService->formatEvent($event, $audience);
})->all();
if ($includeMeetings) {
$parentUserId = $audience === 'parent' ? (int) (auth()->id() ?? 0) : null;
$meetingRows = $this->meetingService->listMeetings($filters['school_year'] ?? '', $audience, $parentUserId);
foreach ($meetingRows as $row) {
$formatted[] = $this->formatterService->formatMeetingEvent($row, $audience);
}
}
return $this->success([
'events' => SchoolCalendarEventResource::collection($formatted),
]);
}
public function show(int $eventId): JsonResponse
{
$event = $this->queryService->findEvent($eventId);
if (!$event) {
return $this->error('Event not found.', Response::HTTP_NOT_FOUND);
}
return $this->success([
'event' => new SchoolCalendarEventDetailResource($event),
]);
}
public function store(SchoolCalendarStoreRequest $request): JsonResponse
{
$payload = $request->validated();
$targets = $this->extractEmailTargets($payload);
try {
$result = $this->mutationService->create($payload, $targets);
} catch (\Throwable $e) {
Log::error('SchoolCalendar store failed: ' . $e->getMessage());
return $this->error('Failed to save event.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->success([
'event' => new SchoolCalendarEventDetailResource($result['event']),
'email_status' => $result['email_status'] ?? [],
], 'Event added successfully.', Response::HTTP_CREATED);
}
public function update(SchoolCalendarUpdateRequest $request, int $eventId): JsonResponse
{
$event = $this->queryService->findEvent($eventId);
if (!$event) {
return $this->error('Event not found.', Response::HTTP_NOT_FOUND);
}
$payload = $request->validated();
$targets = $this->extractEmailTargets($payload);
try {
$result = $this->mutationService->update($event, $payload, $targets);
} catch (\Throwable $e) {
Log::error('SchoolCalendar update failed: ' . $e->getMessage());
return $this->error('Failed to update event.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->success([
'event' => new SchoolCalendarEventDetailResource($result['event']),
'email_status' => $result['email_status'] ?? [],
], 'Event updated successfully.');
}
public function destroy(int $eventId): JsonResponse
{
$event = $this->queryService->findEvent($eventId);
if (!$event) {
return $this->error('Event not found.', Response::HTTP_NOT_FOUND);
}
try {
$this->mutationService->delete($event);
} catch (\Throwable $e) {
Log::error('SchoolCalendar delete failed: ' . $e->getMessage());
return $this->error('Failed to delete event.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->success(null, 'Event deleted successfully.');
}
private function extractEmailTargets(array $payload): array
{
return [
'parents' => !empty($payload['send_email_parent']),
'teachers' => !empty($payload['send_email_teacher']),
'admins' => !empty($payload['send_email_admin']),
];
}
}
@@ -0,0 +1,25 @@
<?php
namespace App\Http\Requests\Settings;
use App\Http\Requests\ApiFormRequest;
use Illuminate\Validation\Rule;
class SchoolCalendarIndexRequest extends ApiFormRequest
{
public function authorize(): bool
{
return auth()->check();
}
public function rules(): array
{
return [
'school_year' => ['nullable', 'string', 'max:20'],
'semester' => ['nullable', 'string', 'max:20'],
'audience' => ['nullable', 'string', Rule::in(['parent', 'teacher', 'admin'])],
'include_meetings' => ['nullable', 'boolean'],
'sort_dir' => ['nullable', 'string', Rule::in(['asc', 'desc'])],
];
}
}
@@ -0,0 +1,18 @@
<?php
namespace App\Http\Requests\Settings;
use App\Http\Requests\ApiFormRequest;
class SchoolCalendarOptionsRequest extends ApiFormRequest
{
public function authorize(): bool
{
return auth()->check();
}
public function rules(): array
{
return [];
}
}
@@ -0,0 +1,36 @@
<?php
namespace App\Http\Requests\Settings;
use App\Http\Requests\ApiFormRequest;
use App\Services\Settings\SchoolCalendar\SchoolCalendarContextService;
use Illuminate\Validation\Rule;
class SchoolCalendarStoreRequest extends ApiFormRequest
{
public function authorize(): bool
{
return auth()->check();
}
public function rules(): array
{
$eventTypes = app(SchoolCalendarContextService::class)->eventTypes();
return [
'title' => ['required', 'string', 'min:3', 'max:255'],
'description' => ['nullable', 'string'],
'event_type' => ['nullable', 'string', Rule::in($eventTypes)],
'date' => ['required', 'date_format:Y-m-d'],
'notify_parent' => ['nullable', 'boolean'],
'notify_teacher' => ['nullable', 'boolean'],
'notify_admin' => ['nullable', 'boolean'],
'no_school' => ['nullable', 'boolean'],
'school_year' => ['nullable', 'string', 'max:20'],
'semester' => ['nullable', 'string', 'max:20'],
'send_email_parent' => ['nullable', 'boolean'],
'send_email_teacher' => ['nullable', 'boolean'],
'send_email_admin' => ['nullable', 'boolean'],
];
}
}
@@ -0,0 +1,36 @@
<?php
namespace App\Http\Requests\Settings;
use App\Http\Requests\ApiFormRequest;
use App\Services\Settings\SchoolCalendar\SchoolCalendarContextService;
use Illuminate\Validation\Rule;
class SchoolCalendarUpdateRequest extends ApiFormRequest
{
public function authorize(): bool
{
return auth()->check();
}
public function rules(): array
{
$eventTypes = app(SchoolCalendarContextService::class)->eventTypes();
return [
'title' => ['sometimes', 'string', 'min:3', 'max:255'],
'description' => ['nullable', 'string'],
'event_type' => ['nullable', 'string', Rule::in($eventTypes)],
'date' => ['sometimes', 'date_format:Y-m-d'],
'notify_parent' => ['nullable', 'boolean'],
'notify_teacher' => ['nullable', 'boolean'],
'notify_admin' => ['nullable', 'boolean'],
'no_school' => ['nullable', 'boolean'],
'school_year' => ['nullable', 'string', 'max:20'],
'semester' => ['nullable', 'string', 'max:20'],
'send_email_parent' => ['nullable', 'boolean'],
'send_email_teacher' => ['nullable', 'boolean'],
'send_email_admin' => ['nullable', 'boolean'],
];
}
}
@@ -0,0 +1,27 @@
<?php
namespace App\Http\Resources\Settings;
use Illuminate\Http\Resources\Json\JsonResource;
class SchoolCalendarEventDetailResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => (int) ($this->id ?? 0),
'title' => (string) ($this->title ?? ''),
'description' => (string) ($this->description ?? ''),
'event_type' => $this->event_type ?? null,
'date' => (string) ($this->date ?? ''),
'notify_parent' => (int) ($this->notify_parent ?? 0),
'notify_teacher' => (int) ($this->notify_teacher ?? 0),
'notify_admin' => (int) ($this->notify_admin ?? 0),
'no_school' => (int) ($this->no_school ?? 0),
'semester' => (string) ($this->semester ?? ''),
'school_year' => (string) ($this->school_year ?? ''),
'created_at' => $this->created_at ?? null,
'updated_at' => $this->updated_at ?? null,
];
}
}
@@ -0,0 +1,19 @@
<?php
namespace App\Http\Resources\Settings;
use Illuminate\Http\Resources\Json\JsonResource;
class SchoolCalendarEventResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this['id'] ?? null,
'title' => (string) ($this['title'] ?? ''),
'start' => (string) ($this['start'] ?? ''),
'description' => (string) ($this['description'] ?? ''),
'extendedProps' => (array) ($this['extendedProps'] ?? []),
];
}
}