libra/docs/stories/story-2.5-account-creation-...

6.9 KiB

Story 2.5: Account Creation Email Notification

Epic Reference

Epic 2: User Management System

User Story

As an admin, I want welcome emails sent automatically when I create client accounts, So that clients receive their login credentials and know how to access the platform.

Story Context

Existing System Integration

  • Integrates with: User creation flow, Laravel Mail
  • Technology: Laravel Mailable, queued jobs
  • Follows pattern: Laravel notification/mailable patterns
  • Touch points: User model events, email templates

Acceptance Criteria

Welcome Email Trigger

  • Email sent automatically on account creation
  • Works for both individual and company accounts
  • Queued for performance (async sending)
  • No email sent for admin accounts

Email Content

  • Personalized greeting:
    • Individual: "Dear [Name]"
    • Company: "Dear [Company Name]"
  • Message: "Your account has been created"
  • Login credentials:
    • Email address
    • Password (shown in email)
  • Login URL (clickable button/link)
  • Brief platform introduction
  • Contact information for questions

Email Design

  • Professional template with Libra branding
  • Colors: Navy blue (#0A1F44) and Gold (#D4AF37)
  • Libra logo in header
  • Footer with firm information
  • Mobile-responsive layout

Sender Configuration

  • From: no-reply@libra.ps
  • From Name: Libra Law Firm / مكتب ليبرا للمحاماة
  • Reply-To: (firm contact email)

Language Support

  • Email in user's preferred_language
  • Arabic email for Arabic preference
  • English email for English preference
  • All text translated

Plain Text Fallback

  • Plain text version generated
  • All essential information included
  • Readable without HTML

Quality Requirements

  • Email passes spam filters
  • Links work correctly
  • Password visible but not overly prominent
  • Tests verify email sending

Technical Notes

Mailable Class

<?php

namespace App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class WelcomeEmail extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(
        public User $user,
        public string $password
    ) {}

    public function envelope(): Envelope
    {
        $locale = $this->user->preferred_language ?? 'ar';

        return new Envelope(
            subject: $locale === 'ar'
                ? 'مرحباً بك في مكتب ليبرا للمحاماة'
                : 'Welcome to Libra Law Firm',
        );
    }

    public function content(): Content
    {
        $locale = $this->user->preferred_language ?? 'ar';

        return new Content(
            markdown: "emails.welcome.{$locale}",
            with: [
                'user' => $this->user,
                'password' => $this->password,
                'loginUrl' => route('login'),
            ],
        );
    }
}

Email Template (Arabic)

<!-- resources/views/emails/welcome/ar.blade.php -->
<x-mail::message>
# مرحباً بك في مكتب ليبرا للمحاماة

@if($user->user_type === 'company')
عزيزي {{ $user->company_name }},
@else
عزيزي {{ $user->name }},
@endif

تم إنشاء حسابك بنجاح على منصة مكتب ليبرا للمحاماة.

**بيانات تسجيل الدخول:**

- **البريد الإلكتروني:** {{ $user->email }}
- **كلمة المرور:** {{ $password }}

<x-mail::button :url="$loginUrl">
تسجيل الدخول
</x-mail::button>

يمكنك الآن الوصول إلى:
- حجز المواعيد
- متابعة قضاياك
- عرض التحديثات

إذا كان لديك أي استفسار، لا تتردد في التواصل معنا.

مع أطيب التحيات,<br>
مكتب ليبرا للمحاماة
</x-mail::message>

Email Template (English)

<!-- resources/views/emails/welcome/en.blade.php -->
<x-mail::message>
# Welcome to Libra Law Firm

@if($user->user_type === 'company')
Dear {{ $user->company_name }},
@else
Dear {{ $user->name }},
@endif

Your account has been successfully created on the Libra Law Firm platform.

**Login Credentials:**

- **Email:** {{ $user->email }}
- **Password:** {{ $password }}

<x-mail::button :url="$loginUrl">
Login Now
</x-mail::button>

You can now access:
- Book consultations
- Track your cases
- View updates

If you have any questions, please don't hesitate to contact us.

Best regards,<br>
Libra Law Firm
</x-mail::message>

Trigger on User Creation

// In User creation flow (Story 2.1/2.2)
public function create(): void
{
    $validated = $this->validate();
    $plainPassword = $this->password;

    $user = User::create([
        ...$validated,
        'password' => Hash::make($this->password),
    ]);

    // Send welcome email with plain password
    Mail::to($user)->queue(new WelcomeEmail($user, $plainPassword));

    session()->flash('success', __('messages.user_created'));
}

Email Theme Customization

// In AppServiceProvider boot()
use Illuminate\Support\Facades\View;

View::composer('vendor.mail.*', function ($view) {
    $view->with('logoUrl', asset('images/logo.png'));
    $view->with('primaryColor', '#0A1F44');
    $view->with('accentColor', '#D4AF37');
});

Testing

use App\Mail\WelcomeEmail;
use Illuminate\Support\Facades\Mail;

it('sends welcome email on user creation', function () {
    Mail::fake();

    // Create user through admin flow
    // ...

    Mail::assertQueued(WelcomeEmail::class, function ($mail) use ($user) {
        return $mail->user->id === $user->id;
    });
});

it('sends email in user preferred language', function () {
    Mail::fake();

    $user = User::factory()->create(['preferred_language' => 'ar']);

    Mail::to($user)->send(new WelcomeEmail($user, 'password123'));

    Mail::assertSent(WelcomeEmail::class, function ($mail) {
        return str_contains($mail->envelope()->subject, 'مرحباً');
    });
});

Definition of Done

  • Welcome email sent on user creation
  • Email contains all required information
  • Login credentials included
  • Branding matches design guidelines
  • Arabic email for Arabic preference
  • English email for English preference
  • Plain text fallback works
  • Email queued (not blocking)
  • Tests verify email sending
  • Code formatted with Pint

Dependencies

  • Story 2.1: Individual client creation
  • Story 2.2: Company client creation
  • Epic 8: Full email infrastructure (shared base template)

Risk Assessment

  • Primary Risk: Email delivery failures
  • Mitigation: Queue with retry, logging, admin notification on failure
  • Rollback: Manual credential sharing if email fails

Estimation

Complexity: Medium Estimated Effort: 3-4 hours