From 66b58ced5496abdc933c0564f2d586bd749c663e Mon Sep 17 00:00:00 2001 From: guillaume91 Date: Tue, 25 Jun 2024 13:38:47 +0200 Subject: [PATCH] wip (notification) --- laravel_app/app/Imports/ExcelFileImport.php | 26 ++++++++++ laravel_app/app/Imports/HarvestDataImport.php | 16 ++++++ .../Components/DownloadNotification.php | 15 ++++++ .../app/Livewire/Projects/ProjectManager.php | 6 +++ .../app/Livewire/Projects/Tabs/Settings.php | 5 ++ laravel_app/app/Models/Project.php | 20 +++++++- laravel_app/app/Models/ProjectDownload.php | 6 +++ laravel_app/app/Rules/HarvestFile.php | 1 - ...dd_min_harvest_date_column_to_projects.php | 28 ++++++++++ .../resources/views/layouts/app.blade.php | 1 + .../download-notification.blade.php | 45 ++++++++++++++++ .../Unit/Imports/HarvestDataImportTest.php | 51 +++++++++++++++++++ 12 files changed, 218 insertions(+), 2 deletions(-) create mode 100644 laravel_app/app/Imports/ExcelFileImport.php create mode 100644 laravel_app/app/Imports/HarvestDataImport.php create mode 100644 laravel_app/app/Livewire/Components/DownloadNotification.php create mode 100644 laravel_app/database/migrations/2024_06_25_092126_add_min_harvest_date_column_to_projects.php create mode 100644 laravel_app/resources/views/livewire/components/download-notification.blade.php create mode 100644 laravel_app/tests/Unit/Imports/HarvestDataImportTest.php diff --git a/laravel_app/app/Imports/ExcelFileImport.php b/laravel_app/app/Imports/ExcelFileImport.php new file mode 100644 index 0000000..eefeb70 --- /dev/null +++ b/laravel_app/app/Imports/ExcelFileImport.php @@ -0,0 +1,26 @@ +min = now(); + } + + /** + * @param Collection $collection + */ + public function collection(Collection $collection) + { + return collect([1,2,3]); + foreach ($collection as $row){ + + } + } +} diff --git a/laravel_app/app/Imports/HarvestDataImport.php b/laravel_app/app/Imports/HarvestDataImport.php new file mode 100644 index 0000000..c482810 --- /dev/null +++ b/laravel_app/app/Imports/HarvestDataImport.php @@ -0,0 +1,16 @@ +formData['harvest_file'] = $this->harvestDataFiles[0] ?? null; $this->formData['download_path'] = $this->formData['download_path'] ?? $this->formData['name']; } + + } diff --git a/laravel_app/app/Livewire/Projects/Tabs/Settings.php b/laravel_app/app/Livewire/Projects/Tabs/Settings.php index 1b70e9a..ba0a941 100644 --- a/laravel_app/app/Livewire/Projects/Tabs/Settings.php +++ b/laravel_app/app/Livewire/Projects/Tabs/Settings.php @@ -8,11 +8,13 @@ use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Validator; use Illuminate\Validation\Rule; +use Laravel\Jetstream\InteractsWithBanner; use Livewire\Component; use Livewire\WithFileUploads; class Settings extends Component { + use InteractsWithBanner; use WithFileUploads; public $formData = []; @@ -63,6 +65,7 @@ public function saveProject() //dd(route('project.show',[$this->formData['name'],$this->formData['id'],'settings'])); return redirect()->route('project.show',[$this->formData['name'],$this->formData['id'],'settings']); //$this->dispatch('saved'); + } public function saveMailSettings() @@ -82,6 +85,7 @@ public function saveSettings() $this->shouldReload(); Project::saveWithFormData($this->formData); if($this->isDirty) redirect()->route('project.show',[$this->formData['name'],$this->formData['id'],'settings']); + $this->dispatch('download_notify',title:'Download',message:'hallo'); $this->dispatch('saved'); } @@ -195,6 +199,7 @@ private function validateForm() }], 'harvest_file' => ['sometimes', new HarvestFile], ])->validate(); + } private function validateEmailSettingsForm() diff --git a/laravel_app/app/Models/Project.php b/laravel_app/app/Models/Project.php index 8e3eb53..3af9cdd 100644 --- a/laravel_app/app/Models/Project.php +++ b/laravel_app/app/Models/Project.php @@ -13,7 +13,8 @@ use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; -use function Livewire\off; +use Maatwebsite\Excel\Facades\Excel; +use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDate; class Project extends Model { @@ -331,4 +332,21 @@ public function getMosaicsFor(Carbon $endDate, int $offset= 7): array ->filter() ->toArray(); } + + public function missingDownloadsInHarvestFile() + { + if(!$this->harvest_json_path){ + return; + } + $dates = $this->downloads->map(fn($d)=>$d->date)->sort(); + } + private function getMinimumDateFromHarvestExcelFile() + { + $value = Storage::disk('local')->path($this->harvest_json_path); + $data = Excel::toCollection(new \App\Imports\ExcelFileImport(),$value); + $season_start_index = $data->first()->first()->search('season_start'); + return collect($data->first()->slice(1))->reduce(function($carry,$value,$key) use ($season_start_index){ + return min($carry,Carbon::instance(SharedDate::excelToDateTimeObject($value[$season_start_index]))); + },now()); + } } diff --git a/laravel_app/app/Models/ProjectDownload.php b/laravel_app/app/Models/ProjectDownload.php index ea3af3d..fe773bc 100644 --- a/laravel_app/app/Models/ProjectDownload.php +++ b/laravel_app/app/Models/ProjectDownload.php @@ -4,6 +4,7 @@ use App\Events\ProjectDownloadStatus; use App\Traits\HasStatus; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -24,6 +25,11 @@ public function project(): \Illuminate\Database\Eloquent\Relations\BelongsTo return $this->belongsTo(Project::class); } + public function getDateAttribute():Carbon + { + return Carbon::parse(explode('.',$this->name)[0]); + } + protected static function booted(): void { parent::booted(); diff --git a/laravel_app/app/Rules/HarvestFile.php b/laravel_app/app/Rules/HarvestFile.php index 14e26e7..1f98a77 100644 --- a/laravel_app/app/Rules/HarvestFile.php +++ b/laravel_app/app/Rules/HarvestFile.php @@ -21,7 +21,6 @@ public function validate(string $attribute, mixed $value, Closure $fail): void if(!in_array($value['extension'], ['xlsx', 'xls', 'csv', 'ods'])) { $fail($value['extension'].' is not a valid file (.xlsx, .xls, .csv, .ods).'); } - try{ $excelHeaderArray = (new HeadingRowImport)->toArray($value['path']); $header = $excelHeaderArray[0][0]; diff --git a/laravel_app/database/migrations/2024_06_25_092126_add_min_harvest_date_column_to_projects.php b/laravel_app/database/migrations/2024_06_25_092126_add_min_harvest_date_column_to_projects.php new file mode 100644 index 0000000..964692f --- /dev/null +++ b/laravel_app/database/migrations/2024_06_25_092126_add_min_harvest_date_column_to_projects.php @@ -0,0 +1,28 @@ +date('min_harvest_date')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('projects', function (Blueprint $table) { + $table->dropColumn('min_harvest_date'); + }); + } +}; diff --git a/laravel_app/resources/views/layouts/app.blade.php b/laravel_app/resources/views/layouts/app.blade.php index 2f65886..9cdbf0f 100644 --- a/laravel_app/resources/views/layouts/app.blade.php +++ b/laravel_app/resources/views/layouts/app.blade.php @@ -37,6 +37,7 @@ + @stack('modals') @stack('map') diff --git a/laravel_app/resources/views/livewire/components/download-notification.blade.php b/laravel_app/resources/views/livewire/components/download-notification.blade.php new file mode 100644 index 0000000..e77526c --- /dev/null +++ b/laravel_app/resources/views/livewire/components/download-notification.blade.php @@ -0,0 +1,45 @@ + +
+ +
+ +
+
+
+
+ +
+
+

+

+
+ + +
+
+ +
+
+
+
+
diff --git a/laravel_app/tests/Unit/Imports/HarvestDataImportTest.php b/laravel_app/tests/Unit/Imports/HarvestDataImportTest.php new file mode 100644 index 0000000..b2d6ffa --- /dev/null +++ b/laravel_app/tests/Unit/Imports/HarvestDataImportTest.php @@ -0,0 +1,51 @@ +path('Chemba/Data/harvest.xlsx'); + $data = Excel::toCollection(new \App\Imports\ExcelFileImport(),$value); + $season_start_index = $data->first()->first()->search('season_start'); + $min = now(); + collect($data->first()->slice(1))->each(function($value) use (&$min,$season_start_index){ + $min = min($min,Carbon::instance(SharedDate::excelToDateTimeObject($value[$season_start_index]))); + }); + } + + public function test_it_gets_the_min_value() + { + $value = Storage::disk('local')->path('Chemba/Data/harvest.xlsx'); + $data = Excel::toCollection(new \App\Imports\ExcelFileImport(),$value); + $season_start_index = $data->first()->first()->search('season_start'); + $min = now(); + collect($data->first()->slice(1))->each(function($value) use (&$min,$season_start_index){ + $min = min($min,Carbon::instance(SharedDate::excelToDateTimeObject($value[$season_start_index]))); + }); + + assertEquals(Carbon::parse('2023-01-12'),$min); + } + + public function test_it_gets_the_min_value_using_reduce() + { + $value = Storage::disk('local')->path('chemba/Data/harvest.xlsx'); + $data = Excel::toCollection(new \App\Imports\ExcelFileImport(),$value); + $season_start_index = $data->first()->first()->search('season_start'); + $min = collect($data->first()->slice(1))->reduce(function($carry,$value,$key) use ($season_start_index){ + return min($carry,Carbon::instance(SharedDate::excelToDateTimeObject($value[$season_start_index]))); + },now()); + assertEquals(Carbon::parse('2023-01-12'),$min); + } + + +}