From ebdf764ad08a8eee734fd42f2d5e0e6505aa9b89 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Mon, 3 Jan 2022 01:45:33 +0100 Subject: [PATCH] Fixed: Sync missing subactivity --- app/Initialize/InitializeMembers.php | 21 +++++++++-- tests/Feature/Initialize/InitializeTest.php | 42 ++++++++++++++++++++- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/app/Initialize/InitializeMembers.php b/app/Initialize/InitializeMembers.php index 0a4ae70b..31fd0e62 100644 --- a/app/Initialize/InitializeMembers.php +++ b/app/Initialize/InitializeMembers.php @@ -100,9 +100,24 @@ class InitializeMembers { [$groupAll, $groupName, $groupId] = $groupMatches; $group = Group::create(['name' => $groupName, 'nami_id' => $groupId]); } - $subactivityId = $membership['entries_untergliederung'] === '' - ? null - : Subactivity::where('name', $membership['entries_untergliederung'])->firstOrFail()->id; + if ($membership['entries_untergliederung'] === '') { + $subactivityId = null; + } else if (!Subactivity::where('name', $membership['entries_untergliederung'])->exists()) { + try { + $singleMembership = $this->api->membership($member->id, $membership['id']); + } catch (RightException $e) { + continue; + } + app(ActivityCreator::class)->createFor($this->api, $singleMembership['gruppierungId']); + $subactivity = Subactivity::where('nami_id', $singleMembership['untergliederungId'])->firstOrFail(); + $subactivityId = $subactivity->id; + $group = Group::firstOrCreate(['nami_id' => $singleMembership['gruppierungId']], [ + 'nami_id' => $singleMembership['gruppierungId'], + 'name' => $singleMembership['gruppierung'], + ]); + } else { + $subactivityId = Subactivity::where('name', $membership['entries_untergliederung'])->first()->id; + } $activity = Activity::where('nami_id', (int) $activityMatches[1])->first(); if (!$activity) { try { diff --git a/tests/Feature/Initialize/InitializeTest.php b/tests/Feature/Initialize/InitializeTest.php index 2f9aa691..f16f8f42 100644 --- a/tests/Feature/Initialize/InitializeTest.php +++ b/tests/Feature/Initialize/InitializeTest.php @@ -164,7 +164,47 @@ class InitializeTest extends TestCase public function membershipDataProvider() { return [ - 'fetch_group_from_backend' => [ + 'fetch_subactivity_from_group' => [ + [ + 'gruppierung' => '::newgroup:: 22', + 'id' => 1077, + 'untergliederung' => '::newuntertaetigkeit::', + ], + function($db) { + $db->assertDatabaseHas('subactivities', ['name' => 'wö2', 'nami_id' => 3007]); + $db->assertDatabaseHas('activity_subactivity', [ + 'activity_id' => Activity::where('nami_id', 305)->firstOrFail()->id, + 'subactivity_id' => Subactivity::where('nami_id', 3007)->firstOrFail()->id, + ]); + $db->assertDatabaseHas('memberships', [ + 'activity_id' => Activity::where('nami_id', 305)->firstOrFail()->id, + 'subactivity_id' => Subactivity::where('nami_id', 3007)->firstOrFail()->id, + 'group_id' => Group::where('nami_id', 9056)->firstOrFail()->id, + 'nami_id' => 1077, + ]); + }, + function($backend) { + return $backend->fakeSingleMembership(116, 1077, [ + 'aktivVon' => '2021-08-22 00:00:00', + 'aktivBis' => '', + 'gruppierungId' => 9056, + 'gruppierung' => '::newgroup::', + 'id' => 1077, + 'taetigkeitId' => 305, // default + 'taetigkeit' => 'wö', + 'untergliederung' => '::newuntertaetigkeit::', + 'untergliederungId' => 3007, + ]) + ->fakeActivities(9056, [['name' => '€ leiter', 'id' => 305]]) + ->fakeSubactivities([ + 305 => [ + ['name' => 'wö', 'id' => 306], + ['name' => 'wö2', 'id' => 3007], + ] + ]); + } + ], + 'fetch_activity_from_group' => [ [ 'gruppierung' => '::newgroup:: 22', 'id' => 1077,