From afabcc8ae62ba952971561746b267ccc2422ebd7 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 8 Apr 2022 09:59:59 -0300 Subject: [PATCH] HUB Users implementation --- src/IssSupernova.php | 6 ++ src/Observers/User/UserObserver.php | 99 +++++++++++++++++++++++++++++ src/Resources/Users.php | 31 +++++++++ 3 files changed, 136 insertions(+) create mode 100644 src/Observers/User/UserObserver.php create mode 100644 src/Resources/Users.php diff --git a/src/IssSupernova.php b/src/IssSupernova.php index 797e120..4b01faa 100755 --- a/src/IssSupernova.php +++ b/src/IssSupernova.php @@ -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; @@ -190,4 +191,9 @@ public function customerPersonalReferences() { return new CustomerPersonalReferences($this); } + + public function users() + { + return new Users($this); + } } diff --git a/src/Observers/User/UserObserver.php b/src/Observers/User/UserObserver.php new file mode 100644 index 0000000..b26ff3b --- /dev/null +++ b/src/Observers/User/UserObserver.php @@ -0,0 +1,99 @@ +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; + } +} diff --git a/src/Resources/Users.php b/src/Resources/Users.php new file mode 100644 index 0000000..b3da720 --- /dev/null +++ b/src/Resources/Users.php @@ -0,0 +1,31 @@ +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(); + } +}