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.

Store.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace App\Models;
  3. use App\Enums\StoreCategory;
  4. use App\Enums\StoreLanguage;
  5. use App\Enums\StoreStatus;
  6. use Carbon\CarbonInterface;
  7. use Database\Factories\StoreFactory;
  8. use Illuminate\Database\Eloquent\Attributes\Fillable;
  9. use Illuminate\Database\Eloquent\Builder;
  10. use Illuminate\Database\Eloquent\Factories\HasFactory;
  11. use Illuminate\Database\Eloquent\Model;
  12. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  13. use Illuminate\Database\Eloquent\Relations\HasMany;
  14. use Illuminate\Support\Carbon;
  15. use Illuminate\Support\Str;
  16. /**
  17. * A merchant's online store. Every store-related table points back here with `store_id`.
  18. *
  19. * @property int $id
  20. * @property int $user_id
  21. * @property string $name
  22. * @property string $slug
  23. * @property StoreCategory $category
  24. * @property string $description
  25. * @property StoreLanguage $language
  26. * @property StoreStatus $status
  27. * @property array<string, mixed>|null $plan
  28. * @property string|null $whatsapp_number
  29. * @property int $shipping_west
  30. * @property int $shipping_east
  31. * @property int|null $free_shipping_over
  32. * @property bool $announcement_enabled
  33. * @property list<string>|null $announcement_messages The merchant's own announcement lines
  34. * @property array<string, mixed>|null $published_plan
  35. * @property CarbonInterface|null $published_at
  36. * @property Carbon|null $created_at
  37. * @property Carbon|null $updated_at
  38. */
  39. #[Fillable(['name', 'category', 'description', 'language'])]
  40. class Store extends Model
  41. {
  42. /** @use HasFactory<StoreFactory> */
  43. use HasFactory;
  44. /**
  45. * Slugs that can never be used, because they will clash with our own subdomains.
  46. *
  47. * @var list<string>
  48. */
  49. public const array RESERVED_SLUGS = [
  50. 'www', 'app', 'api', 'admin', 'dashboard', 'mail', 'store', 'stores', 'create',
  51. 'localhost', 'static', 'assets', 'cdn', 'help', 'support', 'status', 'blog', 'billing', 'login', 'register',
  52. ];
  53. /**
  54. * Default values for new stores.
  55. *
  56. * @var array<string, string|int>
  57. */
  58. protected $attributes = [
  59. 'language' => 'en',
  60. 'status' => 'draft',
  61. // Starting shipping fees in sen, so a new store has sensible numbers before the merchant changes them.
  62. 'shipping_west' => 800,
  63. 'shipping_east' => 1500,
  64. ];
  65. protected static function booted(): void
  66. {
  67. static::creating(function (Store $store) {
  68. if (blank($store->slug)) {
  69. $store->slug = static::uniqueSlug($store->name);
  70. }
  71. });
  72. }
  73. /**
  74. * Build a web-safe slug from a store name that no other store is using.
  75. */
  76. public static function uniqueSlug(string $name): string
  77. {
  78. $base = Str::slug($name) ?: 'store';
  79. $slug = $base;
  80. $suffix = 2;
  81. while (in_array($slug, self::RESERVED_SLUGS, true) || static::query()->where('slug', $slug)->exists()) {
  82. $slug = $base.'-'.$suffix++;
  83. }
  84. return $slug;
  85. }
  86. public function getRouteKeyName(): string
  87. {
  88. return 'slug';
  89. }
  90. /**
  91. * @return BelongsTo<User, $this>
  92. */
  93. public function user(): BelongsTo
  94. {
  95. return $this->belongsTo(User::class);
  96. }
  97. /**
  98. * Stores that shoppers can open.
  99. *
  100. * @param Builder<Store> $query
  101. */
  102. public function scopePublished(Builder $query): void
  103. {
  104. $query->where('status', StoreStatus::Published)->whereNotNull('published_plan');
  105. }
  106. public function isPublished(): bool
  107. {
  108. return $this->status === StoreStatus::Published && $this->published_plan !== null;
  109. }
  110. /**
  111. * True when the working design differs from the copy shoppers see.
  112. */
  113. public function hasUnpublishedChanges(): bool
  114. {
  115. return $this->isPublished() && $this->plan != $this->published_plan;
  116. }
  117. /**
  118. * @return HasMany<Product, $this>
  119. */
  120. public function products(): HasMany
  121. {
  122. return $this->hasMany(Product::class);
  123. }
  124. /**
  125. * Photos uploaded for the design, such as a hero banner.
  126. *
  127. * @return HasMany<StoreImage, $this>
  128. */
  129. public function images(): HasMany
  130. {
  131. return $this->hasMany(StoreImage::class);
  132. }
  133. /**
  134. * @return HasMany<DiscountCode, $this>
  135. */
  136. public function discountCodes(): HasMany
  137. {
  138. return $this->hasMany(DiscountCode::class);
  139. }
  140. /**
  141. * @return HasMany<AutomaticDiscount, $this>
  142. */
  143. public function automaticDiscounts(): HasMany
  144. {
  145. return $this->hasMany(AutomaticDiscount::class);
  146. }
  147. /**
  148. * @return HasMany<Order, $this>
  149. */
  150. public function orders(): HasMany
  151. {
  152. return $this->hasMany(Order::class);
  153. }
  154. /**
  155. * The AI jobs run for this store, newest first.
  156. *
  157. * @return HasMany<StoreGeneration, $this>
  158. */
  159. public function generations(): HasMany
  160. {
  161. return $this->hasMany(StoreGeneration::class)->latest('id');
  162. }
  163. /**
  164. * Get the attributes that should be cast.
  165. *
  166. * @return array<string, string>
  167. */
  168. protected function casts(): array
  169. {
  170. return [
  171. 'category' => StoreCategory::class,
  172. 'language' => StoreLanguage::class,
  173. 'status' => StoreStatus::class,
  174. 'plan' => 'array',
  175. 'published_plan' => 'array',
  176. 'published_at' => 'datetime',
  177. 'shipping_west' => 'integer',
  178. 'shipping_east' => 'integer',
  179. 'free_shipping_over' => 'integer',
  180. 'announcement_enabled' => 'boolean',
  181. 'announcement_messages' => 'array',
  182. ];
  183. }
  184. }