Files
fewo-stommer/app/Actions/ConfigureChannelExport.php
T
2026-10-27 19:00:25 +01:00

37 lines
1.6 KiB
PHP

<?php
namespace App\Actions;
use App\Channels\ICal\ICalExportFields;
use App\Models\Channel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
class ConfigureChannelExport
{
public function handle(Channel $channel, array $mapping, bool $allowNames): Channel
{
Validator::make(['export_mapping' => $mapping], [
'export_mapping' => ['array', 'max:3'],
'export_mapping.*.field' => ['required', 'distinct', Rule::in(array_keys(ICalExportFields::ICAL_FIELDS))],
'export_mapping.*.items' => ['required', 'array', 'min:1'],
'export_mapping.*.items.*' => ['required', Rule::in(array_keys(ICalExportFields::FIELDS))],
'export_mapping.*.separator' => ['nullable', 'string', 'max:30'],
])->validate();
foreach ($mapping as $row) {
if (! $allowNames && array_intersect($row['items'], ['guest_first_name', 'guest_full_name']) !== []) {
throw ValidationException::withMessages(['export_names' => 'Für Gastnamen die Namensweitergabe an genau diesen Zielkanal ausdrücklich einschalten.']);
}
}
return DB::transaction(function () use ($channel, $mapping, $allowNames): Channel {
$locked = Channel::query()->whereKey($channel->id)->lockForUpdate()->firstOrFail();
$locked->forceFill(['ical_export_mapping' => array_values($mapping), 'ical_export_names' => $allowNames])->save();
return $locked;
});
}
}