2022-11-16 22:59:49 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Membership\Actions;
|
|
|
|
|
|
|
|
use App\Activity;
|
2023-02-21 13:02:31 +01:00
|
|
|
use App\Group;
|
2023-10-16 15:41:37 +02:00
|
|
|
use App\Lib\JobMiddleware\JobChannels;
|
2023-10-16 13:27:55 +02:00
|
|
|
use App\Lib\JobMiddleware\WithJobState;
|
|
|
|
use App\Lib\Queue\TracksJob;
|
2023-07-30 11:08:17 +02:00
|
|
|
use App\Maildispatcher\Actions\ResyncAction;
|
2022-11-16 22:59:49 +01:00
|
|
|
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;
|
2023-10-16 15:41:37 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2022-11-16 22:59:49 +01:00
|
|
|
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;
|
2023-02-08 01:39:46 +01:00
|
|
|
use Zoomyboy\LaravelNami\Exceptions\HttpException;
|
2022-11-16 22:59:49 +01:00
|
|
|
|
|
|
|
class MembershipStoreAction
|
|
|
|
{
|
|
|
|
use AsAction;
|
2023-10-16 13:27:55 +02:00
|
|
|
use TracksJob;
|
2022-11-16 22:59:49 +01:00
|
|
|
|
2023-10-16 13:27:55 +02:00
|
|
|
public function handle(Member $member, Activity $activity, ?Subactivity $subactivity, Group $group, ?Carbon $promisedAt): Membership
|
2022-11-16 22:59:49 +01:00
|
|
|
{
|
|
|
|
$from = now()->startOfDay();
|
|
|
|
|
2023-02-17 02:14:32 +01:00
|
|
|
$subactivity = $subactivity ?: new Subactivity(['nami_id' => null, 'id' => null]);
|
|
|
|
|
2024-04-19 11:12:41 +02:00
|
|
|
if ($this->syncable($member, $activity, $subactivity)) {
|
2023-02-17 02:14:32 +01:00
|
|
|
try {
|
2023-10-16 13:27:55 +02:00
|
|
|
$namiId = app(NamiSettings::class)->login()->putMembership($member->nami_id, NamiMembership::from([
|
2023-02-17 02:14:32 +01:00
|
|
|
'startsAt' => $from,
|
2023-02-21 13:02:31 +01:00
|
|
|
'groupId' => $group->nami_id,
|
2023-02-17 02:14:32 +01:00
|
|
|
'activityId' => $activity->nami_id,
|
|
|
|
'subactivityId' => $subactivity->nami_id,
|
|
|
|
]));
|
|
|
|
} catch (HttpException $e) {
|
|
|
|
throw ValidationException::withMessages(['nami' => htmlspecialchars($e->getMessage())]);
|
|
|
|
}
|
2022-11-16 22:59:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$membership = $member->memberships()->create([
|
|
|
|
'activity_id' => $activity->id,
|
2023-02-17 02:14:32 +01:00
|
|
|
'subactivity_id' => $subactivity->id,
|
2022-12-11 21:00:48 +01:00
|
|
|
'promised_at' => $promisedAt,
|
2023-02-21 13:02:31 +01:00
|
|
|
'group_id' => $group->id,
|
|
|
|
'from' => $from,
|
|
|
|
'nami_id' => $namiId ?? null,
|
2022-11-16 22:59:49 +01:00
|
|
|
]);
|
|
|
|
|
2024-04-19 11:12:41 +02:00
|
|
|
if ($this->syncable($member, $activity, $subactivity)) {
|
2023-02-17 02:14:32 +01:00
|
|
|
$member->syncVersion();
|
|
|
|
}
|
2022-11-16 22:59:49 +01:00
|
|
|
|
2023-10-16 13:27:55 +02:00
|
|
|
ResyncAction::dispatch();
|
|
|
|
|
2022-11-16 22:59:49 +01:00
|
|
|
return $membership;
|
|
|
|
}
|
|
|
|
|
2024-04-19 11:12:41 +02:00
|
|
|
protected function syncable(Member $member, Activity $activity, ?Subactivity $subactivity): bool
|
|
|
|
{
|
|
|
|
return $activity->hasNami && ($subactivity->id === null || $subactivity->hasNami) && $member->hasNami;
|
|
|
|
}
|
|
|
|
|
2022-11-16 22:59:49 +01:00
|
|
|
/**
|
|
|
|
* @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'],
|
2023-02-21 13:06:19 +01:00
|
|
|
'group_id' => ['required', 'exists:groups,id'],
|
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',
|
2023-02-21 13:06:19 +01:00
|
|
|
'group_id' => 'Gruppierung',
|
2022-11-16 22:59:49 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-10-16 15:41:37 +02:00
|
|
|
public function asController(Member $member, ActionRequest $request): JsonResponse
|
2022-11-16 22:59:49 +01:00
|
|
|
{
|
2023-10-16 13:27:55 +02:00
|
|
|
$this->startJob(
|
2022-11-16 22:59:49 +01:00
|
|
|
$member,
|
|
|
|
Activity::find($request->activity_id),
|
|
|
|
$request->subactivity_id ? Subactivity::find($request->subactivity_id) : null,
|
2023-02-21 13:02:31 +01:00
|
|
|
Group::findOrFail($request->input('group_id', -1)),
|
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
|
|
|
);
|
|
|
|
|
2023-10-16 15:41:37 +02:00
|
|
|
return response()->json([]);
|
2023-10-16 13:27:55 +02:00
|
|
|
}
|
2023-07-30 11:08:17 +02:00
|
|
|
|
2023-10-16 13:27:55 +02:00
|
|
|
/**
|
|
|
|
* @param mixed $parameters
|
|
|
|
*/
|
|
|
|
public function jobState(WithJobState $jobState, ...$parameters): WithJobState
|
|
|
|
{
|
|
|
|
$member = $parameters[0];
|
|
|
|
|
|
|
|
return $jobState
|
|
|
|
->before('Mitgliedschaft für ' . $member->fullname . ' wird gespeichert')
|
|
|
|
->after('Mitgliedschaft für ' . $member->fullname . ' gespeichert')
|
|
|
|
->failed('Fehler beim Erstellen der Mitgliedschaft für ' . $member->fullname)
|
2023-10-16 15:41:37 +02:00
|
|
|
->shouldReload(JobChannels::make()->add('member')->add('membership'));
|
2022-11-16 22:59:49 +01:00
|
|
|
}
|
|
|
|
}
|