no message

This commit is contained in:
StudioTem committed 2024-05-09 14:47:38 +02:00
1 parent 7b5528ea51
commit 1fd4524890
3 files changed
+77 -15

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;
}
}
+2 -1
View File
@@ -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();
}
}
/**
+38 -14
View File
@@ -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
@@ -45,14 +44,6 @@ 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) {
@@ -70,6 +61,13 @@ class KaratService
}
}
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();
@@ -133,6 +131,7 @@ class KaratService
$arrayToInsert['created_at'] = Carbon::now()->toDateTimeString();
$arrayToInsert['updated_at'] = Carbon::now()->toDateTimeString();
$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 ) {
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();