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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Models;
  3. use App\Enums\GenerationStatus;
  4. use App\Enums\GenerationType;
  5. use Carbon\CarbonInterface;
  6. use Database\Factories\StoreGenerationFactory;
  7. use Illuminate\Database\Eloquent\Attributes\Fillable;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  12. use Illuminate\Support\Carbon;
  13. /**
  14. * One AI job for a store: a full design or one chat edit.
  15. *
  16. * @property int $id
  17. * @property int $store_id
  18. * @property int $user_id
  19. * @property GenerationType $type
  20. * @property GenerationStatus $status
  21. * @property bool $is_regeneration
  22. * @property string|null $instruction
  23. * @property string|null $reply
  24. * @property bool|null $changed
  25. * @property string|null $error
  26. * @property string|null $provider
  27. * @property string|null $model
  28. * @property int $input_tokens
  29. * @property int $output_tokens
  30. * @property CarbonInterface|null $started_at
  31. * @property CarbonInterface|null $finished_at
  32. * @property Carbon|null $created_at
  33. * @property Carbon|null $updated_at
  34. * @property-read Store $store
  35. */
  36. #[Fillable(['user_id', 'type', 'status', 'is_regeneration', 'instruction'])]
  37. class StoreGeneration extends Model
  38. {
  39. /** @use HasFactory<StoreGenerationFactory> */
  40. use HasFactory;
  41. /**
  42. * @return BelongsTo<Store, $this>
  43. */
  44. public function store(): BelongsTo
  45. {
  46. return $this->belongsTo(Store::class);
  47. }
  48. /**
  49. * Jobs that are waiting or running.
  50. *
  51. * @param Builder<StoreGeneration> $query
  52. */
  53. public function scopeActive(Builder $query): void
  54. {
  55. $query->whereIn('status', [GenerationStatus::Queued, GenerationStatus::Running]);
  56. }
  57. /**
  58. * Mark jobs that have been waiting or running for too long as failed.
  59. * This happens when the background worker is not running or was stopped mid-job.
  60. */
  61. public static function failStale(int $storeId): void
  62. {
  63. static::query()
  64. ->where('store_id', $storeId)
  65. ->active()
  66. ->where('created_at', '<', now()->subMinutes((int) config('plans.stale_after_minutes', 10)))
  67. ->update([
  68. 'status' => GenerationStatus::Failed,
  69. 'error' => 'This took too long and was stopped. Please try again.',
  70. 'finished_at' => now(),
  71. ]);
  72. }
  73. public function markFailed(string $message): void
  74. {
  75. $this->status = GenerationStatus::Failed;
  76. $this->error = mb_substr($message, 0, 500);
  77. $this->finished_at = now();
  78. $this->save();
  79. }
  80. /**
  81. * @return array<string, string>
  82. */
  83. protected function casts(): array
  84. {
  85. return [
  86. 'type' => GenerationType::class,
  87. 'status' => GenerationStatus::class,
  88. 'is_regeneration' => 'boolean',
  89. 'changed' => 'boolean',
  90. 'started_at' => 'datetime',
  91. 'finished_at' => 'datetime',
  92. ];
  93. }
  94. }