Added email notification on new news
parent
814cafd05d
commit
149155fbda
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Engelsystem\Migrations;
|
||||||
|
|
||||||
|
use Engelsystem\Database\Migration\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
|
||||||
|
class AddEmailNewsToUsersSettings extends Migration
|
||||||
|
{
|
||||||
|
use Reference;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the migration
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
$this->schema->table(
|
||||||
|
'users_settings',
|
||||||
|
function (Blueprint $table) {
|
||||||
|
$table->boolean('email_news')->default(false)->after('email_shiftinfo');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migration
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
$this->schema->table(
|
||||||
|
'users_settings',
|
||||||
|
function (Blueprint $table) {
|
||||||
|
$table->dropColumn('email_news');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
{% block title %}{{ __('Hi %s,', [username]) }}{% endblock %}
|
{% block title %}{{ __('Hi %s,', [username]) }}{% endblock %}
|
||||||
|
|
||||||
{% block introduction %}{{ __('here is a message for you from the %s:', [config('app_name')]) }}{% endblock %}
|
{% block introduction %}{{ __('here is a message for you from the %s:', [config('app_name')]) }}{% endblock %}
|
||||||
|
|
||||||
{% block message %}{{ message|raw }}{% endblock %}
|
{% block message %}{{ message|raw }}{% endblock %}
|
||||||
|
|
||||||
{% block footer %}{{ __('This email is autogenerated and has not been signed. You got this email because you are registered in the %s.', [config('app_name')]) }}{% endblock %}
|
{% block footer %}{{ __('This email is autogenerated and has not been signed. You got this email because you are registered in the %s.', [config('app_name')]) }}{% endblock %}
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
{% extends "emails/mail.twig" %}
|
||||||
|
|
||||||
|
{% block introduction %}
|
||||||
|
{{ __('notification.news.new.introduction', [news.title, news.text, url('/news/' ~ news.id)]) }}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block message %}
|
||||||
|
{{ __('notification.news.new.text', [news.title, news.text, url('/news/' ~ news.id)]) }}
|
||||||
|
{% endblock %}
|
@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Engelsystem\Events\Listener;
|
||||||
|
|
||||||
|
use Engelsystem\Mail\EngelsystemMailer;
|
||||||
|
use Engelsystem\Models\News as NewsModel;
|
||||||
|
use Engelsystem\Models\User\Settings as UserSettings;
|
||||||
|
use Engelsystem\Models\User\User;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Swift_SwiftException as SwiftException;
|
||||||
|
|
||||||
|
class News
|
||||||
|
{
|
||||||
|
/** @var LoggerInterface */
|
||||||
|
protected $log;
|
||||||
|
|
||||||
|
/** @var EngelsystemMailer */
|
||||||
|
protected $mailer;
|
||||||
|
|
||||||
|
/** @var UserSettings */
|
||||||
|
protected $settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param LoggerInterface $log
|
||||||
|
* @param EngelsystemMailer $mailer
|
||||||
|
* @param UserSettings $settings
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
LoggerInterface $log,
|
||||||
|
EngelsystemMailer $mailer,
|
||||||
|
UserSettings $settings
|
||||||
|
) {
|
||||||
|
$this->log = $log;
|
||||||
|
$this->mailer = $mailer;
|
||||||
|
$this->settings = $settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param NewsModel $news
|
||||||
|
*/
|
||||||
|
public function created(NewsModel $news)
|
||||||
|
{
|
||||||
|
/** @var UserSettings[]|Collection $recipients */
|
||||||
|
$recipients = $this->settings
|
||||||
|
->whereEmailNews(true)
|
||||||
|
->with('user')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
foreach ($recipients as $recipient) {
|
||||||
|
$this->sendMail($news, $recipient->user, 'notification.news.new', 'emails/news-new');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param NewsModel $news
|
||||||
|
* @param User $user
|
||||||
|
* @param string $subject
|
||||||
|
* @param string $template
|
||||||
|
*/
|
||||||
|
protected function sendMail(NewsModel $news, User $user, string $subject, string $template)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$this->mailer->sendViewTranslated(
|
||||||
|
$user,
|
||||||
|
$subject,
|
||||||
|
$template,
|
||||||
|
['title' => $news->title, 'news' => $news, 'username' => $user->name]
|
||||||
|
);
|
||||||
|
} catch (SwiftException $e) {
|
||||||
|
$this->log->error(
|
||||||
|
'Unable to send email "{title}" to user {user} with {exception}',
|
||||||
|
['title' => $subject, 'user' => $user->name, 'exception' => $e]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,101 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Engelsystem\Test\Unit\Events\Listener;
|
||||||
|
|
||||||
|
use Engelsystem\Events\Listener\News;
|
||||||
|
use Engelsystem\Helpers\Authenticator;
|
||||||
|
use Engelsystem\Mail\EngelsystemMailer;
|
||||||
|
use Engelsystem\Models\News as NewsModel;
|
||||||
|
use Engelsystem\Models\User\Settings;
|
||||||
|
use Engelsystem\Models\User\User;
|
||||||
|
use Engelsystem\Test\Unit\HasDatabase;
|
||||||
|
use Engelsystem\Test\Unit\TestCase;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Psr\Log\Test\TestLogger;
|
||||||
|
use Swift_SwiftException as SwiftException;
|
||||||
|
|
||||||
|
class NewsTest extends TestCase
|
||||||
|
{
|
||||||
|
use HasDatabase;
|
||||||
|
|
||||||
|
/** @var TestLogger */
|
||||||
|
protected $log;
|
||||||
|
|
||||||
|
/** @var EngelsystemMailer|MockObject */
|
||||||
|
protected $mailer;
|
||||||
|
|
||||||
|
/** @var User */
|
||||||
|
protected $user;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \Engelsystem\Events\Listener\News::created
|
||||||
|
* @covers \Engelsystem\Events\Listener\News::__construct
|
||||||
|
* @covers \Engelsystem\Events\Listener\News::sendMail
|
||||||
|
*/
|
||||||
|
public function testCreated()
|
||||||
|
{
|
||||||
|
$news = new NewsModel([
|
||||||
|
'title' => 'Foo',
|
||||||
|
'text' => 'Bar',
|
||||||
|
'user_id' => 1,
|
||||||
|
]);
|
||||||
|
$news->save();
|
||||||
|
|
||||||
|
$i = 0;
|
||||||
|
$this->mailer->expects($this->exactly(2))
|
||||||
|
->method('sendViewTranslated')
|
||||||
|
->willReturnCallback(function (User $user, string $subject, string $template, array $data) use (&$i) {
|
||||||
|
$this->assertEquals(1, $user->id);
|
||||||
|
$this->assertEquals('notification.news.new', $subject);
|
||||||
|
$this->assertEquals('emails/news-new', $template);
|
||||||
|
$this->assertEquals('Foo', array_values($data)[0]);
|
||||||
|
|
||||||
|
if ($i++ > 0) {
|
||||||
|
throw new SwiftException('Oops');
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
});
|
||||||
|
|
||||||
|
/** @var News $listener */
|
||||||
|
$listener = $this->app->make(News::class);
|
||||||
|
$error = 'Unable to send email';
|
||||||
|
|
||||||
|
$listener->created($news);
|
||||||
|
$this->assertFalse($this->log->hasErrorThatContains($error));
|
||||||
|
|
||||||
|
$listener->created($news);
|
||||||
|
$this->assertTrue($this->log->hasErrorThatContains($error));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->initDatabase();
|
||||||
|
|
||||||
|
$this->log = new TestLogger();
|
||||||
|
$this->app->instance(LoggerInterface::class, $this->log);
|
||||||
|
|
||||||
|
$this->mailer = $this->createMock(EngelsystemMailer::class);
|
||||||
|
$this->app->instance(EngelsystemMailer::class, $this->mailer);
|
||||||
|
|
||||||
|
$this->user = new User([
|
||||||
|
'name' => 'test',
|
||||||
|
'password' => '',
|
||||||
|
'email' => 'foo@bar.baz',
|
||||||
|
'api_key' => '',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->user->save();
|
||||||
|
|
||||||
|
$settings = new Settings([
|
||||||
|
'language' => '',
|
||||||
|
'theme' => 1,
|
||||||
|
'email_news' => true,
|
||||||
|
]);
|
||||||
|
$settings->user()
|
||||||
|
->associate($this->user)
|
||||||
|
->save();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue