From 1b9a4bdb620fa304735c263aec72ca7db648f779 Mon Sep 17 00:00:00 2001 From: FredPeal Date: Fri, 20 Dec 2024 02:35:44 -0400 Subject: [PATCH 1/2] refactor: for nested where conditions --- .../Souk/Handlers/HasAddressHandler.php | 23 ++++++++++++++++++- graphql/schemas/Souk/order.graphql | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/GraphQL/Souk/Handlers/HasAddressHandler.php b/app/GraphQL/Souk/Handlers/HasAddressHandler.php index 091b33d76..2271d7132 100644 --- a/app/GraphQL/Souk/Handlers/HasAddressHandler.php +++ b/app/GraphQL/Souk/Handlers/HasAddressHandler.php @@ -20,13 +20,34 @@ public function __invoke( string $boolean = 'and', ): void { $addressBuilder = Address::query(); - if ($column = $whereConditions['column'] ?? null) { $this->assertValidColumnReference($column); $this->operator->applyConditions($addressBuilder, $whereConditions, $boolean); } // @to do: add nested where conditions $addressBuilder->from((new Address())->getFullTableName()); + + if (array_key_exists('AND', $whereConditions)) { + $this->nestedConditions($addressBuilder, $whereConditions['AND'][0], 'and'); + } elseif (array_key_exists('OR', $whereConditions)) { + $this->nestedConditions($addressBuilder, $whereConditions['OR'][0], 'or'); + } + $builder->whereExists($addressBuilder); } + + public function nestedConditions(object $builder, array $whereConditions, string $boolean = 'and'): bool + { + if ($column = $whereConditions['column'] ?? null) { + $this->assertValidColumnReference($column); + $this->operator->applyConditions($builder, $whereConditions, $boolean); + } + if (array_key_exists('AND', $whereConditions)) { + $this->nestedConditions($builder, $whereConditions['AND'][0], 'and'); + } elseif (array_key_exists('OR', $whereConditions)) { + $this->nestedConditions($builder, $whereConditions['OR'][0], 'or'); + } + + return true; + } } diff --git a/graphql/schemas/Souk/order.graphql b/graphql/schemas/Souk/order.graphql index 0c0642c0a..1ecef81a0 100644 --- a/graphql/schemas/Souk/order.graphql +++ b/graphql/schemas/Souk/order.graphql @@ -224,7 +224,7 @@ extend type Query @guard { "county" "state" "zip" - "country" + "countries_id" ] handler: "App\\GraphQL\\Souk\\Handlers\\HasAddressHandler" ) From 8b900863a3da3953577ca39bab423a1a72f93e80 Mon Sep 17 00:00:00 2001 From: kaioken Date: Mon, 23 Dec 2024 11:09:49 -0400 Subject: [PATCH 2/2] refact: branch listing --- .../Companies/Models/CompaniesBranches.php | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/Kanvas/Companies/Models/CompaniesBranches.php b/src/Kanvas/Companies/Models/CompaniesBranches.php index 38a03ae35..e3ee5aeb9 100644 --- a/src/Kanvas/Companies/Models/CompaniesBranches.php +++ b/src/Kanvas/Companies/Models/CompaniesBranches.php @@ -4,11 +4,11 @@ namespace Kanvas\Companies\Models; +use Baka\Traits\AddressTraitRelationship; use Baka\Traits\NoAppRelationshipTrait; use Baka\Traits\UuidTrait; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\BelongsTo; -use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Support\Facades\Auth; use Kanvas\Apps\Models\Apps; @@ -20,7 +20,6 @@ use Kanvas\Users\Models\Users; use Kanvas\Users\Models\UsersAssociatedCompanies; use Laravel\Scout\Searchable; -use Baka\Traits\AddressTraitRelationship; /** * Companies Model. @@ -112,23 +111,33 @@ public function shouldBeSearchable(): bool public function scopeUserAssociated(Builder $query): Builder { $user = Auth::user(); - - return $query->join('users_associated_company', function ($join) use ($user) { - $join->on('users_associated_company.companies_id', '=', 'companies_branches.companies_id') - ->where('users_associated_company.is_deleted', '=', 0); - }) - ->join('users_associated_apps', function ($join) { - $join->on('users_associated_apps.companies_id', '=', 'companies_branches.companies_id') - ->where('users_associated_apps.apps_id', app(Apps::class)->getId()); - }) - ->when(! $user->isAdmin(), function ($query) use ($user) { - $query->where('users_associated_company.users_id', $user->getId()); - }) - ->when(app()->bound(CompaniesBranches::class), function ($query) use ($user) { - $query->where('users_associated_apps.companies_id', app(CompaniesBranches::class)->company()->first()->getId()); - }) - ->where('companies_branches.is_deleted', '=', 0) - ->groupBy('companies_branches.id'); + $appId = app(Apps::class)->getId(); + $companyBranch = app()->bound(CompaniesBranches::class) + ? app(CompaniesBranches::class)->company()->first() + : null; + + return $query + ->select('companies_branches.*') // Explicitly select all columns from companies_branches + ->join('users_associated_company', function ($join) { + $join->on('users_associated_company.companies_id', '=', 'companies_branches.companies_id') + ->where('users_associated_company.is_deleted', '=', 0); + }) + ->join('users_associated_apps', function ($join) use ($appId) { + $join->on('users_associated_apps.companies_id', '=', 'companies_branches.companies_id') + ->where('users_associated_apps.apps_id', $appId); + }) + ->when( + ! $user->isAdmin(), + fn ($query) => + $query->where('users_associated_company.users_id', $user->getId()) + ) + ->when( + $companyBranch, + fn ($query) => + $query->where('users_associated_apps.companies_id', $companyBranch->getId()) + ) + ->where('companies_branches.is_deleted', 0) + ->groupBy('companies_branches.id'); } public function users(): HasManyThrough