Skip to content

Commit

Permalink
Merge pull request #884 from plentymarkets/fix/customer_registration_…
Browse files Browse the repository at this point in the history
…address_validation

FIX do address validation for register before the contact is created
  • Loading branch information
Dominik2809 authored Sep 14, 2020
2 parents e055e2e + f4eeed3 commit 90be13a
Showing 1 changed file with 75 additions and 61 deletions.
136 changes: 75 additions & 61 deletions src/Services/CustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ public function __construct(
$this->contactClassRepository = $contactClassRepository;
$this->sessionStorageRepository = $sessionStorageRepository;

$dispatcher->listen(AfterBasketChanged::class, function()
{
$dispatcher->listen(
AfterBasketChanged::class,
function () {
$this->resetMemoryCache();
});
}
);
}

/**
Expand Down Expand Up @@ -203,6 +205,8 @@ public function getContactClassMinimumOrderQuantity(): int
*/
public function registerCustomer(array $contactData, $billingAddressData = null, $deliveryAddressData = null)
{
$contact = null;

/** @var BasketService $basketService */
$basketService = pluginApp(BasketService::class);

Expand All @@ -212,73 +216,74 @@ public function registerCustomer(array $contactData, $billingAddressData = null,
$newBillingAddress = null;
$newDeliveryAddress = null;

$guestBillingAddress = null;
//$guestBillingAddressId = $this->sessionStorageRepository->getSessionValue(SessionStorageRepositoryContract::BILLING_ADDRESS_ID);
$guestBillingAddressId = $basketService->getBillingAddressId();
if ((int)$guestBillingAddressId > 0) {
$guestBillingAddress = $this->addressRepository->findAddressById($guestBillingAddressId);
}
$newBillingAddressData = $this->determineNewCustomerAddress(AddressType::BILLING, $billingAddressData);
$newDeliveryAddressData = $this->determineNewCustomerAddress(AddressType::DELIVERY, $deliveryAddressData);

$guestDeliveryAddress = null;
//$guestDeliveryAddressId = $this->sessionStorageRepository->getSessionValue(SessionStorageRepositoryContract::DELIVERY_ADDRESS_ID);
$guestDeliveryAddressId = $basketService->getDeliveryAddressId();
if ((int)$guestDeliveryAddressId > 0) {
$guestDeliveryAddress = $this->addressRepository->findAddressById($guestDeliveryAddressId);
}
try {
if (!is_null($newBillingAddressData)) {
AddressValidator::validateOrFail($newBillingAddressData);
}

$contact = $this->createContact($contactData);
$contact = $this->createContact($contactData);

if (!is_null($contact) && $contact->id > 0) {
//Login
$authenticationService->loginWithContactId($contact->id, (string)$contactData['password']);
if (!is_null($contact) && $contact->id > 0) {
//Login
$authenticationService->loginWithContactId($contact->id, (string)$contactData['password']);

if ($guestBillingAddress !== null) {
$newBillingAddress = $this->createAddress(
$guestBillingAddress->toArray(),
AddressType::BILLING
);
//$this->sessionStorageRepository->setSessionValue(SessionStorageRepositoryContract::BILLING_ADDRESS_ID, $newBillingAddress->id);
$basketService->setBillingAddressId($newBillingAddress->id);
}
if ($newBillingAddressData !== null) {
$newBillingAddress = $this->createAddress($newBillingAddressData, AddressType::BILLING);
$basketService->setBillingAddressId($newBillingAddress->id);
}

if ($guestDeliveryAddress !== null) {
$newDeliveryAddress = $this->createAddress(
$guestDeliveryAddress->toArray(),
AddressType::DELIVERY
);
//$this->sessionStorageRepository->setSessionValue(SessionStorageRepositoryContract::DELIVERY_ADDRESS_ID, $newDeliveryAddress->id);
$basketService->setDeliveryAddressId($newDeliveryAddress->id);
}
if ($newDeliveryAddressData !== null) {
$newDeliveryAddress = $this->createAddress($newDeliveryAddressData, AddressType::DELIVERY);
$basketService->setDeliveryAddressId($newDeliveryAddress->id);
}

if ($billingAddressData !== null) {
$newBillingAddress = $this->createAddress($billingAddressData, AddressType::BILLING);
//$this->sessionStorageRepository->setSessionValue(SessionStorageRepositoryContract::BILLING_ADDRESS_ID, $newBillingAddress->id);
$basketService->setBillingAddressId($newBillingAddress->id);
if ($newBillingAddress instanceof Address) {
$contact = $this->updateContactWithAddressData($newBillingAddress);
}
}

if ($deliveryAddressData !== null) {
$newDeliveryAddress = $this->createAddress($deliveryAddressData, AddressType::DELIVERY);
//$this->sessionStorageRepository->setSessionValue(SessionStorageRepositoryContract::DELIVERY_ADDRESS_ID, $newDeliveryAddress->id);
$basketService->setDeliveryAddressId($newDeliveryAddress->id);
}
if ($contact instanceof Contact && $contact->id > 0) {
$params = [
'contactId' => $contact->id,
'clientId' => Utils::getWebstoreId(),
'password' => $contactData['password'],
'language' => Utils::getLang()
];

if ($newBillingAddress instanceof Address) {
$contact = $this->updateContactWithAddressData($newBillingAddress);
$this->sendMail(AutomaticEmailTemplate::CONTACT_REGISTRATION, AutomaticEmailContact::class, $params);
}
} catch (\Exception $exception) {
throw $exception;
}

if ($contact instanceof Contact && $contact->id > 0) {
$params = [
'contactId' => $contact->id,
'clientId' => Utils::getWebstoreId(),
'password' => $contactData['password'],
'language' => Utils::getLang()
];
return $contact;
}

$this->sendMail(AutomaticEmailTemplate::CONTACT_REGISTRATION, AutomaticEmailContact::class, $params);
private function determineNewCustomerAddress($typeId, $addressData)
{
if (!is_null($addressData)) {
return $addressData;
}

return $contact;
/** @var BasketService $basketService */
$basketService = pluginApp(BasketService::class); //TODO class member

if ($typeId === AddressType::BILLING) {
$guestBillingAddressId = $basketService->getBillingAddressId();
if ((int)$guestBillingAddressId > 0) {
return $this->addressRepository->findAddressById($guestBillingAddressId)->toArray();
}
} elseif ($typeId === AddressType::DELIVERY) {
$guestDeliveryAddressId = $basketService->getDeliveryAddressId();
if ((int)$guestDeliveryAddressId > 0) {
return $this->addressRepository->findAddressById($guestDeliveryAddressId)->toArray();
}
}

return null;
}

/**
Expand Down Expand Up @@ -520,7 +525,10 @@ function () use ($newPassword, $contactId, $contactRepo) {
public function getAddresses($typeId = null)
{
if ($this->contactRepository->getContactId() > 0) {
$addresses = $this->contactAddressRepository->getAddresses($this->contactRepository->getContactId(), $typeId);
$addresses = $this->contactAddressRepository->getAddresses(
$this->contactRepository->getContactId(),
$typeId
);

if (count($addresses)) {
foreach ($addresses as $key => $address) {
Expand Down Expand Up @@ -567,7 +575,11 @@ public function getAddress(int $addressId, int $typeId): Address
$address = null;

if ($this->contactRepository->getContactId() > 0) {
$address = $this->contactAddressRepository->getAddress($addressId, $this->contactRepository->getContactId(), $typeId);
$address = $this->contactAddressRepository->getAddress(
$addressId,
$this->contactRepository->getContactId(),
$typeId
);
} else {
/**
* @var BasketService $basketService
Expand Down Expand Up @@ -641,12 +653,11 @@ public function createAddress(array $addressData, int $typeId): Address
&& (int)$account->id > 0
&& count($contact->addresses) === 1
&& $contact->addresses[0]->id === $newAddress->id) {

$defaultClassId = (int)$this->contactRepository->getDefaultContactClassId();

// update contact class id only when current class id on contact is the default contact class id
// default contact class id is set by default to contact
if ($defaultClassId > 0 && $contact->classId === $defaultClassId) {
if ($defaultClassId > 0 && $contact->classId === $defaultClassId) {
/** @var TemplateConfigService $templateConfigService */
$templateConfigService = pluginApp(TemplateConfigService::class);
$classId = $templateConfigService->getInteger('global.default_contact_class_b2b');
Expand Down Expand Up @@ -824,7 +835,6 @@ public function updateAddress(int $addressId, array $addressData, int $typeId):
$this->contactRepository->getContactId(),
$typeId
);

} else {
//case for guests
$addressData['options'] = $this->buildAddressEmailOptions([], true, $addressData);
Expand Down Expand Up @@ -900,7 +910,11 @@ public function deleteAddress(int $addressId, int $typeId = 0): void
false
);

$this->contactAddressRepository->deleteAddress($addressId, $this->contactRepository->getContactId(), $typeId);
$this->contactAddressRepository->deleteAddress(
$addressId,
$this->contactRepository->getContactId(),
$typeId
);

if ($typeId == AddressType::BILLING) {
$basketService->setBillingAddressId(0);
Expand Down

0 comments on commit 90be13a

Please sign in to comment.