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'])] #[Validate(['boolean'])]
public bool $smtpEnabled = false; public bool $smtpEnabled = false;
#[Validate(['boolean'])]
public bool $useInstanceEmailSettings = false;
#[Validate(['nullable', 'email'])] #[Validate(['nullable', 'email'])]
public ?string $smtpFromAddress = null; public ?string $smtpFromAddress = null;
@@ -50,29 +47,47 @@ class Email extends Component
public ?int $smtpTimeout = null; public ?int $smtpTimeout = null;
#[Validate(['boolean'])] #[Validate(['boolean'])]
public bool $smtpNotificationsTest = false; public bool $resendEnabled = 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;
#[Validate(['nullable', 'string'])] #[Validate(['nullable', 'string'])]
public ?string $resendApiKey = null; 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'])] #[Validate(['nullable', 'email'])]
public ?string $testEmailAddress = null; public ?string $testEmailAddress = null;
@@ -91,47 +106,67 @@ class Email extends Component
{ {
if ($toModel) { if ($toModel) {
$this->validate(); $this->validate();
$this->team->smtp_enabled = $this->smtpEnabled; $settings = $this->team->emailNotificationSettings;
$this->team->smtp_from_address = $this->smtpFromAddress; $settings->smtp_enabled = $this->smtpEnabled;
$this->team->smtp_from_name = $this->smtpFromName; $settings->smtp_from_address = $this->smtpFromAddress;
$this->team->smtp_host = $this->smtpHost; $settings->smtp_from_name = $this->smtpFromName;
$this->team->smtp_port = $this->smtpPort; $settings->smtp_recipients = $this->smtpRecipients;
$this->team->smtp_encryption = $this->smtpEncryption; $settings->smtp_host = $this->smtpHost;
$this->team->smtp_username = $this->smtpUsername; $settings->smtp_port = $this->smtpPort;
$this->team->smtp_password = $this->smtpPassword; $settings->smtp_encryption = $this->smtpEncryption;
$this->team->smtp_timeout = $this->smtpTimeout; $settings->smtp_username = $this->smtpUsername;
$this->team->smtp_recipients = $this->smtpRecipients; $settings->smtp_password = $this->smtpPassword;
$this->team->smtp_notifications_test = $this->smtpNotificationsTest; $settings->smtp_timeout = $this->smtpTimeout;
$this->team->smtp_notifications_deployments = $this->smtpNotificationsDeployments;
$this->team->smtp_notifications_status_changes = $this->smtpNotificationsStatusChanges; $settings->resend_enabled = $this->resendEnabled;
$this->team->smtp_notifications_database_backups = $this->smtpNotificationsDatabaseBackups; $settings->resend_api_key = $this->resendApiKey;
$this->team->smtp_notifications_scheduled_tasks = $this->smtpNotificationsScheduledTasks;
$this->team->smtp_notifications_server_disk_usage = $this->smtpNotificationsServerDiskUsage; $settings->use_instance_email_settings = $this->useInstanceEmailSettings;
$this->team->use_instance_email_settings = $this->useInstanceEmailSettings;
$this->team->resend_enabled = $this->resendEnabled; $settings->deployment_success_email_notifications = $this->deploymentSuccessEmailNotifications;
$this->team->resend_api_key = $this->resendApiKey; $settings->deployment_failure_email_notifications = $this->deploymentFailureEmailNotifications;
$this->team->save(); $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(); refreshSession();
} else { } else {
$this->smtpEnabled = $this->team->smtp_enabled; $settings = $this->team->emailNotificationSettings;
$this->smtpFromAddress = $this->team->smtp_from_address;
$this->smtpFromName = $this->team->smtp_from_name; $this->smtpEnabled = $settings->smtp_enabled;
$this->smtpHost = $this->team->smtp_host; $this->smtpFromAddress = $settings->smtp_from_address;
$this->smtpPort = $this->team->smtp_port; $this->smtpFromName = $settings->smtp_from_name;
$this->smtpEncryption = $this->team->smtp_encryption; $this->smtpRecipients = $settings->smtp_recipients;
$this->smtpUsername = $this->team->smtp_username; $this->smtpHost = $settings->smtp_host;
$this->smtpPassword = $this->team->smtp_password; $this->smtpPort = $settings->smtp_port;
$this->smtpTimeout = $this->team->smtp_timeout; $this->smtpEncryption = $settings->smtp_encryption;
$this->smtpRecipients = $this->team->smtp_recipients; $this->smtpUsername = $settings->smtp_username;
$this->smtpNotificationsTest = $this->team->smtp_notifications_test; $this->smtpPassword = $settings->smtp_password;
$this->smtpNotificationsDeployments = $this->team->smtp_notifications_deployments; $this->smtpTimeout = $settings->smtp_timeout;
$this->smtpNotificationsStatusChanges = $this->team->smtp_notifications_status_changes;
$this->smtpNotificationsDatabaseBackups = $this->team->smtp_notifications_database_backups; $this->resendEnabled = $settings->resend_enabled;
$this->smtpNotificationsScheduledTasks = $this->team->smtp_notifications_scheduled_tasks; $this->resendApiKey = $settings->resend_api_key;
$this->smtpNotificationsServerDiskUsage = $this->team->smtp_notifications_server_disk_usage;
$this->useInstanceEmailSettings = $this->team->use_instance_email_settings; $this->useInstanceEmailSettings = $settings->use_instance_email_settings;
$this->resendEnabled = $this->team->resend_enabled;
$this->resendApiKey = $this->team->resend_api_key; $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 { try {
$this->validate([ $this->validate([
'smtpHost' => 'required', 'smtpEnabled' => 'boolean',
'smtpFromAddress' => 'required|email',
'smtpFromName' => 'required|string',
'smtpHost' => 'required|string',
'smtpPort' => 'required|numeric', '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.', 'smtpHost.required' => 'SMTP Host is required.',
'smtpPort.required' => 'SMTP Port 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->resendEnabled = false;
$this->useInstanceEmailSettings = false;
$this->saveModel(); $this->saveModel();
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->smtpEnabled = false; $this->smtpEnabled = false;
@@ -197,11 +245,18 @@ class Email extends Component
{ {
try { try {
$this->validate([ $this->validate([
'resendApiKey' => 'required', 'resendEnabled' => 'boolean',
'resendApiKey' => 'required|string',
'smtpFromAddress' => 'required|email',
'smtpFromName' => 'required|string',
], [ ], [
'resendApiKey.required' => 'Resend API Key is required.', '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->smtpEnabled = false;
$this->useInstanceEmailSettings = false;
$this->saveModel(); $this->saveModel();
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->resendEnabled = false; $this->resendEnabled = false;
@@ -258,6 +313,81 @@ class Email extends Component
$this->dispatch('error', 'Instance SMTP/Resend settings are not enabled.'); $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() public function render()
{ {
return view('livewire.notifications.email'); return view('livewire.notifications.email');

View File

@@ -14,11 +14,10 @@
Copy from Instance Settings Copy from Instance Settings
</x-forms.button> </x-forms.button>
@endif @endif
@if (isEmailEnabled($team) && auth()->user()->isAdminFromSession() && isTestEmailEnabled($team)) @if (isEmailEnabled($team) && auth()->user()->isAdminFromSession())
<x-modal-input buttonTitle="Send Test Email" title="Send Test Email"> <x-modal-input buttonTitle="Send Test Email" title="Send Test Email">
<form wire:submit.prevent="sendTestEmail" class="flex flex-col w-full gap-2"> <form wire:submit.prevent="sendTestEmail" class="flex flex-col w-full gap-2">
<x-forms.input wire:model="testEmailAddress" placeholder="test@example.com" id="testEmailAddress" <x-forms.input wire:model="testEmailAddress" placeholder="test@example.com" id="testEmailAddress" label="Recipients" required />
label="Recipients" required />
<x-forms.button type="submit" @click="modalOpen=false"> <x-forms.button type="submit" @click="modalOpen=false">
Send Email Send Email
</x-forms.button> </x-forms.button>
@@ -28,27 +27,24 @@
</div> </div>
@if (!isCloud()) @if (!isCloud())
<div class="w-96"> <div class="w-96">
<x-forms.checkbox instantSave="instantSaveInstance" id="useInstanceEmailSettings" <x-forms.checkbox instantSave="instantSaveInstance" id="useInstanceEmailSettings" label="Use system wide (transactional) email settings" />
label="Use system wide (transactional) email settings" />
</div> </div>
@endif @endif
@if (!$useInstanceEmailSettings) @if (!$useInstanceEmailSettings)
<div class="flex gap-4"> <div class="flex gap-4">
<x-forms.input required id="smtpFromName" helper="Name used in emails." label="From Name" /> <x-forms.input required id="smtpFromName" helper="Name used in emails." label="From Name" />
<x-forms.input required id="smtpFromAddress" helper="Email address used in emails." <x-forms.input required id="smtpFromAddress" helper="Email address used in emails." label="From Address" />
label="From Address" />
</div> </div>
@endif @endif
</form> </form>
@if (isCloud()) @if (isCloud())
<div class="w-64 py-4"> <div class="w-64 py-4">
<x-forms.checkbox instantSave="instantSaveInstance" id="useInstanceEmailSettings" <x-forms.checkbox instantSave="instantSaveInstance" id="useInstanceEmailSettings" label="Use Hosted Email Service" />
label="Use Hosted Email Service" />
</div> </div>
@endif @endif
@if (!$useInstanceEmailSettings) @if (!$useInstanceEmailSettings)
<div class="flex flex-col gap-4"> <div class="flex flex-col gap-4">
<form wire:submit='submit' class="p-4 border dark:border-coolgray-300 flex flex-col gap-2"> <form wire:submit='submitSmtp' class="p-4 border dark:border-coolgray-300 flex flex-col gap-2">
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<h3>SMTP Server</h3> <h3>SMTP Server</h3>
<x-forms.button type="submit"> <x-forms.button type="submit">
@@ -72,13 +68,12 @@
<div class="flex flex-col w-full gap-2 xl:flex-row"> <div class="flex flex-col w-full gap-2 xl:flex-row">
<x-forms.input id="smtpUsername" label="SMTP Username" /> <x-forms.input id="smtpUsername" label="SMTP Username" />
<x-forms.input id="smtpPassword" type="password" label="SMTP Password" /> <x-forms.input id="smtpPassword" type="password" label="SMTP Password" />
<x-forms.input id="smtpTimeout" helper="Timeout value for sending emails." <x-forms.input id="smtpTimeout" helper="Timeout value for sending emails." label="Timeout" />
label="Timeout" />
</div> </div>
</div> </div>
</div> </div>
</form> </form>
<form wire:submit='submit' class="p-4 border dark:border-coolgray-300 flex flex-col gap-2"> <form wire:submit='submitResend' class="p-4 border dark:border-coolgray-300 flex flex-col gap-2">
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<h3>Resend</h3> <h3>Resend</h3>
<x-forms.button type="submit"> <x-forms.button type="submit">
@@ -91,8 +86,7 @@
<div class="flex flex-col"> <div class="flex flex-col">
<div class="flex flex-col gap-4"> <div class="flex flex-col gap-4">
<div class="flex flex-col w-full gap-2 xl:flex-row"> <div class="flex flex-col w-full gap-2 xl:flex-row">
<x-forms.input required type="password" id="resendApiKey" placeholder="API key" <x-forms.input required type="password" id="resendApiKey" placeholder="API key" label="API Key" />
label="API Key" />
</div> </div>
</div> </div>
</div> </div>
@@ -100,19 +94,42 @@
</div> </div>
@endif @endif
@if (isEmailEnabled($team) || $useInstanceEmailSettings) @if (isEmailEnabled($team) || $useInstanceEmailSettings)
<h2 class="mt-4">Subscribe to events</h2> <h2 class="mt-8 mb-4">Notification Settings</h2>
<div class="w-64"> <p class="mb-4">
@if (isDev()) Select events for which you would like to receive email notifications.
<x-forms.checkbox instantSave="saveModel" id="smtpNotificationsTest" label="Test" /> </p>
@endif <div class="flex flex-col gap-4 max-w-2xl">
<x-forms.checkbox instantSave="saveModel" id="smtpNotificationsStatusChanges" <div class="border dark:border-coolgray-300 p-4 rounded-lg">
label="Container Status Changes" /> <h3 class="font-medium mb-3">Deployments</h3>
<x-forms.checkbox instantSave="saveModel" id="smtpNotificationsDeployments" <div class="flex flex-col gap-1.5 pl-1">
label="Application Deployments" /> <x-forms.checkbox instantSave="saveModel" id="deploymentSuccessEmailNotifications" label="Deployment Success" />
<x-forms.checkbox instantSave="saveModel" id="smtpNotificationsDatabaseBackups" label="Backup Status" /> <x-forms.checkbox instantSave="saveModel" id="deploymentFailureEmailNotifications" label="Deployment Failure" />
<x-forms.checkbox instantSave="saveModel" id="smtpNotificationsScheduledTasks" <x-forms.checkbox instantSave="saveModel" helper="Send an email when a container status changes. It will send and email for Stopped and Restarted events of a container." id="statusChangeEmailNotifications" label="Container Status Changes" />
label="Scheduled Tasks Status" /> </div>
<x-forms.checkbox instantSave="saveModel" id="smtpNotificationsServerDiskUsage" label="Server Disk Usage" /> </div>
<div class="border dark:border-coolgray-300 p-4 rounded-lg">
<h3 class="font-medium mb-3">Backups</h3>
<div class="flex flex-col gap-1.5 pl-1">
<x-forms.checkbox instantSave="saveModel" id="backupSuccessEmailNotifications" label="Backup Success" />
<x-forms.checkbox instantSave="saveModel" id="backupFailureEmailNotifications" label="Backup Failure" />
</div>
</div>
<div class="border dark:border-coolgray-300 p-4 rounded-lg">
<h3 class="font-medium mb-3">Scheduled Tasks</h3>
<div class="flex flex-col gap-1.5 pl-1">
<x-forms.checkbox instantSave="saveModel" id="scheduledTaskSuccessEmailNotifications" label="Scheduled Task Success" />
<x-forms.checkbox instantSave="saveModel" id="scheduledTaskFailureEmailNotifications" label="Scheduled Task Failure" />
</div>
</div>
<div class="border dark:border-coolgray-300 p-4 rounded-lg">
<h3 class="font-medium mb-3">Server</h3>
<div class="flex flex-col gap-1.5 pl-1">
<x-forms.checkbox instantSave="saveModel" helper="Send an email when Docker Cleanup is run on a server." id="dockerCleanupEmailNotifications" label="Docker Cleanup" />
<x-forms.checkbox instantSave="saveModel" helper="Send an email when server disk usage is high." id="serverDiskUsageEmailNotifications" label="Server Disk Usage" />
<x-forms.checkbox instantSave="saveModel" id="serverReachableEmailNotifications" label="Server Reachable" />
<x-forms.checkbox instantSave="saveModel" id="serverUnreachableEmailNotifications" label="Server Unreachable" />
</div>
</div>
</div> </div>
@endif @endif
</div> </div>