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

UsageLimits.php 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Billing;
  3. use App\Enums\GenerationStatus;
  4. use App\Enums\GenerationType;
  5. use App\Models\Plan;
  6. use App\Models\StoreGeneration;
  7. use App\Models\User;
  8. /**
  9. * What a user's plan allows, and how much of it they have used.
  10. *
  11. * - Failed AI jobs never count.
  12. * - The first design of a store is free; only redesigns count as regenerations.
  13. * - With no plan (trial over, no subscription) every limit is zero.
  14. */
  15. class UsageLimits
  16. {
  17. /**
  18. * @return array{active: bool, plan: string|null, plan_name: string, period: string, on_trial: bool, trial_ends_at: string|null, stores: array{used: int, limit: int}, regenerations: array{used: int, limit: int}, edits: array{used: int, limit: int}}
  19. */
  20. public function for(User $user): array
  21. {
  22. $plan = $user->currentPlan();
  23. $period = $plan !== null ? $plan->period : 'month';
  24. $paying = $plan?->isPaid() ?? false;
  25. return [
  26. 'active' => $plan !== null,
  27. 'plan' => $plan?->key,
  28. 'plan_name' => $plan !== null ? $plan->name : 'No plan',
  29. 'period' => $period,
  30. 'on_trial' => $plan !== null && ! $paying,
  31. 'trial_ends_at' => $user->trial_ends_at?->toIso8601String(),
  32. 'stores' => [
  33. 'used' => $user->stores()->count(),
  34. 'limit' => $plan !== null ? $plan->stores : 0,
  35. ],
  36. 'regenerations' => [
  37. 'used' => $this->used($user, GenerationType::Generate, $period),
  38. 'limit' => $plan !== null ? $plan->regenerations : 0,
  39. ],
  40. 'edits' => [
  41. 'used' => $this->used($user, GenerationType::Edit, $period),
  42. 'limit' => $plan !== null ? $plan->edits : 0,
  43. ],
  44. ];
  45. }
  46. /**
  47. * Whether the user has a plan at all: a paid one, or a free trial that has not ended.
  48. */
  49. public function hasPlan(User $user): bool
  50. {
  51. return $user->currentPlan() instanceof Plan;
  52. }
  53. public function canCreateStore(User $user): bool
  54. {
  55. return $this->hasRoom($this->for($user)['stores']);
  56. }
  57. public function canRegenerate(User $user): bool
  58. {
  59. return $this->hasRoom($this->for($user)['regenerations']);
  60. }
  61. public function canEdit(User $user): bool
  62. {
  63. return $this->hasRoom($this->for($user)['edits']);
  64. }
  65. /**
  66. * @param array{used: int, limit: int} $usage
  67. */
  68. private function hasRoom(array $usage): bool
  69. {
  70. return $usage['used'] < $usage['limit'];
  71. }
  72. private function used(User $user, GenerationType $type, string $period): int
  73. {
  74. return StoreGeneration::query()
  75. ->where('user_id', $user->id)
  76. ->where('type', $type)
  77. ->where('status', '!=', GenerationStatus::Failed)
  78. ->when($type === GenerationType::Generate, fn ($query) => $query->where('is_regeneration', true))
  79. ->when($period === 'month', fn ($query) => $query->where('created_at', '>=', now()->startOfMonth()))
  80. ->count();
  81. }
  82. }