Skip to content

Commit 6120770

Browse files
committed
Add Admin Report emails
1 parent 194bbb0 commit 6120770

4 files changed

Lines changed: 96 additions & 0 deletions

File tree

app/Mail/AdminReport.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace App\Mail;
4+
5+
use App\Models\Report;
6+
use App\Services\AccountService;
7+
use App\Services\ReportService;
8+
use Illuminate\Bus\Queueable;
9+
use Illuminate\Mail\Mailable;
10+
use Illuminate\Mail\Mailables\Content;
11+
use Illuminate\Mail\Mailables\Envelope;
12+
use Illuminate\Queue\SerializesModels;
13+
14+
class AdminReport extends Mailable
15+
{
16+
use Queueable, SerializesModels;
17+
18+
public $report;
19+
20+
/**
21+
* Create a new message instance.
22+
*/
23+
public function __construct(Report $report)
24+
{
25+
$this->report = $report;
26+
}
27+
28+
/**
29+
* Get the message envelope.
30+
*/
31+
public function envelope(): Envelope
32+
{
33+
return new Envelope(
34+
subject: 'Report #'.$this->report->id,
35+
);
36+
}
37+
38+
/**
39+
* Get the message content definition.
40+
*/
41+
public function content(): Content
42+
{
43+
$reporter = AccountService::compact($this->report->reporter_profile_id);
44+
$reporterUsername = data_get($reporter, 'username', 'a user');
45+
$reportedEntityType = $this->report->reportEntityType();
46+
47+
return new Content(
48+
markdown: 'emails.admin.report',
49+
with: [
50+
'id' => $this->report->id,
51+
'entity' => $reportedEntityType,
52+
'reporterUsername' => $reporterUsername,
53+
'reportType' => ReportService::getById($this->report->report_type),
54+
'url' => $this->report->adminUrl(),
55+
]
56+
);
57+
}
58+
59+
/**
60+
* Get the attachments for the message.
61+
*
62+
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
63+
*/
64+
public function attachments(): array
65+
{
66+
return [];
67+
}
68+
}

app/Models/Report.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace App\Models;
44

5+
use App\Mail\AdminReport;
56
use Illuminate\Database\Eloquent\Attributes\Scope;
67
use Illuminate\Database\Eloquent\Builder;
78
use Illuminate\Database\Eloquent\Factories\HasFactory;
89
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Support\Facades\DB;
11+
use Illuminate\Support\Facades\Mail;
1012

1113
/**
1214
* @property int $id
@@ -72,6 +74,15 @@ protected function casts(): array
7274
];
7375
}
7476

77+
protected static function booted(): void
78+
{
79+
static::created(function (Report $report) {
80+
if (config('loops.admin_mails.to') && config('loops.admin_mails.reports') && filter_var(config('loops.admin_mails.to'), FILTER_VALIDATE_EMAIL)) {
81+
Mail::to(config('loops.admin_mails.to'))->send(new AdminReport($report));
82+
}
83+
});
84+
}
85+
7586
#[Scope]
7687
protected function search(Builder $query, ?string $search): Builder
7788
{

config/loops.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,9 @@
7474
'admin_dashboard' => [
7575
'autoUpdate' => (bool) env('LOOPS_ADMIN_DASHBOARD_AUTOUPDATE', true),
7676
],
77+
78+
'admin_mails' => [
79+
'to' => env('LOOPS_ADMIN_MAILS_TO'),
80+
'reports' => (bool) env('LOOPS_ADMIN_MAILS_REPORTS', false),
81+
],
7782
];
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<x-mail::message>
2+
# New Report #{{ $id }}
3+
4+
A user (**{{$reporterUsername}}**) reported a **{{$entity}}** for **{{$reportType}}**.
5+
6+
<x-mail::button :url="$url">
7+
View Report
8+
</x-mail::button>
9+
10+
Thanks,<br>
11+
{{ config('app.name') }}
12+
</x-mail::message>

0 commit comments

Comments
 (0)