2023-03-07 00:22:01 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Activity\Api;
|
|
|
|
|
|
|
|
use App\Activity;
|
|
|
|
use App\Member\Membership;
|
|
|
|
use App\Subactivity;
|
2023-03-07 00:25:11 +01:00
|
|
|
use DB;
|
2023-03-07 00:22:01 +01:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Lorisleiva\Actions\ActionRequest;
|
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
|
|
|
|
class SubactivityUpdateAction
|
|
|
|
{
|
|
|
|
use AsAction;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, string|array<int, int>> $payload
|
|
|
|
*/
|
|
|
|
public function handle(Subactivity $subactivity, array $payload): Subactivity
|
|
|
|
{
|
2023-03-07 00:25:11 +01:00
|
|
|
return DB::transaction(function () use ($subactivity, $payload) {
|
|
|
|
$subactivity->update(Arr::except($payload, 'activities'));
|
2023-03-07 00:22:01 +01:00
|
|
|
|
2023-03-07 00:25:11 +01:00
|
|
|
if (null !== data_get($payload, 'activities')) {
|
|
|
|
$subactivity->activities()->sync($payload['activities']);
|
|
|
|
}
|
2023-03-07 00:22:01 +01:00
|
|
|
|
2023-03-07 00:25:11 +01:00
|
|
|
return $subactivity;
|
|
|
|
});
|
2023-03-07 00:22:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-07 00:25:11 +01:00
|
|
|
* @return array<string, string|array<int, mixed>>
|
2023-03-07 00:22:01 +01:00
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
2023-03-07 09:37:49 +01:00
|
|
|
/** @var Subactivity */
|
|
|
|
$subactivity = request()->route('subactivity');
|
|
|
|
|
2023-03-07 00:22:01 +01:00
|
|
|
return [
|
2023-03-07 09:37:49 +01:00
|
|
|
'name' => ['required', 'string', 'max:255', Rule::unique('subactivities', 'name')->ignore($subactivity->id)],
|
2023-03-07 00:22:01 +01:00
|
|
|
'activities' => ['present', 'array', 'min:1'],
|
2023-03-07 01:35:39 +01:00
|
|
|
'activities.*' => 'integer',
|
2023-03-07 00:22:01 +01:00
|
|
|
'is_filterable' => 'present|boolean',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
public function getValidationAttributes(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'activities' => 'Tätigkeiten',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function asController(ActionRequest $request, Subactivity $subactivity): JsonResponse
|
|
|
|
{
|
|
|
|
if ($subactivity->hasNami) {
|
|
|
|
$this->validateNami($subactivity, $request->validated());
|
|
|
|
}
|
|
|
|
|
|
|
|
$removingActivities = $subactivity->activities()->whereNotIn('id', $request->validated('activities'))->pluck('id');
|
|
|
|
|
2023-03-07 09:37:49 +01:00
|
|
|
if ($removingActivities->first(fn ($activity) => Membership::where(['activity_id' => $activity, 'subactivity_id' => $subactivity->id])->exists())) {
|
2023-03-07 00:22:01 +01:00
|
|
|
throw ValidationException::withMessages(['activities' => 'Tätigkeit hat noch Mitglieder.']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($this->handle($subactivity, $request->validated()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo handle this with a model event on the pivot model
|
|
|
|
*
|
2023-03-07 09:37:49 +01:00
|
|
|
* @param array{name: string, activities: array<int, int>} $payload
|
2023-03-07 00:22:01 +01:00
|
|
|
*/
|
|
|
|
private function validateNami(Subactivity $subactivity, array $payload): void
|
|
|
|
{
|
|
|
|
if ($subactivity->name !== $payload['name']) {
|
|
|
|
throw ValidationException::withMessages(['name' => 'Untertätigkeit ist in NaMi. Update des Namens nicht möglich.']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$removingActivities = $subactivity->activities()->whereNotIn('id', $payload['activities'])->pluck('id');
|
|
|
|
|
|
|
|
if ($removingActivities->first(fn ($activity) => Activity::find($activity)->hasNami)) {
|
|
|
|
throw ValidationException::withMessages(['activities' => 'Tätigkeit kann nicht entfernt werden.']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$addingActivities = collect($payload['activities'])->filter(fn ($activityId) => $subactivity->activities->doesntContain($activityId));
|
|
|
|
|
|
|
|
if ($addingActivities->first(fn ($activity) => Activity::find($activity)->hasNami)) {
|
|
|
|
throw ValidationException::withMessages(['activities' => 'Tätigkeit kann nicht hinzugefügt werden.']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|