From 52fafc6aa926dc47e987f810ddf5b59a20334cca Mon Sep 17 00:00:00 2001 From: Michal Feher Date: Tue, 12 Dec 2023 15:24:10 +0100 Subject: [PATCH] no message --- be/app/Http/Resources/BlockerResource.php | 1 + be/app/Services/Intranet/IntranetService.php | 23 +++++++++++++ be/app/Services/Intranet/Vacation.php | 7 ++-- ..._135118_add_approved_to_blockers_table.php | 32 +++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 be/database/migrations/2023_12_12_135118_add_approved_to_blockers_table.php diff --git a/be/app/Http/Resources/BlockerResource.php b/be/app/Http/Resources/BlockerResource.php index 2c6c986..c729c9a 100644 --- a/be/app/Http/Resources/BlockerResource.php +++ b/be/app/Http/Resources/BlockerResource.php @@ -22,6 +22,7 @@ class BlockerResource extends JsonResource 'end' => $this->end, 'type' => $this->type, 'color' => $this->color, + 'approved' => $this->approved, ]; } } diff --git a/be/app/Services/Intranet/IntranetService.php b/be/app/Services/Intranet/IntranetService.php index 29d1d24..ea64d76 100644 --- a/be/app/Services/Intranet/IntranetService.php +++ b/be/app/Services/Intranet/IntranetService.php @@ -22,6 +22,8 @@ class IntranetService { $dbVacations = Blocker::whereIn('type', ['vacation', 'home_office', 'free_day'])->get(); $this->insertVacationsFromIntranet($intranetVacations, $dbVacations); + + $this->checkIfVacationsGotAprroved($intranetVacations); } private function insertVacationsFromIntranet( Collection $intranetVacations, Collection $dbVacations ) { @@ -40,4 +42,25 @@ class IntranetService { Blocker::insert($intranetVacationsToInsert->toArray()); } + + 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) { + return $item->approved === true; + }); + + $vacationsToApprove = $approvedVacations->filter(function ($item) use ($notApprovedDBVacations) { + return $notApprovedDBVacations->contains('intranet_id', $item->intranet_id); + }); + + // get intranet_id from vacations to approve and update them in db + $vacationsToApproveIds = $vacationsToApprove->map(function ($item) { + return $item->intranet_id; + }); + + // mass update in DB + Blocker::whereIn('intranet_id', $vacationsToApproveIds)->update(['approved' => true]); + } } diff --git a/be/app/Services/Intranet/Vacation.php b/be/app/Services/Intranet/Vacation.php index 1ad34cc..f802b32 100644 --- a/be/app/Services/Intranet/Vacation.php +++ b/be/app/Services/Intranet/Vacation.php @@ -13,8 +13,9 @@ class Vacation extends Api { public $end_date; public $type; public $name; + public $approved; - public function __construct($intranet_id, $ldap_username, $start_date, $end_date, $type, $halfday) { + public function __construct($intranet_id, $ldap_username, $start_date, $end_date, $type, $halfday, $approved) { $startend = $this->getStartEndDate($start_date, $end_date, $halfday, $ldap_username); $this->intranet_id = $intranet_id; @@ -23,13 +24,14 @@ class Vacation extends Api { $this->end_date = (isset($startend['end'])) ? $startend['end'] : null; $this->type = $type; $this->name = $this->getVacationName($type); + $this->approved = $approved; } 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']); + return new Vacation( $vacation['id'], $vacation['user']['ldap_username'], $vacation['start_date'], $vacation['end_date'], $vacation['vacation_type']['name'], $vacation['halfday_position'], $vacation['approved']); }, $vacations))->reject(function($vacation) { return $vacation->user_id === null; }); @@ -43,6 +45,7 @@ class Vacation extends Api { 'end' => date('Y-m-d H:i:s', strtotime($this->end_date)), 'type' => $this->type, 'name' => $this->name, + 'approved' => $this->approved, ]; } diff --git a/be/database/migrations/2023_12_12_135118_add_approved_to_blockers_table.php b/be/database/migrations/2023_12_12_135118_add_approved_to_blockers_table.php new file mode 100644 index 0000000..154a4ae --- /dev/null +++ b/be/database/migrations/2023_12_12_135118_add_approved_to_blockers_table.php @@ -0,0 +1,32 @@ +boolean('approved')->after('end')->nullable()->default(NULL); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('blockers', function (Blueprint $table) { + // + }); + } +};