AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

StoreController.php 6.0KB

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