90f9857b06
API CI/CD / Validate (composer + pint) (push) Successful in 3m7s
API CI/CD / Test (PHPUnit) (push) Failing after 5m46s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Security audit (push) Failing after 49s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
127 lines
3.8 KiB
PHP
127 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Settings\SchoolCalendar;
|
|
|
|
use App\Models\CalendarEvent;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class SchoolCalendarMutationService
|
|
{
|
|
public function __construct(
|
|
private SchoolCalendarContextService $context,
|
|
private SchoolCalendarNotificationService $notificationService
|
|
) {}
|
|
|
|
public function create(array $payload, array $targets = []): array
|
|
{
|
|
$data = $this->normalizePayload($payload);
|
|
|
|
try {
|
|
$event = DB::transaction(function () use ($data) {
|
|
return CalendarEvent::query()->create($data);
|
|
});
|
|
} catch (\Throwable $e) {
|
|
Log::error('SchoolCalendar create failed: '.$e->getMessage());
|
|
throw $e;
|
|
}
|
|
|
|
$emailStatus = $this->notificationService->notify($event->toArray(), $targets);
|
|
|
|
return [
|
|
'event' => $event,
|
|
'email_status' => $emailStatus,
|
|
];
|
|
}
|
|
|
|
public function update(CalendarEvent $event, array $payload, array $targets = []): array
|
|
{
|
|
$data = $this->normalizePayload($payload, true);
|
|
|
|
try {
|
|
DB::transaction(function () use ($event, $data) {
|
|
$event->fill($data);
|
|
$event->save();
|
|
});
|
|
} catch (\Throwable $e) {
|
|
Log::error('SchoolCalendar update failed: '.$e->getMessage());
|
|
throw $e;
|
|
}
|
|
|
|
$event->refresh();
|
|
$emailStatus = $this->notificationService->notify($event->toArray(), $targets);
|
|
|
|
return [
|
|
'event' => $event,
|
|
'email_status' => $emailStatus,
|
|
];
|
|
}
|
|
|
|
public function delete(CalendarEvent $event): void
|
|
{
|
|
try {
|
|
DB::transaction(function () use ($event) {
|
|
$event->delete();
|
|
});
|
|
} catch (\Throwable $e) {
|
|
Log::error('SchoolCalendar delete failed: '.$e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
private function normalizePayload(array $payload, bool $partial = false): array
|
|
{
|
|
if ($partial) {
|
|
$data = [];
|
|
foreach ([
|
|
'title',
|
|
'description',
|
|
'event_type',
|
|
'date',
|
|
'notify_parent',
|
|
'notify_teacher',
|
|
'notify_admin',
|
|
'no_school',
|
|
'school_year',
|
|
'semester',
|
|
] as $key) {
|
|
if (! array_key_exists($key, $payload)) {
|
|
continue;
|
|
}
|
|
|
|
if (in_array($key, ['notify_parent', 'notify_teacher', 'notify_admin', 'no_school'], true)) {
|
|
$data[$key] = ! empty($payload[$key]) ? 1 : 0;
|
|
continue;
|
|
}
|
|
|
|
$data[$key] = $payload[$key];
|
|
}
|
|
|
|
if (! CalendarEvent::supportsEventType()) {
|
|
unset($data['event_type']);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
$data = [
|
|
'title' => (string) ($payload['title'] ?? ''),
|
|
'description' => $payload['description'] ?? null,
|
|
'event_type' => $payload['event_type'] ?? null,
|
|
'date' => $payload['date'] ?? $payload['start'] ?? null,
|
|
'notify_parent' => ! empty($payload['notify_parent']) ? 1 : 0,
|
|
'notify_teacher' => ! empty($payload['notify_teacher']) ? 1 : 0,
|
|
'notify_admin' => ! empty($payload['notify_admin']) ? 1 : 0,
|
|
'no_school' => ! empty($payload['no_school']) ? 1 : 0,
|
|
'school_year' => $payload['school_year'] ?? $this->context->defaultSchoolYear(),
|
|
'semester' => $payload['semester'] ?? $this->context->defaultSemester(),
|
|
];
|
|
|
|
if (! CalendarEvent::supportsEventType()) {
|
|
unset($data['event_type']);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|