mirror of
https://bitbucket.org/studiotem/jandl-taskmanager-app.git
synced 2026-10-21 14:29:34 +00:00
no message
This commit is contained in:
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,4 +44,14 @@ class Api
|
|||||||
{
|
{
|
||||||
return self::doRequest('TM_GET_USERS')['users'];
|
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'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Services\Karat;
|
namespace App\Services\Karat;
|
||||||
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use Carbon\Carbon;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
@@ -32,9 +33,12 @@ class KaratService
|
|||||||
$karatUsersToInsert = $karatUsers->reject(function ($item) use ($emailsToSkip) {
|
$karatUsersToInsert = $karatUsers->reject(function ($item) use ($emailsToSkip) {
|
||||||
return $emailsToSkip->contains($item->email);
|
return $emailsToSkip->contains($item->email);
|
||||||
})->map(function ($item) {
|
})->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());
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user