From 22222990b6598e624f91cc46b680a4264ce58d11 Mon Sep 17 00:00:00 2001 From: Martin Folkerts Date: Tue, 10 Sep 2024 11:53:31 +0200 Subject: [PATCH] added report-not-found email --- .../app/Livewire/Forms/MailingForm.php | 32 +++++---- laravel_app/app/Mail/ReportNotFound.php | 68 +++++++++++++++++++ laravel_app/app/Models/ProjectReport.php | 7 +- laravel_app/app/ProjectLogger.php | 11 ++- .../views/emails/report-not-found.blade.php | 62 +++++++++++++++++ laravel_app/routes/web.php | 26 +++++-- 6 files changed, 185 insertions(+), 21 deletions(-) create mode 100644 laravel_app/app/Mail/ReportNotFound.php create mode 100644 laravel_app/resources/views/emails/report-not-found.blade.php diff --git a/laravel_app/app/Livewire/Forms/MailingForm.php b/laravel_app/app/Livewire/Forms/MailingForm.php index 8f5c4d1..6d5d0d8 100644 --- a/laravel_app/app/Livewire/Forms/MailingForm.php +++ b/laravel_app/app/Livewire/Forms/MailingForm.php @@ -41,20 +41,28 @@ public function save() } public static function saveAndSendMailing($report, $subject, $message, $recipients) { - $mailing = $report->project->mailings()->create([ - 'subject' => $subject, - 'message' => $message, - 'report_id' => $report->id, - ]); + if ($report->documentExists()) { + $mailing = $report->project->mailings()->create([ + 'subject' => $subject, + 'message' => $message, + 'report_id' => $report->id, + ]); - $mailing->attachments()->create([ - 'name' => $report->name, - 'path' => $report->path, - ]); + $mailing->attachments()->create([ + 'name' => $report->name, + 'path' => $report->path, + ]); - $mailing->recipients()->createMany($recipients); - Mail::to($mailing->recipients()->pluck('email')->toArray()) - ->send(new \App\Mail\ReportMailer($mailing, $report)); + $mailing->recipients()->createMany($recipients); + Mail::to($mailing->recipients()->pluck('email')->toArray()) + ->send(new \App\Mail\ReportMailer($mailing, $report)); + } else { + self::sendReportNotFoundNotificationToAdmin($report); + } + } + private static function sendReportNotFoundNotificationToAdmin($report) { + Mail::to(config('mail.from.address')) + ->send(new \App\Mail\ReportNotFound($report)); } } diff --git a/laravel_app/app/Mail/ReportNotFound.php b/laravel_app/app/Mail/ReportNotFound.php new file mode 100644 index 0000000..d3ebdad --- /dev/null +++ b/laravel_app/app/Mail/ReportNotFound.php @@ -0,0 +1,68 @@ +report = $report; + } + + /** + * Get the message envelope. + */ + public function envelope(): Envelope + { + return new Envelope( + subject: sprintf('Report Not Found for project %s', $this->report->project->name) + ); + } + + /** + * Get the message content definition. + */ + public function content(): Content + { + return new Content( + markdown: 'emails.report-not-found', + with: [ + 'logoPath'=> resource_path('images/smartcane.png'), + ], + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + $path = ProjectLogger::getLogFileForToday($this->report->project); + if ($path) { + $filename = sprintf('%s_%s.log', $this->report->project->name, now()->format('Ymd')); + return [ + Attachment::fromPath($path) + ->as($filename) + ->withMime('text/plain'), + ]; + } + return []; + } +} diff --git a/laravel_app/app/Models/ProjectReport.php b/laravel_app/app/Models/ProjectReport.php index de9ef24..63a2230 100644 --- a/laravel_app/app/Models/ProjectReport.php +++ b/laravel_app/app/Models/ProjectReport.php @@ -70,9 +70,14 @@ public static function getReportDateForYearAndWeek(Project $project, $year, $wee return $reportDay; } + public function documentExists() + { + return File::exists($this->getFullPathName()); + } + public function deleteMe() { - if (File::exists($this->getFullPathName())) { + if ($this->documentExists()) { File::delete($this->getFullPathName()); } $this->delete(); diff --git a/laravel_app/app/ProjectLogger.php b/laravel_app/app/ProjectLogger.php index d128647..de242a2 100644 --- a/laravel_app/app/ProjectLogger.php +++ b/laravel_app/app/ProjectLogger.php @@ -28,6 +28,15 @@ public static function getAsList(Project $project) ]; } } - return collect($logs); + return collect($logs)->orderBy('name', 'desc'); + } + + public static function getLogFileForToday(Project $project) + { + $logPath = $project->download_path . '/logs/' . now()->format('Ymd') . '.log'; + if(Storage::exists($logPath)) { + return Storage::path($logPath); + } + return null; } } diff --git a/laravel_app/resources/views/emails/report-not-found.blade.php b/laravel_app/resources/views/emails/report-not-found.blade.php new file mode 100644 index 0000000..05e59f2 --- /dev/null +++ b/laravel_app/resources/views/emails/report-not-found.blade.php @@ -0,0 +1,62 @@ + + {{-- Header --}} + + + + Smartcane Logo + + + + {{-- Body --}} +#

Report not found

+* please view log in attachment.* +*This report is automatically generated. Do not reply* +## Thanks. +## Contact + + + + + + + + + + + + + + + + +
+

+ +

+
+

+31 6 2037 6734

+
+

+ +

+
+

info@smartcane.org

+
+

+ +

+
+

Bevrijdingsstraat 38, 6703AA, Wageningen, The Netherlands

+
+ + {{-- Footer --}} + + +

SmartCane is an innovative spinoff from the Resilience Network, which includes SmartFarming BV and IRIPO Lda.

+

+ © {{ date('Y') }} {{ config('app.name') }}. {{ __('All rights reserved.') }} +

+ +
+
+
diff --git a/laravel_app/routes/web.php b/laravel_app/routes/web.php index 3d5a7ae..b3ddef5 100644 --- a/laravel_app/routes/web.php +++ b/laravel_app/routes/web.php @@ -16,11 +16,21 @@ Route::get('/', function () { return view('welcome'); }); +if (config('app.env') === 'local') { + Route::get('no-report-found', function () { + $report = App\Models\ProjectReport::find(287); + return new App\Mail\ReportNotFound($report); + }); + Route::get('/mailable/{id}', function ($id) { + if ($project_mailing = App\Models\ProjectMailing::find($id)) { + return new App\Mail\ReportMailer($project_mailing); + } + return abort(404); + })->name('mail'); +} + + -Route::get('/mailable/{id}', function ($id) { - if($project_mailing = App\Models\ProjectMailing::find($id)) return new App\Mail\ReportMailer($project_mailing); - return abort(404); -})->name('mail'); Route::middleware([ 'auth:sanctum', config('jetstream.auth_session'), @@ -29,15 +39,17 @@ Route::get('/dashboard', function () { return view('dashboard'); })->name('dashboard'); - Route::get('/projects/{projectName}/{currentTab?}', [\App\Http\Controllers\ProjectController::class, 'show'])->name('project.show'); - Route::get('/projects/{projectReport}/download', [\App\Http\Controllers\ProjectReportController::class, 'download'])->name('project.report.download'); + Route::get('/projects/{projectName}/{currentTab?}', + [\App\Http\Controllers\ProjectController::class, 'show'])->name('project.show'); + Route::get('/projects/{projectReport}/download', + [\App\Http\Controllers\ProjectReportController::class, 'download'])->name('project.report.download'); Route::get('/projects', [\App\Http\Controllers\ProjectController::class, 'index'])->name('project'); }); Route::get('download/{token}', function ($token) { $report = App\Models\ProjectReport::where('token', $token)->first(); if ($report) { - $path = $report->project->download_path . '/' . $report->path; + $path = $report->project->download_path.'/'.$report->path; $report->updateStatistics(); return response()->download(Storage::path($path)); }