Skip to content

Commit

Permalink
Merge pull request #2715 from bakaphp/hotfix/branch-listing
Browse files Browse the repository at this point in the history
Merge pull request #2708 from bakaphp/order-shipping-address
  • Loading branch information
kaioken authored Dec 23, 2024
2 parents 7e81efb + 8b90086 commit d829272
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
23 changes: 22 additions & 1 deletion app/GraphQL/Souk/Handlers/HasAddressHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion graphql/schemas/Souk/order.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ extend type Query @guard {
"county"
"state"
"zip"
"country"
"countries_id"
]
handler: "App\\GraphQL\\Souk\\Handlers\\HasAddressHandler"
)
Expand Down
47 changes: 28 additions & 19 deletions src/Kanvas/Companies/Models/CompaniesBranches.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,7 +20,6 @@
use Kanvas\Users\Models\Users;
use Kanvas\Users\Models\UsersAssociatedCompanies;
use Laravel\Scout\Searchable;
use Baka\Traits\AddressTraitRelationship;

/**
* Companies Model.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d829272

Please sign in to comment.