Fixed: Sync subgroups of subgroups
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
d719ebca23
commit
c17334d9a6
|
@ -19,9 +19,15 @@ class InitializeGroups
|
|||
$this->api->groups()->each(function ($group) {
|
||||
$parent = Group::updateOrCreate(['nami_id' => $group->id], ['nami_id' => $group->id, 'name' => $group->name]);
|
||||
|
||||
$this->api->subgroupsOf($group->id)->each(function ($subgroup) use ($parent) {
|
||||
Group::updateOrCreate(['nami_id' => $subgroup->id], ['nami_id' => $subgroup->id, 'name' => $subgroup->name, 'parent_id' => $parent->id]);
|
||||
});
|
||||
$this->syncChildren($group->id, $parent);
|
||||
});
|
||||
}
|
||||
|
||||
private function syncChildren(int $groupId, Group $parent): void
|
||||
{
|
||||
$this->api->subgroupsOf($groupId)->each(function ($subgroup) use ($parent) {
|
||||
$newParent = Group::updateOrCreate(['nami_id' => $subgroup->id], ['nami_id' => $subgroup->id, 'name' => $subgroup->name, 'parent_id' => $parent->id]);
|
||||
$this->syncChildren($subgroup->id, $newParent);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ class Initializer
|
|||
{
|
||||
public NamiSettings $settings;
|
||||
public array $initializers = [
|
||||
InitializeGroups::class,
|
||||
InitializeNationalities::class,
|
||||
InitializeFees::class,
|
||||
InitializeConfessions::class,
|
||||
|
|
|
@ -73,9 +73,13 @@ class InitializeGroupsTest extends TestCase
|
|||
$this->api->method('groups')->willReturn(
|
||||
collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')])
|
||||
);
|
||||
$this->api->method('subgroupsOf')->willReturn(
|
||||
collect([(new Group())->setParentId(150)->setId(200)->setName('subgroup')])
|
||||
);
|
||||
$this->api->method('subgroupsOf')->willReturnCallback(function ($groupId) {
|
||||
if (150 === $groupId) {
|
||||
return collect([(new Group())->setParentId(150)->setId(200)->setName('subgroup')]);
|
||||
}
|
||||
|
||||
return collect([]);
|
||||
});
|
||||
|
||||
(new InitializeGroups($this->api))->handle();
|
||||
|
||||
|
@ -85,15 +89,41 @@ class InitializeGroupsTest extends TestCase
|
|||
$this->assertEquals(150, $subgroup->parent->nami_id);
|
||||
}
|
||||
|
||||
public function testItSynchsSubgroupsOfSubgroups(): void
|
||||
{
|
||||
GroupModel::factory()->create(['nami_id' => 150]);
|
||||
$this->api->method('groups')->willReturn(
|
||||
collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')])
|
||||
);
|
||||
$this->api->method('subgroupsOf')->willReturnCallback(function ($groupId) {
|
||||
if (150 === $groupId) {
|
||||
return collect([(new Group())->setParentId(150)->setId(200)->setName('subgroup')]);
|
||||
}
|
||||
if (200 === $groupId) {
|
||||
return collect([(new Group())->setParentId(200)->setId(201)->setName('subsubgroup')]);
|
||||
}
|
||||
|
||||
return collect([]);
|
||||
});
|
||||
|
||||
(new InitializeGroups($this->api))->handle();
|
||||
|
||||
$this->assertDatabaseCount('groups', 3);
|
||||
}
|
||||
|
||||
public function testItAssignsIdAndParentToAnExistingSubgroup(): void
|
||||
{
|
||||
GroupModel::factory()->create(['nami_id' => 200]);
|
||||
$this->api->method('groups')->willReturn(
|
||||
collect([(new Group())->setParentId(null)->setId(150)->setName('root')])
|
||||
);
|
||||
$this->api->method('subgroupsOf')->willReturn(
|
||||
collect([(new Group())->setParentId(150)->setId(200)->setName('child')])
|
||||
);
|
||||
$this->api->method('subgroupsOf')->willReturnCallback(function ($groupId) {
|
||||
if (150 === $groupId) {
|
||||
return collect([(new Group())->setParentId(150)->setId(200)->setName('child')]);
|
||||
}
|
||||
|
||||
return collect([]);
|
||||
});
|
||||
|
||||
(new InitializeGroups($this->api))->handle();
|
||||
|
||||
|
|
Loading…
Reference in New Issue