no message

This commit is contained in:
StudioTem committed 2023-03-24 13:46:29 +01:00
1 parent 04c42c2762
commit d69fcf328b
8 files changed
+286 -2

No files matched your search

@@ -0,0 +1,36 @@
<?php
namespace App\Console\Commands;
use App\Services\Intranet\IntranetService;
use Illuminate\Console\Command;
class SyncVacationsFromIntranet extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'intranet:sync-vacations';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Syncs vacations from Intranet to our DB';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$intranetService = new IntranetService();
$intranetService->syncVacations();
return Command::SUCCESS;
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace App\Services\Intranet;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
class Api
{
private static mixed $token;
private static mixed $endpoint;
public function __construct()
{
self::$token = Config::get('services.intranet.token');
self::$endpoint = Config::get('services.intranet.endpoint');
}
private static function doRequest($command, $dateFrom)
{
$response = Http::timeout(60)
->withOptions([
'debug' => true,
'verify' => false,
])->get(self::$endpoint . $command, [
'date-from' => $dateFrom,
'api-token' => self::$token,
]);
return $response->json();
}
public static function getVacations($dateFrom)
{
return self::doRequest('vacations', $dateFrom)['vacations'];
}
}
@@ -0,0 +1,43 @@
<?php
namespace App\Services\Intranet;
use App\Models\Blocker;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Collection;
class IntranetService {
private Api $api;
public function __construct()
{
$this->api = new Api();
}
public function syncVacations()
{
$intranetVacations = Vacation::fromDate(Carbon::yesterday()->toDateString());
$dbVacations = Blocker::whereIn('type', ['vacation', 'home_office', 'free_day'])->get();
$this->insertVacationsFromIntranet($intranetVacations, $dbVacations);
}
private function insertVacationsFromIntranet( Collection $intranetVacations, Collection $dbVacations ) {
$vacationsToSkip = $dbVacations->pluck('intranet_id' )->intersect( $intranetVacations->pluck('intranet_id'));
$intranetVacationsToInsert = $intranetVacations->reject(function ($item) use ($vacationsToSkip) {
return $vacationsToSkip->contains($item->intranet_id);
})->map(function ($item) {
$arrayToInsert = $item->toArray();
$arrayToInsert['created_at'] = Carbon::now()->toDateTimeString();
$arrayToInsert['updated_at'] = Carbon::now()->toDateTimeString();
return $arrayToInsert;
});
Blocker::insert($intranetVacationsToInsert->toArray());
}
}
+98
View File
@@ -0,0 +1,98 @@
<?php
namespace App\Services\Intranet;
use App\Models\User;
use Carbon\Carbon;
class Vacation extends Api {
public $intranet_id;
public $user_id;
public $start_date;
public $end_date;
public $type;
public $name;
public function __construct($intranet_id, $ldap_username, $start_date, $end_date, $type, $halfday) {
$startend = $this->getStartEndDate($start_date, $end_date, $halfday, $ldap_username);
$this->intranet_id = $intranet_id;
$this->user_id = $this->getUserId($ldap_username);
$this->start_date = (isset($startend['start'])) ? $startend['start'] : null;
$this->end_date = (isset($startend['end'])) ? $startend['end'] : null;
$this->type = $type;
$this->name = $this->getVacationName($type);
}
public static function fromDate($date) {
$vacations = self::getVacations($date);
return collect(array_map(function($vacation) {
return new Vacation( $vacation['id'], $vacation['user']['ldap_username'], $vacation['start_date'], $vacation['end_date'], $vacation['vacation_type']['name'], $vacation['halfday_position']);
}, $vacations))->reject(function($vacation) {
return $vacation->user_id === null;
});
}
public function toArray() {
return [
'intranet_id' => $this->intranet_id,
'user_id' => $this->user_id,
'start' => date('Y-m-d H:i:s', strtotime($this->start_date)),
'end' => date('Y-m-d H:i:s', strtotime($this->end_date)),
'type' => $this->type,
'name' => $this->name,
];
}
private function getVacationName($vacation_type) {
switch ($vacation_type) {
case 'home_office':
return 'Home Office';
case 'vacation':
return 'Vacation';
case 'free_day':
return 'Free day';
default:
return 'Other';
}
}
private function getUserId($ldap_username) {
$user = User::where('ldap_username', $ldap_username)->first();
return ($user) ? $user->id : null;
}
// vyratanie zaciatku a konca dovolenky na zaklade toho, ci sa jedna o cely den alebo polden a na zaklade toho, co ma user zadane v pracovnej dobe
private function getStartEndDate($start_date, $end_date, $halfday, $ldap_username) {
$user = User::where('ldap_username', $ldap_username)->first();
if (!$user) return null;
$workStart = $user->work_start;
$workEnd = $user->work_end;
// convert hour integer number to time hours
$workStart = sprintf('%02d:%02d', (int) $workStart, fmod($workStart, 1) * 60);
$workEnd = sprintf('%02d:%02d', (int) $workEnd, fmod($workEnd, 1) * 60);
if ($halfday == 'start') {
$start = $start_date . ' ' . $workStart;
$end = date('Y-m-d H:i:s', strtotime($start . ' + 4 hours'));
return ['start' => $start, 'end' => $end];
} else if ($halfday == 'end') {
$end = $end_date . ' ' . $workEnd;
$start = date('Y-m-d H:i:s', strtotime($end . ' - 4 hours'));
return ['start' => $start, 'end' => $end];
} else if ($halfday == null) {
$start = date('Y-m-d H:i:s', strtotime($start_date . ' ' . $workStart));
$end = date('Y-m-d H:i:s', strtotime($end_date . ' ' . $workEnd));
return ['start' => $start, 'end' => $end];
}
}
}
+5 -2
View File
@@ -7,19 +7,21 @@ class User extends Api {
public $firstname;
public $lastname;
public $email;
public $ldap_username;
public function __construct($code, $firstname, $lastname, $email) {
public function __construct($code, $firstname, $lastname, $email, $ldap_username) {
$this->code = $code;
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->email = $email;
$this->ldap_username = $ldap_username;
}
public static function all() {
$users = self::getUsers();
return collect(array_map(function($user) {
return new User( $user['id'], $user['name'], $user['surname'], $user['email'] );
return new User( $user['id'], $user['name'], $user['surname'], $user['email'], $user['ldap_userName'] );
}, $users))->reject(function ($item) {
// return only if email is not null
return $item->email === '';
@@ -32,6 +34,7 @@ class User extends Api {
'firstname' => $this->firstname,
'lastname' => $this->lastname,
'email' => $this->email,
'ldap_username' => $this->ldap_username,
];
}