mirror of
https://bitbucket.org/studiotem/jandl-taskmanager-app.git
synced 2026-10-21 14:29:34 +00:00
67 lines
2.3 KiB
PHP
67 lines
2.3 KiB
PHP
<?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);
|
|
|
|
$this->checkIfVacationsGotAprroved($intranetVacations);
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
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]);
|
|
}
|
|
}
|