feat(Contact & membership forms update with their notification)
All checks were successful
Deploy Roxane to Preprod / deploy (push) Successful in 26h10m46s
All checks were successful
Deploy Roxane to Preprod / deploy (push) Successful in 26h10m46s
This commit is contained in:
@@ -7,32 +7,33 @@ use App\Http\Requests\Forms\ContactRequest;
|
||||
use App\Services\ContactService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class ContactFormController extends Controller
|
||||
{
|
||||
public function __construct(protected ContactService $contactService) {}
|
||||
/**
|
||||
* Show the contact form page.
|
||||
*/
|
||||
public function create()
|
||||
|
||||
public function create(): Response
|
||||
{
|
||||
return Inertia::render('forms/contact');
|
||||
return Inertia::render('forms/contact', [
|
||||
'captcha_question' => $this->generateCaptcha(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming contact form submission.
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function store(ContactRequest $request): RedirectResponse
|
||||
{
|
||||
$validated = $request->validated();
|
||||
|
||||
try {
|
||||
$this->contactService->registerNewContactRequest($validated);
|
||||
} catch (\Throwable $e) {
|
||||
\Log::error('Erreur lors de la création d\'un contact', [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
'data' => $validated,
|
||||
'data' => $validated,
|
||||
]);
|
||||
|
||||
return to_route('contact')->with('error', __('contacts.responses.error'));
|
||||
@@ -41,4 +42,13 @@ class ContactFormController extends Controller
|
||||
return to_route('contact')->with('success', __('contacts.responses.success'));
|
||||
}
|
||||
|
||||
private function generateCaptcha(): string
|
||||
{
|
||||
$a = random_int(1, 9);
|
||||
$b = random_int(1, 9);
|
||||
|
||||
session(['captcha_contact' => (string) ($a + $b)]);
|
||||
|
||||
return "Combien font {$a} + {$b} ?";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,55 +4,69 @@ namespace App\Http\Controllers\Forms;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Forms\MembershipRequest;
|
||||
use App\Models\Membership;
|
||||
use App\Models\Package;
|
||||
use App\Models\Service;
|
||||
use App\Services\MemberService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class MembershipFormController extends Controller
|
||||
{
|
||||
public function __construct(protected MemberService $memberService) {}
|
||||
|
||||
/**
|
||||
* Show the contact form page.
|
||||
*/
|
||||
public function create()
|
||||
public function create(): Response
|
||||
{
|
||||
$remainingMonths = 13 - now()->month;
|
||||
|
||||
$plans = Package::query()
|
||||
->where('is_active', true)
|
||||
->select('id', 'identifier', 'name', 'price', 'description')
|
||||
->get()
|
||||
->map(fn (Package $p) => [
|
||||
'id' => $p->id,
|
||||
'identifier' => $p->identifier,
|
||||
'name' => $p->name,
|
||||
'description' => $p->description,
|
||||
'price' => $p->identifier === 'custom' ? $remainingMonths : (float) $p->price,
|
||||
'months' => $p->identifier === 'custom' ? $remainingMonths : null,
|
||||
]);
|
||||
|
||||
return Inertia::render('forms/membership', [
|
||||
'plans' => Package::query()
|
||||
->where('is_active', true)
|
||||
->select('id', 'identifier', 'name', 'price', 'description')
|
||||
->get()
|
||||
'plans' => $plans,
|
||||
'services' => Service::query()->select('name', 'description')->get(),
|
||||
'captcha_question' => $this->generateCaptcha(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming membership form request.
|
||||
*
|
||||
*/
|
||||
public function store(MembershipRequest $request): RedirectResponse
|
||||
{
|
||||
dd($request->validated());
|
||||
$validated = $request->validated();
|
||||
|
||||
try {
|
||||
$this->memberService->registerNewMember($validated);
|
||||
} catch (\Throwable $e) {
|
||||
\Log::error('Erreur lors de la création d’un membre', [
|
||||
\Log::error('Erreur lors de la création d\'un membre', [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
'data' => $validated,
|
||||
'data' => $validated,
|
||||
]);
|
||||
|
||||
return redirect()
|
||||
->route('membership')
|
||||
->with('error', Membership::getAttributeLabel('memberships.subscription.error'));
|
||||
return to_route('membership')
|
||||
->with('error', __('memberships.fields.subscription.error'));
|
||||
}
|
||||
|
||||
return redirect()
|
||||
->route('membership')
|
||||
->with('success', Membership::getAttributeLabel('memberships.subscription.success'));
|
||||
return to_route('membership')
|
||||
->with('success', __('memberships.fields.subscription.success'));
|
||||
}
|
||||
|
||||
private function generateCaptcha(): string
|
||||
{
|
||||
$a = random_int(1, 9);
|
||||
$b = random_int(1, 9);
|
||||
|
||||
session(['captcha_membership' => (string) ($a + $b)]);
|
||||
|
||||
return "Combien font {$a} + {$b} ?";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,21 +2,17 @@
|
||||
|
||||
namespace App\Http\Requests\Forms;
|
||||
|
||||
use App\Rules\ValidCaptcha;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ContactRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
@@ -25,9 +21,10 @@ class ContactRequest extends FormRequest
|
||||
'lastname' => 'required|string|max:255',
|
||||
'firstname' => 'required|string|max:255',
|
||||
'email' => 'required|email|max:255',
|
||||
'address' => 'string|max:255',
|
||||
'address' => 'nullable|string|max:255',
|
||||
'subject' => 'required|string|max:255',
|
||||
'message' => 'required|string',
|
||||
'captcha' => ['required', new ValidCaptcha('captcha_contact')],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,39 +2,34 @@
|
||||
|
||||
namespace App\Http\Requests\Forms;
|
||||
|
||||
use App\Rules\ValidCaptcha;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class MembershipRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
// Member
|
||||
'lastname' => 'required|string|max:255',
|
||||
'firstname' => 'required|string|max:255',
|
||||
'email' => 'required|email|max:255',
|
||||
'company' => 'string|max:255',
|
||||
'company' => 'nullable|string|max:255',
|
||||
'address' => 'required|string|max:255',
|
||||
'zipcode' => 'required|string|max:255',
|
||||
'city' => 'required|string|max:255',
|
||||
'phone1' => 'required|string|max:255',
|
||||
|
||||
// Membership
|
||||
'package' => 'required|string|max:255',
|
||||
'amount' => 'required|string|max:255',
|
||||
'amount' => 'required|numeric|min:0',
|
||||
'cgu' => 'required|accepted',
|
||||
'captcha' => ['required', new ValidCaptcha('captcha_membership')],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
65
app/Notifications/MemberNewRequestAdminNotification.php
Normal file
65
app/Notifications/MemberNewRequestAdminNotification.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Filament\Resources\Members\MemberResource;
|
||||
use App\Models\Member;
|
||||
use App\Models\NotificationTemplate;
|
||||
use App\Models\Package;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class MemberNewRequestAdminNotification extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public function __construct(
|
||||
public readonly Member $member,
|
||||
public readonly Package $package,
|
||||
public readonly float $amount,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
return ['mail'];
|
||||
}
|
||||
|
||||
public function toMail(object $notifiable): MailMessage
|
||||
{
|
||||
$template = NotificationTemplate::findByIdentifier('member_new_request_admin');
|
||||
|
||||
$vars = [
|
||||
'member_name' => $this->member->full_name,
|
||||
'member_email' => $this->member->email ?? '',
|
||||
'member_phone' => $this->member->phone1 ?? '',
|
||||
'member_address' => implode(', ', array_filter([
|
||||
$this->member->address,
|
||||
$this->member->zipcode,
|
||||
$this->member->city,
|
||||
])),
|
||||
'package_name' => $this->package->name,
|
||||
'amount' => number_format($this->amount, 2, ',', ' '),
|
||||
'member_url' => MemberResource::getUrl('edit', ['record' => $this->member->id]),
|
||||
'app_name' => config('app.name'),
|
||||
];
|
||||
|
||||
return (new MailMessage)
|
||||
->subject($template->renderSubject($vars))
|
||||
->view('notifications.mail-template', [
|
||||
'body' => $template->renderBody($vars),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(object $notifiable): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
21
app/Rules/ValidCaptcha.php
Normal file
21
app/Rules/ValidCaptcha.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class ValidCaptcha implements ValidationRule
|
||||
{
|
||||
public function __construct(private readonly string $sessionKey = 'captcha_answer') {}
|
||||
|
||||
/**
|
||||
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
if (trim((string) $value) !== (string) session($this->sessionKey)) {
|
||||
$fail('Le code de vérification est incorrect.');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use App\Models\MemberGroup;
|
||||
use App\Models\Package;
|
||||
use App\Notifications\MemberDeactivatedAdminNotification;
|
||||
use App\Notifications\MemberDeactivatedMemberNotification;
|
||||
use App\Notifications\MemberNewRequestAdminNotification;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
|
||||
class MemberService
|
||||
@@ -51,6 +52,9 @@ class MemberService
|
||||
|
||||
]);
|
||||
|
||||
Notification::route('mail', config('app.admin_email'))
|
||||
->notify(new MemberNewRequestAdminNotification($member, $package, (float) $data['amount']));
|
||||
|
||||
event(new MemberRegistered($member));
|
||||
|
||||
return $member;
|
||||
|
||||
Reference in New Issue
Block a user