2022-11-16 22:59:49 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Membership\Actions;
|
|
|
|
|
|
|
|
use App\Activity;
|
|
|
|
use App\Member\Member;
|
|
|
|
use App\Member\Membership;
|
|
|
|
use App\Setting\NamiSettings;
|
|
|
|
use App\Subactivity;
|
2022-12-11 21:00:48 +01:00
|
|
|
use Carbon\Carbon;
|
2022-11-16 22:59:49 +01:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use Illuminate\Validation\Rules\In;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Lorisleiva\Actions\ActionRequest;
|
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
use Zoomyboy\LaravelNami\Data\Membership as NamiMembership;
|
|
|
|
use Zoomyboy\LaravelNami\NamiException;
|
|
|
|
|
|
|
|
class MembershipStoreAction
|
|
|
|
{
|
|
|
|
use AsAction;
|
|
|
|
|
2022-12-11 21:00:48 +01:00
|
|
|
public function handle(Member $member, Activity $activity, ?Subactivity $subactivity, ?Carbon $promisedAt, NamiSettings $settings): Membership
|
2022-11-16 22:59:49 +01:00
|
|
|
{
|
|
|
|
$from = now()->startOfDay();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$namiId = $settings->login()->putMembership($member->nami_id, NamiMembership::fromArray([
|
|
|
|
'startsAt' => $from,
|
|
|
|
'groupId' => $member->group->nami_id,
|
|
|
|
'activityId' => $activity->nami_id,
|
|
|
|
'subactivityId' => $subactivity ? $subactivity->nami_id : null,
|
|
|
|
]));
|
|
|
|
} catch (NamiException $e) {
|
|
|
|
throw ValidationException::withMessages(['nami' => htmlspecialchars($e->getMessage())]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$membership = $member->memberships()->create([
|
|
|
|
'activity_id' => $activity->id,
|
|
|
|
'subactivity_id' => $subactivity ? $subactivity->id : null,
|
2022-12-11 21:00:48 +01:00
|
|
|
'promised_at' => $promisedAt,
|
2022-11-16 22:59:49 +01:00
|
|
|
...['nami_id' => $namiId, 'group_id' => $member->group->id, 'from' => $from],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$member->syncVersion();
|
|
|
|
|
|
|
|
return $membership;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, array<int, string|In>>
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
$subactivityRule = request()->activity_id ? ['nullable', Rule::exists('activity_subactivity', 'subactivity_id')->where('activity_id', request()->activity_id)] : ['nullable'];
|
|
|
|
|
|
|
|
return [
|
|
|
|
'activity_id' => ['bail', 'required', 'exists:activities,id'],
|
|
|
|
'subactivity_id' => $subactivityRule,
|
2022-12-11 21:00:48 +01:00
|
|
|
'promised_at' => ['nullable', 'date'],
|
2022-11-16 22:59:49 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
public function getValidationAttributes(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'activity_id' => 'Tätigkeit',
|
|
|
|
'subactivity_id' => 'Untertätigkeit',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function asController(Member $member, ActionRequest $request, NamiSettings $settings): RedirectResponse
|
|
|
|
{
|
|
|
|
$this->handle(
|
|
|
|
$member,
|
|
|
|
Activity::find($request->activity_id),
|
|
|
|
$request->subactivity_id ? Subactivity::find($request->subactivity_id) : null,
|
2022-12-11 21:00:48 +01:00
|
|
|
$request->promised_at ? Carbon::parse($request->promised_at) : null,
|
2022-11-16 22:59:49 +01:00
|
|
|
$settings,
|
|
|
|
);
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
}
|