Send mail message when cron has hit.

This commit is contained in:
James Cole 2018-06-25 16:01:45 +02:00
parent 89f8f9b45b
commit 7591f3fa29
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
8 changed files with 261 additions and 5 deletions

View File

@ -0,0 +1,45 @@
<?php
namespace FireflyIII\Events;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Log;
/**
* Class RequestedReportOnJournals
*/
class RequestedReportOnJournals
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/** @var Collection */
public $journals;
/** @var int */
public $userId;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(int $userId, Collection $journals)
{
Log::debug('In event RequestedReportOnJournals.');
$this->userId = $userId;
$this->journals = $journals;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

View File

@ -0,0 +1,71 @@
<?php
/**
* AutomationHandler.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Handlers\Events;
use Exception;
use FireflyIII\Events\RequestedReportOnJournals;
use FireflyIII\Mail\ReportNewJournalsMail;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Log;
use Mail;
/**
* Class AutomationHandler
*/
class AutomationHandler
{
/**
* @param RequestedReportOnJournals $event
*
* @return bool
*/
public function reportJournals(RequestedReportOnJournals $event): bool
{
Log::debug('In reportJournals.');
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$user = $repository->findNull($event->userId);
if (null === $user) {
Log::debug('User is NULL');
return true;
}
if ($event->journals->count() === 0) {
Log::debug('No journals.');
return true;
}
try {
Log::debug('Trying to mail...');
Mail::to($user->email)->send(new ReportNewJournalsMail($user->email, '127.0.0.1', $event->journals));
// @codeCoverageIgnoreStart
} catch (Exception $e) {
Log::error($e->getMessage());
}
Log::debug('Done!');
// @codeCoverageIgnoreEnd
return true;
}
}

View File

@ -113,7 +113,7 @@ class UserEventHandler
*
* @return bool
*/
function demoUserBackToEnglish(Login $event): bool
public function demoUserBackToEnglish(Login $event): bool
{
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);

View File

@ -3,11 +3,14 @@
namespace FireflyIII\Jobs;
use Carbon\Carbon;
use FireflyIII\Events\RequestedReportOnJournals;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\RecurrenceRepetition;
use FireflyIII\Models\RecurrenceTransaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@ -29,6 +32,8 @@ class CreateRecurringTransactions implements ShouldQueue
private $journalRepository;
/** @var RecurringRepositoryInterface */
private $repository;
/** @var UserRepositoryInterface */
private $userRepository;
/**
* Create a new job instance.
@ -41,6 +46,7 @@ class CreateRecurringTransactions implements ShouldQueue
$this->date = $date;
$this->repository = app(RecurringRepositoryInterface::class);
$this->journalRepository = app(JournalRepositoryInterface::class);
$this->userRepository = app(UserRepositoryInterface::class);
}
/**
@ -76,14 +82,16 @@ class CreateRecurringTransactions implements ShouldQueue
$this->journalRepository->setUser($recurrence->user);
Log::debug(sprintf('Now at recurrence #%d', $recurrence->id));
$created = $this->handleRepetitions($recurrence);
Log::debug(sprintf('Done with recurrence #%c', $recurrence->id));
Log::debug(sprintf('Done with recurrence #%d', $recurrence->id));
$result[$recurrence->user_id] = $result[$recurrence->user_id]->merge($created);
}
Log::debug('Now running report thing.');
// will now send email to users.
foreach($result as $userId => $journals) {
$this->sendReport($userId, $journals);
foreach ($result as $userId => $journals) {
// random bunch to make mail.
$journals = TransactionJournal::where('user_id', $userId)->inRandomOrder()->take(1)->get();
event(new RequestedReportOnJournals($userId, $journals));
}
Log::debug('Done with handle()');

View File

@ -0,0 +1,71 @@
<?php
/**
* ReportNewJournalsMail.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
/**
* Class ReportNewJournalsMail.
*
* Sends a list of newly created journals to the user.
*/
class ReportNewJournalsMail extends Mailable
{
use Queueable, SerializesModels;
/** @var string Email address of the user */
public $email;
/** @var string IP address of user (if known) */
public $ipAddress;
/** @var Collection A collection of journals */
public $journals;
/**
* ConfirmEmailChangeMail constructor.
*
* @param string $email
* @param string $ipAddress
* @param Collection $journals
*/
public function __construct(string $email, string $ipAddress, Collection $journals)
{
$this->email = $email;
$this->ipAddress = $ipAddress;
$this->journals = $journals;
}
/**
* Build the message.
*
* @return $this
*/
public function build(): self
{
return $this->view('emails.report-new-journals-html')->text('emails.report-new-journals-text')
->subject('Firefly III has created new transactions');
}
}

View File

@ -26,6 +26,7 @@ use Exception;
use FireflyIII\Events\AdminRequestedTestMessage;
use FireflyIII\Events\RegisteredUser;
use FireflyIII\Events\RequestedNewPassword;
use FireflyIII\Events\RequestedReportOnJournals;
use FireflyIII\Events\RequestedVersionCheckStatus;
use FireflyIII\Events\StoredTransactionJournal;
use FireflyIII\Events\UpdatedTransactionJournal;
@ -70,6 +71,9 @@ class EventServiceProvider extends ServiceProvider
RequestedVersionCheckStatus::class => [
'FireflyIII\Handlers\Events\VersionCheckEventHandler@checkForUpdates',
],
RequestedReportOnJournals::class => [
'FireflyIII\Handlers\Events\AutomationHandler@reportJournals',
],
// is a User related event.
RequestedNewPassword::class => [

View File

@ -0,0 +1,32 @@
{% include 'emails.header-html' %}
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
{% if journals.count == 1 %}
Firefly III has created a transaction for you.
{% endif %}
{% if journals.count > 1 %}
Firefly III has created {{ journals.count }} transactions for you.
{% endif %}
</p>
{% if journals.count == 1 %}
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
You can find in in your Firefly III installation:
{% for journal in journals %}
<a href="{{ route('transactions.show', journal.id) }}">{{ journal.description }}</a>
{% endfor %}
</p>
{% endif %}
{% if journals.count > 1 %}
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
You can find them in your Firefly III installation:
</p>
<ul>
{% for journal in journals %}
<li><a href="{{ route('transactions.show', journal.id) }}">{{ journal.description }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% include 'emails.footer-html' %}

View File

@ -0,0 +1,25 @@
{% include 'emails.header-text' %}
{% if journals.count == 1 %}
Firefly III has created a transaction for you.
{% endif %}
{% if journals.count > 1 %}
Firefly III has created {{ journals.count }} transactions for you.
{% endif %}
{% if journals.count == 1 %}
You can find in in your Firefly III installation:
{% for journal in journals %}
{{ journal.description }}: {{ route('transactions.show', journal.id) }}
{% endfor %}
{% endif %}
{% if journals.count > 1 %}
You can find them in your Firefly III installation:
{% for journal in journals %}
- {{ journal.description }}: {{ route('transactions.show', journal.id) }}
{% endfor %}
{% endif %}
{% include 'emails.footer-text' %}