Merge branch 'develop' into trunk
This commit is contained in:
commit
87bd150f6b
3 changed files with 179 additions and 19 deletions
|
|
@ -24,23 +24,32 @@ final class UpdateCustomer implements Action
|
|||
#[\Override]
|
||||
public function __invoke(ActionInput $input): UpdateCustomerOutput
|
||||
{
|
||||
Assert::isInstanceOf($input, UnsubscribeFromNewsletterInput::class);
|
||||
Assert::isInstanceOf($input, UpdateCustomerInput::class);
|
||||
|
||||
$customer = $this->authService->getCustomerAsMandatory();
|
||||
|
||||
try {
|
||||
/** @var array{firstname:string,lastname:string,birth_date:string,phone?:string,address?:string,postcode:string,city:string,opt_in?:bool} $data */
|
||||
$data = [
|
||||
'firstname' => $input->firstname,
|
||||
'lastname' => $input->lastname,
|
||||
'birth_date' => $input->birthDate,
|
||||
'postcode' => $input->postcode,
|
||||
'city' => $input->city,
|
||||
'opt_in' => $input->optIn,
|
||||
];
|
||||
|
||||
if ($input->phone !== null) {
|
||||
$data['phone'] = $input->phone;
|
||||
}
|
||||
|
||||
if ($input->address !== null) {
|
||||
$data['address'] = $input->address;
|
||||
}
|
||||
|
||||
$result = $this->crmService->updateCustomerInfos(
|
||||
$customer,
|
||||
[
|
||||
'firstname' => $input->firstname,
|
||||
'lastname' => $input->lastname,
|
||||
'birth_date' => $input->birthDate,
|
||||
'phone' => $input->phone,
|
||||
'address' => $input->address,
|
||||
'postcode' => $input->postcode,
|
||||
'city' => $input->city,
|
||||
'opt_in' => $input->optIn,
|
||||
]
|
||||
$data
|
||||
);
|
||||
|
||||
return new UpdateCustomerOutput($result);
|
||||
|
|
|
|||
|
|
@ -34,35 +34,37 @@ class CustomerController extends Controller
|
|||
{
|
||||
$this->authorize('manageOwnProfile', Customer::class);
|
||||
|
||||
$crmCustomer = $this->executor->execute(
|
||||
$output = $this->executor->execute(
|
||||
$getCrmCustomerQuery,
|
||||
new GetCrmCustomerQueryInput()
|
||||
);
|
||||
|
||||
abort_unless(
|
||||
$crmCustomer->customer instanceof CrmCustomer,
|
||||
$output->customer instanceof CrmCustomer,
|
||||
503,
|
||||
trans('An error occurred, please try again later.')
|
||||
);
|
||||
|
||||
return view('fo.customer.edit.infos', compact('crmCustomer'));
|
||||
return view('fo.customer.edit.infos', [
|
||||
'crmCustomer' => $output->customer,
|
||||
]);
|
||||
}
|
||||
|
||||
public function editInfosSubmit(UpdateInfosRequest $request, UpdateCustomer $updateCustomer): RedirectResponse
|
||||
{
|
||||
$this->authorize('manageOwnProfile', Customer::class);
|
||||
|
||||
$result = $this->executor->execute(
|
||||
$output = $this->executor->execute(
|
||||
$updateCustomer,
|
||||
$request->toUpdateCustomerInput()
|
||||
);
|
||||
|
||||
return match ($result) {
|
||||
$result instanceof CrmCustomer => back()
|
||||
return match (true) {
|
||||
$output->result instanceof CrmCustomer => back()
|
||||
->with('success', trans('Your information has been saved.')),
|
||||
$result instanceof ValidationException => back()
|
||||
$output->result instanceof ValidationException => back()
|
||||
->withInput()
|
||||
->withErrors($result->getMessage()),
|
||||
->withErrors($output->result->getMessage()),
|
||||
default => back()
|
||||
->withInput()
|
||||
->withErrors(trans('An error occurred, please try again later.')),
|
||||
|
|
|
|||
|
|
@ -3,9 +3,16 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Customer;
|
||||
use App\Models\Employee;
|
||||
use App\Models\Page;
|
||||
use App\Services\CrmService;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Database\Seeders\ContentSeeder;
|
||||
use Domain\Enums\Country;
|
||||
use Domain\Models\ValueObjects\Code;
|
||||
use Domain\Models\ValueObjects\CrmCustomer;
|
||||
use Domain\Models\ValueObjects\Email;
|
||||
use Domain\Models\ValueObjects\Matricule;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\View;
|
||||
|
|
@ -87,4 +94,146 @@ describe('CustomerController', function () {
|
|||
// THEN
|
||||
$response->assertStatus(302);
|
||||
});
|
||||
|
||||
it('renders customer edit infos page with crm customer data', function () {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$this->seed(ContentSeeder::class);
|
||||
View::share('PageList', Page::getCached()
|
||||
->mapWithKeys(fn ($page) => [
|
||||
$page->slug->value() => $page,
|
||||
]));
|
||||
Cache::flush();
|
||||
|
||||
$hashcode = new CrmHashcode(Uuid::uuid4());
|
||||
$customer = Customer::forceCreate([
|
||||
'id' => Uuid::uuid7()->toString(),
|
||||
'hashcode' => $hashcode->value,
|
||||
'email' => new Email('jean.dupont@example.com'),
|
||||
]);
|
||||
|
||||
Employee::forceCreate([
|
||||
'id' => 8882,
|
||||
'customer_id' => $customer->id,
|
||||
'matricule' => new Matricule('8888002'),
|
||||
'nom' => 'Dupont',
|
||||
'prenom' => 'Jean',
|
||||
'code' => new Code('ABCDEF13'),
|
||||
'etablissement' => 'ETAB',
|
||||
'site' => 'SITE',
|
||||
'service' => '101',
|
||||
'created_at' => CarbonImmutable::now(),
|
||||
'updated_at' => CarbonImmutable::now(),
|
||||
]);
|
||||
|
||||
$crmCustomer = new CrmCustomer(
|
||||
hashcode: $hashcode->value,
|
||||
email: 'jean.dupont@example.com',
|
||||
password: 'pwd',
|
||||
firstname: 'Jean',
|
||||
lastname: 'Dupont',
|
||||
birthDate: '1985-05-15',
|
||||
phone: '0123456789',
|
||||
address: '10 rue de la Paix',
|
||||
address2: null,
|
||||
city: 'Paris',
|
||||
postcode: '75002',
|
||||
country: Country::france,
|
||||
optIn: true,
|
||||
status: true,
|
||||
lastConnection: null,
|
||||
);
|
||||
|
||||
$crmMock = \Mockery::mock(CrmService::class)->makePartial();
|
||||
$crmMock->shouldReceive('getCustomerFromHashCode')->andReturn($crmCustomer);
|
||||
App::instance(CrmService::class, $crmMock);
|
||||
|
||||
$this->actingAs($customer, 'frontend');
|
||||
|
||||
// WHEN
|
||||
$response = $this->get('/compte/profil');
|
||||
|
||||
// THEN
|
||||
$response->assertOk();
|
||||
$response->assertViewIs('fo.customer.edit.infos');
|
||||
$response->assertViewHas('crmCustomer', $crmCustomer);
|
||||
$response->assertSee('value="Dupont"', false);
|
||||
$response->assertSee('value="Jean"', false);
|
||||
});
|
||||
|
||||
it('successfully updates customer infos and redirects back', function () {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$this->seed(ContentSeeder::class);
|
||||
View::share('PageList', Page::getCached()
|
||||
->mapWithKeys(fn ($page) => [
|
||||
$page->slug->value() => $page,
|
||||
]));
|
||||
Cache::flush();
|
||||
|
||||
$hashcode = new CrmHashcode(Uuid::uuid4());
|
||||
$customer = Customer::forceCreate([
|
||||
'id' => Uuid::uuid7()->toString(),
|
||||
'hashcode' => $hashcode->value,
|
||||
'email' => new Email('jean.dupont@example.com'),
|
||||
]);
|
||||
|
||||
Employee::forceCreate([
|
||||
'id' => 8883,
|
||||
'customer_id' => $customer->id,
|
||||
'matricule' => new Matricule('8888003'),
|
||||
'nom' => 'Dupont',
|
||||
'prenom' => 'Jean',
|
||||
'code' => new Code('ABCDEF14'),
|
||||
'etablissement' => 'ETAB',
|
||||
'site' => 'SITE',
|
||||
'service' => '101',
|
||||
'created_at' => CarbonImmutable::now(),
|
||||
'updated_at' => CarbonImmutable::now(),
|
||||
]);
|
||||
|
||||
$updatedCrmCustomer = new CrmCustomer(
|
||||
hashcode: $hashcode->value,
|
||||
email: 'jean.dupont@example.com',
|
||||
password: 'pwd',
|
||||
firstname: 'Jean-Pierre',
|
||||
lastname: 'Durand',
|
||||
birthDate: '1985-05-15',
|
||||
phone: '0123456789',
|
||||
address: '20 avenue Foch',
|
||||
address2: null,
|
||||
city: 'Lyon',
|
||||
postcode: '69001',
|
||||
country: Country::france,
|
||||
optIn: true,
|
||||
status: true,
|
||||
lastConnection: null,
|
||||
);
|
||||
|
||||
$crmMock = \Mockery::mock(CrmService::class);
|
||||
$crmMock->shouldReceive('getCustomerFromHashCode')
|
||||
->andReturn($updatedCrmCustomer);
|
||||
$crmMock->shouldReceive('updateCustomerInfos')
|
||||
->once()
|
||||
->andReturn($updatedCrmCustomer);
|
||||
App::instance(CrmService::class, $crmMock);
|
||||
|
||||
$this->actingAs($customer, 'frontend');
|
||||
|
||||
// WHEN
|
||||
$response = $this->post('/compte/profil', [
|
||||
'firstname' => 'Jean-Pierre',
|
||||
'lastname' => 'Durand',
|
||||
'birth_date' => '1985-05-15',
|
||||
'phone' => '0123456789',
|
||||
'address' => '20 avenue Foch',
|
||||
'city' => 'Lyon',
|
||||
'postcode' => '69001',
|
||||
'opt_in' => true,
|
||||
]);
|
||||
|
||||
// THEN
|
||||
$response->assertRedirect();
|
||||
$response->assertSessionHas('success', trans('Your information has been saved.'));
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue