From d69fcf328b55a40cd45c2d1b2f282739dd99f328 Mon Sep 17 00:00:00 2001 From: Michal Feher Date: Fri, 24 Mar 2023 13:46:29 +0100 Subject: [PATCH] no message --- .../Commands/SyncVacationsFromIntranet.php | 36 +++++++ be/app/Services/Intranet/Api.php | 38 +++++++ be/app/Services/Intranet/IntranetService.php | 43 ++++++++ be/app/Services/Intranet/Vacation.php | 98 +++++++++++++++++++ be/app/Services/Karat/User.php | 7 +- be/config/services.php | 4 + ...1218_add_external_id_to_blockers_table.php | 32 ++++++ ...03_23_092000_add_l_d_a_pto_users_table.php | 30 ++++++ 8 files changed, 286 insertions(+), 2 deletions(-) create mode 100644 be/app/Console/Commands/SyncVacationsFromIntranet.php create mode 100644 be/app/Services/Intranet/Api.php create mode 100644 be/app/Services/Intranet/IntranetService.php create mode 100644 be/app/Services/Intranet/Vacation.php create mode 100644 be/database/migrations/2023_03_22_101218_add_external_id_to_blockers_table.php create mode 100644 be/database/migrations/2023_03_23_092000_add_l_d_a_pto_users_table.php diff --git a/be/app/Console/Commands/SyncVacationsFromIntranet.php b/be/app/Console/Commands/SyncVacationsFromIntranet.php new file mode 100644 index 0000000..224e80a --- /dev/null +++ b/be/app/Console/Commands/SyncVacationsFromIntranet.php @@ -0,0 +1,36 @@ +syncVacations(); + return Command::SUCCESS; + } +} diff --git a/be/app/Services/Intranet/Api.php b/be/app/Services/Intranet/Api.php new file mode 100644 index 0000000..28062f7 --- /dev/null +++ b/be/app/Services/Intranet/Api.php @@ -0,0 +1,38 @@ +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']; + } +} diff --git a/be/app/Services/Intranet/IntranetService.php b/be/app/Services/Intranet/IntranetService.php new file mode 100644 index 0000000..29d1d24 --- /dev/null +++ b/be/app/Services/Intranet/IntranetService.php @@ -0,0 +1,43 @@ +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()); + } +} diff --git a/be/app/Services/Intranet/Vacation.php b/be/app/Services/Intranet/Vacation.php new file mode 100644 index 0000000..1ad34cc --- /dev/null +++ b/be/app/Services/Intranet/Vacation.php @@ -0,0 +1,98 @@ +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]; + } + } +} diff --git a/be/app/Services/Karat/User.php b/be/app/Services/Karat/User.php index b02abc1..83e05f4 100644 --- a/be/app/Services/Karat/User.php +++ b/be/app/Services/Karat/User.php @@ -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, ]; } diff --git a/be/config/services.php b/be/config/services.php index af7867b..5c3c932 100644 --- a/be/config/services.php +++ b/be/config/services.php @@ -35,7 +35,11 @@ return [ 'username' => env('KARAT_USERNAME'), 'password' => env('KARAT_PASSWORD'), 'endpoint' => 'https://86.110.228.181:39001/service', + ], + 'intranet' => [ + 'token' => env('INTRANET_TOKEN'), + 'endpoint' => 'http://api.jandlagency.com:23243/api/v1/', ], ]; diff --git a/be/database/migrations/2023_03_22_101218_add_external_id_to_blockers_table.php b/be/database/migrations/2023_03_22_101218_add_external_id_to_blockers_table.php new file mode 100644 index 0000000..8339af8 --- /dev/null +++ b/be/database/migrations/2023_03_22_101218_add_external_id_to_blockers_table.php @@ -0,0 +1,32 @@ +bigInteger('intranet_id')->unsigned()->index()->nullable()->default(NULL)->after('id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('blockers', function (Blueprint $table) { + // + }); + } +}; diff --git a/be/database/migrations/2023_03_23_092000_add_l_d_a_pto_users_table.php b/be/database/migrations/2023_03_23_092000_add_l_d_a_pto_users_table.php new file mode 100644 index 0000000..9afcdbb --- /dev/null +++ b/be/database/migrations/2023_03_23_092000_add_l_d_a_pto_users_table.php @@ -0,0 +1,30 @@ +text('ldap_username')->after('code'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +};