mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-09-02 21:31:01 +00:00
refactor: adopt modular domain architecture (#747)
* refactor: stabilize model identities for domain migration * refactor: extract module platform context * refactor: assign models to domain contexts * refactor: extract ai platform context * refactor: extract storage platform context * refactor: extract mail platform context * refactor: extract pdf platform context * refactor: extract operations platform context * refactor: move installation into operations platform * refactor: extract money domain context * refactor: extract taxation domain context * refactor: extract catalog domain context * refactor: extract metadata domain context * refactor: extract reporting domain context * refactor: extract purchases domain context * refactor: extract receivables domain context * refactor: extract accounts domain context * refactor: complete reporting statement boundary * refactor: extract contacts domain context * refactor: extract sales domain context * refactor: remove legacy application layers * fix: migrate legacy bouncer role identities
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Platform\Modules\Http\Controllers\Admin;
|
||||
|
||||
use App\Platform\Http\Controller;
|
||||
use App\Platform\Modules\Marketplace\MarketplaceClient;
|
||||
use App\Platform\Modules\Models\MarketplaceCredential;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
|
||||
class MarketplacePairingController extends Controller
|
||||
{
|
||||
public function __construct(private MarketplaceClient $client) {}
|
||||
|
||||
public function start(): JsonResponse
|
||||
{
|
||||
$this->authorize('manage modules');
|
||||
$response = $this->client->beginPairing();
|
||||
$data = $response->json();
|
||||
if (! $response->successful() || ! is_array($data) || ! is_string($data['device_code'] ?? null)) {
|
||||
return response()->json(['error' => 'marketplace_unavailable'], 503);
|
||||
}
|
||||
|
||||
$ttl = max(60, (int) ($data['expires_in'] ?? 600));
|
||||
Cache::put($this->cacheKey(), $data['device_code'], now()->addSeconds($ttl));
|
||||
|
||||
return response()->json([
|
||||
'device_code' => $data['device_code'],
|
||||
'user_code' => $data['user_code'] ?? null,
|
||||
'verification_uri' => $data['verification_uri'] ?? $data['verification_uri_complete'] ?? null,
|
||||
'verification_uri_complete' => $data['verification_uri_complete'] ?? null,
|
||||
'expires_in' => $ttl,
|
||||
'interval' => max(1, (int) ($data['interval'] ?? 5)),
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function poll(): JsonResponse
|
||||
{
|
||||
$this->authorize('manage modules');
|
||||
$deviceCode = Cache::get($this->cacheKey());
|
||||
if (! is_string($deviceCode)) {
|
||||
return response()->json(['error' => 'pairing_expired'], 422);
|
||||
}
|
||||
|
||||
$response = $this->client->pollPairing($deviceCode);
|
||||
$data = $response->json();
|
||||
if ($response->status() === 428 || ($data['error'] ?? null) === 'authorization_pending') {
|
||||
return response()->json(['status' => 'pending']);
|
||||
}
|
||||
if (! $response->successful() || ! is_array($data) || ! is_string($data['installation_token'] ?? null)) {
|
||||
return response()->json(['error' => 'pairing_failed'], 422);
|
||||
}
|
||||
|
||||
MarketplaceCredential::query()->delete();
|
||||
MarketplaceCredential::query()->create([
|
||||
'credential' => Crypt::encryptString($data['installation_token']),
|
||||
'device_id' => is_scalar($data['installation']['id'] ?? null) ? (string) $data['installation']['id'] : null,
|
||||
'paired_at' => now(),
|
||||
]);
|
||||
Cache::forget($this->cacheKey());
|
||||
|
||||
return response()->json(['status' => 'paired']);
|
||||
}
|
||||
|
||||
public function status(): JsonResponse
|
||||
{
|
||||
$this->authorize('manage modules');
|
||||
$credential = MarketplaceCredential::query()->latest('id')->first();
|
||||
|
||||
return response()->json([
|
||||
'paired' => $credential !== null,
|
||||
'expired' => $credential?->expires_at?->isPast() ?? false,
|
||||
'paired_at' => $credential?->paired_at?->toIso8601String(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function disconnect(): JsonResponse
|
||||
{
|
||||
$this->authorize('manage modules');
|
||||
if (MarketplaceCredential::query()->exists()) {
|
||||
// Revocation releases any entitlement activation tied to this
|
||||
// installation. Local disconnect still succeeds if the control
|
||||
// plane is temporarily unavailable.
|
||||
$this->client->revokeInstallation();
|
||||
}
|
||||
MarketplaceCredential::query()->delete();
|
||||
Cache::forget($this->cacheKey());
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
private function cacheKey(): string
|
||||
{
|
||||
return 'marketplace.device-pairing';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Platform\Modules\Http\Controllers\Admin;
|
||||
|
||||
use App\Platform\Http\Controller;
|
||||
use App\Platform\Modules\Http\Requests\InstallMarketplaceModuleRequest;
|
||||
use App\Platform\Modules\Http\Requests\UninstallMarketplaceModuleRequest;
|
||||
use App\Platform\Modules\Marketplace\MarketplaceInstaller;
|
||||
use App\Platform\Modules\Marketplace\MarketplaceUninstaller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class ModuleInstallationController extends Controller
|
||||
{
|
||||
public function install(InstallMarketplaceModuleRequest $request, MarketplaceInstaller $installer): JsonResponse
|
||||
{
|
||||
$this->authorize('manage modules');
|
||||
|
||||
$response = $installer->install(
|
||||
$request->string('slug')->toString(),
|
||||
$request->string('version')->toString(),
|
||||
$request->string('channel')->toString() ?: (string) config('invoiceshelf.marketplace.channel', 'stable'),
|
||||
);
|
||||
|
||||
return response()->json($response, $response['success'] ? 200 : 422);
|
||||
}
|
||||
|
||||
public function uninstall(string $module, UninstallMarketplaceModuleRequest $request, MarketplaceUninstaller $uninstaller): JsonResponse
|
||||
{
|
||||
$this->authorize('manage modules');
|
||||
|
||||
$response = $uninstaller->uninstall(
|
||||
$module,
|
||||
$request->boolean('remove_data'),
|
||||
$request->string('confirmation')->toString() ?: null,
|
||||
);
|
||||
|
||||
$status = match ($response['error'] ?? null) {
|
||||
'module_not_installed' => 404,
|
||||
'operation_in_progress', 'module_runtime_missing', 'dependent_modules_installed' => 409,
|
||||
default => $response['success'] ? 200 : 422,
|
||||
};
|
||||
|
||||
return response()->json($response, $status);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Platform\Modules\Http\Controllers\Admin;
|
||||
|
||||
use App\Platform\Http\Controller;
|
||||
use App\Platform\Modules\Events\ModuleDisabledEvent;
|
||||
use App\Platform\Modules\Events\ModuleEnabledEvent;
|
||||
use App\Platform\Modules\Http\Resources\ModuleResource;
|
||||
use App\Platform\Modules\Marketplace\MarketplaceClient;
|
||||
use App\Platform\Modules\Models\Module as ModelsModule;
|
||||
use App\Platform\Modules\Runtime\DatabaseActivator;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Nwidart\Modules\Facades\Module;
|
||||
|
||||
class ModulesController extends Controller
|
||||
{
|
||||
public function index(MarketplaceClient $client)
|
||||
{
|
||||
$this->authorize('manage modules');
|
||||
|
||||
$response = $client->catalog();
|
||||
$body = $response->json();
|
||||
$modules = is_array($body) ? ($body['modules'] ?? $body['data'] ?? null) : null;
|
||||
|
||||
if (! $response->successful() || ! is_array($modules)) {
|
||||
return response()->json(['error' => 'marketplace_unavailable'], 503);
|
||||
}
|
||||
|
||||
return ModuleResource::collection(collect($modules));
|
||||
}
|
||||
|
||||
public function show(string $module, MarketplaceClient $client)
|
||||
{
|
||||
$this->authorize('manage modules');
|
||||
|
||||
$response = $client->module($module);
|
||||
$body = $response->json();
|
||||
|
||||
if ($response->status() === 404) {
|
||||
return response()->json(['error' => 'not_found'], 404);
|
||||
}
|
||||
|
||||
if (! $response->successful() || ! is_array($body) || ! is_array($body['module'] ?? null)) {
|
||||
return response()->json(['error' => 'marketplace_unavailable'], 503);
|
||||
}
|
||||
|
||||
return (new ModuleResource($body['module']))
|
||||
->additional(['meta' => [
|
||||
'modules' => ModuleResource::collection(
|
||||
collect($body['meta']['modules'] ?? [])
|
||||
),
|
||||
]]);
|
||||
}
|
||||
|
||||
public function enable(string $module): JsonResponse
|
||||
{
|
||||
$this->authorize('manage modules');
|
||||
|
||||
$module = ModelsModule::query()
|
||||
->where('name', $module)
|
||||
->where('installed', true)
|
||||
->firstOrFail();
|
||||
$installedModule = Module::find($module->name);
|
||||
|
||||
if ($installedModule === null) {
|
||||
$this->markRuntimeMissing($module);
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'error' => 'module_runtime_missing',
|
||||
], 409);
|
||||
}
|
||||
|
||||
$installedModule->enable();
|
||||
$module->refresh();
|
||||
|
||||
ModuleEnabledEvent::dispatch($module);
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
public function disable(string $module, DatabaseActivator $activator): JsonResponse
|
||||
{
|
||||
$this->authorize('manage modules');
|
||||
|
||||
$module = ModelsModule::query()
|
||||
->where('name', $module)
|
||||
->where('installed', true)
|
||||
->firstOrFail();
|
||||
|
||||
$activator->setActiveByName($module->name, false);
|
||||
|
||||
if (Module::find($module->name) === null) {
|
||||
$this->markRuntimeMissing($module);
|
||||
} else {
|
||||
$module->refresh();
|
||||
}
|
||||
|
||||
ModuleDisabledEvent::dispatch($module);
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
private function markRuntimeMissing(ModelsModule $module): void
|
||||
{
|
||||
$module->update([
|
||||
'installed' => false,
|
||||
'enabled' => false,
|
||||
'state' => 'failed',
|
||||
'last_error' => 'module_runtime_missing',
|
||||
'last_failed_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user