Extract api from initializing

This commit is contained in:
philipp lang 2023-02-12 23:18:58 +01:00
parent a399c91a03
commit 363f51872a
10 changed files with 279 additions and 151 deletions

View File

@ -0,0 +1,37 @@
<?php
namespace App\Actions;
use App\Course\Models\Course;
use App\Member\Member;
use Illuminate\Support\Collection;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Data\Course as NamiCourse;
class InsertCoursesAction
{
use AsAction;
/**
* @param Collection<int, NamiCourse> $courses
*/
public function handle(Member $member, Collection $courses): void
{
if (!$member->hasNami) {
return;
}
foreach ($courses as $course) {
$member->courses()->updateOrCreate(['nami_id' => $course->id], [
'course_id' => Course::nami($course->courseId)->id,
'organizer' => $course->organizer,
'nami_id' => $course->id,
'completed_at' => $course->completedAt,
'event_name' => $course->eventName,
]);
}
$courseIds = $courses->map(fn ($course) => $course->id)->toArray();
$member->courses()->whereNotIn('nami_id', $courseIds)->whereNotNull('nami_id')->delete();
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace App\Actions;
use App\Confession;
use App\Country;
use App\Fee;
use App\Gender;
use App\Group;
use App\Member\Member;
use App\Nationality;
use App\Region;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Data\Member as NamiMember;
class InsertMemberAction
{
use AsAction;
public function handle(NamiMember $member): Member
{
$region = Region::firstWhere('nami_id', $member->regionId ?: -1);
return Member::updateOrCreate(['nami_id' => $member->id], [
'firstname' => $member->firstname,
'lastname' => $member->lastname,
'joined_at' => $member->joinedAt,
'birthday' => $member->birthday,
'send_newspaper' => $member->sendNewspaper,
'address' => $member->address,
'zip' => $member->zip,
'location' => $member->location,
'nickname' => $member->nickname,
'other_country' => $member->otherCountry,
'further_address' => $member->furtherAddress,
'main_phone' => $member->mainPhone,
'mobile_phone' => $member->mobilePhone,
'work_phone' => $member->workPhone,
'fax' => $member->fax,
'email' => $member->email,
'email_parents' => $member->emailParents,
'nami_id' => $member->id,
'group_id' => Group::firstOrCreate(['nami_id' => $member->groupId], ['nami_id' => $member->groupId, 'name' => $member->groupName])->id,
'gender_id' => optional(Gender::firstWhere('nami_id', $member->genderId ?: -1))->id,
'confession_id' => optional(Confession::firstWhere('nami_id', $member->confessionId ?: -1))->id,
'region_id' => $region && !$region->is_null ? $region->id : null,
'country_id' => optional(Country::where('nami_id', $member->countryId)->first())->id,
'subscription_id' => $this->getSubscriptionId($member),
'nationality_id' => Nationality::where('nami_id', $member->nationalityId)->firstOrFail()->id,
'version' => $member->version,
]);
}
public function getSubscriptionId(NamiMember $member): ?int
{
$fee = Fee::firstWhere('nami_id', $member->feeId ?: -1);
if (is_null($fee)) {
return null;
}
return optional($fee->subscriptions()->first())->id;
}
}

View File

@ -0,0 +1,113 @@
<?php
namespace App\Actions;
use App\Activity;
use App\Group;
use App\Initialize\ActivityCreator;
use App\Member\Member;
use App\Member\Membership;
use App\Setting\NamiSettings;
use App\Subactivity;
use Illuminate\Support\Collection;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Data\MembershipEntry as NamiMembershipEntry;
class InsertMembershipsAction
{
use AsAction;
/**
* @param Collection<int, NamiMembershipEntry> $memberships
*/
public function handle(Member $member, Collection $memberships): void
{
if (!$member->hasNami) {
return;
}
foreach ($memberships as $membership) {
$existingMembership = Membership::where('nami_id', $membership->id)->first();
$group = Group::where('name', $membership->group)->whereNotNull('nami_id')->first();
if (!$group) {
continue;
}
if (null !== $this->overviewStrategy($member, $group, $membership)) {
continue;
}
$this->singleStrategy($member, $group, $membership);
}
$membershipIds = $memberships->map(fn ($membership) => $membership->id)->toArray();
$member->memberships()->whereNotIn('nami_id', $membershipIds)->whereNotNull('nami_id')->delete();
}
private function overviewStrategy(Member $member, Group $group, NamiMembershipEntry $membership): ?Membership
{
$activity = 1 === preg_match('/\(([0-9]+)\)/', $membership->activity, $activityMatches)
? Activity::where('nami_id', (int) $activityMatches[1])->first()
: null;
if (!$activity) {
return null;
}
if (null !== $membership->subactivity) {
$subactivity = Subactivity::where('name', $membership->subactivity)->first();
if (!$subactivity) {
return null;
}
} else {
$subactivity = null;
}
return $member->memberships()->updateOrCreate(['nami_id' => $membership->id], [
'nami_id' => $membership->id,
'from' => $membership->startsAt,
'to' => $membership->endsAt,
'group_id' => $group->id,
'activity_id' => $activity->id,
'subactivity_id' => $subactivity?->id,
]);
}
private function singleStrategy(Member $member, Group $group, NamiMembershipEntry $membershipEntry): ?Membership
{
$membership = $this->api()->membership($member->nami_id, $membershipEntry->id);
app(ActivityCreator::class)->createFor($this->api(), $membership->groupId);
$activity = Activity::nami($membership->activityId);
if (!$activity) {
return null;
}
if (null !== $membership->subactivityId) {
$subactivity = Subactivity::nami($membership->subactivityId);
if (!$subactivity) {
return null;
}
} else {
$subactivity = null;
}
return $member->memberships()->updateOrCreate(['nami_id' => $membership->id], [
'nami_id' => $membership->id,
'from' => $membership->startsAt,
'to' => $membership->endsAt,
'group_id' => $group->id,
'activity_id' => $activity->id,
'subactivity_id' => $subactivity?->id,
]);
}
private function api(): Api
{
return app(NamiSettings::class)->login();
}
}

View File

@ -2,8 +2,8 @@
namespace App\Actions;
use App\Course\Models\Course;
use App\Member\Member;
use App\Nami\Api\CoursesOfAction;
use App\Setting\NamiSettings;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Api;
@ -18,20 +18,7 @@ class PullCoursesAction
return;
}
$courses = $this->api()->coursesFor($member->nami_id);
foreach ($courses as $course) {
$member->courses()->updateOrCreate(['nami_id' => $course->id], [
'course_id' => Course::nami($course->courseId)->id,
'organizer' => $course->organizer,
'nami_id' => $course->id,
'completed_at' => $course->completedAt,
'event_name' => $course->eventName,
]);
}
$courseIds = $courses->map(fn ($course) => $course->id)->toArray();
$member->courses()->whereNotIn('nami_id', $courseIds)->whereNotNull('nami_id')->delete();
InsertCoursesAction::run($member, CoursesOfAction::run($this->api(), $member->nami_id));
}
private function api(): Api

View File

@ -2,18 +2,11 @@
namespace App\Actions;
use App\Confession;
use App\Country;
use App\Fee;
use App\Gender;
use App\Group;
use App\Member\Member;
use App\Nationality;
use App\Region;
use App\Nami\Api\MemberAction;
use App\Setting\NamiSettings;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Data\Member as NamiMember;
class PullMemberAction
{
@ -21,48 +14,7 @@ class PullMemberAction
public function handle(int $groupId, int $memberId): Member
{
$member = $this->api()->member($groupId, $memberId);
$region = Region::firstWhere('nami_id', $member->regionId ?: -1);
return Member::updateOrCreate(['nami_id' => $member->id], [
'firstname' => $member->firstname,
'lastname' => $member->lastname,
'joined_at' => $member->joinedAt,
'birthday' => $member->birthday,
'send_newspaper' => $member->sendNewspaper,
'address' => $member->address,
'zip' => $member->zip,
'location' => $member->location,
'nickname' => $member->nickname,
'other_country' => $member->otherCountry,
'further_address' => $member->furtherAddress,
'main_phone' => $member->mainPhone,
'mobile_phone' => $member->mobilePhone,
'work_phone' => $member->workPhone,
'fax' => $member->fax,
'email' => $member->email,
'email_parents' => $member->emailParents,
'nami_id' => $member->id,
'group_id' => Group::firstOrCreate(['nami_id' => $member->groupId], ['nami_id' => $member->groupId, 'name' => $member->groupName])->id,
'gender_id' => optional(Gender::firstWhere('nami_id', $member->genderId ?: -1))->id,
'confession_id' => optional(Confession::firstWhere('nami_id', $member->confessionId ?: -1))->id,
'region_id' => $region && !$region->is_null ? $region->id : null,
'country_id' => optional(Country::where('nami_id', $member->countryId)->first())->id,
'subscription_id' => $this->getSubscriptionId($member),
'nationality_id' => Nationality::where('nami_id', $member->nationalityId)->firstOrFail()->id,
'version' => $member->version,
]);
}
public function getSubscriptionId(NamiMember $member): ?int
{
$fee = Fee::firstWhere('nami_id', $member->feeId ?: -1);
if (is_null($fee)) {
return null;
}
return optional($fee->subscriptions()->first())->id;
return InsertMemberAction::run(MemberAction::run($this->api(), $groupId, $memberId));
}
private function api(): Api

View File

@ -2,16 +2,11 @@
namespace App\Actions;
use App\Activity;
use App\Group;
use App\Initialize\ActivityCreator;
use App\Member\Member;
use App\Member\Membership;
use App\Nami\Api\MembershipsOfAction;
use App\Setting\NamiSettings;
use App\Subactivity;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Data\MembershipEntry as NamiMembershipEntry;
class PullMembershipsAction
{
@ -23,85 +18,7 @@ class PullMembershipsAction
return;
}
$memberships = $this->api()->membershipsOf($member->nami_id);
foreach ($memberships as $membership) {
$existingMembership = Membership::where('nami_id', $membership->id)->first();
$group = Group::where('name', $membership->group)->whereNotNull('nami_id')->first();
if (!$group) {
continue;
}
if (null !== $this->overviewStrategy($member, $group, $membership)) {
continue;
}
$this->singleStrategy($member, $group, $membership);
}
$membershipIds = $memberships->map(fn ($membership) => $membership->id)->toArray();
$member->memberships()->whereNotIn('nami_id', $membershipIds)->whereNotNull('nami_id')->delete();
}
private function overviewStrategy(Member $member, Group $group, NamiMembershipEntry $membership): ?Membership
{
$activity = 1 === preg_match('/\(([0-9]+)\)/', $membership->activity, $activityMatches)
? Activity::where('nami_id', (int) $activityMatches[1])->first()
: null;
if (!$activity) {
return null;
}
if (null !== $membership->subactivity) {
$subactivity = Subactivity::where('name', $membership->subactivity)->first();
if (!$subactivity) {
return null;
}
} else {
$subactivity = null;
}
return $member->memberships()->updateOrCreate(['nami_id' => $membership->id], [
'nami_id' => $membership->id,
'from' => $membership->startsAt,
'to' => $membership->endsAt,
'group_id' => $group->id,
'activity_id' => $activity->id,
'subactivity_id' => $subactivity?->id,
]);
}
private function singleStrategy(Member $member, Group $group, NamiMembershipEntry $membershipEntry): ?Membership
{
$membership = $this->api()->membership($member->nami_id, $membershipEntry->id);
app(ActivityCreator::class)->createFor($this->api(), $membership->groupId);
$activity = Activity::nami($membership->activityId);
if (!$activity) {
return null;
}
if (null !== $membership->subactivityId) {
$subactivity = Subactivity::nami($membership->subactivityId);
if (!$subactivity) {
return null;
}
} else {
$subactivity = null;
}
return $member->memberships()->updateOrCreate(['nami_id' => $membership->id], [
'nami_id' => $membership->id,
'from' => $membership->startsAt,
'to' => $membership->endsAt,
'group_id' => $group->id,
'activity_id' => $activity->id,
'subactivity_id' => $subactivity?->id,
]);
InsertMembershipsAction::run($member, MembershipsOfAction::run($this->api(), $member->nami_id));
}
private function api(): Api

View File

@ -0,0 +1,21 @@
<?php
namespace App\Nami\Api;
use Illuminate\Support\Collection;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Data\Course;
class CoursesOfAction
{
use AsAction;
/**
* @return Collection<int, Course>
*/
public function handle(Api $api, int $namiId): Collection
{
return $api->coursesOf($namiId);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Nami\Api;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Data\Member;
class MemberAction
{
use AsAction;
public function handle(Api $api, int $groupId, int $memberId): Member
{
return $api->member($groupId, $memberId);
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Nami\Api;
use Illuminate\Support\Collection;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Data\MembershipEntry;
class MembershipsOfAction
{
use AsAction;
/**
* @return Collection<int, MembershipEntry>
*/
public function handle(Api $api, int $namiId): Collection
{
return $api->membershipsOf($namiId);
}
}

@ -1 +1 @@
Subproject commit 8c6635ea0bd94c3e26341d32427a492238a4483d
Subproject commit 42a303d16cc7aa094968e3c212a755f762ce7b16