|
|
@@ -3,204 +3,427 @@
|
|
|
namespace App\Http\Livewire;
|
|
|
|
|
|
use Livewire\Component;
|
|
|
-use Illuminate\Support\Facades\Auth;
|
|
|
+use Livewire\WithFileUploads;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Carbon\Carbon;
|
|
|
+use Illuminate\Support\Facades\Storage;
|
|
|
+
|
|
|
use App\Http\Middleware\TenantMiddleware;
|
|
|
-use App\Models\EmailTemplate;
|
|
|
-use App\Models\EmailScheduled;
|
|
|
-use App\Models\Category;
|
|
|
+use App\Models\EmailMessage;
|
|
|
use App\Models\Member;
|
|
|
-use App\Models\User;
|
|
|
-use Carbon\Carbon;
|
|
|
-use Illuminate\Support\Facades\Mail;
|
|
|
-use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
class EmailComunications extends Component
|
|
|
{
|
|
|
- public $records, $subject, $message, $selectedRecipients = [], $scheduledDateTime, $sendNow = 'true';
|
|
|
- public $dataId, $update = false, $add = false;
|
|
|
- public $users = [];
|
|
|
-
|
|
|
- protected $rules = [
|
|
|
- 'subject' => 'required|string|max:255',
|
|
|
- 'message' => 'required|string',
|
|
|
- 'selectedRecipients' => 'required|array|min:1',
|
|
|
- 'scheduledDateTime' => 'required_if:sendNow,false|date|after:now',
|
|
|
- ];
|
|
|
-
|
|
|
- protected $messages = [
|
|
|
- 'subject.required' => 'L\'oggetto è obbligatorio',
|
|
|
- 'message.required' => 'Il messaggio è obbligatorio',
|
|
|
- 'selectedRecipients.required' => 'Seleziona almeno un gruppo di destinatari',
|
|
|
- 'scheduledDateTime.required_if' => 'La data di programmazione è obbligatoria',
|
|
|
- 'scheduledDateTime.after' => 'La data di programmazione deve essere futura',
|
|
|
- ];
|
|
|
-
|
|
|
- public $sortField = 'name';
|
|
|
- public $sortAsc = true;
|
|
|
+ use WithFileUploads;
|
|
|
+
|
|
|
+ public ?int $messageId = null;
|
|
|
+ public string $subject = '';
|
|
|
+ public string $content_html = '';
|
|
|
+
|
|
|
+ public array $recipients = [];
|
|
|
+
|
|
|
+ public array $existingAttachments = [];
|
|
|
+ public $newAttachments = [];
|
|
|
+
|
|
|
+ public string $mode = 'now'; // 'now' | 'schedule'
|
|
|
+ public ?string $schedule_at = null;
|
|
|
+
|
|
|
+ public $records;
|
|
|
+ public $categories;
|
|
|
+ public $courses;
|
|
|
+
|
|
|
+ public bool $showForm = false;
|
|
|
+ public bool $locked = false;
|
|
|
+
|
|
|
+ public $success;
|
|
|
+ public $error;
|
|
|
|
|
|
public function boot()
|
|
|
{
|
|
|
app(TenantMiddleware::class)->setupTenantConnection();
|
|
|
}
|
|
|
|
|
|
- public function sortBy($field)
|
|
|
+ public function mount()
|
|
|
+ {
|
|
|
+ if (auth()->user()?->level != env('LEVEL_ADMIN', 0)) {
|
|
|
+ return redirect()->to('/dashboard');
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->categories = [];
|
|
|
+ $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->orderBy('name')->get(), 0);
|
|
|
+
|
|
|
+ $this->courses = [];
|
|
|
+ $this->getCourses(\App\Models\Course::select('id', 'name')->where('parent_id', null)->orderBy('name', 'ASC')->get(), 0);
|
|
|
+
|
|
|
+ $this->schedule_at = now()->addHour()->format('Y-m-d\TH:i');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function render()
|
|
|
{
|
|
|
- if ($this->sortField === $field) {
|
|
|
- $this->sortAsc = ! $this->sortAsc;
|
|
|
+ if (!$this->showForm) {
|
|
|
+ $this->records = EmailMessage::withCount(['attachments', 'recipients'])
|
|
|
+ ->orderBy('created_at', 'desc')
|
|
|
+ ->get();
|
|
|
} else {
|
|
|
- $this->sortAsc = true;
|
|
|
+ $this->categories = [];
|
|
|
+ $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->orderBy('name')->get(), 0);
|
|
|
+
|
|
|
+ $this->courses = [];
|
|
|
+ $this->getCourses(\App\Models\Course::select('id', 'name')->where('parent_id', null)->orderBy('name', 'ASC')->get(), 0);
|
|
|
}
|
|
|
|
|
|
- $this->sortField = $field;
|
|
|
+ return view('livewire.email_comunications');
|
|
|
}
|
|
|
|
|
|
- public function resetFields()
|
|
|
+ protected function baseRules(): array
|
|
|
{
|
|
|
- $this->subject = '';
|
|
|
- $this->message = '';
|
|
|
- $this->selectedRecipients = [];
|
|
|
- $this->sendNow = true;
|
|
|
- $this->scheduledDateTime = now()->addHour()->format('Y-m-d\TH:i');
|
|
|
- $this->emit('load-data-table');
|
|
|
+ return [
|
|
|
+ 'subject' => 'required|string|max:255',
|
|
|
+ 'content_html' => 'required|string',
|
|
|
+ 'recipients' => 'required|array|min:1',
|
|
|
+ 'recipients.*.email_address' => 'required|email',
|
|
|
+ 'newAttachments' => 'nullable',
|
|
|
+ 'newAttachments.*' => 'file|max:20480',
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
- public function mount()
|
|
|
+ protected function validateDraft(): void
|
|
|
{
|
|
|
- if (Auth::user()->level != env('LEVEL_ADMIN', 0))
|
|
|
- return redirect()->to('/dashboard');
|
|
|
+ $this->validate($this->baseRules());
|
|
|
+ }
|
|
|
|
|
|
- $this->users = Member::select('id', 'last_name', 'email')->get();
|
|
|
- $this->scheduledDateTime = now()->addHour()->format('Y-m-d\TH:i');
|
|
|
+ protected function validateSend(): void
|
|
|
+ {
|
|
|
+ $this->validate($this->baseRules());
|
|
|
}
|
|
|
|
|
|
- public function render()
|
|
|
+ protected function validateSchedule(): void
|
|
|
{
|
|
|
- $this->records = EmailTemplate::orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get();
|
|
|
- return view('livewire.email_comunications');
|
|
|
+ $rules = $this->baseRules();
|
|
|
+ $rules['schedule_at'] = 'required|date|after:now';
|
|
|
+ $this->validate($rules);
|
|
|
}
|
|
|
|
|
|
public function add()
|
|
|
{
|
|
|
- $this->resetFields();
|
|
|
- $this->add = true;
|
|
|
- $this->update = false;
|
|
|
+ $this->reset(['messageId', 'subject', 'content_html', 'recipients', 'newAttachments', 'mode', 'schedule_at']);
|
|
|
+ $this->mode = 'now';
|
|
|
+ $this->schedule_at = now()->addHour()->format('Y-m-d\TH:i');
|
|
|
+ $this->existingAttachments = [];
|
|
|
+
|
|
|
+ $this->showForm = true;
|
|
|
+
|
|
|
+ $this->dispatchBrowserEvent('load-editor', [
|
|
|
+ 'html' => $this->content_html ?? '',
|
|
|
+ 'locked' => $this->locked,
|
|
|
+ ]);
|
|
|
+ $this->dispatchBrowserEvent('init-recipients-table', [
|
|
|
+ 'selected' => collect($this->recipients)->pluck('member_id')->filter()->values()->all(),
|
|
|
+ ]);
|
|
|
}
|
|
|
|
|
|
- public function store()
|
|
|
+ public function edit($id)
|
|
|
{
|
|
|
- $this->validate();
|
|
|
-
|
|
|
try {
|
|
|
- $template = EmailTemplate::create([
|
|
|
- 'name' => $this->subject,
|
|
|
- 'content' => $this->message,
|
|
|
- 'created_by' => Auth::id(),
|
|
|
- ]);
|
|
|
+ $msg = EmailMessage::with(['recipients', 'attachments'])->findOrFail($id);
|
|
|
|
|
|
- $recipients = User::whereIn('id', $this->selectedRecipients)->get();
|
|
|
+ $this->messageId = $msg->id;
|
|
|
+ $this->subject = $msg->subject;
|
|
|
+ $this->content_html = $msg->content_html;
|
|
|
+ $this->recipients = $msg->recipients->map(fn($r) => [
|
|
|
+ 'member_id' => $r->member_id,
|
|
|
+ 'email_address' => $r->email_address,
|
|
|
+ 'first_name' => optional($r->member)->first_name,
|
|
|
+ 'last_name' => optional($r->member)->last_name,
|
|
|
+ ])->toArray();
|
|
|
+ $this->mode = $msg->status === 'scheduled' ? 'schedule' : 'now';
|
|
|
+ $this->schedule_at = optional($msg->schedule_at)?->format('Y-m-d\TH:i');
|
|
|
+ $this->existingAttachments = $msg->attachments->map(fn($a) => [
|
|
|
+ 'id' => $a->id,
|
|
|
+ 'name' => $a->name ?: basename($a->path),
|
|
|
+ 'size' => $a->size_human,
|
|
|
+ 'url' => $a->public_url,
|
|
|
+ 'img' => $a->is_image,
|
|
|
+ ])->toArray();
|
|
|
|
|
|
- if ($this->sendNow === 'true') { // Cambiare il confronto
|
|
|
- $this->sendEmailNow($template, $recipients);
|
|
|
- session()->flash('success', 'Template creato e Email inviate a ' . $recipients->count() . ' destinatari!');
|
|
|
- } else {
|
|
|
- $this->scheduleEmail($template, $recipients);
|
|
|
- $scheduledDate = Carbon::parse($this->scheduledDateTime)->format('d/m/Y H:i');
|
|
|
- session()->flash('success', 'Template creato e Email programmate per ' . $scheduledDate);
|
|
|
- }
|
|
|
+ $this->showForm = true;
|
|
|
+ $this->locked = $msg->isLocked();
|
|
|
|
|
|
- $this->resetFields();
|
|
|
- $this->add = false;
|
|
|
- } catch (\Exception $ex) {
|
|
|
- session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
|
|
|
+ $this->dispatchBrowserEvent('load-editor', [
|
|
|
+ 'html' => $this->content_html ?? '',
|
|
|
+ 'locked' => $this->locked,
|
|
|
+ ]);
|
|
|
+ $this->dispatchBrowserEvent('init-recipients-table', [
|
|
|
+ 'selected' => collect($this->recipients)->pluck('member_id')->filter()->values()->all(),
|
|
|
+ ]);
|
|
|
+ } catch (\Throwable $ex) {
|
|
|
+ $this->error = 'Errore (' . $ex->getMessage() . ')';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function edit($id)
|
|
|
+
|
|
|
+ public function duplicate($id, $withRecipients = true)
|
|
|
{
|
|
|
try {
|
|
|
- $template = EmailTemplate::findOrFail($id);
|
|
|
- if (!$template) {
|
|
|
- session()->flash('error', 'Template Email non trovato');
|
|
|
- } else {
|
|
|
- $this->subject = $template->name;
|
|
|
- $this->message = $template->content;
|
|
|
- $this->dataId = $template->id;
|
|
|
- $this->update = true;
|
|
|
- $this->add = false;
|
|
|
+ $copy = EmailMessage::with(['recipients', 'attachments'])->findOrFail($id)->duplicate($withRecipients);
|
|
|
+ $this->edit($copy->id);
|
|
|
+ $this->success = 'Bozza duplicata';
|
|
|
+ } catch (\Throwable $ex) {
|
|
|
+ $this->error = 'Errore (' . $ex->getMessage() . ')';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function saveDraft($html = null)
|
|
|
+ {
|
|
|
+ if ($html !== null) $this->content_html = $html;
|
|
|
+ $this->validateDraft();
|
|
|
+
|
|
|
+ DB::transaction(function () {
|
|
|
+ $msg = $this->upsertMessage(status: 'draft', scheduleAt: null);
|
|
|
+ $this->upsertRecipients($msg);
|
|
|
+ $this->upsertAttachments($msg);
|
|
|
+ $this->messageId = $msg->id;
|
|
|
+ $this->locked = $msg->isLocked();
|
|
|
+ $this->refreshAttachments($msg);
|
|
|
+ });
|
|
|
+
|
|
|
+ $this->success = 'Bozza salvata';
|
|
|
+
|
|
|
+ $this->dispatchBrowserEvent('load-editor', [
|
|
|
+ 'html' => $this->content_html ?? '',
|
|
|
+ 'locked' => $this->locked,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function sendNow($html = null)
|
|
|
+ {
|
|
|
+ if ($html !== null) $this->content_html = $html;
|
|
|
+ $this->validateSend();
|
|
|
+
|
|
|
+ if ($this->messageId) {
|
|
|
+ $existing = EmailMessage::findOrFail($this->messageId);
|
|
|
+ if ($existing->isLocked()) {
|
|
|
+ $this->error = 'Questa email è già in invio o inviata e non può essere modificata.';
|
|
|
+ return;
|
|
|
}
|
|
|
- } catch (\Exception $ex) {
|
|
|
- session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
|
|
|
}
|
|
|
+
|
|
|
+ DB::transaction(function () {
|
|
|
+ $msg = $this->upsertMessage(status: 'processing', scheduleAt: null);
|
|
|
+ $this->upsertRecipients($msg, true);
|
|
|
+ $this->upsertAttachments($msg, true);
|
|
|
+ $this->messageId = $msg->id;
|
|
|
+ $this->locked = true;
|
|
|
+ $this->refreshAttachments($msg);
|
|
|
+ });
|
|
|
+
|
|
|
+ dispatch(new \App\Jobs\SendEmailMessage($this->messageId));
|
|
|
+ $this->success = 'Invio avviato';
|
|
|
+
|
|
|
+ $this->dispatchBrowserEvent('load-editor', [
|
|
|
+ 'html' => $this->content_html ?? '',
|
|
|
+ 'locked' => $this->locked,
|
|
|
+ ]);
|
|
|
}
|
|
|
|
|
|
- public function update()
|
|
|
+ public function scheduleMessage($html = null)
|
|
|
{
|
|
|
- $this->validate([
|
|
|
- 'subject' => 'required|string|max:255',
|
|
|
- 'message' => 'required|string',
|
|
|
+ if ($html !== null) $this->content_html = $html;
|
|
|
+ $this->validateSchedule();
|
|
|
+
|
|
|
+ if ($this->messageId) {
|
|
|
+ $existing = EmailMessage::findOrFail($this->messageId);
|
|
|
+ if ($existing->isLocked()) {
|
|
|
+ $this->error = 'Questa email è già in invio o inviata e non può essere modificata.';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::transaction(function () {
|
|
|
+ $msg = $this->upsertMessage(status: 'scheduled', scheduleAt: \Carbon\Carbon::parse($this->schedule_at));
|
|
|
+ $this->upsertRecipients($msg, true);
|
|
|
+ $this->upsertAttachments($msg, true);
|
|
|
+ $this->messageId = $msg->id;
|
|
|
+ $this->locked = $msg->isLocked();
|
|
|
+ $this->refreshAttachments($msg);
|
|
|
+ });
|
|
|
+
|
|
|
+ $this->success = 'Email programmata';
|
|
|
+
|
|
|
+ $this->dispatchBrowserEvent('load-editor', [
|
|
|
+ 'html' => $this->content_html ?? '',
|
|
|
+ 'locked' => $this->locked,
|
|
|
]);
|
|
|
+ }
|
|
|
|
|
|
- try {
|
|
|
- EmailTemplate::whereId($this->dataId)->update([
|
|
|
- 'name' => $this->subject,
|
|
|
- 'content' => $this->message,
|
|
|
+ protected function upsertMessage(string $status, $scheduleAt): EmailMessage
|
|
|
+ {
|
|
|
+ return EmailMessage::updateOrCreate(
|
|
|
+ ['id' => $this->messageId],
|
|
|
+ [
|
|
|
+ 'subject' => $this->subject,
|
|
|
+ 'content_html' => $this->content_html,
|
|
|
+ 'status' => $status,
|
|
|
+ 'schedule_at' => $scheduleAt,
|
|
|
+ 'created_by' => auth()->id(),
|
|
|
+ ]
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function upsertRecipients(EmailMessage $msg, bool $force = false): void
|
|
|
+ {
|
|
|
+ if (!$force && $msg->isLocked()) return;
|
|
|
+
|
|
|
+ $msg->recipients()->delete();
|
|
|
+
|
|
|
+ $rows = collect($this->recipients)->map(fn($r) => [
|
|
|
+ 'email_message_id' => $msg->id,
|
|
|
+ 'member_id' => $r['member_id'] ?? null,
|
|
|
+ 'email_address' => $r['email_address'],
|
|
|
+ 'status' => 'pending',
|
|
|
+ 'created_at' => now(),
|
|
|
+ 'updated_at' => now(),
|
|
|
+ ])->values()->all();
|
|
|
+
|
|
|
+ if ($rows) \App\Models\EmailMessageRecipient::insert($rows);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function upsertAttachments(EmailMessage $msg, bool $force = false): void
|
|
|
+ {
|
|
|
+ if (!$force && $msg->isLocked()) return;
|
|
|
+
|
|
|
+ $files = is_array($this->newAttachments) ? $this->newAttachments : [$this->newAttachments];
|
|
|
+ foreach ($files as $upload) {
|
|
|
+ if (!$upload) continue;
|
|
|
+ $path = $upload->store('emails/' . \Illuminate\Support\Str::uuid(), 'public');
|
|
|
+ $msg->attachments()->create([
|
|
|
+ 'disk' => 'public',
|
|
|
+ 'path' => $path,
|
|
|
+ 'name' => $upload->getClientOriginalName(),
|
|
|
+ 'size' => $upload->getSize(),
|
|
|
]);
|
|
|
- session()->flash('success', 'Template Email aggiornato');
|
|
|
- $this->resetFields();
|
|
|
- $this->update = false;
|
|
|
- } catch (\Exception $ex) {
|
|
|
- session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function cancel()
|
|
|
{
|
|
|
- $this->add = false;
|
|
|
- $this->update = false;
|
|
|
- $this->resetFields();
|
|
|
+ $this->showForm = false;
|
|
|
+ $this->reset(['messageId', 'subject', 'content_html', 'recipients', 'newAttachments', 'mode', 'schedule_at']);
|
|
|
+ $this->mode = 'now';
|
|
|
+ $this->schedule_at = now()->addHour()->format('Y-m-d\TH:i');
|
|
|
+
|
|
|
+ $this->dispatchBrowserEvent('init-archive-table');
|
|
|
}
|
|
|
|
|
|
- public function sendTemplate($id)
|
|
|
+ public function getCategories($records, $indentation)
|
|
|
{
|
|
|
- try {
|
|
|
- $template = EmailTemplate::findOrFail($id);
|
|
|
- $this->subject = $template->name;
|
|
|
- $this->message = $template->content;
|
|
|
- $this->add = true;
|
|
|
- $this->update = false;
|
|
|
- } catch (\Exception $ex) {
|
|
|
- session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
|
|
|
+ foreach ($records as $record) {
|
|
|
+ $this->categories[] = array('id' => $record->id, 'name' => $record->getTree());
|
|
|
+ if (count($record->childs))
|
|
|
+ $this->getCategories($record->childs, $indentation + 1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private function sendEmailNow($template, $recipients)
|
|
|
+ public function getCourses($records, $indentation)
|
|
|
{
|
|
|
- foreach ($recipients as $recipient) {
|
|
|
- if ($recipient->email) {
|
|
|
- try {
|
|
|
- Log::info("Email sent to {$recipient->name} ({$recipient->email}): Subject: {$template->name}");
|
|
|
- } catch (\Exception $e) {
|
|
|
- Log::error("Failed to send email to {$recipient->email}: " . $e->getMessage());
|
|
|
+ /** @var \App\Models\Course $record */
|
|
|
+ foreach ($records as $record) {
|
|
|
+ $this->courses[] = array('id' => $record->id, 'name' => $record->getTree());
|
|
|
+ if (count($record->childs))
|
|
|
+ $this->getCourses($record->childs, $indentation + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function toggleRecipient($id)
|
|
|
+ {
|
|
|
+ $id = (int)$id;
|
|
|
+
|
|
|
+ $idx = collect($this->recipients)->search(fn($r) => (int)($r['member_id'] ?? 0) === $id);
|
|
|
+ if ($idx !== false) {
|
|
|
+ array_splice($this->recipients, $idx, 1);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $m = Member::select('id', 'email', 'first_name', 'last_name')->find($id);
|
|
|
+ if (!$m || empty($m->email)) return;
|
|
|
+
|
|
|
+ $this->recipients[] = [
|
|
|
+ 'member_id' => $m->id,
|
|
|
+ 'email_address' => $m->email,
|
|
|
+ 'first_name' => $m->first_name,
|
|
|
+ 'last_name' => $m->last_name,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function removeNewAttachment(int $index): void
|
|
|
+ {
|
|
|
+ if ($this->locked) return;
|
|
|
+ if (is_array($this->newAttachments) && array_key_exists($index, $this->newAttachments)) {
|
|
|
+ array_splice($this->newAttachments, $index, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function removeExistingAttachment(int $id): void
|
|
|
+ {
|
|
|
+ if ($this->locked || !$this->messageId) return;
|
|
|
+
|
|
|
+ $att = \App\Models\EmailMessageAttachment::find($id);
|
|
|
+ if (!$att || $att->email_message_id !== $this->messageId) return;
|
|
|
+
|
|
|
+ try {
|
|
|
+ if ($att->disk && $att->path) {
|
|
|
+ $disk = Storage::disk($att->disk);
|
|
|
+ $dir = dirname($att->path);
|
|
|
+ $disk->delete($att->path);
|
|
|
+
|
|
|
+ if (empty($disk->files($dir)) && empty($disk->directories($dir))) {
|
|
|
+ $disk->deleteDirectory($dir);
|
|
|
}
|
|
|
- } else {
|
|
|
- Log::warning("User {$recipient->id} has no email address");
|
|
|
}
|
|
|
+ } catch (\Throwable $e) {
|
|
|
}
|
|
|
+
|
|
|
+ $att->delete();
|
|
|
+
|
|
|
+ $this->existingAttachments = array_values(array_filter(
|
|
|
+ $this->existingAttachments,
|
|
|
+ fn($a) => (int)$a['id'] !== (int)$id
|
|
|
+ ));
|
|
|
}
|
|
|
|
|
|
- private function scheduleEmail($template, $recipients)
|
|
|
+
|
|
|
+ protected function refreshAttachments(EmailMessage $msg): void
|
|
|
{
|
|
|
- $scheduled = EmailScheduled::create([
|
|
|
- 'template_id' => $template->id,
|
|
|
- 'subject' => $template->name,
|
|
|
- 'content' => $template->content,
|
|
|
- 'scheduled_at' => Carbon::parse($this->scheduledDateTime),
|
|
|
- 'status' => 'scheduled',
|
|
|
- 'created_by' => Auth::id(),
|
|
|
- ]);
|
|
|
+ $this->existingAttachments = $msg->attachments()
|
|
|
+ ->get()
|
|
|
+ ->map(fn($a) => [
|
|
|
+ 'id' => $a->id,
|
|
|
+ 'name' => $a->name ?: basename($a->path),
|
|
|
+ 'size' => $a->size_human,
|
|
|
+ 'url' => $a->public_url,
|
|
|
+ 'img' => $a->is_image,
|
|
|
+ ])->toArray();
|
|
|
+
|
|
|
+ $this->newAttachments = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function deleteMessage(int $id)
|
|
|
+ {
|
|
|
+ $msg = \App\Models\EmailMessage::with(['attachments'])->findOrFail($id);
|
|
|
+
|
|
|
+ if (! in_array($msg->status, ['draft', 'failed'], true)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($msg->attachments as $a) {
|
|
|
+ try {
|
|
|
+ Storage::disk($a->disk ?? 'public')->delete($a->path);
|
|
|
+ } catch (\Throwable $e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- $scheduled->recipients()->attach($recipients->pluck('id'));
|
|
|
+ $msg->delete();
|
|
|
|
|
|
- Log::info("Email scheduled for {$this->scheduledDateTime} to {$recipients->count()} recipients: {$template->name}");
|
|
|
+ $this->dispatchBrowserEvent('email-deleted', ['id' => $id]);
|
|
|
}
|
|
|
}
|