middleware(function (Request $request, callable $next) { $this->authorize('fo.gameEnabled'); $this->authorize('submitProof', $request->route('participation')); return $next($request); }); } /** * Display winning proof form */ public function displayForm(Participation $participation): View { $participation->load('instantWin.prize'); /** @var Customer */ $customer = auth('frontend')->user(); $crmCustomer = $customer->api_data; /** @var view-string $view */ $view = 'theme::pages.participation.won-form-only'; return view($view, [ 'participationModel' => $participation, 'prizeModel' => $participation->instantWin->prize, 'crmCustomer' => $crmCustomer, ]); } /** * Submit winning proofs, * - 1 Met à jour le profil du customer (sans lui en faire part), * - 2 Save les pictures de preuve de gains * - 3 Envoi un mail au abonnés (settings notification_emails) avec les infos client * * @throws IlluminateValidationException In case of failure. * * @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber */ public function displayFormSubmit( SubmitWinningProofRequest $request, Participation $participation ): RedirectResponse { // phpcs:enable try { return DB::transaction(function () use ($request, $participation) { $participation->load([ 'instantWin.prize', 'code', 'product', ]); /** @var Settings */ $settings = resolve(Settings::class); // * --- PHASE 1 Update Crm Customer --- /** @var Customer */ $customer = auth('frontend')->user(); $crmCustomer = $customer->api_data; /** @var array */ $data = $request->validated(); // * This field is not standard in crm $data['postcode'] = Arr::get($data, 'postal_code'); Arr::forget($data, 'postal_code'); $customer->updateApiData(Arr::only( $data, [ 'firstname', 'lastname', 'birth_date', 'address', 'address2', 'postcode', 'country', 'city', 'phone', 'email', ] )); // * --- PHASE 2 Update participation --- $participation->fill(Arr::only($request->validated(), [ 'barcode', 'ticket', 'sticker', ])); $participation->save(); $customer->notify(new HasSubmitedWinProof($request->input('firstname'))); // * Dispatch notification to mail list Mail::to($settings->notification_emails)->send(new WinningProofAdminMail( $settings->name, $participation->created_at, $participation->instantWin->prize->label, $crmCustomer, $participation->code?->code_unique, $participation->product?->ean_code, $participation->sticker, $participation->ticket, $participation->barcode, )); return response()->redirectToRoute('fo.homepage') ->with('success', trans( 'front.messages.winning_proof_submitted_will_be_contacted' )); }); } catch (ApiErrorException $e) { if ($e->getApiError()->equals(ApiError::validationFailed)) { throw IlluminateValidationException::withMessages([ 'error' => $e->getMessage(), ]); } report($e); Log::error(self::class . '::create reached end of possibilities.'); throw new RuntimeException(self::class . '::create reached end of possibilities.', $e->getCode(), $e); } catch (CrmValidationException $e) { throw IlluminateValidationException::withMessages([ 'error' => $e->getMessage(), ]); } // end try } }