diff --git a/app/Jobs/Recipe/ImportRecipeJob.php b/app/Jobs/Recipe/ImportRecipeJob.php index 86a278d..272fcdf 100644 --- a/app/Jobs/Recipe/ImportRecipeJob.php +++ b/app/Jobs/Recipe/ImportRecipeJob.php @@ -94,7 +94,13 @@ public function handle(): void App::setLocale($this->getLanguage()); - $this->recipeModel = $this->importRecipe(); + $recipe = $this->importRecipe(); + + if (! $recipe instanceof Recipe) { + return; + } + + $this->recipeModel = $recipe; $this->syncIngredients(); $this->syncAllergens(); @@ -110,29 +116,43 @@ public function handle(): void * * @throws DateMalformedIntervalStringException */ - protected function importRecipe(): Recipe + protected function importRecipe(): ?Recipe { $suffix = $this->isPrimaryLocale() ? 'primary' : 'secondary'; + $data = [ + 'name' => $this->recipe['name'], + 'headline' => $this->recipe['headline'], + 'description' => $this->recipe['descriptionMarkdown'], + 'card_link' => $this->recipe['cardLink'], + 'difficulty' => $this->recipe['difficulty'], + 'prep_time' => $this->parseIsoDuration($this->recipe['prepTime']), + 'total_time' => $this->parseIsoDuration($this->recipe['totalTime']), + 'image_path' => $this->recipe['imagePath'], + 'steps_' . $suffix => $this->transformSteps($this->recipe['steps']), + 'nutrition_' . $suffix => $this->recipe['nutrition'], + 'yields_' . $suffix => $this->recipe['yields'], + 'variant' => $this->recipe['canonical'] !== '' && $this->recipe['canonical'] !== $this->recipe['id'], + 'is_published' => $this->recipe['isPublished'], + 'hellofresh_created_at' => $this->recipe['createdAt'], + 'hellofresh_updated_at' => $this->recipe['updatedAt'], + ]; + + $recipe = $this->country->recipes()->where('hellofresh_id', $this->recipe['id'])->first(); + + if ($recipe) { + $recipe->update($data); + + return $recipe; + } + + if (! $this->recipe['isPublished']) { + return null; + } + /** @var Recipe $recipe */ - $recipe = $this->country->recipes()->updateOrCreate( - ['hellofresh_id' => $this->recipe['id']], - [ - 'name' => $this->recipe['name'], - 'headline' => $this->recipe['headline'], - 'description' => $this->recipe['descriptionMarkdown'], - 'card_link' => $this->recipe['cardLink'], - 'difficulty' => $this->recipe['difficulty'], - 'prep_time' => $this->parseIsoDuration($this->recipe['prepTime']), - 'total_time' => $this->parseIsoDuration($this->recipe['totalTime']), - 'image_path' => $this->recipe['imagePath'], - 'steps_' . $suffix => $this->transformSteps($this->recipe['steps']), - 'nutrition_' . $suffix => $this->recipe['nutrition'], - 'yields_' . $suffix => $this->recipe['yields'], - 'variant' => $this->recipe['canonical'] !== '' && $this->recipe['canonical'] !== $this->recipe['id'], - 'hellofresh_created_at' => $this->recipe['createdAt'], - 'hellofresh_updated_at' => $this->recipe['updatedAt'], - ], + $recipe = $this->country->recipes()->create( + array_merge(['hellofresh_id' => $this->recipe['id']], $data) ); return $recipe; @@ -399,10 +419,6 @@ protected function shouldImport(): bool return false; } - if (! $this->recipe['isPublished']) { - return false; - } - if (empty($this->recipe['imagePath'])) { return false; } diff --git a/app/Models/Recipe.php b/app/Models/Recipe.php index 0cb5312..e4e7907 100644 --- a/app/Models/Recipe.php +++ b/app/Models/Recipe.php @@ -66,6 +66,7 @@ class Recipe extends Model 'yields_primary', 'yields_secondary', 'variant', + 'is_published', 'hellofresh_created_at', 'hellofresh_updated_at', ]; @@ -111,6 +112,7 @@ protected function casts(): array 'yields_secondary' => 'array', 'has_pdf' => 'bool', 'variant' => 'bool', + 'is_published' => 'bool', 'hellofresh_created_at' => 'datetime', 'hellofresh_updated_at' => 'datetime', ]; diff --git a/database/migrations/2026_01_26_000010_add_is_published_to_recipes_table.php b/database/migrations/2026_01_26_000010_add_is_published_to_recipes_table.php new file mode 100644 index 0000000..7f58ea1 --- /dev/null +++ b/database/migrations/2026_01_26_000010_add_is_published_to_recipes_table.php @@ -0,0 +1,30 @@ +boolean('is_published')->default(true)->after('variant'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('recipes', function (Blueprint $table): void { + $table->dropColumn('is_published'); + }); + } +};