Skip to content

Commit

Permalink
[shopsys] separated edit personal data mutation to allow change data …
Browse files Browse the repository at this point in the history
…for self-managed user on b2b domain (#3601)
  • Loading branch information
grossmannmartin authored Nov 20, 2024
1 parent 4b45ff2 commit 67639d0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 29 deletions.
33 changes: 33 additions & 0 deletions src/Model/Customer/User/CustomerUserUpdateDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shopsys\FrontendApiBundle\Model\Customer\User;

use Overblog\GraphQLBundle\Definition\Argument;
use Shopsys\FrameworkBundle\Model\Country\CountryFacade;
use Shopsys\FrameworkBundle\Model\Customer\BillingAddressDataFactory;
use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser;
use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserDataFactory as FrameworkCustomerUserDataFactory;
Expand All @@ -17,11 +18,13 @@ class CustomerUserUpdateDataFactory
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserUpdateDataFactory $customerUserUpdateDataFactory
* @param \Shopsys\FrameworkBundle\Model\Customer\BillingAddressDataFactory $billingAddressDataFactory
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserDataFactory $customerUserDataFactory
* @param \Shopsys\FrameworkBundle\Model\Country\CountryFacade $countryFacade
*/
public function __construct(
protected readonly FrameworkCustomerUserUpdateDataFactory $customerUserUpdateDataFactory,
protected readonly BillingAddressDataFactory $billingAddressDataFactory,
protected readonly FrameworkCustomerUserDataFactory $customerUserDataFactory,
protected readonly CountryFacade $countryFacade,
) {
}

Expand All @@ -48,6 +51,36 @@ public function createFromCustomerUserWithArgument(
return $customerUserUpdateData;
}

/**
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser $customerUser
* @param \Overblog\GraphQLBundle\Definition\Argument $argument
* @return \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserUpdateData
*/
public function createCompanyFromCustomerUserWithArgument(
CustomerUser $customerUser,
Argument $argument,
): CustomerUserUpdateData {
$input = $argument['input'];

$customerUserUpdateData = $this->customerUserUpdateDataFactory->createFromCustomerUser($customerUser);
$customerUserData = $customerUserUpdateData->customerUserData;
$billingAddressData = $customerUserUpdateData->billingAddressData;

foreach ($input as $key => $value) {
if (property_exists(get_class($customerUserData), $key)) {
$customerUserData->{$key} = $value;
}

if (property_exists(get_class($billingAddressData), $key)) {
$billingAddressData->{$key} = $value;
}

$billingAddressData->country = $this->countryFacade->findByCode($input['country']);
}

return $customerUserUpdateData;
}

/**
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser $customerUser
* @return \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserUpdateData
Expand Down
25 changes: 24 additions & 1 deletion src/Model/Mutation/Customer/User/CustomerUserMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,37 @@ public function changePersonalDataMutation(Argument $argument, InputValidator $v
{
$user = $this->runCheckUserIsLogged();

$validator->validate();

$customerUser = $this->customerUserFacade->getByUuid($user->getUuid());
$customerUserUpdateData = $this->customerUserUpdateDataFactory->createFromCustomerUserWithArgument(
$customerUser,
$argument,
);

$this->customerUserFacade->editByCustomerUser($customerUser->getId(), $customerUserUpdateData, $user->getDeviceId());

return $customerUser;
}

/**
* @param \Overblog\GraphQLBundle\Definition\Argument $argument
* @param \Overblog\GraphQLBundle\Validator\InputValidator $validator
* @return \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser
*/
public function changeCompanyDataMutation(Argument $argument, InputValidator $validator): CustomerUser
{
$user = $this->runCheckUserIsLogged();

$validationGroups = $this->computeValidationGroups($argument);
$validator->validate($validationGroups);

$customerUser = $this->customerUserFacade->getByUuid($user->getUuid());
$customerUserUpdateData = $this->customerUserUpdateDataFactory->createFromCustomerUserWithArgument(
$customerUserUpdateData = $this->customerUserUpdateDataFactory->createCompanyFromCustomerUserWithArgument(
$customerUser,
$argument,
);

$this->customerUserFacade->editByCustomerUser($customerUser->getId(), $customerUserUpdateData, $user->getDeviceId());

return $customerUser;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ChangeCompanyDataInputDecorator:
type: input-object
decorator: true
inherits:
- 'BillingAddressInputObjectDecorator'
- 'CompanyInputObjectDecorator'
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
ChangePersonalDataInputDecorator:
type: input-object
decorator: true
inherits:
- 'NameInputObjectDecorator'
- 'TelephoneInputObjectDecorator'
config:
fields:
firstName:
type: "String!"
description: "Customer user first name"
validation:
- NotBlank:
message: "Please enter first name"
- Length:
max: 100
maxMessage: "First name cannot be longer than {{ limit }} characters"
lastName:
type: "String!"
description: "Customer user last name"
validation:
- NotBlank:
message: "Please enter last name"
- Length:
max: 100
maxMessage: "Last name cannot be longer than {{ limit }} characters"
telephone:
type: "String!"
description: "Date and time when the order was created"
validation:
- NotBlank:
message: "Please enter telephone number"
- Length:
max: 30
maxMessage: "Telephone number cannot be longer than {{ limit }} characters"
newsletterSubscription:
type: "Boolean!"
description: "Whether customer user should receive newsletters or not"
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ MutationDecorator:
input:
type: ChangePersonalDataInput!
validation: cascade
access: "@=isGranted('ROLE_API_ALL')"
resolve: "@=mutation('changePersonalDataMutation', args, validator)"
ChangeCompanyData:
type: 'CustomerUser!'
description: "Changes customer user company data"
args:
input:
type: ChangeCompanyDataInput!
validation: cascade
access: "@=isGranted('ROLE_API_ALL')"
resolve: "@=mutation('changeCompanyDataMutation', args, validator)"
Register:
type: 'LoginResult!'
description: "Register new customer user"
Expand Down

0 comments on commit 67639d0

Please sign in to comment.