Skip to content

Commit 8958694

Browse files
authored
Merge pull request #16 from techenby/amn/import-items
Import items from Amazon
2 parents 326746d + 334f79a commit 8958694

31 files changed

+700
-278
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Actions\Inventory;
6+
7+
use App\Enums\ItemType;
8+
use App\Models\Item;
9+
use App\Models\Team;
10+
use Illuminate\Http\UploadedFile;
11+
use Illuminate\Support\Facades\Date;
12+
use Spatie\SimpleExcel\SimpleExcelReader;
13+
14+
final readonly class ImportItemsFromAmazonAction
15+
{
16+
public function handle(UploadedFile $file, Team $team, ?int $parentId = null): array
17+
{
18+
$parent = Item::find($parentId);
19+
abort_if($parent && $parent->team_id !== $team->id, 403);
20+
21+
$stats = [
22+
'imported' => 0,
23+
'skipped' => 0,
24+
];
25+
26+
$toImport = SimpleExcelReader::create($file->getRealPath())->getRows()
27+
->reject(function (array $row) use (&$stats) {
28+
if ($row['Gift Message'] !== 'Not Available' || $row['Gift Recipient Contact'] !== 'Not Available' || $row['Gift Sender Name'] !== 'Not Available') {
29+
$stats['skipped']++;
30+
31+
return true;
32+
}
33+
34+
return false;
35+
})
36+
->map(function (array $row) use ($parent) {
37+
return [
38+
'type' => ItemType::Item,
39+
'parent_id' => $parent?->id,
40+
'name' => html_entity_decode($row['Product Name'], ENT_QUOTES | ENT_HTML5, 'UTF-8'),
41+
'metadata' => [
42+
'Amount Paid' => $row['Total Amount'],
43+
'ASIN' => $row['ASIN'],
44+
'Purchased On' => Date::parse($row['Order Date']),
45+
'Website' => $row['Website'],
46+
],
47+
];
48+
});
49+
50+
$stats['imported'] = count($toImport);
51+
52+
$team->items()->createMany($toImport);
53+
54+
return $stats;
55+
}
56+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Actions;
5+
namespace App\Actions\Recipes;
66

77
use App\Models\Recipe;
88
use App\Models\Team;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Actions;
5+
namespace App\Actions\Recipes;
66

77
use App\Models\Recipe;
88
use App\Models\Team;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Actions;
5+
namespace App\Actions\Recipes;
66

77
use App\Models\Recipe;
88

app/Actions/ImportRecipeFromUrl.php renamed to app/Actions/Recipes/ImportRecipeFromUrl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Actions;
5+
namespace App\Actions\Recipes;
66

77
use Carbon\CarbonInterval;
88
use Illuminate\Support\Facades\Http;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Actions;
5+
namespace App\Actions\Recipes;
66

77
use App\Models\Recipe;
88

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Actions;
5+
namespace App\Actions\Recipes;
66

77
use App\Models\Recipe;
88
use Illuminate\Http\UploadedFile;

app/Http/Controllers/Api/RecipeController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace App\Http\Controllers\Api;
66

7-
use App\Actions\CreateRecipe;
8-
use App\Actions\DeleteRecipe;
9-
use App\Actions\UpdateRecipe;
7+
use App\Actions\Recipes\CreateRecipe;
8+
use App\Actions\Recipes\DeleteRecipe;
9+
use App\Actions\Recipes\UpdateRecipe;
1010
use App\Http\Controllers\Controller;
1111
use App\Http\Requests\Api\StoreRecipeRequest;
1212
use App\Http\Requests\Api\UpdateRecipeRequest;

app/Livewire/Forms/Recipes/RecipeForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace App\Livewire\Forms\Recipes;
66

7-
use App\Actions\CreateRecipe;
8-
use App\Actions\UpdateRecipe;
7+
use App\Actions\Recipes\CreateRecipe;
8+
use App\Actions\Recipes\UpdateRecipe;
99
use App\Models\Recipe;
1010
use Illuminate\Support\Facades\Auth;
1111
use Illuminate\Support\Facades\Storage;

app/Models/Item.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
use App\Enums\ItemType;
88
use Database\Factories\ItemFactory;
9+
use Illuminate\Database\Eloquent\Casts\Attribute;
910
use Illuminate\Database\Eloquent\Factories\HasFactory;
1011
use Illuminate\Database\Eloquent\Model;
1112
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1213
use Illuminate\Database\Eloquent\Relations\HasMany;
14+
use Illuminate\Support\Str;
1315

1416
class Item extends Model
1517
{
@@ -51,4 +53,11 @@ protected function casts(): array
5153
'metadata' => 'array',
5254
];
5355
}
56+
57+
protected function truncatedName(): Attribute
58+
{
59+
return Attribute::make(
60+
get: fn () => Str::limit($this->name, 75),
61+
);
62+
}
5463
}

0 commit comments

Comments
 (0)