AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Billing\UsageLimits;
  4. use App\Commerce\ChatActions;
  5. use App\Enums\GenerationStatus;
  6. use App\Enums\GenerationType;
  7. use App\Jobs\RunStoreGeneration;
  8. use App\Models\Store;
  9. use App\Models\StoreGeneration;
  10. use Illuminate\Http\RedirectResponse;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Validation\ValidationException;
  13. use Inertia\Inertia;
  14. /**
  15. * Starts AI jobs for a store. The work itself happens in the background (RunStoreGeneration).
  16. */
  17. class StoreGenerationController extends Controller
  18. {
  19. /**
  20. * Design the whole store with AI. The first design is free; later ones count as regenerations.
  21. */
  22. public function generate(Request $request, UsageLimits $usage, string $slug): RedirectResponse
  23. {
  24. $store = $this->ownedStore($request, $slug);
  25. $isRegeneration = $store->plan !== null;
  26. if ($this->isBusy($store)) {
  27. return $this->refuse($store, __('Your store is already being worked on. Please wait for it to finish.'));
  28. }
  29. if (! $usage->hasPlan($request->user())) {
  30. return $this->refuse($store, __('Your free trial has ended. Choose a plan to keep using AI.'));
  31. }
  32. if ($isRegeneration && ! $usage->canRegenerate($request->user())) {
  33. return $this->refuse($store, __('You have used all the regenerations in your plan. Upgrade for more.'));
  34. }
  35. $this->start($request, $store, [
  36. 'type' => GenerationType::Generate,
  37. 'is_regeneration' => $isRegeneration,
  38. ]);
  39. return to_route('stores.show', $store->slug);
  40. }
  41. /**
  42. * Ask the AI for one small change, in plain words.
  43. */
  44. public function edit(Request $request, UsageLimits $usage, string $slug): RedirectResponse
  45. {
  46. $store = $this->ownedStore($request, $slug);
  47. abort_if($store->plan === null, 404);
  48. $validated = $request->validate([
  49. 'instruction' => ['required', 'string', 'min:3', 'max:500'],
  50. ]);
  51. if ($this->isBusy($store)) {
  52. return $this->refuse($store, __('Your store is already being worked on. Please wait for it to finish.'));
  53. }
  54. if (! $usage->canEdit($request->user())) {
  55. return $this->refuse($store, $usage->hasPlan($request->user())
  56. ? __('You have used all the AI edits in your plan. Upgrade for more.')
  57. : __('Your free trial has ended. Choose a plan to keep using AI.'));
  58. }
  59. $this->start($request, $store, [
  60. 'type' => GenerationType::Edit,
  61. 'instruction' => $validated['instruction'],
  62. ]);
  63. return to_route('stores.show', $store->slug);
  64. }
  65. /**
  66. * Do the store changes the AI proposed in a chat edit. They are checked again first,
  67. * and either all of them happen or none.
  68. */
  69. public function confirm(Request $request, string $slug, int $generation): RedirectResponse
  70. {
  71. $store = $this->ownedStore($request, $slug);
  72. $edit = $this->pendingActions($store, $generation);
  73. try {
  74. ChatActions::apply($store, $edit->actions ?? []);
  75. } catch (ValidationException $e) {
  76. // Something changed since the AI proposed it, such as a code made by hand in the meantime.
  77. return $this->refuse($store, __('Nothing was changed: :problem', ['problem' => collect($e->errors())->flatten()->first()]));
  78. }
  79. $edit->actions_status = StoreGeneration::ACTIONS_CONFIRMED;
  80. $edit->save();
  81. Inertia::flash('toast', ['type' => 'success', 'message' => __('Done. Your store is updated.')]);
  82. return to_route('stores.show', $store->slug);
  83. }
  84. public function cancel(Request $request, string $slug, int $generation): RedirectResponse
  85. {
  86. $store = $this->ownedStore($request, $slug);
  87. $edit = $this->pendingActions($store, $generation);
  88. $edit->actions_status = StoreGeneration::ACTIONS_CANCELLED;
  89. $edit->save();
  90. return to_route('stores.show', $store->slug);
  91. }
  92. private function pendingActions(Store $store, int $id): StoreGeneration
  93. {
  94. return $store->generations()
  95. ->whereKey($id)
  96. ->where('actions_status', StoreGeneration::ACTIONS_PENDING)
  97. ->firstOrFail();
  98. }
  99. /**
  100. * @param array<string, mixed> $attributes
  101. */
  102. private function start(Request $request, Store $store, array $attributes): void
  103. {
  104. $generation = $store->generations()->create([
  105. ...$attributes,
  106. 'user_id' => $request->user()->id,
  107. 'status' => GenerationStatus::Queued,
  108. ]);
  109. RunStoreGeneration::dispatch($generation->id);
  110. }
  111. /**
  112. * One AI job per store at a time.
  113. */
  114. private function isBusy(Store $store): bool
  115. {
  116. StoreGeneration::failStale($store->id);
  117. return $store->generations()->active()->exists();
  118. }
  119. private function refuse(Store $store, string $message): RedirectResponse
  120. {
  121. Inertia::flash('toast', ['type' => 'error', 'message' => $message]);
  122. return to_route('stores.show', $store->slug);
  123. }
  124. private function ownedStore(Request $request, string $slug): Store
  125. {
  126. return $request->user()->stores()->where('slug', $slug)->firstOrFail();
  127. }
  128. }