Skip to content

Commit

Permalink
Allow to create customer without address
Browse files Browse the repository at this point in the history
  • Loading branch information
lopes-vincent committed Jan 31, 2024
1 parent 0973c5c commit b20a257
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>2.2.10</version>
<version>2.2.11</version>
<authors>
<author>
<name>Vincent Lopes-Vicente</name>
Expand Down
43 changes: 29 additions & 14 deletions Controller/Front/CustomerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Thelia\Core\Translation\Translator;
use Thelia\Model\Address;
use Thelia\Model\Customer;
use Thelia\Model\CustomerQuery;

/**
* @Route("/customer", name="customer")
Expand Down Expand Up @@ -127,25 +128,37 @@ public function createCustomer(Request $request, ModelFactory $modelFactory)
*/
$con = Propel::getConnection();
$con->beginTransaction();

$theliaAddress = null;
try {
$email = $data['customer']['email'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \Exception(Translator::getInstance()->trans("Invalid email", [], OpenApi::DOMAIN_NAME));
}

$accountAlreadyExist = CustomerQuery::create()->filterByEmail($email)->findOne();
if (null !== $accountAlreadyExist) {
throw new \Exception(Translator::getInstance()->trans("Something went wrong during account creation", [], OpenApi::DOMAIN_NAME));
}

/** @var Customer $theliaCustomer */
$theliaCustomer = $openApiCustomer->toTheliaModel();
$theliaCustomer->setPassword($data['password'])->save();
$openApiCustomer->setId($theliaCustomer->getId());

/** We must catch the validation exception if it is thrown to rollback the Propel transaction before throwing the exception again */
/** @var OpenApiAddress $openApiAddress */
$openApiAddress = $modelFactory->buildModel('Address', $data['address']);
$openApiAddress->setCustomer($openApiCustomer)->validate(self::GROUP_CREATE);

/** @var Address $theliaAddress */
$theliaAddress = $openApiAddress->toTheliaModel();
$theliaAddress
->setLabel(Translator::getInstance()->trans('Main Address', [], OpenApi::DOMAIN_NAME))
->setIsDefault(1)
->save()
;
if (isset($data['address'])) {
/** We must catch the validation exception if it is thrown to rollback the Propel transaction before throwing the exception again */
/** @var OpenApiAddress $openApiAddress */
$openApiAddress = $modelFactory->buildModel('Address', $data['address']);
$openApiAddress->setCustomer($openApiCustomer)->validate(self::GROUP_CREATE);

/** @var Address $theliaAddress */
$theliaAddress = $openApiAddress->toTheliaModel();
$theliaAddress
->setLabel(Translator::getInstance()->trans('Main Address', [], OpenApi::DOMAIN_NAME))
->setIsDefault(1)
->save()
;
}
} catch (\Exception $exception) {
$con->rollBack();
throw $exception;
Expand All @@ -154,7 +167,9 @@ public function createCustomer(Request $request, ModelFactory $modelFactory)
/* If everything went fine, we actually commit the changes to the base. */
$con->commit();

$openApiCustomer->setDefaultAddressId($theliaAddress->getId());
if (null !== $theliaAddress) {
$openApiCustomer->setDefaultAddressId($theliaAddress->getId());
}

return OpenApiService::jsonResponse($openApiCustomer);
}
Expand Down
30 changes: 0 additions & 30 deletions Model/Api/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,6 @@ class Customer extends BaseApiModel
*/
protected $rememberMe;

/**
* @var float
* @OA\Property(
* type="number",
* format="float",
* )
* @Constraint\NotBlank(groups={"create", "update"})
*/
protected $discount;

/**
* @var bool
* @OA\Property(
Expand Down Expand Up @@ -289,26 +279,6 @@ public function setRememberMe($rememberMe)
return $this;
}

/**
* @return float
*/
public function getDiscount()
{
return $this->discount;
}

/**
* @param float $discount
*
* @return Customer
*/
public function setDiscount($discount)
{
$this->discount = $discount;

return $this;
}

/**
* @return bool
*/
Expand Down

0 comments on commit b20a257

Please sign in to comment.