adrema/app/Initialize/InitializeMembers.php

171 lines
7.1 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;
use Carbon\Carbon;
2021-11-18 00:09:37 +01:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
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
2020-04-12 00:26:44 +02:00
class InitializeMembers {
private $api;
2022-01-03 10:33:48 +01:00
2022-01-03 01:17:24 +01:00
public function __construct($api) {
2020-04-12 00:26:44 +02:00
$this->api = $api;
}
2021-07-04 12:09:30 +02:00
public function getSubscriptionId($member) {
$fee = Fee::firstWhere('nami_id', $member->fee_id ?: -1);
if (is_null($fee)) {
return null;
}
return optional($fee->subscriptions()->first())->id;
}
2020-04-12 00:26:44 +02:00
public function handle() {
$allMembers = collect([]);
2022-01-03 01:17:24 +01:00
$this->api->search([])->each(function($member) {
$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([
'course_id' => Course::where('nami_id', $course->course_id)->firstOrFail()->id,
'organizer' => $course->organizer,
'event_name' => $course->event_name,
'completed_at' => $course->completed_at,
'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) {
if ($membership['entries_aktivBis'] !== '') {
continue;
}
try {
[$activityId, $subactivityId, $groupId] = $this->fetchMembership($member, $membership);
} catch (RightException $e) {
continue;
}
if (is_null($activityId)) {
continue;
}
$m->memberships()->create([
'nami_id' => $membership['id'],
'from' => $membership['entries_aktivVon'],
'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
private function fetchMembership($member, $membership) {
if ($this->shouldSyncMembership($membership)) {
$singleMembership = $this->api->membership($member->id, $membership['id']);
app(ActivityCreator::class)->createFor($this->api, $singleMembership['gruppierungId']);
$group = Group::firstOrCreate(['nami_id' => $singleMembership['gruppierungId']], [
'nami_id' => $singleMembership['gruppierungId'],
'name' => $singleMembership['gruppierung'],
]);
try {
$activityId = Activity::where('nami_id', $singleMembership['taetigkeitId'])->firstOrFail()->id;
$subactivityId = $singleMembership['untergliederungId']
? Subactivity::where('nami_id', $singleMembership['untergliederungId'])->firstOrFail()->id
: null;
return [$activityId, $subactivityId, $group->id];
} catch (ModelNotFoundException $e) {
return [null, null, null];
}
}
if ($membership['entries_untergliederung'] === '') {
$subactivityId = null;
} else {
$subactivityId = Subactivity::where('name', $membership['entries_untergliederung'])->firstOrFail()->id;
}
preg_match('/\(([0-9]+)\)$/', $membership['entries_taetigkeit'], $activityMatches);
$activityId = Activity::where('nami_id', $activityMatches[1])->firstOrFail()->id;
$groupId = Group::where('name', $membership['entries_gruppierung'])->firstOrFail()->id;
return [$activityId, $subactivityId, $groupId];
}
private function shouldSyncMembership($membership) {
if (!Group::where('name', $membership['entries_gruppierung'])->exists()) {
return true;
}
if (preg_match('/\(([0-9]+)\)/', $membership['entries_taetigkeit'], $activityMatches) !== 1) {
throw new NamiException("ID in taetigkeit string not found: {$membership['entries_taetigkeit']}");
}
if (!Activity::where('nami_id', (int) $activityMatches[1])->exists()) {
return true;
}
if ($membership['entries_untergliederung'] === '') {
return false;
}
return !Subactivity::where('name', $membership['entries_untergliederung'])->exists();
}
2020-04-12 00:26:44 +02:00
}