feat: New email notification settings

This commit is contained in:
peaklabs-dev
2024-12-09 13:56:52 +01:00
parent 586a1a86f0
commit a6086ee38b
2 changed files with 262 additions and 115 deletions

View File

@@ -19,9 +19,6 @@ class Email extends Component
#[Validate(['boolean'])]
public bool $smtpEnabled = false;
#[Validate(['boolean'])]
public bool $useInstanceEmailSettings = false;
#[Validate(['nullable', 'email'])]
public ?string $smtpFromAddress = null;
@@ -50,29 +47,47 @@ class Email extends Component
public ?int $smtpTimeout = null;
#[Validate(['boolean'])]
public bool $smtpNotificationsTest = false;
#[Validate(['boolean'])]
public bool $smtpNotificationsDeployments = false;
#[Validate(['boolean'])]
public bool $smtpNotificationsStatusChanges = false;
#[Validate(['boolean'])]
public bool $smtpNotificationsDatabaseBackups = false;
#[Validate(['boolean'])]
public bool $smtpNotificationsScheduledTasks = false;
#[Validate(['boolean'])]
public bool $smtpNotificationsServerDiskUsage = false;
#[Validate(['boolean'])]
public bool $resendEnabled;
public bool $resendEnabled = false;
#[Validate(['nullable', 'string'])]
public ?string $resendApiKey = null;
#[Validate(['boolean'])]
public bool $useInstanceEmailSettings = false;
#[Validate(['boolean'])]
public bool $deploymentSuccessEmailNotifications = false;
#[Validate(['boolean'])]
public bool $deploymentFailureEmailNotifications = true;
#[Validate(['boolean'])]
public bool $statusChangeEmailNotifications = false;
#[Validate(['boolean'])]
public bool $backupSuccessEmailNotifications = false;
#[Validate(['boolean'])]
public bool $backupFailureEmailNotifications = true;
#[Validate(['boolean'])]
public bool $scheduledTaskSuccessEmailNotifications = false;
#[Validate(['boolean'])]
public bool $scheduledTaskFailureEmailNotifications = true;
#[Validate(['boolean'])]
public bool $dockerCleanupEmailNotifications = false;
#[Validate(['boolean'])]
public bool $serverDiskUsageEmailNotifications = true;
#[Validate(['boolean'])]
public bool $serverReachableEmailNotifications = false;
#[Validate(['boolean'])]
public bool $serverUnreachableEmailNotifications = true;
#[Validate(['nullable', 'email'])]
public ?string $testEmailAddress = null;
@@ -91,47 +106,67 @@ class Email extends Component
{
if ($toModel) {
$this->validate();
$this->team->smtp_enabled = $this->smtpEnabled;
$this->team->smtp_from_address = $this->smtpFromAddress;
$this->team->smtp_from_name = $this->smtpFromName;
$this->team->smtp_host = $this->smtpHost;
$this->team->smtp_port = $this->smtpPort;
$this->team->smtp_encryption = $this->smtpEncryption;
$this->team->smtp_username = $this->smtpUsername;
$this->team->smtp_password = $this->smtpPassword;
$this->team->smtp_timeout = $this->smtpTimeout;
$this->team->smtp_recipients = $this->smtpRecipients;
$this->team->smtp_notifications_test = $this->smtpNotificationsTest;
$this->team->smtp_notifications_deployments = $this->smtpNotificationsDeployments;
$this->team->smtp_notifications_status_changes = $this->smtpNotificationsStatusChanges;
$this->team->smtp_notifications_database_backups = $this->smtpNotificationsDatabaseBackups;
$this->team->smtp_notifications_scheduled_tasks = $this->smtpNotificationsScheduledTasks;
$this->team->smtp_notifications_server_disk_usage = $this->smtpNotificationsServerDiskUsage;
$this->team->use_instance_email_settings = $this->useInstanceEmailSettings;
$this->team->resend_enabled = $this->resendEnabled;
$this->team->resend_api_key = $this->resendApiKey;
$this->team->save();
$settings = $this->team->emailNotificationSettings;
$settings->smtp_enabled = $this->smtpEnabled;
$settings->smtp_from_address = $this->smtpFromAddress;
$settings->smtp_from_name = $this->smtpFromName;
$settings->smtp_recipients = $this->smtpRecipients;
$settings->smtp_host = $this->smtpHost;
$settings->smtp_port = $this->smtpPort;
$settings->smtp_encryption = $this->smtpEncryption;
$settings->smtp_username = $this->smtpUsername;
$settings->smtp_password = $this->smtpPassword;
$settings->smtp_timeout = $this->smtpTimeout;
$settings->resend_enabled = $this->resendEnabled;
$settings->resend_api_key = $this->resendApiKey;
$settings->use_instance_email_settings = $this->useInstanceEmailSettings;
$settings->deployment_success_email_notifications = $this->deploymentSuccessEmailNotifications;
$settings->deployment_failure_email_notifications = $this->deploymentFailureEmailNotifications;
$settings->status_change_email_notifications = $this->statusChangeEmailNotifications;
$settings->backup_success_email_notifications = $this->backupSuccessEmailNotifications;
$settings->backup_failure_email_notifications = $this->backupFailureEmailNotifications;
$settings->scheduled_task_success_email_notifications = $this->scheduledTaskSuccessEmailNotifications;
$settings->scheduled_task_failure_email_notifications = $this->scheduledTaskFailureEmailNotifications;
$settings->docker_cleanup_email_notifications = $this->dockerCleanupEmailNotifications;
$settings->server_disk_usage_email_notifications = $this->serverDiskUsageEmailNotifications;
$settings->server_reachable_email_notifications = $this->serverReachableEmailNotifications;
$settings->server_unreachable_email_notifications = $this->serverUnreachableEmailNotifications;
$settings->save();
refreshSession();
} else {
$this->smtpEnabled = $this->team->smtp_enabled;
$this->smtpFromAddress = $this->team->smtp_from_address;
$this->smtpFromName = $this->team->smtp_from_name;
$this->smtpHost = $this->team->smtp_host;
$this->smtpPort = $this->team->smtp_port;
$this->smtpEncryption = $this->team->smtp_encryption;
$this->smtpUsername = $this->team->smtp_username;
$this->smtpPassword = $this->team->smtp_password;
$this->smtpTimeout = $this->team->smtp_timeout;
$this->smtpRecipients = $this->team->smtp_recipients;
$this->smtpNotificationsTest = $this->team->smtp_notifications_test;
$this->smtpNotificationsDeployments = $this->team->smtp_notifications_deployments;
$this->smtpNotificationsStatusChanges = $this->team->smtp_notifications_status_changes;
$this->smtpNotificationsDatabaseBackups = $this->team->smtp_notifications_database_backups;
$this->smtpNotificationsScheduledTasks = $this->team->smtp_notifications_scheduled_tasks;
$this->smtpNotificationsServerDiskUsage = $this->team->smtp_notifications_server_disk_usage;
$this->useInstanceEmailSettings = $this->team->use_instance_email_settings;
$this->resendEnabled = $this->team->resend_enabled;
$this->resendApiKey = $this->team->resend_api_key;
$settings = $this->team->emailNotificationSettings;
$this->smtpEnabled = $settings->smtp_enabled;
$this->smtpFromAddress = $settings->smtp_from_address;
$this->smtpFromName = $settings->smtp_from_name;
$this->smtpRecipients = $settings->smtp_recipients;
$this->smtpHost = $settings->smtp_host;
$this->smtpPort = $settings->smtp_port;
$this->smtpEncryption = $settings->smtp_encryption;
$this->smtpUsername = $settings->smtp_username;
$this->smtpPassword = $settings->smtp_password;
$this->smtpTimeout = $settings->smtp_timeout;
$this->resendEnabled = $settings->resend_enabled;
$this->resendApiKey = $settings->resend_api_key;
$this->useInstanceEmailSettings = $settings->use_instance_email_settings;
$this->deploymentSuccessEmailNotifications = $settings->deployment_success_email_notifications;
$this->deploymentFailureEmailNotifications = $settings->deployment_failure_email_notifications;
$this->statusChangeEmailNotifications = $settings->status_change_email_notifications;
$this->backupSuccessEmailNotifications = $settings->backup_success_email_notifications;
$this->backupFailureEmailNotifications = $settings->backup_failure_email_notifications;
$this->scheduledTaskSuccessEmailNotifications = $settings->scheduled_task_success_email_notifications;
$this->scheduledTaskFailureEmailNotifications = $settings->scheduled_task_failure_email_notifications;
$this->dockerCleanupEmailNotifications = $settings->docker_cleanup_email_notifications;
$this->serverDiskUsageEmailNotifications = $settings->server_disk_usage_email_notifications;
$this->serverReachableEmailNotifications = $settings->server_reachable_email_notifications;
$this->serverUnreachableEmailNotifications = $settings->server_unreachable_email_notifications;
}
}
@@ -178,13 +213,26 @@ class Email extends Component
{
try {
$this->validate([
'smtpHost' => 'required',
'smtpEnabled' => 'boolean',
'smtpFromAddress' => 'required|email',
'smtpFromName' => 'required|string',
'smtpHost' => 'required|string',
'smtpPort' => 'required|numeric',
'smtpEncryption' => 'required|string|in:tls,ssl,none',
'smtpUsername' => 'nullable|string',
'smtpPassword' => 'nullable|string',
'smtpTimeout' => 'nullable|numeric',
], [
'smtpFromAddress.required' => 'From Address is required.',
'smtpFromAddress.email' => 'Please enter a valid email address.',
'smtpFromName.required' => 'From Name is required.',
'smtpHost.required' => 'SMTP Host is required.',
'smtpPort.required' => 'SMTP Port is required.',
'smtpPort.numeric' => 'SMTP Port must be a number.',
'smtpEncryption.required' => 'Encryption type is required.',
]);
$this->resendEnabled = false;
$this->useInstanceEmailSettings = false;
$this->saveModel();
} catch (\Throwable $e) {
$this->smtpEnabled = false;
@@ -197,11 +245,18 @@ class Email extends Component
{
try {
$this->validate([
'resendApiKey' => 'required',
'resendEnabled' => 'boolean',
'resendApiKey' => 'required|string',
'smtpFromAddress' => 'required|email',
'smtpFromName' => 'required|string',
], [
'resendApiKey.required' => 'Resend API Key is required.',
'smtpFromAddress.required' => 'From Address is required.',
'smtpFromAddress.email' => 'Please enter a valid email address.',
'smtpFromName.required' => 'From Name is required.',
]);
$this->smtpEnabled = false;
$this->useInstanceEmailSettings = false;
$this->saveModel();
} catch (\Throwable $e) {
$this->resendEnabled = false;
@@ -258,6 +313,81 @@ class Email extends Component
$this->dispatch('error', 'Instance SMTP/Resend settings are not enabled.');
}
public function submitSmtp()
{
try {
$this->resetErrorBag();
$this->validate([
'smtpEnabled' => 'boolean',
'smtpFromAddress' => 'required|email',
'smtpFromName' => 'required|string',
'smtpHost' => 'required|string',
'smtpPort' => 'required|numeric',
'smtpEncryption' => 'required|string|in:tls,ssl,none',
'smtpUsername' => 'nullable|string',
'smtpPassword' => 'nullable|string',
'smtpTimeout' => 'nullable|numeric',
], [
'smtpFromAddress.required' => 'From Address is required.',
'smtpFromAddress.email' => 'Please enter a valid email address.',
'smtpFromName.required' => 'From Name is required.',
'smtpHost.required' => 'SMTP Host is required.',
'smtpPort.required' => 'SMTP Port is required.',
'smtpPort.numeric' => 'SMTP Port must be a number.',
'smtpEncryption.required' => 'Encryption type is required.',
]);
$settings = $this->team->emailNotificationSettings;
$settings->smtp_enabled = $this->smtpEnabled;
$settings->smtp_from_address = $this->smtpFromAddress;
$settings->smtp_from_name = $this->smtpFromName;
$settings->smtp_host = $this->smtpHost;
$settings->smtp_port = $this->smtpPort;
$settings->smtp_encryption = $this->smtpEncryption;
$settings->smtp_username = $this->smtpUsername;
$settings->smtp_password = $this->smtpPassword;
$settings->smtp_timeout = $this->smtpTimeout;
$settings->save();
refreshSession();
$this->dispatch('success', 'SMTP settings saved.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function submitResend()
{
try {
$this->resetErrorBag();
$this->validate([
'resendEnabled' => 'boolean',
'resendApiKey' => 'required|string',
'smtpFromAddress' => 'required|email',
'smtpFromName' => 'required|string',
], [
'resendApiKey.required' => 'Resend API Key is required.',
'smtpFromAddress.required' => 'From Address is required.',
'smtpFromAddress.email' => 'Please enter a valid email address.',
'smtpFromName.required' => 'From Name is required.',
]);
$settings = $this->team->emailNotificationSettings;
$settings->resend_enabled = $this->resendEnabled;
$settings->resend_api_key = $this->resendApiKey;
$settings->smtp_from_address = $this->smtpFromAddress;
$settings->smtp_from_name = $this->smtpFromName;
$settings->save();
refreshSession();
$this->dispatch('success', 'Resend settings saved.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function render()
{
return view('livewire.notifications.email');