From f80e658e5800bb74fbb37bcaf20a41c6d1b3592d Mon Sep 17 00:00:00 2001 From: Michal Feher Date: Fri, 12 Jul 2024 10:21:11 +0200 Subject: [PATCH] no message --- be/app/Services/Intranet/IntranetService.php | 35 +++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/be/app/Services/Intranet/IntranetService.php b/be/app/Services/Intranet/IntranetService.php index ea64d76..cf0be85 100644 --- a/be/app/Services/Intranet/IntranetService.php +++ b/be/app/Services/Intranet/IntranetService.php @@ -3,11 +3,11 @@ namespace App\Services\Intranet; use App\Models\Blocker; -use App\Models\User; use Carbon\Carbon; use Illuminate\Support\Collection; -class IntranetService { +class IntranetService +{ private Api $api; public function __construct() @@ -19,20 +19,26 @@ class IntranetService { { $intranetVacations = Vacation::fromDate(Carbon::yesterday()->toDateString()); - $dbVacations = Blocker::whereIn('type', ['vacation', 'home_office', 'free_day'])->get(); + // toto sa pouziva pre delete starych dovoleniek. je to najstarsi datum, ktory mame v produkcnej DB. ak by sme ho tam nemali, API by nevratila stare dovolenky a povazovalo by sa to za vymazanu dovolenku a tym padom by sa to chybne odstranilo z nasej DB + $intranetVacationsFromPast = Vacation::fromDate('2023-07-27'); + + $dbVacations = Blocker::whereIn('type', ['vacation', 'home_office', 'free_day'])->get(); $this->insertVacationsFromIntranet($intranetVacations, $dbVacations); $this->checkIfVacationsGotAprroved($intranetVacations); + + $this->deleteVacations($intranetVacationsFromPast, $dbVacations); } - private function insertVacationsFromIntranet( Collection $intranetVacations, Collection $dbVacations ) { - $vacationsToSkip = $dbVacations->pluck('intranet_id' )->intersect( $intranetVacations->pluck('intranet_id')); + 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 = $item->toArray(); $arrayToInsert['created_at'] = Carbon::now()->toDateTimeString(); $arrayToInsert['updated_at'] = Carbon::now()->toDateTimeString(); @@ -43,8 +49,10 @@ class IntranetService { Blocker::insert($intranetVacationsToInsert->toArray()); } - private function checkIfVacationsGotAprroved( Collection $intranetVacations ) { - $notApprovedDBVacations = Blocker::whereIn('type', ['vacation', 'home_office', 'free_day'])->where('approved', false)->get(); + private function checkIfVacationsGotAprroved(Collection $intranetVacations) + { + $notApprovedDBVacations = Blocker::whereIn('type', ['vacation', 'home_office', 'free_day'])->where('approved', + false)->get(); // filter vacations from intranet with approved parameter to be true $approvedVacations = $intranetVacations->filter(function ($item) { @@ -63,4 +71,15 @@ class IntranetService { // mass update in DB Blocker::whereIn('intranet_id', $vacationsToApproveIds)->update(['approved' => true]); } + + private function deleteVacations(Collection $intranetVacations, Collection $dbVacations) + { + $vacationsToDelete = $dbVacations->reject(function ($item) use ($intranetVacations) { + return $intranetVacations->pluck('intranet_id')->contains($item->intranet_id); + }); + + $vacationsToDeleteIds = $vacationsToDelete->pluck('id'); + + Blocker::destroy($vacationsToDeleteIds->toArray()); + } }