AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

StoreController.php 6.8KB

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