adrema/app/Initialize/InitializeMembers.php

180 lines
7.2 KiB
PHP
Raw Normal View History

2022-01-03 10:33:48 +01:00
<?php
2020-04-12 00:26:44 +02:00
namespace App\Initialize;
2021-11-17 22:32:52 +01:00
use App\Activity;
2021-04-10 01:39:39 +02:00
use App\Confession;
use App\Country;
2021-11-19 22:58:27 +01:00
use App\Course\Models\Course;
2021-04-10 03:08:34 +02:00
use App\Fee;
2021-11-17 22:32:52 +01:00
use App\Gender;
2021-06-13 11:33:50 +02:00
use App\Group;
2021-11-17 22:32:52 +01:00
use App\Member\Member;
use App\Nationality;
use App\Region;
use App\Subactivity;
2021-11-18 00:09:37 +01:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
2022-02-12 16:16:56 +01:00
use Zoomyboy\LaravelNami\Api;
2022-03-06 02:57:39 +01:00
use Zoomyboy\LaravelNami\Data\MembershipEntry;
2022-01-03 01:17:24 +01:00
use Zoomyboy\LaravelNami\Exceptions\RightException;
2021-11-17 22:32:52 +01:00
use Zoomyboy\LaravelNami\Member as NamiMember;
use Zoomyboy\LaravelNami\NamiException;
2021-04-10 01:39:39 +02:00
2022-03-11 20:19:17 +01:00
class InitializeMembers
{
2022-02-12 16:16:56 +01:00
private Api $api;
2022-01-03 10:33:48 +01:00
2022-03-11 20:19:17 +01:00
public function __construct(Api $api)
{
2020-04-12 00:26:44 +02:00
$this->api = $api;
}
2022-02-12 16:16:56 +01:00
public function getSubscriptionId(NamiMember $member): ?int
{
2021-07-04 12:09:30 +02:00
$fee = Fee::firstWhere('nami_id', $member->fee_id ?: -1);
if (is_null($fee)) {
return null;
}
return optional($fee->subscriptions()->first())->id;
}
2022-02-12 16:16:56 +01:00
public function handle(): void
{
2020-04-12 00:26:44 +02:00
$allMembers = collect([]);
2022-03-11 20:19:17 +01:00
$this->api->search([])->each(function (NamiMember $member): void {
2022-01-03 01:17:24 +01:00
$member = NamiMember::fromNami($this->api->member($member->group_id, $member->id));
if (!$member->joined_at) {
return;
}
try {
$m = Member::create([
'firstname' => $member->firstname,
'lastname' => $member->lastname,
'joined_at' => $member->joined_at,
'birthday' => $member->birthday,
'send_newspaper' => $member->send_newspaper,
'address' => $member->address,
'zip' => $member->zip,
'location' => $member->location,
'nickname' => $member->nickname,
'other_country' => $member->other_country,
'further_address' => $member->further_address,
'main_phone' => $member->main_phone,
'mobile_phone' => $member->mobile_phone,
'work_phone' => $member->work_phone,
'fax' => $member->fax,
'email' => $member->email,
'email_parents' => $member->email_parents,
'nami_id' => $member->id,
'group_id' => Group::firstOrCreate(['nami_id' => $member->group_id], ['nami_id' => $member->group_id, 'name' => $member->group_name])->id,
'gender_id' => optional(Gender::firstWhere('nami_id', $member->gender_id ?: -1))->id,
'confession_id' => optional(Confession::firstWhere('nami_id', $member->confession_id ?: -1))->id,
'region_id' => optional(Region::firstWhere('nami_id', $member->region_id ?: -1))->id,
'country_id' => optional(Country::where('nami_id', $member->country_id)->first())->id,
'subscription_id' => $this->getSubscriptionId($member),
'nationality_id' => Nationality::where('nami_id', $member->nationality_id)->firstOrFail()->id,
'version' => $member->version,
]);
2022-01-11 02:37:53 +01:00
try {
foreach ($this->api->coursesFor($member->id) as $course) {
$m->courses()->create([
2022-03-04 01:57:03 +01:00
'course_id' => Course::where('nami_id', $course->courseId)->firstOrFail()->id,
2022-01-11 02:37:53 +01:00
'organizer' => $course->organizer,
2022-03-04 01:57:03 +01:00
'event_name' => $course->eventName,
'completed_at' => $course->completedAt,
2022-01-11 02:37:53 +01:00
'nami_id' => $course->id,
]);
}
} catch (RightException $e) {
2022-01-03 01:17:24 +01:00
}
2021-11-18 01:54:27 +01:00
2022-01-11 02:37:53 +01:00
try {
foreach ($this->api->membershipsOf($member->id) as $membership) {
2022-03-11 20:19:17 +01:00
if (null !== $membership->endsAt) {
2022-01-11 02:37:53 +01:00
continue;
}
try {
[$activityId, $subactivityId, $groupId] = $this->fetchMembership($member, $membership);
} catch (RightException $e) {
continue;
}
if (is_null($activityId)) {
continue;
}
$m->memberships()->create([
2022-03-06 02:57:39 +01:00
'nami_id' => $membership->id,
'from' => $membership->startsAt,
2022-01-11 02:37:53 +01:00
'group_id' => $groupId,
'activity_id' => $activityId,
'subactivity_id' => $subactivityId,
]);
2022-01-03 01:45:33 +01:00
}
2022-01-11 02:37:53 +01:00
} catch (RightException $e) {
2021-11-18 00:09:37 +01:00
}
2022-01-03 01:17:24 +01:00
} catch (ModelNotFoundException $e) {
dd($e->getMessage(), $member);
}
2020-04-12 00:26:44 +02:00
});
}
2022-01-03 10:33:48 +01:00
2022-03-06 02:57:39 +01:00
private function fetchMembership(NamiMember $member, MembershipEntry $membershipEntry): array
2022-02-12 16:16:56 +01:00
{
2022-03-06 02:57:39 +01:00
if ($this->shouldSyncMembership($membershipEntry)) {
$membership = $this->api->membership($member->id, $membershipEntry->id);
2022-03-15 12:02:47 +01:00
if (is_null($membership)) {
return [null, null, null];
}
2022-03-06 02:57:39 +01:00
app(ActivityCreator::class)->createFor($this->api, $membership->groupId);
$group = Group::firstOrCreate(['nami_id' => $membership->groupId], [
'nami_id' => $membership->groupId,
'name' => $membership->group,
2022-01-03 10:33:48 +01:00
]);
try {
2022-03-06 02:57:39 +01:00
$activityId = Activity::where('nami_id', $membership->activityId)->firstOrFail()->id;
$subactivityId = $membership->subactivityId
? Subactivity::where('nami_id', $membership->subactivityId)->firstOrFail()->id
2022-01-03 10:33:48 +01:00
: null;
2022-03-11 20:19:17 +01:00
2022-01-03 10:33:48 +01:00
return [$activityId, $subactivityId, $group->id];
} catch (ModelNotFoundException $e) {
return [null, null, null];
}
}
2022-03-11 20:19:17 +01:00
if (null === $membershipEntry->subactivity) {
2022-01-03 10:33:48 +01:00
$subactivityId = null;
} else {
2022-03-06 02:57:39 +01:00
$subactivityId = Subactivity::where('name', $membershipEntry->subactivity)->firstOrFail()->id;
2022-01-03 10:33:48 +01:00
}
2022-03-06 02:57:39 +01:00
preg_match('/\(([0-9]+)\)$/', $membershipEntry->activity, $activityMatches);
2022-01-03 10:33:48 +01:00
$activityId = Activity::where('nami_id', $activityMatches[1])->firstOrFail()->id;
2022-03-06 02:57:39 +01:00
$groupId = Group::where('name', $membershipEntry->group)->firstOrFail()->id;
2022-01-03 10:33:48 +01:00
return [$activityId, $subactivityId, $groupId];
}
2022-03-06 02:57:39 +01:00
private function shouldSyncMembership(MembershipEntry $membershipEntry): bool
2022-02-12 16:16:56 +01:00
{
2022-03-06 02:57:39 +01:00
if (!Group::where('name', $membershipEntry->group)->exists()) {
2022-01-03 10:33:48 +01:00
return true;
}
2022-03-11 20:19:17 +01:00
if (1 !== preg_match('/\(([0-9]+)\)/', $membershipEntry->activity, $activityMatches)) {
2022-03-06 02:57:39 +01:00
throw new NamiException("ID in taetigkeit string not found: {$membershipEntry->activity}");
2022-01-03 10:33:48 +01:00
}
if (!Activity::where('nami_id', (int) $activityMatches[1])->exists()) {
return true;
}
2022-03-11 20:19:17 +01:00
if (null === $membershipEntry->subactivity) {
2022-01-03 10:33:48 +01:00
return false;
}
2022-03-06 02:57:39 +01:00
return !Subactivity::where('name', $membershipEntry->subactivity)->exists();
2022-01-03 10:33:48 +01:00
}
2020-04-12 00:26:44 +02:00
}