Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fronta e-mailů 2 #1045

Merged
merged 23 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\AdminModule\ConfigurationModule\Forms;

use App\AdminModule\Forms\BaseFormFactory;
use App\Model\Mailing\Commands\CreateTemplateMail;
use App\Model\Mailing\Template;
use App\Model\Mailing\TemplateVariable;
use App\Model\Settings\Commands\SetSettingArrayValue;
Expand All @@ -15,7 +16,6 @@
use App\Model\Settings\Queries\SettingStringValueQuery;
use App\Model\Settings\Settings;
use App\Services\CommandBus;
use App\Services\IMailService;
use App\Services\QueryBus;
use App\Utils\Validators;
use Doctrine\Common\Collections\ArrayCollection;
Expand Down Expand Up @@ -48,7 +48,6 @@ public function __construct(
private readonly BaseFormFactory $baseFormFactory,
private readonly CommandBus $commandBus,
private readonly QueryBus $queryBus,
private readonly IMailService $mailService,
private readonly LinkGenerator $linkGenerator,
private readonly Validators $validators,
) {
Expand Down Expand Up @@ -107,15 +106,15 @@ public function processForm(Form $form, stdClass $values): void

$link = $this->linkGenerator->link('Api:Mail:verify', ['code' => $verificationCode]);

$this->mailService->sendMailFromTemplate(
$this->commandBus->handle(new CreateTemplateMail(
null,
new ArrayCollection([$values->seminarEmail]),
Template::EMAIL_VERIFICATION,
[
TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)),
TemplateVariable::EMAIL_VERIFICATION_LINK => $link,
],
);
));
}

$contactFormRecipients = array_map(
Expand Down
9 changes: 5 additions & 4 deletions app/AdminModule/Forms/EditUserSeminarFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use App\Model\CustomInput\CustomTextValue;
use App\Model\CustomInput\Repositories\CustomInputRepository;
use App\Model\CustomInput\Repositories\CustomInputValueRepository;
use App\Model\Mailing\Commands\CreateTemplateMail;
use App\Model\Mailing\Template;
use App\Model\Mailing\TemplateVariable;
use App\Model\Settings\Queries\SettingStringValueQuery;
Expand All @@ -30,8 +31,8 @@
use App\Model\User\User;
use App\Services\AclService;
use App\Services\ApplicationService;
use App\Services\CommandBus;
use App\Services\FilesService;
use App\Services\IMailService;
use App\Services\QueryBus;
use App\Services\UserService;
use App\Utils\Helpers;
Expand Down Expand Up @@ -71,6 +72,7 @@ class EditUserSeminarFormFactory

public function __construct(
private readonly BaseFormFactory $baseFormFactory,
private readonly CommandBus $commandBus,
private readonly QueryBus $queryBus,
private readonly EntityManagerInterface $em,
private readonly UserRepository $userRepository,
Expand All @@ -80,7 +82,6 @@ public function __construct(
private readonly ApplicationService $applicationService,
private readonly Validators $validators,
private readonly FilesService $filesService,
private readonly IMailService $mailService,
private readonly AclService $aclService,
private readonly UserService $userService,
) {
Expand Down Expand Up @@ -336,10 +337,10 @@ public function processForm(Form $form, stdClass $values): void

if ($customInputValueChanged) {
assert($this->user instanceof User);
$this->mailService->sendMailFromTemplate(new ArrayCollection([$this->user]), null, Template::CUSTOM_INPUT_VALUE_CHANGED, [
$this->commandBus->handle(new CreateTemplateMail(new ArrayCollection([$this->user]), null, Template::CUSTOM_INPUT_VALUE_CHANGED, [
TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)),
TemplateVariable::USER => $this->user->getDisplayName(),
]);
]));
}
});
}
Expand Down
36 changes: 12 additions & 24 deletions app/AdminModule/MailingModule/Forms/SendFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
use App\AdminModule\Forms\BaseFormFactory;
use App\Model\Acl\Repositories\RoleRepository;
use App\Model\Acl\Role;
use App\Model\Mailing\Commands\CreateMail;
use App\Model\Structure\Repositories\SubeventRepository;
use App\Model\User\Repositories\UserRepository;
use App\Services\AclService;
use App\Services\IMailService;
use App\Services\CommandBus;
use App\Services\SubeventService;
use Doctrine\Common\Collections\ArrayCollection;
use Nette;
use Nette\Application\UI\Form;
use Nette\Mail\SendException;
use stdClass;
use Throwable;
use Tracy\Debugger;
use Tracy\ILogger;

/**
* Formulář pro vytvoření e-mailu.
Expand All @@ -28,14 +26,9 @@ class SendFormFactory
{
use Nette\SmartObject;

/**
* Stav odeslání e-mailu.
*/
public bool $mailSuccess;

public function __construct(
private readonly BaseFormFactory $baseFormFactory,
private readonly IMailService $mailService,
private readonly CommandBus $commandBus,
private readonly RoleRepository $roleRepository,
private readonly UserRepository $userRepository,
private readonly SubeventRepository $subeventRepository,
Expand Down Expand Up @@ -111,20 +104,15 @@ public function create(): Form
*/
public function processForm(Form $form, stdClass $values): void
{
try {
$recipientsRoles = $this->roleRepository->findRolesByIds($values->recipientRoles);
$recipientsSubevents = $this->subeventRepository->findSubeventsByIds($values->recipientSubevents);
$recipientsUsers = $this->userRepository->findUsersByIds($values->recipientUsers);
$recipientsEmails = new ArrayCollection();
if (! empty($values->copy)) {
$recipientsEmails->add($values->copy);
}

$this->mailService->sendMail($recipientsRoles, $recipientsSubevents, $recipientsUsers, $recipientsEmails, $values->subject, $values->text);
$this->mailSuccess = true;
} catch (SendException $ex) {
Debugger::log($ex, ILogger::WARNING);
$this->mailSuccess = false;
$recipientsUsers = $this->userRepository->findUsersByIds($values->recipientUsers);
$recipientsRoles = $this->roleRepository->findRolesByIds($values->recipientRoles);
$recipientsSubevents = $this->subeventRepository->findSubeventsByIds($values->recipientSubevents);
$recipientsEmails = new ArrayCollection();

if (! empty($values->copy)) {
$recipientsEmails->add($values->copy);
}

$this->commandBus->handle(new CreateMail($recipientsUsers, $recipientsRoles, $recipientsSubevents, $recipientsEmails, $values->subject, $values->text));
}
}
7 changes: 1 addition & 6 deletions app/AdminModule/MailingModule/Presenters/SendPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ protected function createComponentSendForm(): Form
$form = $this->sendFormFactory->create();

$form->onSuccess[] = function (Form $form, stdClass $values): void {
if ($this->sendFormFactory->mailSuccess) {
$this->flashMessage('admin.mailing.send.sent', 'success');
} else {
$this->flashMessage('admin.mailing.send.error', 'danger');
}

$this->flashMessage('admin.mailing.send.sent', 'success');
$this->redirect('this');
};

Expand Down
13 changes: 13 additions & 0 deletions app/ApiModule/Presenters/MailPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

use App\Model\Acl\Permission;
use App\Model\Acl\SrsResource;
use App\Model\Mailing\Commands\SendMails;
use App\Model\Settings\Commands\SetSettingStringValue;
use App\Model\Settings\Queries\SettingStringValueQuery;
use App\Model\Settings\Settings;
use App\Services\CommandBus;
use App\Services\QueryBus;
use Nette\Application\AbortException;
use Nette\Application\Responses\TextResponse;
use Nette\DI\Attributes\Inject;
use Throwable;

Expand All @@ -26,6 +28,17 @@ class MailPresenter extends ApiBasePresenter
#[Inject]
public QueryBus $queryBus;

/**
* Odešle e-maily z fronty.
*/
public function actionSend(): void
{
$this->commandBus->handle(new SendMails());

$response = new TextResponse(null);
$this->sendResponse($response);
}

/**
* Ověří e-mail semináře.
*
Expand Down
13 changes: 7 additions & 6 deletions app/ApiModule/Presenters/MaturityPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use App\Model\Acl\Repositories\RoleRepository;
use App\Model\Acl\Role;
use App\Model\Enums\ApplicationState;
use App\Model\Mailing\Commands\CreateTemplateMail;
use App\Model\Mailing\Template;
use App\Model\Mailing\TemplateVariable;
use App\Model\Settings\Queries\SettingIntValueQuery;
use App\Model\Settings\Queries\SettingStringValueQuery;
use App\Model\Settings\Settings;
use App\Model\User\Repositories\UserRepository;
use App\Services\ApplicationService;
use App\Services\IMailService;
use App\Services\CommandBus;
use App\Services\QueryBus;
use App\Utils\Helpers;
use DateTimeImmutable;
Expand All @@ -29,6 +30,9 @@
*/
class MaturityPresenter extends ApiBasePresenter
{
#[Inject]
public CommandBus $commandBus;

#[Inject]
public QueryBus $queryBus;

Expand All @@ -41,9 +45,6 @@ class MaturityPresenter extends ApiBasePresenter
#[Inject]
public RoleRepository $roleRepository;

#[Inject]
public IMailService $mailService;

#[Inject]
public ApplicationService $applicationService;

Expand Down Expand Up @@ -120,10 +121,10 @@ public function actionSendReminders(): void
$maturityDate = $application->getMaturityDate();

if ($maturityReminderDate == $maturityDate) {
$this->mailService->sendMailFromTemplate(new ArrayCollection([$application->getUser()]), null, Template::MATURITY_REMINDER, [
$this->commandBus->handle(new CreateTemplateMail(new ArrayCollection([$application->getUser()]), null, Template::MATURITY_REMINDER, [
TemplateVariable::SEMINAR_NAME => $this->queryBus->handle(new SettingStringValueQuery(Settings::SEMINAR_NAME)),
TemplateVariable::APPLICATION_MATURITY => $maturityDate->format(Helpers::DATE_FORMAT),
]);
]));
}
}
}
Expand Down
8 changes: 1 addition & 7 deletions app/Mailing/SrsMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ class SrsMail extends AbstractMail implements IComposableMail
public function compose(Message $message, IMessageData|null $mailData = null): void
{
$message->setFrom($mailData->getFrom()->getEmail(), $mailData->getFrom()->getName());

foreach ($mailData->getRecipients() as $recipient) {
if (! empty($recipient->getEmail())) {
$message->addBcc($recipient->getEmail(), $recipient->getName());
}
}

$message->addTo($mailData->getTo()->getEmail(), $mailData->getTo()->getName());
$message->setSubject($mailData->getSubject());

$this->template->subject = $mailData->getSubject();
Expand Down
15 changes: 7 additions & 8 deletions app/Mailing/SrsMailData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
class SrsMailData implements IMessageData
{
/**
* @param Recipient $from Odesilatel mailu.
* @param Recipient[] $recipients Příjemci mailu.
* @param string $subject Předmět mailu.
* @param string $text Text mailu.
* @param Recipient $from Odesilatel mailu.
* @param Recipient $to Příjemce mailu.
* @param string $subject Předmět mailu.
* @param string $text Text mailu.
*/
public function __construct(
private readonly Recipient $from,
private readonly array $recipients,
private readonly Recipient $to,
private readonly string $subject,
private readonly string $text,
) {
Expand All @@ -31,10 +31,9 @@ public function getFrom(): Recipient
return $this->from;
}

/** @return Recipient[] */
public function getRecipients(): array
public function getTo(): Recipient
{
return $this->recipients;
return $this->to;
}

public function getSubject(): string
Expand Down
69 changes: 69 additions & 0 deletions app/Model/Mailing/Commands/CreateMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace App\Model\Mailing\Commands;

use App\Model\Acl\Role;
use App\Model\Structure\Subevent;
use App\Model\User\User;
use Doctrine\Common\Collections\Collection;

class CreateMail
{
/**
* @param Collection<int, User>|null $recipientUsers
* @param Collection<int, Role>|null $recipientRoles
* @param Collection<int, Subevent>|null $recipientSubevents
* @param Collection<int, string>|null $recipientEmails
*/
public function __construct(
private readonly Collection|null $recipientUsers,
private readonly Collection|null $recipientRoles,
private readonly Collection|null $recipientSubevents,
private readonly Collection|null $recipientEmails,
private readonly string $subject,
private readonly string $text,
private readonly bool $automatic = false,
) {
}

/** @return Collection<int, User>|null */
public function getRecipientUsers(): Collection|null
{
return $this->recipientUsers;
}

/** @return Collection<int, Role>|null */
public function getRecipientRoles(): Collection|null
{
return $this->recipientRoles;
}

/** @return Collection<int, Subevent>|null */
public function getRecipientSubevents(): Collection|null
{
return $this->recipientSubevents;
}

/** @return Collection<int, string>|null */
public function getRecipientEmails(): Collection|null
{
return $this->recipientEmails;
}

public function getSubject(): string
{
return $this->subject;
}

public function getText(): string
{
return $this->text;
}

public function isAutomatic(): bool
{
return $this->automatic;
}
}
Loading