diff --git a/be/app/Console/Commands/SyncUsersFromKarat.php b/be/app/Console/Commands/SyncUsersFromKarat.php index c1aa39e..72795fe 100644 --- a/be/app/Console/Commands/SyncUsersFromKarat.php +++ b/be/app/Console/Commands/SyncUsersFromKarat.php @@ -31,8 +31,7 @@ class SyncUsersFromKarat extends Command { $karatService = new KaratService(); - dd($karatService->syncUsers()); - + $karatService->syncUsers(); return Command::SUCCESS; } diff --git a/be/app/Models/User.php b/be/app/Models/User.php index 8640624..0e17462 100644 --- a/be/app/Models/User.php +++ b/be/app/Models/User.php @@ -22,6 +22,8 @@ class User extends Authenticatable 'password', ]; + protected $guarded = ['id']; + /** * The attributes that should be hidden for serialization. * diff --git a/be/app/Services/Karat/Api.php b/be/app/Services/Karat/Api.php index f2ce08f..68c8187 100644 --- a/be/app/Services/Karat/Api.php +++ b/be/app/Services/Karat/Api.php @@ -10,38 +10,38 @@ class Api /** * @var array|mixed */ - private mixed $username; + private static mixed $username; /** * @var array|mixed */ - private mixed $password; + private static mixed $password; /** * @var array|mixed */ - private mixed $endpoint; + private static mixed $endpoint; public function __construct() { - $this->username = Config::get('services.karat.username'); - $this->password = Config::get('services.karat.password'); - $this->endpoint = Config::get('services.karat.endpoint'); + self::$username = Config::get('services.karat.username'); + self::$password = Config::get('services.karat.password'); + self::$endpoint = Config::get('services.karat.endpoint'); } - private function doRequest($command) + private static function doRequest($command) { $response = Http::withOptions([ 'debug' => true, 'verify' => false, - ])->withBasicAuth($this->username, $this->password) - ->post($this->endpoint, [ + ])->withBasicAuth(self::$username, self::$password) + ->post(self::$endpoint, [ 'command' => $command, ]); return $response->json(); } - public function getUsers() + public static function getUsers() { - return $this->doRequest('TM_GET_USERS'); + return self::doRequest('TM_GET_USERS')['users']; } } diff --git a/be/app/Services/Karat/KaratService.php b/be/app/Services/Karat/KaratService.php index 6baf116..7bf9fb6 100644 --- a/be/app/Services/Karat/KaratService.php +++ b/be/app/Services/Karat/KaratService.php @@ -2,6 +2,10 @@ namespace App\Services\Karat; +use App\Models\User; +use Illuminate\Support\Arr; +use Illuminate\Support\Collection; + class KaratService { private Api $api; @@ -13,11 +17,35 @@ class KaratService public function syncUsers() { - $users = $this->api->getUsers(); - - dd($users); + $karatUsers = \App\Services\Karat\User::all(); + $dbUsers = User::all(); + + $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; } diff --git a/be/app/Services/Karat/User.php b/be/app/Services/Karat/User.php new file mode 100644 index 0000000..b02abc1 --- /dev/null +++ b/be/app/Services/Karat/User.php @@ -0,0 +1,38 @@ +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, + ]; + } + +} diff --git a/be/database/migrations/2023_02_25_160300_add_default_values_to_users_table.php b/be/database/migrations/2023_02_25_160300_add_default_values_to_users_table.php new file mode 100644 index 0000000..86f9639 --- /dev/null +++ b/be/database/migrations/2023_02_25_160300_add_default_values_to_users_table.php @@ -0,0 +1,33 @@ +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) { + // + }); + } +}; diff --git a/be/database/migrations/2023_02_25_161605_allow_null_password_for_users.php b/be/database/migrations/2023_02_25_161605_allow_null_password_for_users.php new file mode 100644 index 0000000..6df0a8c --- /dev/null +++ b/be/database/migrations/2023_02_25_161605_allow_null_password_for_users.php @@ -0,0 +1,30 @@ +string('password')->nullable()->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +};