lasthourcloud/app/Livewire/Notifications/Email.php

216 lines
6.8 KiB
PHP
Raw Normal View History

2023-05-25 16:27:52 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Notifications;
2023-05-25 16:27:52 +00:00
use App\Models\Team;
2023-07-28 08:55:26 +00:00
use App\Notifications\Test;
2024-06-10 20:43:34 +00:00
use Livewire\Component;
2023-05-25 16:27:52 +00:00
class Email extends Component
2023-05-25 16:27:52 +00:00
{
2023-08-31 13:00:59 +00:00
public Team $team;
2024-06-10 20:43:34 +00:00
2023-07-28 08:55:26 +00:00
public string $emails;
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
public bool $sharedEmailEnabled = false;
2023-05-25 16:27:52 +00:00
protected $rules = [
2023-08-31 13:00:59 +00:00
'team.smtp_enabled' => 'nullable|boolean',
'team.smtp_from_address' => 'required|email',
'team.smtp_from_name' => 'required',
'team.smtp_recipients' => 'nullable',
'team.smtp_host' => 'required',
'team.smtp_port' => 'required',
'team.smtp_encryption' => 'nullable',
'team.smtp_username' => 'nullable',
'team.smtp_password' => 'nullable',
'team.smtp_timeout' => 'nullable',
'team.smtp_notifications_test' => 'nullable|boolean',
'team.smtp_notifications_deployments' => 'nullable|boolean',
'team.smtp_notifications_status_changes' => 'nullable|boolean',
'team.smtp_notifications_database_backups' => 'nullable|boolean',
'team.smtp_notifications_scheduled_tasks' => 'nullable|boolean',
2023-08-31 13:00:59 +00:00
'team.use_instance_email_settings' => 'boolean',
'team.resend_enabled' => 'nullable|boolean',
'team.resend_api_key' => 'nullable',
2023-05-25 16:27:52 +00:00
];
2024-06-10 20:43:34 +00:00
2023-05-25 16:27:52 +00:00
protected $validationAttributes = [
2023-08-31 13:00:59 +00:00
'team.smtp_from_address' => 'From Address',
'team.smtp_from_name' => 'From Name',
'team.smtp_recipients' => 'Recipients',
'team.smtp_host' => 'Host',
'team.smtp_port' => 'Port',
'team.smtp_encryption' => 'Encryption',
'team.smtp_username' => 'Username',
'team.smtp_password' => 'Password',
'team.smtp_timeout' => 'Timeout',
'team.resend_enabled' => 'Resend Enabled',
'team.resend_api_key' => 'Resend API Key',
2023-05-25 16:27:52 +00:00
];
public function mount()
{
2023-09-06 12:31:38 +00:00
$this->team = auth()->user()->currentTeam();
2023-08-31 13:00:59 +00:00
['sharedEmailEnabled' => $this->sharedEmailEnabled] = $this->team->limits;
$this->emails = auth()->user()->email;
}
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
public function submitFromFields()
{
try {
$this->resetErrorBag();
$this->validate([
'team.smtp_from_address' => 'required|email',
'team.smtp_from_name' => 'required',
]);
$this->team->save();
2023-09-15 15:30:26 +00:00
refreshSession();
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Settings saved.');
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-08-31 13:00:59 +00:00
}
}
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
public function sendTestNotification()
{
2023-12-13 11:01:27 +00:00
$this->team?->notify(new Test($this->emails));
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Test Email sent.');
2023-08-31 13:00:59 +00:00
}
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
public function instantSaveInstance()
2023-06-20 19:18:14 +00:00
{
2023-08-31 13:00:59 +00:00
try {
2024-06-10 20:43:34 +00:00
if (! $this->sharedEmailEnabled) {
2023-08-31 13:00:59 +00:00
throw new \Exception('Not allowed to change settings. Please upgrade your subscription.');
2023-06-20 19:18:14 +00:00
}
2023-08-31 13:00:59 +00:00
$this->team->smtp_enabled = false;
$this->team->resend_enabled = false;
$this->team->save();
2023-09-15 15:30:26 +00:00
refreshSession();
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Settings saved.');
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-06-20 19:18:14 +00:00
}
}
2023-08-31 13:00:59 +00:00
public function instantSaveResend()
{
try {
$this->team->smtp_enabled = false;
$this->submitResend();
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
2023-08-31 13:00:59 +00:00
$this->team->smtp_enabled = false;
2024-06-10 20:43:34 +00:00
return handleError($e, $this);
2023-08-31 13:00:59 +00:00
}
}
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
public function instantSave()
{
try {
$this->team->resend_enabled = false;
$this->submit();
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
2023-08-31 13:00:59 +00:00
$this->team->smtp_enabled = false;
2024-06-10 20:43:34 +00:00
return handleError($e, $this);
2023-08-31 13:00:59 +00:00
}
}
2024-06-10 20:43:34 +00:00
2023-09-07 06:48:16 +00:00
public function saveModel()
{
$this->team->save();
2023-09-15 15:30:26 +00:00
refreshSession();
2023-12-07 18:06:32 +00:00
$this->dispatch('success', 'Settings saved.');
2023-09-07 06:48:16 +00:00
}
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
public function submit()
{
try {
$this->resetErrorBag();
2024-06-10 20:43:34 +00:00
if (! $this->team->use_instance_email_settings) {
2024-03-20 11:54:06 +00:00
$this->validate([
'team.smtp_from_address' => 'required|email',
'team.smtp_from_name' => 'required',
'team.smtp_host' => 'required',
'team.smtp_port' => 'required|numeric',
'team.smtp_encryption' => 'nullable',
'team.smtp_username' => 'nullable',
'team.smtp_password' => 'nullable',
'team.smtp_timeout' => 'nullable',
]);
}
2023-08-31 13:00:59 +00:00
$this->team->save();
2023-09-15 15:30:26 +00:00
refreshSession();
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Settings saved.');
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
2023-08-31 13:00:59 +00:00
$this->team->smtp_enabled = false;
2024-06-10 20:43:34 +00:00
return handleError($e, $this);
2023-08-31 13:00:59 +00:00
}
}
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
public function submitResend()
{
try {
$this->resetErrorBag();
$this->validate([
2023-09-15 09:28:44 +00:00
'team.smtp_from_address' => 'required|email',
'team.smtp_from_name' => 'required',
2024-06-10 20:43:34 +00:00
'team.resend_api_key' => 'required',
2023-08-31 13:00:59 +00:00
]);
$this->team->save();
2023-09-15 15:30:26 +00:00
refreshSession();
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Settings saved.');
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
2023-08-31 13:00:59 +00:00
$this->team->resend_enabled = false;
2024-06-10 20:43:34 +00:00
return handleError($e, $this);
2023-08-31 13:00:59 +00:00
}
}
2024-06-10 20:43:34 +00:00
2023-06-20 17:08:43 +00:00
public function copyFromInstanceSettings()
2023-06-07 15:24:37 +00:00
{
2024-07-12 13:45:36 +00:00
$settings = \App\Models\InstanceSettings::get();
if ($settings->smtp_enabled) {
2023-08-22 15:44:49 +00:00
$team = currentTeam();
2023-07-28 09:49:57 +00:00
$team->update([
'smtp_enabled' => $settings->smtp_enabled,
'smtp_from_address' => $settings->smtp_from_address,
'smtp_from_name' => $settings->smtp_from_name,
'smtp_recipients' => $settings->smtp_recipients,
'smtp_host' => $settings->smtp_host,
'smtp_port' => $settings->smtp_port,
'smtp_encryption' => $settings->smtp_encryption,
'smtp_username' => $settings->smtp_username,
'smtp_password' => $settings->smtp_password,
'smtp_timeout' => $settings->smtp_timeout,
]);
2023-08-31 13:00:59 +00:00
refreshSession();
$this->team = $team;
2023-12-07 18:06:32 +00:00
$this->dispatch('success', 'Settings saved.');
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
return;
2023-06-20 19:18:14 +00:00
}
2023-08-31 13:00:59 +00:00
if ($settings->resend_enabled) {
$team = currentTeam();
$team->update([
'resend_enabled' => $settings->resend_enabled,
'resend_api_key' => $settings->resend_api_key,
]);
2023-08-22 15:44:49 +00:00
refreshSession();
2023-08-31 13:00:59 +00:00
$this->team = $team;
2023-12-07 18:06:32 +00:00
$this->dispatch('success', 'Settings saved.');
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
return;
2023-05-25 16:27:52 +00:00
}
2023-12-07 18:06:32 +00:00
$this->dispatch('error', 'Instance SMTP/Resend settings are not enabled.');
2023-05-25 16:27:52 +00:00
}
2024-06-10 20:43:34 +00:00
public function render()
{
return view('livewire.notifications.email');
}
}