Skip to content

Commit

Permalink
Merge pull request #1411 from bakaphp/1.x
Browse files Browse the repository at this point in the history
1.x
  • Loading branch information
kaioken committed May 30, 2024
2 parents 3a0349e + f22bec7 commit 8f0278b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ public function sendNotificationBaseOnTemplate(mixed $root, array $request): boo
$userToNotify = UsersRepository::findUsersByArray($request['users'], $company);
}

$data = Str::isJson($request['data']) ? json_decode($request['data'], true) : (array) $request['data']; // This can have more validation like validate if is array o json
$data['app'] = app(Apps::class);

$notification = new Blank(
$request['template_name'],
Str::isJson($request['data']) ? json_decode($request['data'], true) : (array) $request['data'], // This can have more validation like validate if is array o json
$data,
$request['via'],
$user,
key_exists('attachment', $request) ? $request['attachment'] : null
Expand All @@ -52,14 +55,15 @@ public function anonymousNotification(mixed $root, array $request)
{
$data = Str::isJson($request['data']) ? json_decode($request['data'], true) : (array) $request['data'];
$data['app'] = app(Apps::class);
$user = auth()->user();

$notification = new Blank(
$request['template_name'],
$data,
['mail'],
auth()->user()
$user
);
$notification->setFromUser(auth()->user());
$notification->setFromUser($user);
$notification->setSubject($request['subject']);
Notification::route('mail', $request['email'])->notify($notification);

Expand Down
44 changes: 24 additions & 20 deletions src/Kanvas/Apps/Support/SmtpRuntimeConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Baka\Contracts\AppInterface;
use Baka\Contracts\CompanyInterface;
use Baka\Contracts\HashTableInterface;
use Illuminate\Support\Facades\Config;

class SmtpRuntimeConfiguration
{
Expand All @@ -24,31 +25,38 @@ public function __construct(
/**
* Load SMTP settings from the given source.
*/
protected function loadSmtpSettingsFromSource(string $provider, HashTableInterface $source): void
protected function loadSmtpSettingsFromSource(string $provider, HashTableInterface $source): string
{
config([
"mail.mailers.{$provider}.host" => $source->get('smtp_host'),
"mail.mailers.{$provider}.port" => $source->get('smtp_port'),
"mail.mailers.{$provider}.username" => $source->get('smtp_username'),
"mail.mailers.{$provider}.password" => $source->get('smtp_password'),
"mail.mailers.{$provider}.encryption" => $source->get('smtp_encryption') ?? 'tls'
]);
$config = [
'transport' => 'smtp',
'host' => $source->get('smtp_host'),
'port' => $source->get('smtp_port'),
'encryption' => $source->get('smtp_encryption') ?? 'tls',
'username' => $source->get('smtp_username'),
'password' => $source->get('smtp_password'),
'timeout' => null,
];

Config::set('mail.mailers.' . $provider, $config);

return $provider;
}


/**
* Load SMTP settings from the app.
*/
protected function loadAppSettings(): void
protected function loadAppSettings(): string
{
$this->loadSmtpSettingsFromSource($this->appSmtp, $this->app);
return $this->loadSmtpSettingsFromSource($this->appSmtp, $this->app);
}

/**
* Load SMTP settings from the company config.
*/
protected function loadCompanySettings(): void
protected function loadCompanySettings(): string
{
$this->loadSmtpSettingsFromSource($this->companySmtp, $this->company);
return $this->loadSmtpSettingsFromSource($this->companySmtp, $this->company);
}

/**
Expand All @@ -58,13 +66,9 @@ protected function loadCompanySettings(): void
public function loadSmtpSettings(): string
{
if ($this->company !== null && $this->company->get('smtp_host')) {
$this->loadCompanySettings();

return $this->companySmtp;
return $this->loadCompanySettings();
} elseif ($this->app->get('smtp_host')) {
$this->loadAppSettings();

return $this->appSmtp;
return $this->loadAppSettings();
}

return $this->defaultSmtp;
Expand All @@ -77,8 +81,8 @@ public function getFromEmail(): array
{
if ($this->company !== null && $this->company->get('from_email_address')) {
return [
'name' => $this->company->get('from_email_name'),
'address' => $this->company->get('from_email_address'),
'name' => $this->company->get('from_email_name') ?? config('mail.from.name'),
'address' => $this->company->get('from_email_address') ?? config('mail.from.address'),
];
}

Expand Down
1 change: 1 addition & 0 deletions src/Kanvas/Notifications/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Notification extends LaravelNotification implements EmailInterfaces, Shoul
protected ?UserInterface $fromUser = null;
protected ?UserInterface $toUser = null;
protected ?CompanyInterface $company = null;
public ?array $pathAttachment = null;

public array $channels = [
'mail',
Expand Down

0 comments on commit 8f0278b

Please sign in to comment.