Skip to content

Commit

Permalink
refact: branch listing
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken committed Dec 23, 2024
1 parent 01257dd commit 8b90086
Showing 1 changed file with 28 additions and 19 deletions.
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 8b90086

Please sign in to comment.