From 6093ce896ef207c1c8b41cb4db848ddcc72e0250 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Tue, 20 Aug 2024 17:21:00 +0800 Subject: [PATCH 1/3] Allow user account to see and use all vouchers that belongs to all employees under the same company in checkout --- .../Filters/HasActiveRewardWithCompany.php | 53 +++++++++++++++++++ ...Reward.php => HasActiveRewardWithUser.php} | 2 +- .../Services/FetchesBookingQuotation.php | 20 ++++++- .../ControllersLogic/ValidateVoucherLogic.php | 21 +++++++- .../ValidateVoucherifyVoucherObject.php | 4 +- .../BookingToVoucherifyProcessor.php | 21 +++++++- app/Http/Resources/VoucherResource.php | 4 +- .../elements/AvailableVouchersComponent.vue | 2 +- .../elements/ListVouchersComponent.vue | 2 +- .../CustomerRewardsAdminSectionComponent.vue | 2 +- .../CustomerRewardsSectionComponent.vue | 2 +- 11 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 app/Classes/General/Eloquent/Filters/HasActiveRewardWithCompany.php rename app/Classes/General/Eloquent/Filters/{HasActiveReward.php => HasActiveRewardWithUser.php} (96%) diff --git a/app/Classes/General/Eloquent/Filters/HasActiveRewardWithCompany.php b/app/Classes/General/Eloquent/Filters/HasActiveRewardWithCompany.php new file mode 100644 index 00000000..c9ff1f53 --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/HasActiveRewardWithCompany.php @@ -0,0 +1,53 @@ +type, RoleTypes::ADMIN_ROLES)){ + // $userId = $value !== 1 ? $value : Auth::user()->id; + $userId = $value; + $user = User::where('id', $userId)->first(); + $users = $user->company()->first()->employees; + $userIds = $users->pluck('id'); + + return $builder->whereIn('user_id', $userIds) + ->where(function ($query) { + $query->whereHas('reward', function ($subquery) { + $subquery->where('is_active', true); + }) + ->orWhereDoesntHave('reward'); + }) + ->whereDoesntHave('voucher.redemptions.transaction.booking.company.employees', function ($query) use ($userId) { + $query->where('user_id', $userId); + }); + } + else{ + $user = User::where('id', Auth::user()->id)->first(); + $users = $user->company()->first()->employees; + $userIds = $users->pluck('id'); + + return $builder->whereIn('user_id', $userIds) + ->where(function ($query) { + $query->whereHas('reward', function ($subquery) { + $subquery->where('is_active', true); + }) + ->orWhereDoesntHave('reward'); + }) + ->whereDoesntHave('voucher.redemptions.transaction.owner'); + } + } +} diff --git a/app/Classes/General/Eloquent/Filters/HasActiveReward.php b/app/Classes/General/Eloquent/Filters/HasActiveRewardWithUser.php similarity index 96% rename from app/Classes/General/Eloquent/Filters/HasActiveReward.php rename to app/Classes/General/Eloquent/Filters/HasActiveRewardWithUser.php index 5770ca6e..72d0d6e2 100644 --- a/app/Classes/General/Eloquent/Filters/HasActiveReward.php +++ b/app/Classes/General/Eloquent/Filters/HasActiveRewardWithUser.php @@ -6,7 +6,7 @@ use App\Classes\ValueObjects\Constants\RoleTypes; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\Auth; -class HasActiveReward implements Filter +class HasActiveRewardWithUser implements Filter { /** diff --git a/app/Classes/Modules/Bookings/Services/FetchesBookingQuotation.php b/app/Classes/Modules/Bookings/Services/FetchesBookingQuotation.php index e5535e2a..feafba19 100644 --- a/app/Classes/Modules/Bookings/Services/FetchesBookingQuotation.php +++ b/app/Classes/Modules/Bookings/Services/FetchesBookingQuotation.php @@ -70,8 +70,24 @@ class FetchesBookingQuotation //Voucherify if($voucherCode){ - $employee = $company->employees()->first(); - $validateVoucherifyVoucherObject = new ValidateVoucherifyVoucherObject($company->id, $voucherCode, $calculationObject->getSubTotal(), $employee); + $employeeWhoOwnsTheVoucher = null; + + $employees = $company->first()->employees; + foreach($employees as $singleEmployee){ + $userRewards = $singleEmployee->rewards; + foreach($userRewards as $userReward){ + if ($userReward->voucher && $userReward->voucher->code === $voucherCode) { + Log::info('1. Company with multiple employees: ' . json_encode($singleEmployee) . ", voucher: " . $voucherCode); + $employeeWhoOwnsTheVoucher = $singleEmployee; + } + } + } + + if(!$employeeWhoOwnsTheVoucher){ + $employeeWhoOwnsTheVoucher = $company->employees()->first(); + } + + $validateVoucherifyVoucherObject = new ValidateVoucherifyVoucherObject($company->id, $voucherCode, $calculationObject->getSubTotal(), $employeeWhoOwnsTheVoucher); $result = $this->validatesVoucherifyVoucher->execute($validateVoucherifyVoucherObject); $voucher = [ "code" => $result->code, diff --git a/app/Classes/Modules/Vouchers/ControllersLogic/ValidateVoucherLogic.php b/app/Classes/Modules/Vouchers/ControllersLogic/ValidateVoucherLogic.php index 30e7fe12..4530292f 100644 --- a/app/Classes/Modules/Vouchers/ControllersLogic/ValidateVoucherLogic.php +++ b/app/Classes/Modules/Vouchers/ControllersLogic/ValidateVoucherLogic.php @@ -10,6 +10,7 @@ use App\Classes\Modules\Vouchers\DataTransferObjects\ValidateVoucherifyVoucherOb use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use App\Models\Booking; +use Illuminate\Support\Facades\Log; class ValidateVoucherLogic extends AbstractControllerLogic { @@ -42,11 +43,27 @@ class ValidateVoucherLogic extends AbstractControllerLogic */ public function logic(Request $request) : JsonResponse { + $employeeWhoOwnsTheVoucher = null; $booking = Booking::find($request->input('itemId')); - $employee = $booking->company->employees()->first(); + $employees = $booking->company->employees()->get(); + + foreach($employees as $singleEmployee){ + $userRewards = $singleEmployee->rewards; + foreach($userRewards as $userReward){ + if ($userReward->voucher && $userReward->voucher->code === $request->input('voucherCode')) { + Log::info('2. Company with multiple employees: ' . json_encode($singleEmployee) . ", voucher: " . $request->input('voucherCode')); + $employeeWhoOwnsTheVoucher = $singleEmployee; + } + } + } + + if(!$employeeWhoOwnsTheVoucher){ + $employeeWhoOwnsTheVoucher = $booking->company->employees()->first(); + } + $amount = $this->floatvalue($request->input('amount')); - $validateVoucherifyVoucherObject = new ValidateVoucherifyVoucherObject($booking->company_id, $request->input('voucherCode'), $amount, $employee); + $validateVoucherifyVoucherObject = new ValidateVoucherifyVoucherObject($booking->company_id, $request->input('voucherCode'), $amount, $employeeWhoOwnsTheVoucher); $result = $this->validatesVoucherifyVoucher->execute($validateVoucherifyVoucherObject); return $this->response(['data' => $result]); } diff --git a/app/Classes/Modules/Vouchers/DataTransferObjects/ValidateVoucherifyVoucherObject.php b/app/Classes/Modules/Vouchers/DataTransferObjects/ValidateVoucherifyVoucherObject.php index b48bee16..956afc00 100644 --- a/app/Classes/Modules/Vouchers/DataTransferObjects/ValidateVoucherifyVoucherObject.php +++ b/app/Classes/Modules/Vouchers/DataTransferObjects/ValidateVoucherifyVoucherObject.php @@ -17,8 +17,8 @@ class ValidateVoucherifyVoucherObject implements DataTransferObject /** @var float */ private $amount; - /** @var User */ - private $user; + /** @var User */ + private $user; //this will affect certain voucher that limit user redemption e.g. one user one redemption per campaign /** * ValidateVoucherifyVoucherObject constructor. diff --git a/app/Classes/Modules/Vouchers/Processors/Voucherify/BookingToVoucherifyProcessor.php b/app/Classes/Modules/Vouchers/Processors/Voucherify/BookingToVoucherifyProcessor.php index 6f89916b..90fba55a 100644 --- a/app/Classes/Modules/Vouchers/Processors/Voucherify/BookingToVoucherifyProcessor.php +++ b/app/Classes/Modules/Vouchers/Processors/Voucherify/BookingToVoucherifyProcessor.php @@ -83,7 +83,24 @@ class BookingToVoucherifyProcessor $voucherify_customer_id = ""; $voucherify_order_id = ""; if($voucherCode){ - $redeemVoucherifyVoucherObject = new RedeemVoucherifyVoucherObject($companyId, $transaction->id, $voucherCode, $amount, $user); + $employeeWhoOwnsTheVoucher = null; + + $employees = $user->company()->first()->employees; + foreach($employees as $singleEmployee){ + $userRewards = $singleEmployee->rewards; + foreach($userRewards as $userReward){ + if ($userReward->voucher && $userReward->voucher->code === $voucherCode) { + Log::info('3. Company with multiple employees: ' . json_encode($singleEmployee) . ", voucher: " . $voucherCode); + $employeeWhoOwnsTheVoucher = $singleEmployee; + } + } + } + + if(!$employeeWhoOwnsTheVoucher){ + $employeeWhoOwnsTheVoucher = $user; + } + + $redeemVoucherifyVoucherObject = new RedeemVoucherifyVoucherObject($companyId, $transaction->id, $voucherCode, $amount, $employeeWhoOwnsTheVoucher); $redeemVoucherResult = $this->redeemsVoucherifyVoucher->execute($redeemVoucherifyVoucherObject); // Log::info('redeemVoucherResult: '.json_encode($redeemVoucherResult)); @@ -101,7 +118,7 @@ class BookingToVoucherifyProcessor $voucher = $this->recordVoucherInfo($redeemedVoucher); $this->createsVoucherRedemption->execute($transaction, $voucher, $redemptionId, $voucherDiscountAmount); - $this->recordVoucherForUserInfo($user, $voucher); + $this->recordVoucherForUserInfo($employeeWhoOwnsTheVoucher, $voucher); } else{ $createVoucherifyOrderObject = new CreateVoucherifyOrderObject($user, $companyId, $transaction->id, $amount, true, $transaction->type == TransactionType::TOP_UP); diff --git a/app/Http/Resources/VoucherResource.php b/app/Http/Resources/VoucherResource.php index 3f3112bb..7e372d5f 100644 --- a/app/Http/Resources/VoucherResource.php +++ b/app/Http/Resources/VoucherResource.php @@ -4,6 +4,7 @@ namespace App\Http\Resources; use ArrayObject; use Illuminate\Http\Resources\Json\JsonResource; +use Illuminate\Support\Facades\Log; class VoucherResource extends JsonResource { @@ -16,7 +17,8 @@ class VoucherResource extends JsonResource public function toArray($request) { $filteredRedemptions = new ArrayObject([]); - if ($request->has('filters') && str_contains($request->input('filters'), "has_active_reward")) { + if ($request->has('filters') && (str_contains($request->input('filters'), "has_active_reward_with_user") )) { + //|| str_contains($request->input('filters'), "has_active_reward_with_company") $filteredRedemptions = new ArrayObject([]); } else{ diff --git a/resources/assets/vue/components/bookings/elements/AvailableVouchersComponent.vue b/resources/assets/vue/components/bookings/elements/AvailableVouchersComponent.vue index a5fb4e3b..3fff52e8 100644 --- a/resources/assets/vue/components/bookings/elements/AvailableVouchersComponent.vue +++ b/resources/assets/vue/components/bookings/elements/AvailableVouchersComponent.vue @@ -95,7 +95,7 @@ fetchVouchers(){ this.isLoading = true; if(this.employee){ - this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_active_reward': this.employee.id} ), 'get', this.section, false, false); + this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_active_reward_with_company': this.employee.id} ), 'get', this.section, false, false); } }, successHandler(response){ diff --git a/resources/assets/vue/components/bookings/elements/ListVouchersComponent.vue b/resources/assets/vue/components/bookings/elements/ListVouchersComponent.vue index e55e5151..3b2eb5a0 100644 --- a/resources/assets/vue/components/bookings/elements/ListVouchersComponent.vue +++ b/resources/assets/vue/components/bookings/elements/ListVouchersComponent.vue @@ -76,7 +76,7 @@ fetchVouchers(){ this.isLoading = true; if(this.employee){ - this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_active_reward': this.employee.id} ), 'get', this.section, false, false); + this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_active_reward_with_company': this.employee.id} ), 'get', this.section, false, false); } }, successHandler(response){ diff --git a/resources/assets/vue/components/companies/sections/CustomerRewardsAdminSectionComponent.vue b/resources/assets/vue/components/companies/sections/CustomerRewardsAdminSectionComponent.vue index f28c88dd..7300f3ba 100644 --- a/resources/assets/vue/components/companies/sections/CustomerRewardsAdminSectionComponent.vue +++ b/resources/assets/vue/components/companies/sections/CustomerRewardsAdminSectionComponent.vue @@ -57,7 +57,7 @@
- + diff --git a/resources/assets/vue/components/companies/sections/CustomerRewardsSectionComponent.vue b/resources/assets/vue/components/companies/sections/CustomerRewardsSectionComponent.vue index 83567aaf..7a97f22f 100644 --- a/resources/assets/vue/components/companies/sections/CustomerRewardsSectionComponent.vue +++ b/resources/assets/vue/components/companies/sections/CustomerRewardsSectionComponent.vue @@ -52,7 +52,7 @@
- + From 2905edef5e2d3b831b22cf8f5037bd2d1d8ff3a8 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Wed, 21 Aug 2024 22:04:09 +0800 Subject: [PATCH 2/3] Amendment meant to resolve merge conflict in development branch from branch dillon/63.6-company-with-multiple-employees --- .../Eloquent/Filters/HasActiveReward.php | 27 +++++++++++++++++++ .../Filters/HasActiveRewardForAdmin.php | 26 ++++++++++++++++++ ...pany.php => HasVouchersAllWithCompany.php} | 2 +- ...ithUser.php => HasVouchersAllWithUser.php} | 2 +- app/Http/Resources/VoucherResource.php | 4 +-- .../elements/AvailableVouchersComponent.vue | 2 +- .../elements/ListVouchersComponent.vue | 2 +- .../CustomerRewardsAdminSectionComponent.vue | 9 +++++-- .../CustomerRewardsSectionComponent.vue | 15 +++++++---- 9 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 app/Classes/General/Eloquent/Filters/HasActiveReward.php create mode 100644 app/Classes/General/Eloquent/Filters/HasActiveRewardForAdmin.php rename app/Classes/General/Eloquent/Filters/{HasActiveRewardWithCompany.php => HasVouchersAllWithCompany.php} (97%) rename app/Classes/General/Eloquent/Filters/{HasActiveRewardWithUser.php => HasVouchersAllWithUser.php} (96%) diff --git a/app/Classes/General/Eloquent/Filters/HasActiveReward.php b/app/Classes/General/Eloquent/Filters/HasActiveReward.php new file mode 100644 index 00000000..26385b96 --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/HasActiveReward.php @@ -0,0 +1,27 @@ +where('user_id', Auth::user()->id) //cief todo: should not use Auth::user()->id + ->where(function ($query) { + $query->whereHas('reward', function ($subquery) { + $subquery->where('is_active', true); + }); + // ->orWhereDoesntHave('reward'); + }); + } + +} diff --git a/app/Classes/General/Eloquent/Filters/HasActiveRewardForAdmin.php b/app/Classes/General/Eloquent/Filters/HasActiveRewardForAdmin.php new file mode 100644 index 00000000..5d69aa4b --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/HasActiveRewardForAdmin.php @@ -0,0 +1,26 @@ +where('user_id', $value) + ->where(function ($query) { + $query->whereHas('reward', function ($subquery) { + $subquery->where('is_active', true); + }); + // ->orWhereDoesntHave('reward'); + }); + } + +} diff --git a/app/Classes/General/Eloquent/Filters/HasActiveRewardWithCompany.php b/app/Classes/General/Eloquent/Filters/HasVouchersAllWithCompany.php similarity index 97% rename from app/Classes/General/Eloquent/Filters/HasActiveRewardWithCompany.php rename to app/Classes/General/Eloquent/Filters/HasVouchersAllWithCompany.php index c9ff1f53..be4d335b 100644 --- a/app/Classes/General/Eloquent/Filters/HasActiveRewardWithCompany.php +++ b/app/Classes/General/Eloquent/Filters/HasVouchersAllWithCompany.php @@ -7,7 +7,7 @@ use App\Models\User; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\Auth; -class HasActiveRewardWithCompany implements Filter +class HasVouchersAllWithCompany implements Filter { /** diff --git a/app/Classes/General/Eloquent/Filters/HasActiveRewardWithUser.php b/app/Classes/General/Eloquent/Filters/HasVouchersAllWithUser.php similarity index 96% rename from app/Classes/General/Eloquent/Filters/HasActiveRewardWithUser.php rename to app/Classes/General/Eloquent/Filters/HasVouchersAllWithUser.php index 72d0d6e2..79a02914 100644 --- a/app/Classes/General/Eloquent/Filters/HasActiveRewardWithUser.php +++ b/app/Classes/General/Eloquent/Filters/HasVouchersAllWithUser.php @@ -6,7 +6,7 @@ use App\Classes\ValueObjects\Constants\RoleTypes; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\Auth; -class HasActiveRewardWithUser implements Filter +class HasVouchersAllWithUser implements Filter { /** diff --git a/app/Http/Resources/VoucherResource.php b/app/Http/Resources/VoucherResource.php index 7e372d5f..55cc2b56 100644 --- a/app/Http/Resources/VoucherResource.php +++ b/app/Http/Resources/VoucherResource.php @@ -17,8 +17,8 @@ class VoucherResource extends JsonResource public function toArray($request) { $filteredRedemptions = new ArrayObject([]); - if ($request->has('filters') && (str_contains($request->input('filters'), "has_active_reward_with_user") )) { - //|| str_contains($request->input('filters'), "has_active_reward_with_company") + if ($request->has('filters') && (str_contains($request->input('filters'), "has_vouchers_all_with_user") )) { + //|| str_contains($request->input('filters'), "has_vouchers_all_with_company") $filteredRedemptions = new ArrayObject([]); } else{ diff --git a/resources/assets/vue/components/bookings/elements/AvailableVouchersComponent.vue b/resources/assets/vue/components/bookings/elements/AvailableVouchersComponent.vue index 3fff52e8..0d52ff13 100644 --- a/resources/assets/vue/components/bookings/elements/AvailableVouchersComponent.vue +++ b/resources/assets/vue/components/bookings/elements/AvailableVouchersComponent.vue @@ -95,7 +95,7 @@ fetchVouchers(){ this.isLoading = true; if(this.employee){ - this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_active_reward_with_company': this.employee.id} ), 'get', this.section, false, false); + this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_vouchers_all_with_company': this.employee.id} ), 'get', this.section, false, false); } }, successHandler(response){ diff --git a/resources/assets/vue/components/bookings/elements/ListVouchersComponent.vue b/resources/assets/vue/components/bookings/elements/ListVouchersComponent.vue index 3b2eb5a0..cc8afbf2 100644 --- a/resources/assets/vue/components/bookings/elements/ListVouchersComponent.vue +++ b/resources/assets/vue/components/bookings/elements/ListVouchersComponent.vue @@ -76,7 +76,7 @@ fetchVouchers(){ this.isLoading = true; if(this.employee){ - this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_active_reward_with_company': this.employee.id} ), 'get', this.section, false, false); + this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_vouchers_all_with_company': this.employee.id} ), 'get', this.section, false, false); } }, successHandler(response){ diff --git a/resources/assets/vue/components/companies/sections/CustomerRewardsAdminSectionComponent.vue b/resources/assets/vue/components/companies/sections/CustomerRewardsAdminSectionComponent.vue index 7300f3ba..d41c2b63 100644 --- a/resources/assets/vue/components/companies/sections/CustomerRewardsAdminSectionComponent.vue +++ b/resources/assets/vue/components/companies/sections/CustomerRewardsAdminSectionComponent.vue @@ -57,7 +57,7 @@
- + @@ -74,10 +74,15 @@
- + + +
diff --git a/resources/assets/vue/components/companies/sections/CustomerRewardsSectionComponent.vue b/resources/assets/vue/components/companies/sections/CustomerRewardsSectionComponent.vue index 7a97f22f..f6143340 100644 --- a/resources/assets/vue/components/companies/sections/CustomerRewardsSectionComponent.vue +++ b/resources/assets/vue/components/companies/sections/CustomerRewardsSectionComponent.vue @@ -19,7 +19,7 @@
-
+
@@ -52,7 +52,7 @@
- + @@ -69,11 +69,16 @@
- + +
From 8c9264b26a015d9b17723e65f29f103eea846b84 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Fri, 23 Aug 2024 12:24:18 +0800 Subject: [PATCH 3/3] Solve a problem where voucher cannot be added to a user account if user does not exist at Voucherify --- .../General/Eloquent/Filters/RandomName.php | 20 ------------------- .../ControllersLogic/CreateVoucherLogic.php | 14 ++++++++++++- .../CreateVoucherifyCustomerObject.php | 6 +++--- .../NewCustomerToVoucherifyProcessor.php | 4 +++- 4 files changed, 19 insertions(+), 25 deletions(-) delete mode 100644 app/Classes/General/Eloquent/Filters/RandomName.php diff --git a/app/Classes/General/Eloquent/Filters/RandomName.php b/app/Classes/General/Eloquent/Filters/RandomName.php deleted file mode 100644 index 54024e96..00000000 --- a/app/Classes/General/Eloquent/Filters/RandomName.php +++ /dev/null @@ -1,20 +0,0 @@ -where('is_active', $value); - } - -} 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); }