no message

This commit is contained in:
StudioTem committed 2023-02-27 15:32:59 +01:00
1 parent fc02df862f
commit 3128d3657c
7 files changed
+147 -17

No files matched your search

@@ -31,8 +31,7 @@ class SyncUsersFromKarat extends Command
{ {
$karatService = new KaratService(); $karatService = new KaratService();
dd($karatService->syncUsers()); $karatService->syncUsers();
return Command::SUCCESS; return Command::SUCCESS;
} }
+2
View File
@@ -22,6 +22,8 @@ class User extends Authenticatable
'password', 'password',
]; ];
protected $guarded = ['id'];
/** /**
* The attributes that should be hidden for serialization. * The attributes that should be hidden for serialization.
* *
+11 -11
View File
@@ -10,38 +10,38 @@ class Api
/** /**
* @var array|mixed * @var array|mixed
*/ */
private mixed $username; private static mixed $username;
/** /**
* @var array|mixed * @var array|mixed
*/ */
private mixed $password; private static mixed $password;
/** /**
* @var array|mixed * @var array|mixed
*/ */
private mixed $endpoint; private static mixed $endpoint;
public function __construct() public function __construct()
{ {
$this->username = Config::get('services.karat.username'); self::$username = Config::get('services.karat.username');
$this->password = Config::get('services.karat.password'); self::$password = Config::get('services.karat.password');
$this->endpoint = Config::get('services.karat.endpoint'); self::$endpoint = Config::get('services.karat.endpoint');
} }
private function doRequest($command) private static function doRequest($command)
{ {
$response = Http::withOptions([ $response = Http::withOptions([
'debug' => true, 'debug' => true,
'verify' => false, 'verify' => false,
])->withBasicAuth($this->username, $this->password) ])->withBasicAuth(self::$username, self::$password)
->post($this->endpoint, [ ->post(self::$endpoint, [
'command' => $command, 'command' => $command,
]); ]);
return $response->json(); return $response->json();
} }
public function getUsers() public static function getUsers()
{ {
return $this->doRequest('TM_GET_USERS'); return self::doRequest('TM_GET_USERS')['users'];
} }
} }
+32 -4
View File
@@ -2,6 +2,10 @@
namespace App\Services\Karat; namespace App\Services\Karat;
use App\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
class KaratService class KaratService
{ {
private Api $api; private Api $api;
@@ -13,11 +17,35 @@ class KaratService
public function syncUsers() public function syncUsers()
{ {
$users = $this->api->getUsers(); $karatUsers = \App\Services\Karat\User::all();
$dbUsers = User::all();
dd($users);
$this->insertUsersFromKarat($karatUsers, $dbUsers);
$this->removeUsersThatAreNotInKaratAnymore($karatUsers, $dbUsers);
}
private function insertUsersFromKarat(Collection $karatUsers, Collection $dbUsers)
{
$emailsToSkip = $dbUsers->pluck('email' )->intersect( $karatUsers->pluck('email'));
$karatUsersToInsert = $karatUsers->reject(function ($item) use ($emailsToSkip) {
return $emailsToSkip->contains($item->email);
})->map(function ($item) {
$array = $item->toArray();
return $array;
});
User::insert($karatUsersToInsert->toArray());
}
private function removeUsersThatAreNotInKaratAnymore( Collection $karatUsers, Collection $dbUsers )
{
$emailsInDbThatAreNotInKaratAnymore = $dbUsers->pluck('email' )->diff( $karatUsers->pluck('email'));
User::whereIn('email', $emailsInDbThatAreNotInKaratAnymore)->delete();
return $users;
} }
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace App\Services\Karat;
class User extends Api {
public $code;
public $firstname;
public $lastname;
public $email;
public function __construct($code, $firstname, $lastname, $email) {
$this->code = $code;
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->email = $email;
}
public static function all() {
$users = self::getUsers();
return collect(array_map(function($user) {
return new User( $user['id'], $user['name'], $user['surname'], $user['email'] );
}, $users))->reject(function ($item) {
// return only if email is not null
return $item->email === '';
});
}
public function toArray() {
return [
'code' => $this->code,
'firstname' => $this->firstname,
'lastname' => $this->lastname,
'email' => $this->email,
];
}
}
@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->float('work_start', 3, 1)->default(9.0)->change();
$table->float('work_end', 3, 1)->default(17.0)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
};
@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('password')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
};