diff --git a/app/Classes/Modules/Vouchers/ControllersLogic/CreateVoucherLogic.php b/app/Classes/Modules/Vouchers/ControllersLogic/CreateVoucherLogic.php index 5c85895a..e345ebcf 100644 --- a/app/Classes/Modules/Vouchers/ControllersLogic/CreateVoucherLogic.php +++ b/app/Classes/Modules/Vouchers/ControllersLogic/CreateVoucherLogic.php @@ -11,6 +11,7 @@ use App\Classes\Modules\Vouchers\Services\Voucherify\ValidatesVoucherifyVoucher; use App\Classes\Modules\Vouchers\Services\Voucherify\CreatesVoucherifyVoucherInACampaign; use App\Classes\Modules\Vouchers\Services\Voucherify\ListsVoucherifyVouchers; use App\Classes\Modules\Vouchers\Services\Voucherify\FetchesVoucherifyCampaign; +use App\Classes\Modules\Vouchers\Processors\Voucherify\NewCustomerToVoucherifyProcessor; use App\Classes\Modules\Vouchers\Services\CreatesVoucher; use App\Classes\Modules\Vouchers\Services\FetchesVoucher; use App\Classes\Modules\Vouchers\Services\UpdatesVoucherCampaign; @@ -75,6 +76,9 @@ class CreateVoucherLogic extends AbstractControllerLogic /** @var UpdatesVoucherCampaign */ private $updatesVoucherCampaign; + /** @var NewCustomerToVoucherifyProcessor */ + private $newCustomerToVoucherifyProcessor; + /** * CreateVoucherLogic constructor. * @param ValidatesVoucherifyVoucher $validatesVoucherifyVoucher @@ -87,8 +91,9 @@ class CreateVoucherLogic extends AbstractControllerLogic * @param CreatesKeyValuePair $createsKeyValuePair * @param UpdatesKeyValuePair $updatesKeyValuePair * @param UpdatesVoucherCampaign $updatesVoucherCampaign + * @param NewCustomerToVoucherifyProcessor $newCustomerToVoucherifyProcessor */ - public function __construct(CreatesUserReward $createsUserReward, ValidatesVoucherifyVoucher $validatesVoucherifyVoucher, CreateVoucherProcessor $createVoucherProcessor, CreatesVoucherifyVoucherInACampaign $createsVoucherifyVoucherInACampaign, CanCreateVoucher $canCreateVoucher, ListsVoucherifyVouchers $listsVoucherifyVouchers, FetchesVoucherifyCampaign $fetchesVoucherifyCampaign, CreatesKeyValuePair $createsKeyValuePair, UpdatesKeyValuePair $updatesKeyValuePair, UpdatesVoucherCampaign $updatesVoucherCampaign) + public function __construct(CreatesUserReward $createsUserReward, ValidatesVoucherifyVoucher $validatesVoucherifyVoucher, CreateVoucherProcessor $createVoucherProcessor, CreatesVoucherifyVoucherInACampaign $createsVoucherifyVoucherInACampaign, CanCreateVoucher $canCreateVoucher, ListsVoucherifyVouchers $listsVoucherifyVouchers, FetchesVoucherifyCampaign $fetchesVoucherifyCampaign, CreatesKeyValuePair $createsKeyValuePair, UpdatesKeyValuePair $updatesKeyValuePair, UpdatesVoucherCampaign $updatesVoucherCampaign, NewCustomerToVoucherifyProcessor $newCustomerToVoucherifyProcessor) { $this->createsUserReward = $createsUserReward; $this->validatesVoucherifyVoucher = $validatesVoucherifyVoucher; @@ -100,6 +105,7 @@ class CreateVoucherLogic extends AbstractControllerLogic $this->createsKeyValuePair = $createsKeyValuePair; $this->updatesKeyValuePair = $updatesKeyValuePair; $this->updatesVoucherCampaign = $updatesVoucherCampaign; + $this->newCustomerToVoucherifyProcessor = $newCustomerToVoucherifyProcessor; } /** @@ -129,6 +135,12 @@ class CreateVoucherLogic extends AbstractControllerLogic $user = $userParam ? $userParam : $user; } + //Voucherify - To check if user exist at Voucherify, create if not exist + $voucherify_entity = $user->voucherifyEntities()->first(); + if(!$voucherify_entity){ + $this->newCustomerToVoucherifyProcessor->execute($user->company()->first()->id, $user, false); + } + //Voucherify - creates new voucher at voucherify if ($voucherCodeInput === Vouchers::SORRY_50 || $voucherCodeInput === Vouchers::SORRY_100 || $voucherCodeInput === Vouchers::SORRY_200 ) { $result = $this->newVoucherifyVoucherIssuanceHandler($voucherCodeInput); diff --git a/app/Classes/Modules/Vouchers/DataTransferObjects/CreateVoucherifyCustomerObject.php b/app/Classes/Modules/Vouchers/DataTransferObjects/CreateVoucherifyCustomerObject.php index 9678dafe..e61d1d00 100644 --- a/app/Classes/Modules/Vouchers/DataTransferObjects/CreateVoucherifyCustomerObject.php +++ b/app/Classes/Modules/Vouchers/DataTransferObjects/CreateVoucherifyCustomerObject.php @@ -65,9 +65,9 @@ class CreateVoucherifyCustomerObject implements DataTransferObject */ public function getAcquisitionChannel(): string { - if(!$this->isNew){ - return ""; - } + // if(!$this->isNew){ + // return ""; + // } return $this->acquisitionChannel; } diff --git a/app/Classes/Modules/Vouchers/Processors/Voucherify/NewCustomerToVoucherifyProcessor.php b/app/Classes/Modules/Vouchers/Processors/Voucherify/NewCustomerToVoucherifyProcessor.php index 006bcd94..3fd2ccee 100644 --- a/app/Classes/Modules/Vouchers/Processors/Voucherify/NewCustomerToVoucherifyProcessor.php +++ b/app/Classes/Modules/Vouchers/Processors/Voucherify/NewCustomerToVoucherifyProcessor.php @@ -44,7 +44,9 @@ class NewCustomerToVoucherifyProcessor $createVoucherifyCustomerObject = new CreateVoucherifyCustomerObject($companyId, $user, $isNew); $result = $this->createsVoucherifyCustomer->execute($createVoucherifyCustomerObject); - if($result && isset($result->id)){ + $voucherify_entity = $user->voucherifyEntities()->get(); + + if($result && isset($result->id) && count($voucherify_entity) === 0){ $voucherEntityObject = new VoucherEntityObject($result->id, VoucherifyEntityType::CUSTOMER); $this->createsVoucherEntityMapping->execute($createVoucherifyCustomerObject->getUser(), $voucherEntityObject); }