mirror of
https://bitbucket.org/studiotem/jandl-taskmanager-app.git
synced 2026-10-21 06:20:21 +00:00
no message
This commit is contained in:
1 parent
7b5528ea51
commit
1fd4524890
3 files changed
+96
-34
No files matched your search
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Services\Karat\KaratService;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class SyncProjectsNameFromKarat extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'karat:sync-projects-name';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Syncs projects name from Karat to our DB';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$karatService = new KaratService();
|
||||
|
||||
$karatService->syncProjectsName();
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ class Kernel extends ConsoleKernel
|
||||
* Define the application's command schedule.
|
||||
*
|
||||
* @param \Illuminate\Console\Scheduling\Schedule $schedule
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function schedule(Schedule $schedule)
|
||||
@@ -28,9 +29,9 @@ class Kernel extends ConsoleKernel
|
||||
$schedule->command('karat:sync-users')->daily();
|
||||
$schedule->command('karat:sync-clients')->hourly();
|
||||
$schedule->command('karat:sync-projects')->everyFiveMinutes();
|
||||
$schedule->command('karat:sync-projects-name')->hourly();
|
||||
$schedule->command('karat:sync-timesheets')->everyFiveMinutes();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,7 +41,7 @@ class Kernel extends ConsoleKernel
|
||||
*/
|
||||
protected function commands()
|
||||
{
|
||||
$this->load(__DIR__.'/Commands');
|
||||
$this->load(__DIR__ . '/Commands');
|
||||
|
||||
require base_path('routes/console.php');
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ namespace App\Services\Karat;
|
||||
use App\Models\Timesheet;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class KaratService
|
||||
@@ -20,7 +19,7 @@ class KaratService
|
||||
public function syncUsers()
|
||||
{
|
||||
$karatUsers = \App\Services\Karat\User::all();
|
||||
$dbUsers = User::withTrashed()->get();
|
||||
$dbUsers = User::withTrashed()->get();
|
||||
|
||||
$this->insertUsersFromKarat($karatUsers, $dbUsers);
|
||||
$this->updateUserNamesFromKarat($karatUsers, $dbUsers);
|
||||
@@ -29,12 +28,12 @@ class KaratService
|
||||
|
||||
private function insertUsersFromKarat(Collection $karatUsers, Collection $dbUsers)
|
||||
{
|
||||
$emailsToSkip = $dbUsers->pluck('email' )->intersect( $karatUsers->pluck('email'));
|
||||
$emailsToSkip = $dbUsers->pluck('email')->intersect($karatUsers->pluck('email'));
|
||||
|
||||
$karatUsersToInsert = $karatUsers->reject(function ($item) use ($emailsToSkip) {
|
||||
return $emailsToSkip->contains($item->email);
|
||||
})->map(function ($item) {
|
||||
$arrayToInsert = $item->toArray();
|
||||
$arrayToInsert = $item->toArray();
|
||||
|
||||
$arrayToInsert['created_at'] = Carbon::now()->toDateTimeString();
|
||||
$arrayToInsert['updated_at'] = Carbon::now()->toDateTimeString();
|
||||
@@ -45,49 +44,48 @@ class KaratService
|
||||
User::insert($karatUsersToInsert->toArray());
|
||||
}
|
||||
|
||||
private function removeUsersThatAreNotInKaratAnymore( Collection $karatUsers, Collection $dbUsers )
|
||||
{
|
||||
$emailsInDbThatAreNotInKaratAnymore = $dbUsers->pluck('email' )->diff( $karatUsers->pluck('email'));
|
||||
|
||||
User::whereIn('email', $emailsInDbThatAreNotInKaratAnymore)->delete();
|
||||
|
||||
}
|
||||
|
||||
private function updateUserNamesFromKarat(Collection $karatUsers, Collection $dbUsers)
|
||||
{
|
||||
foreach ( $karatUsers as $karatUser ) {
|
||||
foreach ($karatUsers as $karatUser) {
|
||||
$dbUser = User::where('email', $karatUser->email)->first();
|
||||
|
||||
if (!$dbUser) {
|
||||
if ( ! $dbUser) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($dbUser->lastname !== $karatUser->lastname || $dbUser->firstname !== $karatUser->firstname) {
|
||||
$dbUser->firstname = $karatUser->firstname;
|
||||
$dbUser->lastname = $karatUser->lastname;
|
||||
$dbUser->lastname = $karatUser->lastname;
|
||||
$dbUser->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function removeUsersThatAreNotInKaratAnymore(Collection $karatUsers, Collection $dbUsers)
|
||||
{
|
||||
$emailsInDbThatAreNotInKaratAnymore = $dbUsers->pluck('email')->diff($karatUsers->pluck('email'));
|
||||
|
||||
User::whereIn('email', $emailsInDbThatAreNotInKaratAnymore)->delete();
|
||||
}
|
||||
|
||||
public function syncClients()
|
||||
{
|
||||
$karatClients = Client::all();
|
||||
$dbClients = \App\Models\Client::all();
|
||||
$dbClients = \App\Models\Client::all();
|
||||
|
||||
$this->insertClientsFromKarat($karatClients, $dbClients);
|
||||
|
||||
$this->removeClientsThatAreNotInKaratAnymore($karatClients, $dbClients);
|
||||
}
|
||||
|
||||
private function insertClientsFromKarat( Collection $karatClients, Collection $dbClients )
|
||||
private function insertClientsFromKarat(Collection $karatClients, Collection $dbClients)
|
||||
{
|
||||
$clientsToSkip = $dbClients->pluck('code' )->intersect( $karatClients->pluck('code'));
|
||||
$clientsToSkip = $dbClients->pluck('code')->intersect($karatClients->pluck('code'));
|
||||
|
||||
$karatClientsToInsert = $karatClients->reject(function ($item) use ($clientsToSkip) {
|
||||
return $clientsToSkip->contains($item->code);
|
||||
})->map(function ($item) {
|
||||
$arrayToInsert = $item->toArray();
|
||||
$arrayToInsert = $item->toArray();
|
||||
|
||||
$arrayToInsert['created_at'] = Carbon::now()->toDateTimeString();
|
||||
$arrayToInsert['updated_at'] = Carbon::now()->toDateTimeString();
|
||||
@@ -98,9 +96,9 @@ class KaratService
|
||||
\App\Models\Client::insert($karatClientsToInsert->toArray());
|
||||
}
|
||||
|
||||
private function removeClientsThatAreNotInKaratAnymore( Collection $karatClients, Collection $dbClients )
|
||||
private function removeClientsThatAreNotInKaratAnymore(Collection $karatClients, Collection $dbClients)
|
||||
{
|
||||
$codesInDbThatAreNotInKaratAnymore = $dbClients->pluck('code' )->diff( $karatClients->pluck('code'));
|
||||
$codesInDbThatAreNotInKaratAnymore = $dbClients->pluck('code')->diff($karatClients->pluck('code'));
|
||||
|
||||
\App\Models\Client::whereIn('code', $codesInDbThatAreNotInKaratAnymore)->delete();
|
||||
}
|
||||
@@ -109,7 +107,7 @@ class KaratService
|
||||
{
|
||||
$karatProjects = Project::all();
|
||||
|
||||
$dbProjects = \App\Models\Project::all();
|
||||
$dbProjects = \App\Models\Project::all();
|
||||
|
||||
// ked sa to davalo naraz, tak to padalo na Prepared statement contains too many placeholders
|
||||
$karatProjects->chunk(300)->each(function ($chunk) use ($dbProjects) {
|
||||
@@ -119,20 +117,21 @@ class KaratService
|
||||
$this->removeProjectsThatAreNotInKaratAnymore($karatProjects, $dbProjects);
|
||||
}
|
||||
|
||||
private function insertProjectsFromKarat( Collection $karatProjects, Collection $dbProjects )
|
||||
private function insertProjectsFromKarat(Collection $karatProjects, Collection $dbProjects)
|
||||
{
|
||||
$projectsToSkip = $dbProjects->pluck('code' )->intersect( $karatProjects->pluck('code'));
|
||||
$projectsToSkip = $dbProjects->pluck('code')->intersect($karatProjects->pluck('code'));
|
||||
|
||||
$karatProjectsToInsert = $karatProjects->reject(function ($item) use ($projectsToSkip) {
|
||||
return $projectsToSkip->contains($item->code);
|
||||
})->map(function ($item) {
|
||||
$arrayToInsert = $item->toArray();
|
||||
$arrayToInsert = $item->toArray();
|
||||
|
||||
$dbClient = \App\Models\Client::where('code', $item->client_id)->first();
|
||||
|
||||
$arrayToInsert['created_at'] = Carbon::now()->toDateTimeString();
|
||||
$arrayToInsert['updated_at'] = Carbon::now()->toDateTimeString();
|
||||
$arrayToInsert['client_id'] = ($dbClient) ? $dbClient->id : null;
|
||||
$arrayToInsert['client_id'] = ($dbClient) ? $dbClient->id : null;
|
||||
|
||||
// $arrayToInsert['client_id'] = 12;
|
||||
|
||||
return $arrayToInsert;
|
||||
@@ -141,24 +140,49 @@ class KaratService
|
||||
\App\Models\Project::insert($karatProjectsToInsert->toArray());
|
||||
}
|
||||
|
||||
private function removeProjectsThatAreNotInKaratAnymore( Collection $karatProjects, Collection $dbProjects ) {
|
||||
$codesInDbThatAreNotInKaratAnymore = $dbProjects->pluck('code' )->diff( $karatProjects->pluck('code'));
|
||||
private function removeProjectsThatAreNotInKaratAnymore(Collection $karatProjects, Collection $dbProjects)
|
||||
{
|
||||
$codesInDbThatAreNotInKaratAnymore = $dbProjects->pluck('code')->diff($karatProjects->pluck('code'));
|
||||
|
||||
\App\Models\Project::whereIn('code', $codesInDbThatAreNotInKaratAnymore)->delete();
|
||||
}
|
||||
|
||||
public function syncTimesheets() {
|
||||
public function syncProjectsName()
|
||||
{
|
||||
$karatProjects = Project::all();
|
||||
|
||||
$dbProjects = \App\Models\Project::all();
|
||||
|
||||
// make the array of dbprojects where key is the project code
|
||||
$dbProjectsArray = [];
|
||||
foreach ($dbProjects as $dbProject) {
|
||||
$dbProjectsArray[$dbProject->code] = $dbProject;
|
||||
}
|
||||
|
||||
// if karat's project name is different than the name in our DB, we update the name in our DB
|
||||
foreach ($karatProjects as $karatProject) {
|
||||
if (isset($dbProjectsArray[$karatProject->code]) && $dbProjectsArray[$karatProject->code]->name !== $karatProject->name) {
|
||||
$dbProjectsArray[$karatProject->code]->name = $karatProject->name;
|
||||
$dbProjectsArray[$karatProject->code]->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function syncTimesheets()
|
||||
{
|
||||
$notSyncedTimesheets = Timesheet::whereNull('synced_to_karat')->get();
|
||||
|
||||
$notSyncedTimesheets->each(function ($timesheet) {
|
||||
if ($timesheet->subtask && $timesheet->subtask->mastertask->project)
|
||||
if ($timesheet->subtask && $timesheet->subtask->mastertask->project) {
|
||||
$this->syncTimesheet($timesheet);
|
||||
else
|
||||
} else {
|
||||
echo "Timesheet with id: " . $timesheet->id . " has no project, subtask or mastertask" . PHP_EOL;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function syncTimesheet(Timesheet $timesheet) {
|
||||
private function syncTimesheet(Timesheet $timesheet)
|
||||
{
|
||||
$this->api->setTimesheet($timesheet);
|
||||
|
||||
$timesheet->synced_to_karat = Carbon::now();
|
||||
|
||||
Reference in new issue
Block a user