Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 40 additions & 24 deletions app/Jobs/Recipe/ImportRecipeJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -399,10 +419,6 @@ protected function shouldImport(): bool
return false;
}

if (! $this->recipe['isPublished']) {
return false;
}

if (empty($this->recipe['imagePath'])) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Recipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Recipe extends Model
'yields_primary',
'yields_secondary',
'variant',
'is_published',
'hellofresh_created_at',
'hellofresh_updated_at',
];
Expand Down Expand Up @@ -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',
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/** @noinspection DuplicatedCode */

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('recipes', static function (Blueprint $table): void {
$table->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');
});
}
};
Loading