184 lines
6.4 KiB
PHP
184 lines
6.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\Customers\SingleOptinRequest;
|
|
use App\Http\Requests\Fo\Customers\VerifyInformationsRequest;
|
|
use App\Models\Customer;
|
|
use App\Models\Participation;
|
|
use App\Repositories\Fo\CrmRepository;
|
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Validation\ValidationException as IlluminateValidationException;
|
|
use LDC\LDCCRM\Enums\ApiError;
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
|
use LDC\LDCCRM\Exceptions\CrmException;
|
|
use LDC\LDCCRM\Exceptions\ValidationException as CrmValidationException;
|
|
use LDC\LDCCRM\Models\SingleOptin;
|
|
use LDC\MESBR\MesBr;
|
|
use Illuminate\Http\Response;
|
|
|
|
class CustomerController extends Controller
|
|
{
|
|
private readonly CrmRepository $crmRepository;
|
|
|
|
/**
|
|
* ParticipateController
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->crmRepository = new CrmRepository();
|
|
}
|
|
|
|
/**
|
|
* Verify profil info form
|
|
*/
|
|
public function profilVerify(): View
|
|
{
|
|
// ! Pas de Gate/Policies Ici volontairement, vu avec Vincent
|
|
/** @var Customer */
|
|
$customer = auth('frontend')->user();
|
|
|
|
$crmCustomer = $customer->api_data;
|
|
|
|
/** @var view-string $view */
|
|
$view = 'theme::auth.profil-verify';
|
|
|
|
return view($view, ['crmCustomer' => $crmCustomer]);
|
|
}
|
|
|
|
/**
|
|
* Profile verify
|
|
*
|
|
* @throws IlluminateValidationException In case of any error validation or within the crm.
|
|
*/
|
|
public function profilVerifySubmit(VerifyInformationsRequest $request): RedirectResponse
|
|
{
|
|
// ! Pas de Gate/Policies Ici volontairement, vu avec Vincent
|
|
try {
|
|
return DB::transaction(function () use ($request) {
|
|
// * --- FILL AND SAVE CUSTOMER
|
|
/** @var Customer */
|
|
$customer = auth('frontend')->user();
|
|
|
|
// * Màj, on ne mets pas à jour le mdp ou l'adresse email, juste les champs pour mesBr
|
|
$customer->updateApiData(\array_merge(Arr::only(
|
|
$request->validated(),
|
|
[
|
|
'firstname',
|
|
'lastname',
|
|
'birth_date',
|
|
'address',
|
|
'country',
|
|
'city',
|
|
'phone',
|
|
'opt_in',
|
|
]
|
|
), ['postcode' => $request->input('postal_code')]));
|
|
Session::flash('success', trans('front.messages.profile_updated_success'));
|
|
|
|
return response()->redirectToRoute('fo.participate');
|
|
});
|
|
} catch (CrmValidationException $e) {
|
|
// * Local Validation failure
|
|
throw IlluminateValidationException::withMessages([
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
} catch (ApiErrorException $e) {
|
|
if ($e->getApiError()->equals(ApiError::validationFailed)) {
|
|
throw IlluminateValidationException::withMessages([
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
}
|
|
report($e);
|
|
Log::error(self::class . '::update reached end of possibilities.');
|
|
} catch (CrmException $e) {
|
|
report($e);
|
|
Log::error(self::class . '::error occured on calling crm.');
|
|
throw IlluminateValidationException::withMessages([
|
|
'error' => trans('Une erreur est survenue, veuillez réssayer plus tard'),
|
|
]);
|
|
} // end try
|
|
|
|
return back()->withInput()->withErrors(trans('front.messages.unexpected_error_try_later'));
|
|
}
|
|
|
|
/**
|
|
* View all customer winnig participations
|
|
*/
|
|
public function viewWinningParticipations(): View
|
|
{
|
|
$this->authorize('viewWonList', Participation::class);
|
|
|
|
/** @var Customer */
|
|
$customer = auth('frontend')->user();
|
|
|
|
// * Recupération des informations CRM
|
|
/** @var \LDC\LDCCRM\Models\Customer */
|
|
$crmUser = $this->crmRepository->fetchCrmCustomer($customer);
|
|
|
|
/** @var MesBr $mesBr */
|
|
$mesBr = resolve('mesBrAPI');
|
|
|
|
/** @var array{coupons?: list<array{quantity: int|string, printed: int|string}>} $brBasket */
|
|
$brBasket = $mesBr->getBasket($crmUser);
|
|
|
|
if (! empty($brBasket['coupons'])) {
|
|
$coupons = collect($brBasket['coupons']);
|
|
$canPrint = $coupons->sum('quantity') - $coupons->sum('printed');
|
|
}
|
|
|
|
$winningList = $customer->participations()
|
|
->with(['instantWin'])->where(function (Builder $q): void {
|
|
$q->whereHas('instantWin');
|
|
})->get();
|
|
|
|
/** @var view-string $view */
|
|
$view = 'theme::pages.participation.winning-participations';
|
|
|
|
return view($view, [
|
|
'winningList' => $winningList,
|
|
'canPrint' => $canPrint ?? null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Call the CRM to add a single optin
|
|
*
|
|
* @throws \LDC\LDCCRM\Exceptions\ValidationException ValidationException.
|
|
* @throws BindingResolutionException BindingResolutionException.
|
|
*/
|
|
public function addOptIn(SingleOptinRequest $request): Response | string
|
|
{
|
|
try {
|
|
SingleOptin::setStatus($request->email, true);
|
|
|
|
return \response(trans('front.messages.newsletter_subscription_success'), 200);
|
|
} catch (ApiErrorException $e) {
|
|
// Il y a deja un customerInfo avec optin a true pour ce customer.
|
|
if ($e->getApiError()->equals(ApiError::singleOptinCustomerExists)) {
|
|
return trans('front.messages.newsletter_email_already_subscribed');
|
|
}
|
|
if (app()->bound('sentry')) {
|
|
resolve('sentry')->captureException($e);
|
|
}
|
|
|
|
return \response(trans('front.messages.unexpected_error_try_later'), 404);
|
|
} catch (CrmException $e) {
|
|
if (app()->bound('sentry')) {
|
|
resolve('sentry')->captureException($e);
|
|
}
|
|
|
|
return \response(trans('front.messages.unexpected_error_try_later'), 404);
|
|
}
|
|
}
|
|
}
|