Skip to content

Commit

Permalink
Merge pull request #901 from bakaphp/add-fields-to-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken authored Feb 7, 2024
2 parents 631c158 + 7041372 commit 8c25fab
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public function addUserToBranch($rootValue, array $request): bool
$branch
);

$branch->set('total_users', $branch->users()->count());

return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::connection('ecosystem')->table('companies_branches', function (Blueprint $table) {
$table->boolean('is_active')->after('phone')->default(1);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('companies_branches', function (Blueprint $table) {
$table->dropColumn('is_active');
});
}
};
4 changes: 4 additions & 0 deletions graphql/schemas/Ecosystem/company.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type CompanyBranch {
builder: "App\\GraphQL\\Ecosystem\\Queries\\Filesystem\\FilesystemQuery@getFileByGraphType"
)
zipcode: Int
total_users: Int!
is_active: Boolean!
is_default: Boolean!
created_at: DateTime
updated_at: DateTime
Expand Down Expand Up @@ -102,6 +104,7 @@ input CompanyBranchInput {
timezone: String
phone: String
files: [FilesystemInputUrl]
is_active: Boolean
}

type CompanySettings {
Expand Down Expand Up @@ -140,6 +143,7 @@ extend type Query @guard {
)

branches(
search: String @search
where: _
@whereConditions(
columns: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function execute(): CompaniesBranches
$companyBranch->email = $this->data->email;
$companyBranch->phone = $this->data->phone;
$companyBranch->zipcode = $this->data->zipcode;
$companyBranch->is_active = $this->data->is_active;

$company->branches()->save($companyBranch);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function execute(int $companyBranchId): CompaniesBranches
$companyBranch->email = $this->data->email;
$companyBranch->phone = $this->data->phone;
$companyBranch->zipcode = $this->data->zipcode;
$companyBranch->is_active = $this->data->is_active;
$companyBranch->updateOrFail();

if ($this->data->files) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ class CompaniesBranchPostData extends Data
/**
* Construct function.
*
* @param string $name
* @param int|null $users_id
* @param array|null $files
*/
public function __construct(
public string $name,
public int $companies_id,
public int $users_id,
public int $is_default = 0,
public bool $is_active = true,
public ?string $email = null,
public ?string $address = null,
public ?string $phone = null,
Expand All @@ -37,8 +36,6 @@ public function __construct(
* Create new instance of DTO from request.
*
* @param Request $request Request Input data
*
* @return self
*/
public static function viaRequest(Request $request): self
{
Expand All @@ -49,15 +46,14 @@ public static function viaRequest(Request $request): self
is_default: (int) $request->get('is_default'),
email : $request->get('email'),
files : $request->get('files'),
is_active: $data['is_active'] ?? true
);
}

/**
* Create new instance of DTO from array of data.
*
* @param array $data Input data
*
* @return self
*/
public static function fromArray(array $data): self
{
Expand All @@ -69,7 +65,8 @@ public static function fromArray(array $data): self
email : $data['email'] ?? null,
phone : $data['phone'] ?? null,
zipcode : $data['zipcode'] ?? null,
files: $data['files'] ?? null
files: $data['files'] ?? null,
is_active: $data['is_active'] ?? true
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ class CompaniesBranchPutData extends Data
/**
* Construct function.
*
* @param string $name
* @param int|null $users_id
* @param array|null $files
*/
public function __construct(
public string $name,
public int $companies_id,
public int $is_default = 0,
public bool $is_active = true,
public ?string $email = null,
public ?string $address = null,
public ?string $phone = null,
Expand All @@ -35,8 +34,6 @@ public function __construct(
* Create new instance of DTO from request.
*
* @param Request $request Request Input data
*
* @return self
*/
public static function viaRequest(Request $request): self
{
Expand All @@ -53,8 +50,6 @@ public static function viaRequest(Request $request): self
* Create new instance of DTO from array of data.
*
* @param array $data Input data
*
* @return self
*/
public static function fromArray(array $data): self
{
Expand Down
48 changes: 47 additions & 1 deletion src/Kanvas/Companies/Models/CompaniesBranches.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
use Baka\Traits\UuidTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Facades\Auth;
use Kanvas\Apps\Models\Apps;
use Kanvas\Companies\Branches\Factories\CompaniesBranchesFactory;
use Kanvas\Companies\Enums\Defaults;
use Kanvas\CustomFields\Traits\HasCustomFields;
use Kanvas\Enums\StateEnums;
use Kanvas\Filesystem\Models\FilesystemEntities;
use Kanvas\Filesystem\Traits\HasFilesystemTrait;
use Kanvas\Models\BaseModel;
use Kanvas\Traits\SearchableDynamicIndexTrait;
use Kanvas\Users\Models\Users;
use Kanvas\Users\Models\UsersAssociatedCompanies;

/**
* Companies Model.
Expand All @@ -30,6 +37,8 @@ class CompaniesBranches extends BaseModel
{
use UuidTrait;
use HasFilesystemTrait;
use SearchableDynamicIndexTrait;
use HasCustomFields;

/**
* The table associated with the model.
Expand Down Expand Up @@ -72,6 +81,25 @@ public function isDefault(): bool
return (bool) $this->is_default;
}

public function getTotalUsersAttribute(): int
{
if (! $this->get('total_users')) {
$this->set('total_users', $this->users()->count());
}

return $this->get('total_users');
}

public static function searchableIndex(): string
{
return Defaults::SEARCHABLE_INDEX->getValue();
}

public function shouldBeSearchable(): bool
{
return $this->is_deleted === StateEnums::NO->getValue();
}

/**
* Filter what the user can see.
*/
Expand All @@ -81,12 +109,30 @@ public function scopeUserAssociated(Builder $query): Builder

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.users_id', '=', $user->getKey())
->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());
})
->where('companies_branches.is_deleted', '=', 0);
}

public function users(): HasManyThrough
{
return $this->hasManyThrough(
Users::class,
UsersAssociatedCompanies::class,
'companies_branches_id',
'id',
'id',
'users_id'
);
}

/**
* Get a branch with id 0 , representing the global branch.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Kanvas/Companies/Observers/CompaniesObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function created(Companies $company): void
{
$app = app(Apps::class);
$user = $company->user()->first();

$app->associateCompany($company);

$createCompanyGroup = new CreateCompanyGroupActions($company, $app);
Expand All @@ -46,6 +45,7 @@ public function created(Companies $company): void
$company->id,
$company->users_id,
StateEnums::YES->getValue(),
true,
$company->email
)
);
Expand Down
1 change: 1 addition & 0 deletions tests/GraphQL/Ecosystem/Companies/CompanyBranchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function branchInputData(int $companyId): array
'phone' => fake()->phoneNumber(),
'email' => fake()->email(),
'country_code' => 'US',
'is_active' => fake()->boolean(),
];
}

Expand Down

0 comments on commit 8c25fab

Please sign in to comment.