Skip to content

Commit

Permalink
Merge pull request #353 from karlomikus/develop
Browse files Browse the repository at this point in the history
Bugfixes
  • Loading branch information
karlomikus authored Nov 7, 2024
2 parents 5a0e940 + 5b81084 commit f76d40f
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v4.0.4
## Fixes
- Check for shelf ingredients before adding them to the shelf
- Fix for missing directories in docker entrypoint

# v4.0.3
## Changes
- Added prod target to docker image
Expand Down
16 changes: 15 additions & 1 deletion app/Console/Commands/BarImportRecipes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Kami\Cocktail\Console\Commands;

use Throwable;
use ZipArchive;
use Illuminate\Support\Str;
use Kami\Cocktail\Models\Bar;
Expand Down Expand Up @@ -104,7 +105,20 @@ public function handle(): int

$this->line('Starting recipes import...');
Cache::flush();
$this->importer->process($tempUnzipDisk, $bar, $user, [BarOptionsEnum::Cocktails, BarOptionsEnum::Ingredients]);

try {
$this->importer->process($tempUnzipDisk, $bar, $user, [BarOptionsEnum::Cocktails, BarOptionsEnum::Ingredients]);
} catch (Throwable $e) {
// TODO: Reset "stuck" bar status
// $bar->status = null;
// $bar->save();
$tempUnzipDisk->deleteDirectory('/');

$this->error('Error occured during import:');
$this->error($e->getMessage());

return Command::FAILURE;
}

$tempUnzipDisk->deleteDirectory('/');

Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

class ImportController extends Controller
{
#[OAT\Post(path: '/import/cocktail', tags: ['Import'], summary: 'Import from recipe schema', parameters: [
#[OAT\Post(path: '/import/cocktail', tags: ['Import'], summary: 'Import recipe', description: 'Import a recipe from a JSON structure that follows Bar Assistant recipe JSON schema. Supported schemas include [Draft 2](https://barassistant.app/cocktail-02.schema.json) and [Draft 1](https://barassistant.app/cocktail-01.schema.json).', parameters: [
new BAO\Parameters\BarIdParameter(),
new BAO\Parameters\BarIdHeaderParameter(),
], requestBody: new OAT\RequestBody(
required: true,
content: [
new OAT\JsonContent(type: 'object', properties: [
new OAT\Property(property: 'source', type: 'string'),
new OAT\Property(property: 'source', type: 'string', description: 'Valid JSON structure to import.'),
new OAT\Property(property: 'duplicate_actions', ref: DuplicateActionsEnum::class, example: 'none', description: 'How to handle duplicates. Cocktails are matched by lowercase name.'),
]),
]
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/ShelfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public function batchStore(Request $request, int $id): Response
}

$barMembership = $user->getBarMembership(bar()->id);
$existingBarIngredients = $barMembership->userIngredients->pluck('ingredient_id');

$ingredients = DB::table('ingredients')
->select('id')
Expand All @@ -146,6 +147,9 @@ public function batchStore(Request $request, int $id): Response

$models = [];
foreach ($ingredients as $dbIngredientId) {
if ($existingBarIngredients->contains($dbIngredientId)) {
continue;
}
$userIngredient = new UserIngredient();
$userIngredient->ingredient_id = $dbIngredientId;
$models[] = $userIngredient;
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Cocktail.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Carbon\Carbon;
use Kami\Cocktail\Utils;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable;
use Spatie\Sluggable\HasSlug;
use Kami\RecipeUtils\Converter;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function getSlugOptions(): SlugOptions

public function getExternalId(): string
{
return str_replace('-' . $this->bar_id, '', $this->slug);
return Str::slug($this->name);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Ingredient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Kami\Cocktail\Models;

use Illuminate\Support\Str;
use Laravel\Scout\Searchable;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function getSlugOptions(): SlugOptions

public function getExternalId(): string
{
return str_replace('-' . $this->bar_id, '', $this->slug);
return Str::slug($this->name);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion docs/openapi-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2307,7 +2307,8 @@ paths:
post:
tags:
- Import
summary: 'Import from recipe schema'
summary: 'Import recipe'
description: 'Import a recipe from a JSON structure that follows Bar Assistant recipe JSON schema. Supported schemas include [Draft 2](https://barassistant.app/cocktail-02.schema.json) and [Draft 1](https://barassistant.app/cocktail-01.schema.json).'
operationId: f46bb44c14109f5d529b854e81d57150
parameters:
-
Expand All @@ -2331,6 +2332,7 @@ paths:
schema:
properties:
source:
description: 'Valid JSON structure to import.'
type: string
duplicate_actions:
$ref: '#/components/schemas/DuplicateActionsEnum'
Expand Down
4 changes: 3 additions & 1 deletion resources/docker/dist/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
set -e

# Create default bar assistant directories
mkdir -p "$APP_BASE_DIR"/storage/bar-assistant/uploads/{cocktails,ingredients,temp}
mkdir -p "$APP_BASE_DIR"/storage/bar-assistant/uploads/cocktails
mkdir -p "$APP_BASE_DIR"/storage/bar-assistant/uploads/ingredients
mkdir -p "$APP_BASE_DIR"/storage/bar-assistant/uploads/temp
mkdir -p "$APP_BASE_DIR"/storage/bar-assistant/backups

# SQLite database location
Expand Down
16 changes: 8 additions & 8 deletions tests/Feature/External/ToDataPackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public function test_export_contains_files(): void
CocktailMethod::factory()->for($membership->bar)->count(3)->create();
PriceCategory::factory()->for($membership->bar)->count(3)->create();
Utensil::factory()->for($membership->bar)->count(3)->create();
$cocktail = Cocktail::factory()->for($membership->bar)->create(['slug' => 'test-cocktail-1']);
$ingredient = Ingredient::factory()->for($membership->bar)->create(['slug' => 'test-ingredient-1']);
$cocktail = Cocktail::factory()->for($membership->bar)->create(['name' => 'Gin and Tonic']);
$ingredient = Ingredient::factory()->for($membership->bar)->create(['name' => 'Jack Daniels']);

$imageCocktailFile = UploadedFile::fake()->createWithContent('image1.jpg', $this->getFakeImageContent('jpg'));
$ingredientCocktailFile = UploadedFile::fake()->createWithContent('image2.jpg', $this->getFakeImageContent('png'));
Expand Down Expand Up @@ -82,18 +82,18 @@ public function test_export_contains_files(): void
$this->assertFileExists($unzippedFilesDisk->path($file));
}

$this->assertFileExists($unzippedFilesDisk->path('cocktails/test-cocktail/data.json'));
$this->assertFileExists($unzippedFilesDisk->path('cocktails/test-cocktail/c-1-img.jpg'));
$this->assertFileExists($unzippedFilesDisk->path('ingredients/test-ingredient/data.json'));
$this->assertFileExists($unzippedFilesDisk->path('ingredients/test-ingredient/i-1-img.png'));
$this->assertFileExists($unzippedFilesDisk->path('cocktails/gin-and-tonic/data.json'));
$this->assertFileExists($unzippedFilesDisk->path('cocktails/gin-and-tonic/c-1-img.jpg'));
$this->assertFileExists($unzippedFilesDisk->path('ingredients/jack-daniels/data.json'));
$this->assertFileExists($unzippedFilesDisk->path('ingredients/jack-daniels/i-1-img.png'));

$cocktailExport = [];
if ($cocktailFixture = file_get_contents($unzippedFilesDisk->path('cocktails/test-cocktail/data.json'))) {
if ($cocktailFixture = file_get_contents($unzippedFilesDisk->path('cocktails/gin-and-tonic/data.json'))) {
$cocktailExport = json_decode($cocktailFixture, true);
}

$ingredientExport = [];
if ($ingredientFixture = file_get_contents($unzippedFilesDisk->path('ingredients/test-ingredient/data.json'))) {
if ($ingredientFixture = file_get_contents($unzippedFilesDisk->path('ingredients/jack-daniels/data.json'))) {
$ingredientExport = json_decode($ingredientFixture, true);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/External/ToRecipeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public function test_default_export_contains_schema_files(): void
CocktailMethod::factory()->for($membership->bar)->count(3)->create();
PriceCategory::factory()->for($membership->bar)->count(3)->create();
Utensil::factory()->for($membership->bar)->count(3)->create();
$cocktail = Cocktail::factory()->for($membership->bar)->create(['slug' => 'test-cocktail-1']);
$ingredient = Ingredient::factory()->for($membership->bar)->create(['slug' => 'test-ingredient-1']);
$cocktail = Cocktail::factory()->for($membership->bar)->create(['name' => 'Gin and Tonic']);
$ingredient = Ingredient::factory()->for($membership->bar)->create(['name' => 'Jack Daniels']);

$imageCocktailFile = UploadedFile::fake()->createWithContent('image1.jpg', $this->getFakeImageContent('jpg'));
$ingredientCocktailFile = UploadedFile::fake()->createWithContent('image2.jpg', $this->getFakeImageContent('png'));
Expand All @@ -70,8 +70,8 @@ public function test_default_export_contains_schema_files(): void
$zip->close();

$this->assertFileExists($unzippedFilesDisk->path('_meta.json'));
$this->assertFileExists($unzippedFilesDisk->path('cocktails/test-cocktail/recipe.json'));
$this->assertFileExists($unzippedFilesDisk->path('cocktails/test-cocktail/c-1-img.jpg'));
$this->assertFileExists($unzippedFilesDisk->path('cocktails/gin-and-tonic/recipe.json'));
$this->assertFileExists($unzippedFilesDisk->path('cocktails/gin-and-tonic/c-1-img.jpg'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/external/recipe.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"recipe": {
"_id": "test-cocktail",
"_id": "gin-and-tonic",
"name": "Gin and Tonic",
"instructions": "Cocktail instructions go here",
"created_at": "2020-01-01T12:00:00+00:00",
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/external/recipe.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<draft2>
<recipe>
<_id>test-cocktail</_id>
<_id>gin-and-tonic</_id>
<name>Gin and Tonic</name>
<instructions>Cocktail instructions go here</instructions>
<created_at>2020-01-01T12:00:00+00:00</created_at>
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/external/recipe.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
recipe:
_id: test-cocktail
_id: gin-and-tonic
name: 'Gin and Tonic'
instructions: 'Cocktail instructions go here'
created_at: '2020-01-01T12:00:00+00:00'
Expand Down

0 comments on commit f76d40f

Please sign in to comment.