diff --git a/app/Actions/InsertCoursesAction.php b/app/Actions/InsertCoursesAction.php new file mode 100644 index 00000000..a0d3e6eb --- /dev/null +++ b/app/Actions/InsertCoursesAction.php @@ -0,0 +1,37 @@ + $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(); + } +} diff --git a/app/Actions/InsertMemberAction.php b/app/Actions/InsertMemberAction.php new file mode 100644 index 00000000..cc1738c7 --- /dev/null +++ b/app/Actions/InsertMemberAction.php @@ -0,0 +1,63 @@ +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; + } +} diff --git a/app/Actions/InsertMembershipsAction.php b/app/Actions/InsertMembershipsAction.php new file mode 100644 index 00000000..5b3869cb --- /dev/null +++ b/app/Actions/InsertMembershipsAction.php @@ -0,0 +1,113 @@ + $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(); + } +} diff --git a/app/Actions/PullCoursesAction.php b/app/Actions/PullCoursesAction.php index a6841edf..49cefaa8 100644 --- a/app/Actions/PullCoursesAction.php +++ b/app/Actions/PullCoursesAction.php @@ -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 diff --git a/app/Actions/PullMemberAction.php b/app/Actions/PullMemberAction.php index 26920695..6dfcda3c 100644 --- a/app/Actions/PullMemberAction.php +++ b/app/Actions/PullMemberAction.php @@ -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 diff --git a/app/Actions/PullMembershipsAction.php b/app/Actions/PullMembershipsAction.php index 428f09da..9cafd823 100644 --- a/app/Actions/PullMembershipsAction.php +++ b/app/Actions/PullMembershipsAction.php @@ -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 diff --git a/app/Nami/Api/CoursesOfAction.php b/app/Nami/Api/CoursesOfAction.php new file mode 100644 index 00000000..bb5d5d01 --- /dev/null +++ b/app/Nami/Api/CoursesOfAction.php @@ -0,0 +1,21 @@ + + */ + public function handle(Api $api, int $namiId): Collection + { + return $api->coursesOf($namiId); + } +} diff --git a/app/Nami/Api/MemberAction.php b/app/Nami/Api/MemberAction.php new file mode 100644 index 00000000..695428fc --- /dev/null +++ b/app/Nami/Api/MemberAction.php @@ -0,0 +1,17 @@ +member($groupId, $memberId); + } +} diff --git a/app/Nami/Api/MembershipsOfAction.php b/app/Nami/Api/MembershipsOfAction.php new file mode 100644 index 00000000..3fd7518b --- /dev/null +++ b/app/Nami/Api/MembershipsOfAction.php @@ -0,0 +1,21 @@ + + */ + public function handle(Api $api, int $namiId): Collection + { + return $api->membershipsOf($namiId); + } +} diff --git a/packages/laravel-nami b/packages/laravel-nami index 8c6635ea..42a303d1 160000 --- a/packages/laravel-nami +++ b/packages/laravel-nami @@ -1 +1 @@ -Subproject commit 8c6635ea0bd94c3e26341d32427a492238a4483d +Subproject commit 42a303d16cc7aa094968e3c212a755f762ce7b16