refactor(integration): resolver credenciales mediante instancias

Resuelve primero la instancia del cliente y luego la del tipo de sitio cuando existe contexto de tenant. Adapta SMTP y Telepagos, con tokens identificados por instancia y version de credenciales.

Separa la edicion de instancias de sus asociaciones y conserva el guardado por cliente creando una instancia nueva. Impide eliminar instancias en uso y adapta las pruebas existentes, incluida la herencia SMTP.
This commit is contained in:
2026-09-04 12:47:47 -03:00
parent e4146288f1
commit c0057c237a
12 changed files with 190 additions and 54 deletions

View File

@@ -29,7 +29,7 @@ class MailService extends BaseIntegrationService
private ?Mailer $mailer = null;
private bool $usesClientMailer = false;
private bool $usesInstanceMailer = false;
public function __construct(?MailFactory $mailFactory = null)
{
@@ -40,12 +40,12 @@ class MailService extends BaseIntegrationService
{
parent::forTenant($tenantCode);
if ($this->clientIntegration) {
if ($this->integrationInstance) {
$this->mailer = $this->resolveMailer();
$this->usesClientMailer = true;
$this->usesInstanceMailer = true;
} else {
$this->mailer = $this->mailFactory->mailer();
$this->usesClientMailer = false;
$this->usesInstanceMailer = false;
}
return $this;
@@ -56,12 +56,12 @@ class MailService extends BaseIntegrationService
parent::forClient($client);
$this->tenant = $this->clientContext?->tenants()->first();
if ($this->clientIntegration) {
if ($this->integrationInstance) {
$this->mailer = $this->resolveMailer();
$this->usesClientMailer = true;
$this->usesInstanceMailer = true;
} else {
$this->mailer = $this->mailFactory->mailer();
$this->usesClientMailer = false;
$this->usesInstanceMailer = false;
}
return $this;
@@ -122,8 +122,8 @@ class MailService extends BaseIntegrationService
public function mailerName(): string
{
return $this->usesClientMailer
? 'client-smtp'
return $this->usesInstanceMailer
? 'integration-smtp'
: (string) config('mail.default');
}
@@ -187,7 +187,7 @@ class MailService extends BaseIntegrationService
private function resolveMailer(): Mailer
{
$data = $this->clientIntegration?->integration_data;
$data = $this->integrationInstance?->integration_data;
if (! is_array($data)) {
throw new InvalidArgumentException('La configuración SMTP del cliente no es válida.');
@@ -205,7 +205,7 @@ class MailService extends BaseIntegrationService
}
$mailer = $this->mailFactory->build([
'name' => 'client-smtp-'.$this->clientContext?->id,
'name' => 'integration-smtp-'.$this->integrationInstance?->id,
'transport' => 'smtp',
'scheme' => $data['MAIL_SCHEME'] ?? null,
'host' => $data['MAIL_HOST'],