diff --git a/app/Http/Controllers/Fo/CustomerController.php b/app/Http/Controllers/Fo/CustomerController.php new file mode 100644 index 0000000..2ae628c --- /dev/null +++ b/app/Http/Controllers/Fo/CustomerController.php @@ -0,0 +1,24 @@ +user(); + + $crmCustomer = $customer->api_data; + + return view('theme::auth.profil-verify', compact('crmCustomer')); + } +} diff --git a/app/Http/Controllers/Fo/ParticipateController.php b/app/Http/Controllers/Fo/ParticipateController.php index 3e8f02d..9ea50e4 100644 --- a/app/Http/Controllers/Fo/ParticipateController.php +++ b/app/Http/Controllers/Fo/ParticipateController.php @@ -3,21 +3,19 @@ namespace App\Http\Controllers\Fo; use App\Http\Controllers\Controller; +use App\Http\Requests\Fo\Customers\CreateAccountRequest; use App\Http\Requests\Fo\ParticipateRequest; use App\Mail\CourrierArcadiaError; use App\Mail\NotifAdmin; use App\Mail\NotifWinAdmin; use App\Mail\PrizeWon; use App\Mail\DiscountWonMail; -use App\Models\BannedIp; use App\Models\Customer; use App\Models\InstantWin; use App\Models\Participation; use App\Models\Discount; -use LDCWeb\MesBR; use App\Models\Settings; use Illuminate\Contracts\View\View; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Auth; @@ -25,12 +23,28 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; +use Illuminate\Support\Facades\Session; +use Illuminate\Support\Facades\Validator; +use Illuminate\Validation\ValidationException; use LDC\LDCCRM\Enums\ApiError; use LDC\LDCCRM\Exceptions\ApiErrorException; use LDC\LDCCRM\Exceptions\CrmException; class ParticipateController extends Controller { + /** + * ParticipateController + * + * @return void + */ + public function __construct() + { + $this->middleware(function (Request $request, callable $next) { + $this->assertUserHasEnoughInfoToUseMesBr(); + return $next($request); + }); + } + /** * Display participation form * @@ -38,7 +52,7 @@ class ParticipateController extends Controller */ public function participate(): View { - if (Gate::check('participate', Participation::class)) { + if (!Gate::check('participate', Participation::class)) { return view('theme::pages.participation-exhausted'); } return view('theme::pages.enter-code'); @@ -52,10 +66,6 @@ class ParticipateController extends Controller */ public function participateSubmit(ParticipateRequest $request): View { - if (Gate::check('participate', Participation::class)) { - return view('theme::pages.participation-exhausted'); - } - return DB::transaction(function () use ($request) { // ! On verrouille le ou les instants gagnant /** @var \App\Models\InstantWin|null */ @@ -121,6 +131,27 @@ class ParticipateController extends Controller }); } + /** + * Undocumented function + * + * @return \Illuminate\Http\Response + */ + public function formWinningProof() + { + $participation = Participation::where([ + ['user_id', Auth::user()->id], + ['sticker', null], + ['ticket', null], + ['barcode', null] + ])->has('instantWin')->first(); + if ($participation) { + return view('theme::winning-proof', compact('participation')); + } + return redirect()->route('fo.homepage'); + } + + // * --- PRIVATE METHODS --- + /** * Handle instant wins * @@ -312,21 +343,54 @@ class ParticipateController extends Controller } /** - * Undocumented function + * Assert that user has enough info filled on his profile to win a Discount + * Or redirect him to the fill form + * ! Also validates its birth date (because beeing an adult is required to participate) * - * @return \Illuminate\Http\Response + * @return void + * @throws \Illuminate\Http\Exceptions\HttpResponseException If customer data doest not validate + * or and error occurs. */ - public function formWinningProof() + private function assertUserHasEnoughInfoToUseMesBr() { - $participation = Participation::where([ - ['user_id', Auth::user()->id], - ['sticker', null], - ['ticket', null], - ['barcode', null] - ])->has('instantWin')->first(); - if ($participation) { - return view('theme::winning-proof', compact('participation')); - } - return redirect()->route('fo.homepage'); + // * Valider que le client dispose des donnés requises pour MesBR + try { + /** @var \App\Models\Customer */ + $authUser = auth('frontend')->user(); + $crmCustomer = $authUser->api_data; + + // * On valide les données selon les critères du site + $rules = collect((new CreateAccountRequest())->rules()); + Validator::make( + [ + 'birth_date' => $crmCustomer->birth_date, + 'address' => $crmCustomer->address, + 'postcode' => $crmCustomer->postcode, + 'country' => $crmCustomer->country->value, + 'city' => $crmCustomer->city + ], + [ + 'birth_date' => $rules->get('birth_date'), + 'address' => $rules->get('address'), + 'postcode' => $rules->get('postcode'), + 'country' => $rules->get('country'), + 'city' => $rules->get('city') + ] + )->validate(); + Session::remove('needCrmInfoUpdate'); + } catch (ValidationException) { + response()->redirectToRoute('fo.profil.verify') + ->with('warning', trans( + 'Vous devez compléter des informations sur votre profil pour pouvoir participer.' + ))->throwResponse(); + } catch (\Exception $e) { + report($e); + // * Retenter ? + response()->redirectToRoute('fo.homepage') + ->with('error', trans('Une erreur est survenue, veuillez réssayer.')) + ->throwResponse(); + } //end try } + + // * --- END PRIVATE METHODS --- } diff --git a/routes/web-fo.php b/routes/web-fo.php index d98c420..c59d6a8 100644 --- a/routes/web-fo.php +++ b/routes/web-fo.php @@ -1,5 +1,6 @@ name('fo.')->group(function () { // * Routes nécessitant d'être connecté. Route::middleware(['auth:frontend', 'verified:fo.verification.notice'])->group(function () { + // * Participation routes Route::get('saisir-code', [ParticipateController::class, 'index'])->name('saisir-code'); Route::post('saisir-code', [ParticipateController::class, 'participate']) ->name('participate'); @@ -40,6 +42,11 @@ Route::middleware(['throttle:credentials'])->name('fo.')->group(function () { ->name('winning-proof'); Route::put('rejouer', [WinningProofController::class, 'sendWinningProof']) ->name('send-winning-proof'); + + // * Fill Missing Customer Info in order to use MesBr + Route::get('profil/verify', [CustomerController::class, 'profilVerify'])->name('profil.verify'); + Route::put('profil/verify', [CustomerController::class, 'profilVerifySubmit']) + ->name('profil.verify-submit'); });