diff --git a/be/app/Console/Commands/SyncClientsFromKarat.php b/be/app/Console/Commands/SyncClientsFromKarat.php new file mode 100644 index 0000000..d82afa4 --- /dev/null +++ b/be/app/Console/Commands/SyncClientsFromKarat.php @@ -0,0 +1,38 @@ +syncClients(); + + return Command::SUCCESS; + } +} diff --git a/be/app/Console/Commands/SyncProjectsFromKarat.php b/be/app/Console/Commands/SyncProjectsFromKarat.php new file mode 100644 index 0000000..a6b56f3 --- /dev/null +++ b/be/app/Console/Commands/SyncProjectsFromKarat.php @@ -0,0 +1,38 @@ +syncProjects(); + + return Command::SUCCESS; + } +} diff --git a/be/app/Services/Karat/Api.php b/be/app/Services/Karat/Api.php index 68c8187..91c57c9 100644 --- a/be/app/Services/Karat/Api.php +++ b/be/app/Services/Karat/Api.php @@ -44,4 +44,14 @@ class Api { return self::doRequest('TM_GET_USERS')['users']; } + + public static function getClients() + { + return self::doRequest('TM_GET_CLIENTS')['clients']; + } + + public static function getProjects() + { + return self::doRequest('TM_GET_CLIENTS')['projects']; + } } diff --git a/be/app/Services/Karat/Client.php b/be/app/Services/Karat/Client.php new file mode 100644 index 0000000..496e5d5 --- /dev/null +++ b/be/app/Services/Karat/Client.php @@ -0,0 +1,29 @@ +code = $code; + $this->name = $name; + } + + public static function all() { + $clients = self::getClients(); + + return collect(array_map(function($client) { + return new Client( $client['id'], $client['name']); + }, $clients)); + } + + public function toArray() { + return [ + 'code' => $this->code, + 'name' => $this->name, + ]; + } + +} diff --git a/be/app/Services/Karat/KaratService.php b/be/app/Services/Karat/KaratService.php index 7bf9fb6..fe024aa 100644 --- a/be/app/Services/Karat/KaratService.php +++ b/be/app/Services/Karat/KaratService.php @@ -3,6 +3,7 @@ namespace App\Services\Karat; use App\Models\User; +use Carbon\Carbon; use Illuminate\Support\Arr; use Illuminate\Support\Collection; @@ -32,9 +33,12 @@ class KaratService $karatUsersToInsert = $karatUsers->reject(function ($item) use ($emailsToSkip) { return $emailsToSkip->contains($item->email); })->map(function ($item) { - $array = $item->toArray(); + $arrayToInsert = $item->toArray(); - return $array; + $arrayToInsert['created_at'] = Carbon::now(); + $arrayToInsert['updated_at'] = Carbon::now(); + + return $arrayToInsert; }); User::insert($karatUsersToInsert->toArray()); @@ -48,5 +52,49 @@ class KaratService } + public function syncClients() + { + $karatClients = Client::all(); + $dbClients = \App\Models\Client::all(); + + $this->insertClientsFromKarat($karatClients, $dbClients); + + $this->removeClientsThatAreNotInKaratAnymore($karatClients, $dbClients); + } + + private function insertClientsFromKarat( Collection $karatClients, Collection $dbClients ) + { + $clientsToSkip = $dbClients->pluck('code' )->intersect( $karatClients->pluck('code')); + + $karatClientsToInsert = $karatClients->reject(function ($item) use ($clientsToSkip) { + return $clientsToSkip->contains($item->code); + })->map(function ($item) { + $arrayToInsert = $item->toArray(); + + $arrayToInsert['created_at'] = Carbon::now(); + $arrayToInsert['updated_at'] = Carbon::now(); + + return $arrayToInsert; + }); + + \App\Models\Client::insert($karatClientsToInsert->toArray()); + } + + private function removeClientsThatAreNotInKaratAnymore( Collection $karatClients, Collection $dbClients ) + { + dd('finish me'); + } + + public function syncProjects() + { + $karatProjects = Project::all(); + $dbProjects = \App\Models\Project::all(); + + $this->insertProjectsFromKarat($karatProjects, $dbProjects); + + $this->removeProjectsThatAreNotInKaratAnymore($karatProjects, $dbProjects); + } + + } diff --git a/be/app/Services/Karat/Project.php b/be/app/Services/Karat/Project.php new file mode 100644 index 0000000..eae095d --- /dev/null +++ b/be/app/Services/Karat/Project.php @@ -0,0 +1,32 @@ +code = $code; + $this->name = $name; + $this->client = $client; + } + + public static function all() { + $projects = self::getProjects(); + + return collect(array_map(function($project) { + return new Project( $project['id'], $project['name'], $project['client_id']); + }, $projects)); + } + + public function toArray() { + return [ + 'code' => $this->code, + 'name' => $this->name, + 'client' => $this->client, + ]; + } + +}