Skip to content

Commit

Permalink
Do not send email to inactive users, better logs when not loading ema…
Browse files Browse the repository at this point in the history
…ils count for inactive users

remp/helpdesk#503
  • Loading branch information
miroc committed Apr 21, 2021
1 parent b9ab6f2 commit fe4251e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Components/MailLogs/MailLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ public function render($userId)
}

$user = $this->usersRepository->find($userId);
if (!$user->active || $this->unclaimedUser->isUnclaimedUser($user)) {
$notLoaded = false;
if ($user->deleted_at !== null || $this->unclaimedUser->isUnclaimedUser($user)) {
$total = 0;
$notLoaded = true;
} else {
$total = $this->totalCount($userId);

Expand All @@ -99,6 +101,7 @@ public function render($userId)
}

$this->template->emails = $logs ?? [];
$this->template->notLoaded = $notLoaded;
$this->template->totals = [
'total' => $total,
'delivered' => $counts['delivered_at'] ?? 0,
Expand Down
9 changes: 7 additions & 2 deletions src/Components/MailLogs/mail_logs.latte
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{if $totals['total'] == 0}
<div class="bs-callout bs-callout-info">
<h4>{_remp_mailer.admin.mail_logs_component.no_content}</h4>
<p class="lead">{_remp_mailer.admin.mail_logs_component.no_content_message}.</p>
{if $notLoaded}
<h4>{_remp_mailer.admin.mail_logs_component.not_loaded}</h4>
<p class="lead">{_remp_mailer.admin.mail_logs_component.not_loaded_message}.</p>
{else}
<h4>{_remp_mailer.admin.mail_logs_component.no_content}</h4>
<p class="lead">{_remp_mailer.admin.mail_logs_component.no_content_message}.</p>
{/if}
</div>
{else}

Expand Down
13 changes: 12 additions & 1 deletion src/Hermes/SendEmailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Crm\RempMailerModule\Hermes;

use Crm\RempMailerModule\Models\Api\Client;
use Crm\UsersModule\Repository\UsersRepository;
use Nette\Utils\Validators;
use Tomaj\Hermes\Handler\HandlerInterface;
use Tomaj\Hermes\Handler\RetryTrait;
Expand All @@ -16,10 +17,14 @@ class SendEmailHandler implements HandlerInterface

private $apiClient;

private $usersRepository;

public function __construct(
Client $apiClient
Client $apiClient,
UsersRepository $usersRepository
) {
$this->apiClient = $apiClient;
$this->usersRepository = $usersRepository;
}

public function handle(MessageInterface $message): bool
Expand All @@ -31,6 +36,12 @@ public function handle(MessageInterface $message): bool
return false;
}

$user = $this->usersRepository->getByEmail($payload['email']);
if (!$user->active) {
Debugger::log('Attempt to send email through REMP Mailer with inactive email address: ' . $payload['email'], ILogger::WARNING);
return false;
}

$mailTemplate = $this->apiClient->getTemplate($payload['mail_template_code']);
if (!$mailTemplate) {
Debugger::log("Could not load mail template: record with code [{$payload['mail_template_code']}] doesn't exist");
Expand Down
2 changes: 2 additions & 0 deletions src/lang/remp_mailer.cs_CZ.neon
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ admin:
header: Odeslané e-maily
no_content: Žádné odeslané e-maily
no_content_message: Zatím nebyl odeslán žádný e-mail
not_loaded: Pro tento typ uživatele se emaily nenačítavají
not_loaded_message: Uživatel je anonymní nebo smazán
total: Celkem
success: Úspěšně
errors: Chybné
Expand Down
2 changes: 2 additions & 0 deletions src/lang/remp_mailer.en_US.neon
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ admin:
header: Sent emails
no_content: No sent emails
no_content_message: No messages have been sent yet
not_loaded: Emails are not loaded for this account type
not_loaded_message: User is unclaimed or deleted
total: Total
success: Success
errors: Errors
Expand Down
2 changes: 2 additions & 0 deletions src/lang/remp_mailer.sk_SK.neon
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ admin:
header: Odoslané e-maily
no_content: Žiadny odoslaný e-mail
no_content_message: Zatiaľ nebol odoslaný žiaden e-mail
not_loaded: Pre tento typ používateľa sa emaily nenačítavajú
not_loaded_message: Používateľ je anonymný alebo zmazaný
total: Celkovo
success: Úspešné
errors: Chybné
Expand Down

0 comments on commit fe4251e

Please sign in to comment.