refactor(vikon): fetch parts from VIKON API instead of hardcoded config

This commit is contained in:
F4ilji
2026-07-05 11:32:54 +05:00
parent 9a9521c570
commit 8cf195efb0
7 changed files with 36 additions and 41 deletions
@@ -49,10 +49,24 @@ class CheckAccessAction
];
}
// Extract parts per module from the API response
$partsByModule = [];
if (isset($body['tree_access']) && is_array($body['tree_access'])) {
foreach ($body['tree_access'] as $moduleId => $moduleData) {
if (isset($moduleData['parts']) && is_array($moduleData['parts'])) {
$partsByModule[$moduleId] = array_map(
fn($p) => ['id' => $p['id'], 'name' => $p['name'] ?? $p['id'], 'access' => $p['access'] ?? true],
$moduleData['parts']
);
}
}
}
return [
'has_access' => true,
'error' => null,
'modules_tree' => $body['tree_access'],
'parts' => $partsByModule,
];
} catch (\Throwable $e) {
Log::error('Vikon access check failed', ['error' => $e->getMessage()]);
@@ -24,11 +24,6 @@ class UpdatePartAction
{
$config = $this->modulesConfig[$moduleId] ?? throw new \RuntimeException("Unknown module: {$moduleId}");
$allowedParts = config('vikon.parts', [])[$moduleId] ?? [];
if (!in_array($part, $allowedParts, true)) {
throw new \RuntimeException("Invalid part '{$part}' for module {$moduleId}");
}
Log::info('Vikon: starting part update', ['module' => $moduleId, 'part' => $part]);
// Step 1: Request generation
@@ -51,21 +51,6 @@ class UpdatePartActionTest extends TestCase
$action->run(999, 'common', 'token');
}
public function test_rejects_invalid_part(): void
{
$http = Mockery::mock(HttpTask::class);
$fs = Mockery::mock(FilesystemTask::class);
$poll = Mockery::mock(PollPartStatusTask::class);
$action = new UpdatePartAction($http, $fs, $poll, $this->tempDir, $this->tempDir, [
1 => ['path' => 'sveden', 'allowed_folders' => ['common']],
]);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage("Invalid part 'nonexistent' for module 1");
$action->run(1, 'nonexistent', 'token');
}
public function test_requests_generation_and_polls_status(): void
{
$http = Mockery::mock(HttpTask::class);
@@ -117,8 +102,6 @@ class UpdatePartActionTest extends TestCase
->once()
->andReturn(true);
config(['vikon.parts' => [1 => ['common']]]);
$action = new UpdatePartAction($http, $fs, $poll, $this->tempDir, $this->tempDir, [
1 => ['path' => 'sveden', 'allowed_folders' => ['common']],
]);
@@ -141,8 +124,6 @@ class UpdatePartActionTest extends TestCase
'message' => 'Generation not available',
]));
config(['vikon.parts' => [1 => ['common']]]);
$action = new UpdatePartAction($http, $fs, $poll, $this->tempDir, $this->tempDir, [
1 => ['path' => 'sveden', 'allowed_folders' => ['common']],
]);
@@ -169,8 +150,6 @@ class UpdatePartActionTest extends TestCase
->once()
->andReturn(['status' => 'failed', 'error' => 'Server error']);
config(['vikon.parts' => [1 => ['common']]]);
$action = new UpdatePartAction($http, $fs, $poll, $this->tempDir, $this->tempDir, [
1 => ['path' => 'sveden', 'allowed_folders' => ['common']],
]);
@@ -36,12 +36,18 @@ class VikonController extends Controller
{
$token = Session::get('vikon_access_token');
$isAuth = $token ? $this->validateToken->run($token) : false;
$parts = [];
if ($isAuth && $token) {
$accessResult = $this->checkAccess->run($token);
$parts = $accessResult['parts'] ?? [];
}
return inertia()->render('Dashboard/VikonUpdates/Index', [
'is_authenticated' => $isAuth,
'current_version' => config('vikon.current_version'),
'modules' => config('vikon.modules'),
'parts' => config('vikon.parts'),
'parts' => $parts,
'vikon_api_domain' => config('vikon.api_domain'),
'vikon_client_id' => config('vikon.client_id'),
]);
@@ -70,12 +76,18 @@ class VikonController extends Controller
$token = Session::get('vikon_access_token');
$isAuth = $token ? $this->validateToken->run($token) : false;
$parts = [];
if ($isAuth && $token) {
$accessResult = $this->checkAccess->run($token);
$parts = $accessResult['parts'] ?? [];
}
return inertia()->render('Dashboard/VikonUpdates/Index', [
'is_authenticated' => $isAuth,
'current_version' => config('vikon.current_version'),
'modules' => config('vikon.modules'),
'parts' => config('vikon.parts'),
'parts' => $parts,
'vikon_api_domain' => config('vikon.api_domain'),
'vikon_client_id' => config('vikon.client_id'),
]);