112 lines
3.8 KiB
PHP
112 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo\Klub;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\Customers\UpdateAccountRequest;
|
|
use App\Http\Requests\Fo\Customers\UpdatePasswordRequest;
|
|
use App\Models\Customer;
|
|
use Illuminate\Auth\Events\PasswordReset;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\ValidationException;
|
|
use LDC\LDCCRM\Enums\ApiError;
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
|
use LDC\LDCCRM\Exceptions\ValidationException as CrmValidationException;
|
|
|
|
class CustomerController extends Controller
|
|
{
|
|
/**
|
|
* Display Klub Customer Homepage
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function profile(): View
|
|
{
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = auth('frontend')->user();
|
|
/** @var \LDC\LDCCRM\Models\Customer $crmCustomer */
|
|
$crmCustomer = $customer->api_data;
|
|
|
|
return view('fo.pages.klub.profile', compact('crmCustomer'));
|
|
}
|
|
|
|
/**
|
|
* Update Customer profile
|
|
*
|
|
* @param \App\Http\Requests\Fo\Customers\UpdateAccountRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function profileUpdate(UpdateAccountRequest $request): RedirectResponse
|
|
{
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = auth('frontend')->user();
|
|
|
|
return $this->updateCrmCustomerInfo($customer, $request->validated()) ?
|
|
back()->with('success', trans('Vos informations ont bien été enregistrées.')) :
|
|
back()->withInput()->withErrors(trans('Une erreur est survenue, merci de réessayer ultérieurement.'));
|
|
}
|
|
|
|
/**
|
|
* Show update customer password form
|
|
*
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function password()
|
|
{
|
|
return view('fo.auth.passwords.change');
|
|
}
|
|
|
|
/**
|
|
* Update customer password
|
|
*
|
|
* @param \App\Http\Requests\Fo\Customers\UpdatePasswordRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function passwordSubmit(UpdatePasswordRequest $request): RedirectResponse
|
|
{
|
|
/** @var \App\Models\Customer */
|
|
$customer = \auth('frontend')->user();
|
|
|
|
if ($this->updateCrmCustomerInfo($customer, ['password' => $request->input('password')])) {
|
|
event(new PasswordReset($customer));
|
|
return back()->with('success', trans('Vos informations ont bien été enregistrées.'));
|
|
}
|
|
|
|
return back()->withInput()->withErrors(trans('Une erreur est survenue, merci de réessayer ultérieurement.'));
|
|
}
|
|
|
|
/**
|
|
* Updates user info
|
|
*
|
|
* @param \App\Models\Customer $customer
|
|
* @param array $data
|
|
* @return boolean
|
|
* @throws \Illuminate\Validation\ValidationException If something failed.
|
|
*/
|
|
private function updateCrmCustomerInfo(Customer $customer, array $data)
|
|
{
|
|
return DB::transaction(function () use ($customer, $data) {
|
|
try {
|
|
if (\array_key_exists('email', $data)) {
|
|
$customer->email = $data['email'];
|
|
$customer->saveOrFail();
|
|
}
|
|
return $customer->updateApiData($data);
|
|
} catch (CrmValidationException $e) {
|
|
throw ValidationException::withMessages([
|
|
'error' => $e->getMessage()
|
|
]);
|
|
} catch (ApiErrorException $e) {
|
|
if ($e->getApiError()->equals(ApiError::validationFailed)) {
|
|
throw ValidationException::withMessages([
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}
|
|
report($e);
|
|
return false;
|
|
} //end try
|
|
});
|
|
}
|
|
}
|