Browse Source

feature: add envio de e-mail ao aprovar prestador na plataforma

Gustavo Mantovani 2 weeks ago
parent
commit
73c3c5b75e

+ 37 - 0
app/Mail/ProviderApprovedMail.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace App\Mail;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Mail\Mailable;
+use Illuminate\Mail\Mailables\Content;
+use Illuminate\Mail\Mailables\Envelope;
+use Illuminate\Queue\SerializesModels;
+
+class ProviderApprovedMail extends Mailable
+{
+    use Queueable, SerializesModels;
+
+    public function __construct(
+        public readonly string $recipientName = '',
+    ) {}
+
+    public function envelope(): Envelope
+    {
+        return new Envelope(
+            subject: __('mail.provider_approved.subject'),
+        );
+    }
+
+    public function content(): Content
+    {
+        return new Content(
+            view: 'emails.provider_approved',
+        );
+    }
+
+    public function attachments(): array
+    {
+        return [];
+    }
+}

+ 12 - 0
app/Services/EmailService.php

@@ -3,6 +3,7 @@
 namespace App\Services;
 
 use App\Mail\EmailReceipt;
+use App\Mail\ProviderApprovedMail;
 use App\Mail\SendCodeMail;
 use Illuminate\Support\Facades\Mail;
 
@@ -20,6 +21,17 @@ class EmailService
         Mail::to($email)->send(new SendCodeMail($code, $recipientName));
     }
 
+    public function sendProviderApproved(string $email, string $recipientName = '', ?string $locale = null): void
+    {
+        $pendingMail = Mail::to($email);
+
+        if (! empty($locale)) {
+            $pendingMail->locale($locale);
+        }
+
+        $pendingMail->send(new ProviderApprovedMail($recipientName));
+    }
+
     public function sendEmailReceipt(
         string $email,
         object $schedule,

+ 48 - 3
app/Services/ProviderService.php

@@ -23,6 +23,7 @@ class ProviderService
     public function __construct(
         private readonly PagarmeRecipientService $pagarmeRecipientService,
         private readonly MediaService $mediaService,
+        private readonly EmailService $emailService,
     ) {}
 
     public function getAll(): Collection
@@ -63,6 +64,8 @@ class ProviderService
             return null;
         }
 
+        $wasAccepted = $model->approval_status === ApprovalStatusEnum::ACCEPTED;
+
         if (isset($data['avatar']) && $data['avatar'] instanceof UploadedFile) {
             $media = $this->mediaService->replaceFile(
                 newFile: $data['avatar'],
@@ -79,7 +82,13 @@ class ProviderService
 
         $model->update($data);
 
-        return $model->fresh(['user', 'profileMedia']);
+        $provider = $model->fresh(['user', 'profileMedia']);
+
+        if (! $wasAccepted && $provider->approval_status === ApprovalStatusEnum::ACCEPTED) {
+            $this->sendApprovedEmail($provider);
+        }
+
+        return $provider;
     }
 
     public function delete(int $id): bool
@@ -108,13 +117,20 @@ class ProviderService
 
     public function approve(int $id): Provider
     {
-        return DB::transaction(function () use ($id) {
+        [$provider, $wasAccepted] = DB::transaction(function () use ($id) {
             $provider = Provider::findOrFail($id);
+            $wasAccepted = $provider->approval_status === ApprovalStatusEnum::ACCEPTED;
 
             $provider->update(['approval_status' => ApprovalStatusEnum::ACCEPTED->value]);
 
-            return $provider->fresh(['user', 'profileMedia']);
+            return [$provider->fresh(['user', 'profileMedia']), $wasAccepted];
         });
+
+        if (! $wasAccepted) {
+            $this->sendApprovedEmail($provider);
+        }
+
+        return $provider;
     }
 
     public function reject(int $id): Provider
@@ -432,4 +448,33 @@ class ProviderService
 
         return $digits === '' ? null : $digits;
     }
+
+    //
+
+    private function sendApprovedEmail(Provider $provider): void
+    {
+        if (! empty($provider->user?->email)) {
+            try {
+                $this->emailService->sendProviderApproved(
+                    email: $provider->user->email,
+                    recipientName: $provider->user->name ?? '',
+                    locale: $provider->user->language?->value,
+                );
+            } catch (\Throwable $exception) {
+                Log::error('Provider approval email failed', [
+                    'provider_id' => $provider->id,
+                    'user_id'     => $provider->user?->id,
+                    'email'       => $provider->user?->email,
+                    'error'       => $exception->getMessage(),
+                ]);
+            }
+
+            return;
+        }
+
+        Log::warning('Provider approval email skipped: provider user has no email', [
+            'provider_id' => $provider->id,
+            'user_id'     => $provider->user?->id,
+        ]);
+    }
 }

+ 11 - 0
lang/en/mail.php

@@ -25,6 +25,17 @@ return [
         'footer_note'        => 'This is an automated email. Please do not reply.',
         'footer_title'       => 'Thank you for using the Diaria App ',
     ],
+    'provider_approved' => [
+        'subject'            => 'Registration approved on Diaria App',
+        'title'              => 'Registration approved',
+        'header_subtitle'    => 'Your provider profile has been approved.',
+        'greeting'           => 'Hello, :name!',
+        'greeting_anonymous' => 'Hello!',
+        'body_intro'         => 'Your registration has been approved by our team. You can now access the app and start receiving service opportunities.',
+        'next_step'          => 'Log in to the app with the same email or phone used during registration to access your account.',
+        'footer_title'       => 'Welcome to Diaria App',
+        'footer_note'        => 'This is an automated email. Please do not reply.',
+    ],
     'service_completed' => [
         'subject'                    => 'Service completed successfully',
         'title'                      => 'Service Completed',

+ 11 - 0
lang/es/mail.php

@@ -25,6 +25,17 @@ return [
         'footer_note'        => 'Este es un correo automático. Por favor, no respondas.',
         'footer_title'       => 'Gracias por usar la aplicación Diaria ',
     ],
+    'provider_approved' => [
+        'subject'            => 'Registro aprobado en Diaria App',
+        'title'              => 'Registro aprobado',
+        'header_subtitle'    => 'Tu perfil de prestador fue aprobado.',
+        'greeting'           => '¡Hola, :name!',
+        'greeting_anonymous' => '¡Hola!',
+        'body_intro'         => 'Tu registro fue aprobado por nuestro equipo. Ahora puedes acceder a la aplicación y comenzar a recibir oportunidades de servicio.',
+        'next_step'          => 'Ingresa a la app con el mismo correo o teléfono usado en el registro para acceder a tu cuenta.',
+        'footer_title'       => 'Bienvenido a Diaria App',
+        'footer_note'        => 'Este es un correo automático. Por favor, no respondas.',
+    ],
     'service_completed' => [
         'subject'                    => 'Servicio finalizado con éxito',
         'title'                      => 'Servicio Finalizado',

+ 11 - 0
lang/pt/mail.php

@@ -25,6 +25,17 @@ return [
         'footer_note'        => 'Este é um e-mail automático. Por favor, não responda.',
         'footer_title'       => 'Obrigado por utilizar o Diaria App ',
     ],
+    'provider_approved' => [
+        'subject'            => 'Cadastro aprovado no Diaria App',
+        'title'              => 'Cadastro aprovado',
+        'header_subtitle'    => 'Seu perfil de prestador foi liberado.',
+        'greeting'           => 'Olá, :name!',
+        'greeting_anonymous' => 'Olá!',
+        'body_intro'         => 'Seu cadastro foi aprovado pela nossa equipe. Agora você já pode acessar o aplicativo e começar a receber oportunidades de serviço.',
+        'next_step'          => 'Entre no app com o mesmo e-mail ou telefone usado no cadastro para acessar sua conta.',
+        'footer_title'       => 'Bem-vindo ao Diaria App',
+        'footer_note'        => 'Este é um e-mail automático. Por favor, não responda.',
+    ],
     'service_completed' => [
         'subject'                    => 'Serviço finalizado com sucesso',
         'title'                      => 'Serviço Finalizado',

+ 135 - 0
resources/views/emails/provider_approved.blade.php

@@ -0,0 +1,135 @@
+<!DOCTYPE html>
+<html lang="pt-BR">
+
+<head>
+    <meta charset="UTF-8" />
+    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+
+    <title>
+        {{ __('mail.provider_approved.subject') }}
+    </title>
+</head>
+
+<body style="margin:0;padding:0;background:#f5f5f7;font-family:Arial,Helvetica,sans-serif;">
+
+    <table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#f5f5f7">
+        <tr>
+            <td align="center" style="padding:25px;">
+
+                <table width="650" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff"
+                    style="border-radius:24px;overflow:hidden;">
+
+                    <tr>
+                        <td style="background:#5a22d6;padding:50px 40px;color:white;">
+
+                            <table width="100%">
+                                <tr>
+                                    <td valign="middle">
+                                        <div style="font-size:44px;font-weight:bold;line-height:1.1;">
+                                            {{ __('mail.provider_approved.title') }}
+                                        </div>
+
+                                        <div style="font-size:20px;margin-top:20px;line-height:1.6;">
+                                            {{ __('mail.provider_approved.header_subtitle') }}
+                                        </div>
+                                    </td>
+
+                                    <td width="220" align="right" valign="middle">
+                                        <img src="{{ asset('images/diarinho_email_welcome.svg') }}" alt="Diarinho"
+                                            width="170"
+                                            style="display:block;border:0;width:170px;max-width:170px;height:auto;margin-left:auto;">
+                                    </td>
+                                </tr>
+                            </table>
+
+                        </td>
+                    </tr>
+
+                    <tr>
+                        <td style="padding:30px;background:#f5f5f7;">
+
+                            <table width="100%" bgcolor="#ffffff"
+                                style="border-radius:24px;padding:35px;margin-bottom:25px;">
+
+                                <tr>
+                                    <td>
+                                        @if (!empty($recipientName))
+                                            <div style="font-size:34px;font-weight:bold;color:#222;line-height:1.4;">
+                                                {{ __('mail.provider_approved.greeting', ['name' => $recipientName]) }}
+                                            </div>
+                                        @else
+                                            <div style="font-size:34px;font-weight:bold;color:#222;line-height:1.4;">
+                                                {{ __('mail.provider_approved.greeting_anonymous') }}
+                                            </div>
+                                        @endif
+                                    </td>
+                                </tr>
+
+                                <tr>
+                                    <td style="padding-top:25px;font-size:20px;color:#666;line-height:1.8;">
+                                        {{ __('mail.provider_approved.body_intro') }}
+                                    </td>
+                                </tr>
+
+                                <tr>
+                                    <td style="padding-top:30px;">
+                                        <table width="100%" bgcolor="#f3ecff"
+                                            style="border-radius:18px;padding:24px;">
+                                            <tr>
+                                                <td style="font-size:18px;color:#5a22d6;line-height:1.8;font-weight:bold;">
+                                                    {{ __('mail.provider_approved.next_step') }}
+                                                </td>
+                                            </tr>
+                                        </table>
+                                    </td>
+                                </tr>
+
+                            </table>
+
+                            <table width="100%" bgcolor="#f1ebff"
+                                style="border-radius:24px;padding:25px;margin-bottom:25px;">
+
+                                <tr>
+                                    <td width="130" valign="middle">
+                                        <img src="{{ asset('images/diarinho_email_code.svg') }}" alt="Diarinho"
+                                            width="120"
+                                            style="display:block;border:0;width:120px;max-width:120px;height:auto;">
+                                    </td>
+
+                                    <td valign="middle">
+                                        <div style="font-size:30px;font-weight:bold;color:#5a22d6;">
+                                            {{ __('mail.provider_approved.footer_title') }}
+                                        </div>
+
+                                        <div style="font-size:18px;color:#666;line-height:1.7;margin-top:10px;">
+                                            {{ __('mail.provider_approved.footer_note') }}
+                                        </div>
+                                    </td>
+                                </tr>
+
+                            </table>
+
+                        </td>
+                    </tr>
+
+                    <tr>
+                        <td style="background:#5a22d6;padding:35px;color:white;">
+                            <div style="text-align:right;font-size:18px;line-height:1.7;">
+                                {{ __('mail.provider_approved.footer_note') }}
+                            </div>
+
+                            <div style="margin-top:25px;text-align:center;font-size:13px;opacity:0.7;">
+                                © {{ date('Y') }}
+                            </div>
+                        </td>
+                    </tr>
+
+                </table>
+
+            </td>
+        </tr>
+    </table>
+
+</body>
+
+</html>