Allow user account to see and use all vouchers that belongs to all employees under the same company in checkout

This commit is contained in:
Dillon Ngo
2024-08-20 17:21:00 +08:00
parent 8331251c98
commit 6093ce896e
11 changed files with 119 additions and 14 deletions
@@ -0,0 +1,53 @@
<?php
namespace App\Classes\General\Eloquent\Filters;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
class HasActiveRewardWithCompany implements Filter
{
/**
* @param Builder $builder
* @param $value
* @return mixed
*/
public static function apply(Builder $builder, $value)
{
if(in_array(Auth::user()->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');
}
}
}
@@ -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
{
/**
@@ -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,
@@ -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]);
}
@@ -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.
@@ -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);
+3 -1
View File
@@ -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{
@@ -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){
@@ -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){
@@ -57,7 +57,7 @@
<div class="col">
<div class="row">
<div class="col" v-if="this.$store.getters.getUserId">
<list-component section="customerVouchersListSection" :endpoint="route('api.voucher.user.list')" :options="{ 'has_active_reward': id }">
<list-component section="customerVouchersListSection" :endpoint="route('api.voucher.user.list')" :options="{ 'has_active_reward_with_user': id }">
<template slot="list" slot-scope="{data}">
<single-user-reward-item-component :data="data"></single-user-reward-item-component>
</template>
@@ -52,7 +52,7 @@
<div class="col">
<div class="row">
<div class="col" v-if="this.$store.getters.getUserId">
<list-component section="customerVouchersListSection" :endpoint="route('api.voucher.user.list')" :options="{ 'has_active_reward': true }">
<list-component section="customerVouchersListSection" :endpoint="route('api.voucher.user.list')" :options="{ 'has_active_reward_with_user': true }">
<template slot="list" slot-scope="{data}">
<single-user-reward-item-component :data="data"></single-user-reward-item-component>
</template>