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.

StoreGeneration.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 string|null $error
  25. * @property string|null $provider
  26. * @property string|null $model
  27. * @property int $input_tokens
  28. * @property int $output_tokens
  29. * @property CarbonInterface|null $started_at
  30. * @property CarbonInterface|null $finished_at
  31. * @property Carbon|null $created_at
  32. * @property Carbon|null $updated_at
  33. * @property-read Store $store
  34. */
  35. #[Fillable(['user_id', 'type', 'status', 'is_regeneration', 'instruction'])]
  36. class StoreGeneration extends Model
  37. {
  38. /** @use HasFactory<StoreGenerationFactory> */
  39. use HasFactory;
  40. /**
  41. * @return BelongsTo<Store, $this>
  42. */
  43. public function store(): BelongsTo
  44. {
  45. return $this->belongsTo(Store::class);
  46. }
  47. /**
  48. * Jobs that are waiting or running.
  49. *
  50. * @param Builder<StoreGeneration> $query
  51. */
  52. public function scopeActive(Builder $query): void
  53. {
  54. $query->whereIn('status', [GenerationStatus::Queued, GenerationStatus::Running]);
  55. }
  56. /**
  57. * Mark jobs that have been waiting or running for too long as failed.
  58. * This happens when the background worker is not running or was stopped mid-job.
  59. */
  60. public static function failStale(int $storeId): void
  61. {
  62. static::query()
  63. ->where('store_id', $storeId)
  64. ->active()
  65. ->where('created_at', '<', now()->subMinutes((int) config('plans.stale_after_minutes', 10)))
  66. ->update([
  67. 'status' => GenerationStatus::Failed,
  68. 'error' => 'This took too long and was stopped. Please try again.',
  69. 'finished_at' => now(),
  70. ]);
  71. }
  72. public function markFailed(string $message): void
  73. {
  74. $this->status = GenerationStatus::Failed;
  75. $this->error = mb_substr($message, 0, 500);
  76. $this->finished_at = now();
  77. $this->save();
  78. }
  79. /**
  80. * @return array<string, string>
  81. */
  82. protected function casts(): array
  83. {
  84. return [
  85. 'type' => GenerationType::class,
  86. 'status' => GenerationStatus::class,
  87. 'is_regeneration' => 'boolean',
  88. 'started_at' => 'datetime',
  89. 'finished_at' => 'datetime',
  90. ];
  91. }
  92. }