AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

UsageLimits.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Billing;
  3. use App\Enums\GenerationStatus;
  4. use App\Enums\GenerationType;
  5. use App\Models\StoreGeneration;
  6. use App\Models\User;
  7. /**
  8. * How much AI a user has used, and how much their plan allows.
  9. *
  10. * Failed jobs never count. The first design of a store is free; only redesigns count as regenerations.
  11. */
  12. class UsageLimits
  13. {
  14. /**
  15. * @return array{plan: string, plan_name: string, period: string, regenerations: array{used: int, limit: int}, edits: array{used: int, limit: int}}
  16. */
  17. public function for(User $user): array
  18. {
  19. $key = array_key_exists($user->plan, config('plans.plans')) ? $user->plan : config('plans.default');
  20. $plan = config("plans.plans.$key");
  21. return [
  22. 'plan' => $key,
  23. 'plan_name' => $plan['name'],
  24. 'period' => $plan['period'],
  25. 'regenerations' => [
  26. 'used' => $this->used($user, GenerationType::Generate, $plan['period']),
  27. 'limit' => (int) $plan['regenerations'],
  28. ],
  29. 'edits' => [
  30. 'used' => $this->used($user, GenerationType::Edit, $plan['period']),
  31. 'limit' => (int) $plan['edits'],
  32. ],
  33. ];
  34. }
  35. public function canRegenerate(User $user): bool
  36. {
  37. $usage = $this->for($user)['regenerations'];
  38. return $usage['used'] < $usage['limit'];
  39. }
  40. public function canEdit(User $user): bool
  41. {
  42. $usage = $this->for($user)['edits'];
  43. return $usage['used'] < $usage['limit'];
  44. }
  45. private function used(User $user, GenerationType $type, string $period): int
  46. {
  47. return StoreGeneration::query()
  48. ->where('user_id', $user->id)
  49. ->where('type', $type)
  50. ->where('status', '!=', GenerationStatus::Failed)
  51. ->when($type === GenerationType::Generate, fn ($query) => $query->where('is_regeneration', true))
  52. ->when($period === 'month', fn ($query) => $query->where('created_at', '>=', now()->startOfMonth()))
  53. ->count();
  54. }
  55. }