- Added client_id field to StoreTenantRequest and UpdateTenantRequest for tenant management. - Updated TenantResource to include client_id in the response. - Created migration to establish clients table and link tenants to clients. - Migrated existing tenant integrations to client_integrations table. - Grouped specific tenants under a shared client (OnTicket) in a new migration. - Updated seeders to reflect new client structure and relationships. - Adjusted integration configurations to require client instead of tenant. - Added tests for client integration functionality and ensured existing tests reflect the new client structure.
56 lines
1.8 KiB
PHP
56 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Integration\Services;
|
|
|
|
use App\Domains\Client\Models\Client;
|
|
use App\Domains\Integration\Models\ClientIntegration;
|
|
use App\Domains\Integration\Models\Integration;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ClientIntegrationService
|
|
{
|
|
public function getClientIntegration(Client $client, string $integrationCode): ?ClientIntegration
|
|
{
|
|
return $client->integrations()
|
|
->where('integration_code', $integrationCode)
|
|
->first();
|
|
}
|
|
|
|
/** @return Collection<int, ClientIntegration> */
|
|
public function getAllForClient(Client $client): Collection
|
|
{
|
|
return $client->integrations()->with('integration')->get();
|
|
}
|
|
|
|
public function updateOrCreateIntegration(
|
|
Client $client,
|
|
Integration $integration,
|
|
array $data,
|
|
): ClientIntegration {
|
|
return DB::transaction(function () use ($client, $integration, $data): ClientIntegration {
|
|
$clientIntegration = ClientIntegration::query()->updateOrCreate(
|
|
[
|
|
'client_id' => $client->id,
|
|
'integration_code' => $integration->integration_code,
|
|
],
|
|
['integration_data' => $data],
|
|
);
|
|
|
|
$service = $this->resolveService($integration->integration_code);
|
|
$service?->forClient($client)->onSetup();
|
|
|
|
return $clientIntegration;
|
|
});
|
|
}
|
|
|
|
protected function resolveService(string $integrationCode): ?BaseIntegrationService
|
|
{
|
|
return match ($integrationCode) {
|
|
'email' => new MailService,
|
|
'telepagos', 'telepagos_homo' => new TelepagosIntegrationService($integrationCode),
|
|
default => null,
|
|
};
|
|
}
|
|
}
|