AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

StoreController.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Billing\UsageLimits;
  4. use App\Enums\GenerationType;
  5. use App\Enums\StoreCategory;
  6. use App\Enums\StoreLanguage;
  7. use App\Http\Requests\Stores\CreateStoreRequest;
  8. use App\Models\Product;
  9. use App\Models\Store;
  10. use App\Models\StoreGeneration;
  11. use App\Storefront\Storefront;
  12. use App\StorePlan\FeaturedProducts;
  13. use App\StorePlan\HeroImages;
  14. use Illuminate\Http\RedirectResponse;
  15. use Illuminate\Http\Request;
  16. use Inertia\Inertia;
  17. use Inertia\Response;
  18. class StoreController extends Controller
  19. {
  20. /**
  21. * Show the dashboard with the user's own stores.
  22. */
  23. public function index(Request $request, UsageLimits $usage): Response
  24. {
  25. return Inertia::render('dashboard', [
  26. 'usage' => $usage->for($request->user()),
  27. 'stores' => $request->user()
  28. ->stores()
  29. ->latest()
  30. ->get()
  31. ->map(fn (Store $store) => $this->present($store)),
  32. ]);
  33. }
  34. /**
  35. * Show the "create a store" form.
  36. */
  37. public function create(Request $request, UsageLimits $usage): Response|RedirectResponse
  38. {
  39. if (! $usage->canCreateStore($request->user())) {
  40. return $this->storeLimitReached($request, $usage);
  41. }
  42. return Inertia::render('stores/create', [
  43. 'categories' => StoreCategory::options(),
  44. 'languages' => StoreLanguage::options(),
  45. ]);
  46. }
  47. /**
  48. * Save a new store for the current user.
  49. */
  50. public function store(CreateStoreRequest $request, UsageLimits $usage): RedirectResponse
  51. {
  52. if (! $usage->canCreateStore($request->user())) {
  53. return $this->storeLimitReached($request, $usage);
  54. }
  55. $store = $request->user()->stores()->create($request->validated());
  56. Inertia::flash('toast', ['type' => 'success', 'message' => __('Store created.')]);
  57. return to_route('stores.show', $store->slug);
  58. }
  59. /**
  60. * Show one store. Looking it up through the user means other people's stores give a 404.
  61. */
  62. public function show(Request $request, UsageLimits $usage, string $slug): Response
  63. {
  64. $store = $request->user()->stores()->where('slug', $slug)->firstOrFail();
  65. StoreGeneration::failStale($store->id);
  66. $latest = $store->generations()->first();
  67. return Inertia::render('stores/show', [
  68. 'store' => $this->present($store),
  69. // The newest AI job. The page keeps refreshing while it is queued or running.
  70. 'generation' => $latest ? $this->presentGeneration($latest) : null,
  71. 'edits' => $store->generations()
  72. ->where('type', GenerationType::Edit)
  73. ->limit(5)
  74. ->get()
  75. ->map(fn (StoreGeneration $edit) => $this->presentGeneration($edit)),
  76. 'usage' => $usage->for($request->user()),
  77. // Featured product sections of the working design, and the products they can show.
  78. 'featuredSections' => $store->plan !== null ? FeaturedProducts::sections($store->plan) : [],
  79. // Hero sections of the working design, and the photos they can use.
  80. 'heroSections' => $store->plan !== null ? HeroImages::sections($store->plan) : [],
  81. 'heroChoices' => HeroImages::choices($store),
  82. 'productOptions' => $store->products()
  83. ->active()
  84. ->orderBy('name')
  85. ->get(['slug', 'name', 'price'])
  86. ->map(fn (Product $product) => ['slug' => $product->slug, 'name' => $product->name, 'price' => $product->price / 100]),
  87. ]);
  88. }
  89. /**
  90. * The plan does not allow another store. Send the user to the billing page with the reason.
  91. */
  92. private function storeLimitReached(Request $request, UsageLimits $usage): RedirectResponse
  93. {
  94. Inertia::flash('toast', ['type' => 'error', 'message' => $usage->hasPlan($request->user())
  95. ? __('Your plan does not allow more stores. Upgrade to add another.')
  96. : __('Your free trial has ended. Choose a plan to continue.')]);
  97. return to_route('billing');
  98. }
  99. /**
  100. * The store fields the frontend is allowed to see.
  101. *
  102. * @return array<string, mixed>
  103. */
  104. private function present(Store $store): array
  105. {
  106. return [
  107. 'id' => $store->id,
  108. 'name' => $store->name,
  109. 'slug' => $store->slug,
  110. 'category' => $store->category->value,
  111. 'category_label' => $store->category->label(),
  112. 'description' => $store->description,
  113. 'language' => $store->language->value,
  114. 'language_label' => $store->language->label(),
  115. 'status' => $store->status->value,
  116. 'status_label' => $store->status->label(),
  117. 'has_design' => $store->plan !== null,
  118. 'is_published' => $store->isPublished(),
  119. 'has_unpublished_changes' => $store->hasUnpublishedChanges(),
  120. 'published_at' => $store->published_at?->toIso8601String(),
  121. // Where shoppers open the store once it is published.
  122. 'live_url' => Storefront::liveUrl($store, request()),
  123. // Changes whenever the design is saved, so the preview knows to reload.
  124. 'design_version' => $store->updated_at?->getTimestamp(),
  125. 'created_at' => $store->created_at?->toIso8601String(),
  126. ];
  127. }
  128. /**
  129. * The AI job fields the frontend is allowed to see.
  130. *
  131. * @return array<string, mixed>
  132. */
  133. private function presentGeneration(StoreGeneration $generation): array
  134. {
  135. return [
  136. 'id' => $generation->id,
  137. 'type' => $generation->type->value,
  138. 'status' => $generation->status->value,
  139. 'instruction' => $generation->instruction,
  140. 'reply' => $generation->reply,
  141. 'changed' => $generation->changed,
  142. // Store changes waiting for (or given) the merchant's OK, in plain sentences.
  143. 'actions' => array_map(fn (array $action) => $action['summary'], $generation->actions ?? []),
  144. 'actions_status' => $generation->actions_status,
  145. 'error' => $generation->error,
  146. 'created_at' => $generation->created_at?->toIso8601String(),
  147. ];
  148. }
  149. }