| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Billing\UsageLimits;
- use App\Enums\GenerationType;
- use App\Enums\StoreCategory;
- use App\Enums\StoreLanguage;
- use App\Http\Requests\Stores\CreateStoreRequest;
- use App\Models\Product;
- use App\Models\Store;
- use App\Models\StoreGeneration;
- use App\Storefront\Storefront;
- use App\StorePlan\FeaturedProducts;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Inertia\Inertia;
- use Inertia\Response;
-
- class StoreController extends Controller
- {
- /**
- * Show the dashboard with the user's own stores.
- */
- public function index(Request $request, UsageLimits $usage): Response
- {
- return Inertia::render('dashboard', [
- 'usage' => $usage->for($request->user()),
- 'stores' => $request->user()
- ->stores()
- ->latest()
- ->get()
- ->map(fn (Store $store) => $this->present($store)),
- ]);
- }
-
- /**
- * Show the "create a store" form.
- */
- public function create(Request $request, UsageLimits $usage): Response|RedirectResponse
- {
- if (! $usage->canCreateStore($request->user())) {
- return $this->storeLimitReached($request, $usage);
- }
-
- return Inertia::render('stores/create', [
- 'categories' => StoreCategory::options(),
- 'languages' => StoreLanguage::options(),
- ]);
- }
-
- /**
- * Save a new store for the current user.
- */
- public function store(CreateStoreRequest $request, UsageLimits $usage): RedirectResponse
- {
- if (! $usage->canCreateStore($request->user())) {
- return $this->storeLimitReached($request, $usage);
- }
-
- $store = $request->user()->stores()->create($request->validated());
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Store created.')]);
-
- return to_route('stores.show', $store->slug);
- }
-
- /**
- * Show one store. Looking it up through the user means other people's stores give a 404.
- */
- public function show(Request $request, UsageLimits $usage, string $slug): Response
- {
- $store = $request->user()->stores()->where('slug', $slug)->firstOrFail();
-
- StoreGeneration::failStale($store->id);
-
- $latest = $store->generations()->first();
-
- return Inertia::render('stores/show', [
- 'store' => $this->present($store),
- // The newest AI job. The page keeps refreshing while it is queued or running.
- 'generation' => $latest ? $this->presentGeneration($latest) : null,
- 'edits' => $store->generations()
- ->where('type', GenerationType::Edit)
- ->limit(5)
- ->get()
- ->map(fn (StoreGeneration $edit) => $this->presentGeneration($edit)),
- 'usage' => $usage->for($request->user()),
- // Featured product sections of the working design, and the products they can show.
- 'featuredSections' => $store->plan !== null ? FeaturedProducts::sections($store->plan) : [],
- 'productOptions' => $store->products()
- ->active()
- ->orderBy('name')
- ->get(['slug', 'name', 'price'])
- ->map(fn (Product $product) => ['slug' => $product->slug, 'name' => $product->name, 'price' => $product->price / 100]),
- ]);
- }
-
- /**
- * The plan does not allow another store. Send the user to the billing page with the reason.
- */
- private function storeLimitReached(Request $request, UsageLimits $usage): RedirectResponse
- {
- Inertia::flash('toast', ['type' => 'error', 'message' => $usage->hasPlan($request->user())
- ? __('Your plan does not allow more stores. Upgrade to add another.')
- : __('Your free trial has ended. Choose a plan to continue.')]);
-
- return to_route('billing');
- }
-
- /**
- * The store fields the frontend is allowed to see.
- *
- * @return array<string, mixed>
- */
- private function present(Store $store): array
- {
- return [
- 'id' => $store->id,
- 'name' => $store->name,
- 'slug' => $store->slug,
- 'category' => $store->category->value,
- 'category_label' => $store->category->label(),
- 'description' => $store->description,
- 'language' => $store->language->value,
- 'language_label' => $store->language->label(),
- 'status' => $store->status->value,
- 'status_label' => $store->status->label(),
- 'has_design' => $store->plan !== null,
- 'is_published' => $store->isPublished(),
- 'has_unpublished_changes' => $store->hasUnpublishedChanges(),
- 'published_at' => $store->published_at?->toIso8601String(),
- // Where shoppers open the store once it is published.
- 'live_url' => Storefront::liveUrl($store, request()),
- // Changes whenever the design is saved, so the preview knows to reload.
- 'design_version' => $store->updated_at?->getTimestamp(),
- 'created_at' => $store->created_at?->toIso8601String(),
- ];
- }
-
- /**
- * The AI job fields the frontend is allowed to see.
- *
- * @return array<string, mixed>
- */
- private function presentGeneration(StoreGeneration $generation): array
- {
- return [
- 'id' => $generation->id,
- 'type' => $generation->type->value,
- 'status' => $generation->status->value,
- 'instruction' => $generation->instruction,
- 'reply' => $generation->reply,
- 'changed' => $generation->changed,
- 'error' => $generation->error,
- 'created_at' => $generation->created_at?->toIso8601String(),
- ];
- }
- }
|