feat(integration): add requires_tenant_configuration field and update related logic and tests

This commit is contained in:
2026-07-22 09:23:58 -03:00
parent 03d8361e5f
commit 7e1ffbf417
9 changed files with 70 additions and 3 deletions

View File

@@ -13,10 +13,12 @@ class Integration extends Model
'name',
'url',
'integration_data_schema',
'requires_tenant_configuration',
];
protected $casts = [
'integration_data_schema' => 'array',
'requires_tenant_configuration' => 'boolean',
];
public function tenantIntegrations()

View File

@@ -18,6 +18,7 @@ class StoreIntegrationRequest extends FormRequest
'name' => ['required', 'string', 'max:255'],
'url' => ['nullable', 'url', 'max:255'],
'integration_data_schema' => ['nullable', 'array'],
'requires_tenant_configuration' => ['sometimes', 'boolean'],
];
}
}

View File

@@ -19,6 +19,7 @@ class UpdateIntegrationRequest extends FormRequest
'name' => ['sometimes', 'required', 'string', 'max:255'],
'url' => ['nullable', 'url', 'max:255'],
'integration_data_schema' => ['nullable', 'array'],
'requires_tenant_configuration' => ['sometimes', 'boolean'],
// the code shouldn't ideally be updatable, but if it is:
'integration_code' => ['sometimes', 'required', 'string', 'unique:integrations,integration_code,' . ($integration->id ?? '')],
];

View File

@@ -94,7 +94,7 @@ abstract class BaseIntegrationService
->where('integration_code', $this->integrationCode)
->first();
if (!$this->tenantIntegration) {
if (!$this->tenantIntegration && $this->integration->requires_tenant_configuration) {
throw new Exception("Tenant '{$this->tenantCode}' does not have integration '{$this->integrationCode}' configured.");
}
}

View File

@@ -29,6 +29,8 @@ class MailService extends BaseIntegrationService
private ?Tenant $tenant = null;
private bool $usesTenantMailer = false;
public function __construct(?MailFactory $mailFactory = null)
{
$this->mailFactory = $mailFactory ?? app(MailFactory::class);
@@ -41,7 +43,14 @@ class MailService extends BaseIntegrationService
$this->tenant = Tenant::query()
->where('codigo', $tenantCode)
->firstOrFail();
$this->mailer = $this->resolveMailer();
if ($this->tenantIntegration) {
$this->mailer = $this->resolveMailer();
$this->usesTenantMailer = true;
} else {
$this->mailer = $this->mailFactory->mailer();
$this->usesTenantMailer = false;
}
return $this;
}
@@ -82,7 +91,9 @@ class MailService extends BaseIntegrationService
public function mailerName(): string
{
return 'tenant-smtp';
return $this->usesTenantMailer
? 'tenant-smtp'
: (string) config('mail.default');
}
public function onSetup(): void