Skip to content

Commit

Permalink
Merge pull request #4 from bildvitta/feature/hub-users
Browse files Browse the repository at this point in the history
HUB Users implementation
  • Loading branch information
michaelnakamura authored Apr 8, 2022
2 parents 1545639 + afabcc8 commit 1eff253
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/IssSupernova.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Bildvitta\IssSupernova\Contracts\IssSupernovaFactory;
use Bildvitta\IssSupernova\Resources\RealEstateDevelopmentTypologies;
use Bildvitta\IssSupernova\Resources\RealEstateDevelopmentUnits;
use Bildvitta\IssSupernova\Resources\Users;
use Illuminate\Http\Client\Factory as HttpClient;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Cache;
Expand Down Expand Up @@ -190,4 +191,9 @@ public function customerPersonalReferences()
{
return new CustomerPersonalReferences($this);
}

public function users()
{
return new Users($this);
}
}
99 changes: 99 additions & 0 deletions src/Observers/User/UserObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Bildvitta\IssSupernova\Observers\User;

use App\Models\User;
use Bildvitta\IssSupernova\IssSupernova;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;

class UserObserver
{
public function created($user)
{
if (!Config::get('iss-supernova.base_uri')) {
return;
}

$user->loadMissing(
'company',
'groups',
);
$data = $user->toArray();
$data['permissions'] = $user->getAllPermissions();
$data['supervisor_uuid'] = $this->getUserUuidByPermission('supervisor.brokers.' . $user->uuid);
$data['manager_uuid'] = $data['supervisor_uuid'] ? $this->getUserUuidByPermission('manager.supervisors.' . $data['supervisor_uuid']) : null;
$data['sync_to'] = 'sys';

try {
$issSupernova = new IssSupernova();
$response = $issSupernova->users()->create($data);
return $response;
} catch (\Throwable $exception) {
Log::error($exception->getMessage());
throw $exception;
}
}

public function updated($user)
{
if (!Config::get('iss-supernova.base_uri')) {
return;
}

$user->loadMissing(
'company',
'groups',
);
$data = $user->toArray();
$data['permissions'] = $user->getAllPermissions();
$data['supervisor_uuid'] = $this->getUserUuidByPermission('supervisor.brokers.' . $user->uuid);
$data['manager_uuid'] = $data['supervisor_uuid'] ? $this->getUserUuidByPermission('manager.supervisors.' . $data['supervisor_uuid']) : null;
$data['sync_to'] = 'sys';

try {
$issSupernova = new IssSupernova();
$response = $issSupernova->users()->update($data);
return $response;
} catch (\Throwable $exception) {
Log::error($exception->getMessage());
throw $exception;
}
}

public function deleted($user)
{
//
}

protected function getUserUuidByPermission($permission, $projectSlug='modular') {
$permission = is_array($permission) ? $permission : [$permission];
$user = User::where(function ($query) use ($permission, $projectSlug) {
$query->whereHas('groups', function ($query) use ($permission, $projectSlug) {
$query->whereHas('permissions', function ($query) use ($permission, $projectSlug) {
$query->whereIn('name', $permission);

$query->whereHas('project', function ($query) use ($projectSlug) {
$query->where('slug', $projectSlug);
});
});
})->orWhereHas('roles', function ($query) use ($permission, $projectSlug) {
$query->whereHas('permissions', function ($query) use ($permission, $projectSlug) {
$query->whereIn('name', $permission);

$query->whereHas('project', function ($query) use ($projectSlug) {
$query->where('slug', $projectSlug);
});
});
})->orWhereHas('permissions', function ($query) use ($permission, $projectSlug) {
$query->whereIn('name', $permission);

$query->whereHas('project', function ($query) use ($projectSlug) {
$query->where('slug', $projectSlug);
});
});
})->first('uuid');
return $user ? $user->uuid : null;
}
}
31 changes: 31 additions & 0 deletions src/Resources/Users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Bildvitta\IssSupernova\Resources;

use Bildvitta\IssSupernova\IssSupernova;

class Users
{
private IssSupernova $issSupernova;

public function __construct(IssSupernova $issSupernova)
{
$this->issSupernova = $issSupernova;
}

public function create($data)
{
return $this->issSupernova->request->post(
'/users',
$data
)->throw()->object();
}

public function update($data)
{
return $this->issSupernova->request->put(
'/users',
$data
)->throw()->object();
}
}

0 comments on commit 1eff253

Please sign in to comment.