no message

This commit is contained in:
StudioTem committed 2023-03-03 14:22:39 +01:00
1 parent 7320e82212
commit 8c79f045fc
6 files changed
+197 -2

No files matched your search

@@ -0,0 +1,38 @@
<?php
namespace App\Console\Commands;
use App\Services\Karat\KaratService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
class SyncClientsFromKarat extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'karat:sync-clients';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Syncs clients from Karat to our DB';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$karatService = new KaratService();
$karatService->syncClients();
return Command::SUCCESS;
}
}
@@ -0,0 +1,38 @@
<?php
namespace App\Console\Commands;
use App\Services\Karat\KaratService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
class SyncProjectsFromKarat extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'karat:sync-projects';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Syncs projects from Karat to our DB';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$karatService = new KaratService();
$karatService->syncProjects();
return Command::SUCCESS;
}
}
+10
View File
@@ -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'];
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace App\Services\Karat;
class Client extends Api {
public $code;
public $name;
public function __construct($code, $name) {
$this->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,
];
}
}
+50 -2
View File
@@ -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);
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace App\Services\Karat;
class Project extends Api {
public $code;
public $name;
public $client;
public function __construct($code, $name, $client) {
$this->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,
];
}
}