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

StoreController.php 7.1KB

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