فهرست منبع

modulo comunicazioni email - modifiche

ferrari 2 ماه پیش
والد
کامیت
571f260741

+ 36 - 9
app/Console/Commands/DispatchDueEmails.php

@@ -5,6 +5,8 @@ namespace App\Console\Commands;
 use Illuminate\Console\Command;
 use App\Models\EmailMessage;
 use App\Jobs\SendEmailMessage;
+use DateTimeZone;
+use Illuminate\Foundation\Auth\User;
 
 class DispatchDueEmails extends Command
 {
@@ -13,15 +15,40 @@ class DispatchDueEmails extends Command
 
     public function handle()
     {
-        app(\App\Http\Middleware\TenantMiddleware::class)->setupTenantConnection();
-
-        EmailMessage::where('status', 'scheduled')
-            ->where('schedule_at', '<=', now())
-            ->chunkById(100, function ($chunk) {
-                foreach ($chunk as $msg) {
-                    dispatch(new SendEmailMessage($msg->id));
-                }
-            });
+        $users = User::whereNotNull('tenant_host')
+            ->whereNotNull('tenant_database')
+            ->whereNotNull('tenant_username')
+            ->whereNotNull('tenant_password')
+            ->get([
+                'id',
+                'tenant_host',
+                'tenant_database',
+                'tenant_username',
+                'tenant_password',
+            ]);
+
+        if ($users->isEmpty()) {
+            $this->warn('Nessun utente con info di database trovata.');
+            return Command::SUCCESS;
+        }
+
+        $tenants = $users->unique(function ($u) {
+            return $u->tenant_host . '|' . $u->tenant_database . '|' . $u->tenant_username;
+        });
+
+        foreach ($tenants as $userTenant) {
+            $this->info("Processo tenant db={$userTenant->tenant_database} (user id={$userTenant->id})");
+
+            app(\App\Http\Middleware\TenantMiddleware::class)->setupTenantConnection($userTenant);
+
+            EmailMessage::where('status', 'scheduled')
+                ->where('schedule_at', '<=', now())
+                ->chunkById(100, function ($chunk) {
+                    foreach ($chunk as $msg) {
+                        dispatch(new SendEmailMessage($msg->id));
+                    }
+                });
+        }
 
         return Command::SUCCESS;
     }

+ 11 - 7
app/Http/Livewire/EmailComunications.php

@@ -27,6 +27,7 @@ class EmailComunications extends Component
 
     public string $mode = 'now'; // 'now' | 'schedule'
     public ?string $schedule_at = null;
+    public ?string $timezone = 'UTC';
 
     public $records;
     public $categories;
@@ -55,7 +56,7 @@ class EmailComunications extends Component
         $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');
+        $this->schedule_at = now($this->timezone)->addHour()->format('Y-m-d\TH:i');
     }
 
     public function render()
@@ -108,7 +109,7 @@ class EmailComunications extends Component
     {
         $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->schedule_at = now($this->timezone)->addHour()->format('Y-m-d\TH:i');
         $this->existingAttachments = [];
 
         $this->showForm = true;
@@ -137,7 +138,7 @@ class EmailComunications extends Component
                 '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->schedule_at = optional($msg->schedule_at)?->setTimezone($this->timezone)?->format('Y-m-d\TH:i');
             $this->existingAttachments = $msg->attachments->map(fn($a) => [
                 'id'   => $a->id,
                 'name' => $a->name ?: basename($a->path),
@@ -239,8 +240,10 @@ class EmailComunications extends Component
             }
         }
 
-        DB::transaction(function () {
-            $msg = $this->upsertMessage(status: 'scheduled', scheduleAt: \Carbon\Carbon::parse($this->schedule_at));
+        $scheduledAt = \Carbon\Carbon::parse($this->schedule_at, $this->timezone)->setTimezone('UTC');
+
+        DB::transaction(function () use ($scheduledAt) {
+            $msg = $this->upsertMessage(status: 'scheduled', scheduleAt: $scheduledAt);
             $this->upsertRecipients($msg, true);
             $this->upsertAttachments($msg, true);
             $this->messageId = $msg->id;
@@ -265,7 +268,8 @@ class EmailComunications extends Component
                 'content_html' => $this->content_html,
                 'status'       => $status,
                 'schedule_at'  => $scheduleAt,
-                'created_by'   => auth()->id(),
+                // 'created_by'   => auth()->id(),
+                'created_by'   => 8,
             ]
         );
     }
@@ -310,7 +314,7 @@ class EmailComunications extends Component
         $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->schedule_at = now($this->timezone)->addHour()->format('Y-m-d\TH:i');
 
         $this->dispatchBrowserEvent('init-archive-table');
     }

+ 12 - 5
resources/views/livewire/email_comunications.blade.php

@@ -68,15 +68,15 @@
                             <td><span class="badge bg-{{$badgeMap[$state]}}">{{ $record->status }}</span></td>
                             <td>
                                 @if(!empty($record->schedule_at))
-                                    {{ optional($record->schedule_at)->setTimezone('Europe/Rome')->format('d/m/Y H:i') }}
+                                    {{ optional($record->schedule_at)->setTimezone('Europe/Rome')->format('d M Y - H:i') }}
                                 @endif
                             </td>
                             <td>
                                 @if(!empty($record->sent_at))
-                                    {{ optional($record->sent_at)->setTimezone('Europe/Rome')->format('d/m/Y H:i') }}
+                                    {{ optional($record->sent_at)->setTimezone('Europe/Rome')->format('d M Y - H:i') }}
                                 @endif
                             </td>
-                            <td>{{ optional($record->created_at)->setTimezone('Europe/Rome')->format('d/m/Y H:i') }}</td>
+                            <td>{{ optional($record->created_at)->setTimezone('Europe/Rome')->format('d M Y - H:i') }}</td>
                             <td class="d-flex gap-2">
                                 <button type="button" class="btn" wire:click="edit({{ $record->id }})" data-bs-toggle="tooltip" data-bs-trigger="hover focus" data-bs-placement="bottom" title="Modifica">
                                     <i class="fa-regular fa-pen-to-square"></i>
@@ -444,6 +444,8 @@
             </div>
         </div>
     </section>
+
+    <input type="hidden" name="timezone" id="timezone" wire:model="timezone">
 </div>
 
 @if (session()->has('success'))
@@ -491,13 +493,18 @@
                 dt.row(rowEl).remove().draw(false);
             }
         });
-    });
-
 
+        const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
+        @this.set('timezone', tz);
+    });
 
     window.addEventListener('init-recipients-table', (e) => {
         const selected = e.detail?.selected || [];
         loadDataTable(selected);
+
+        
+        const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
+        @this.set('timezone', tz);
     });
 
     function loadArchiveDataTable(){