Files
postshop-backend/app/Models/User.php
Mekan1206 968c9ed42a Add product view tracking and related functionality
- Implemented product view tracking in ProductController to log user views.
- Added a relationship for viewed products in the User model.
- Introduced a method in ProductRepository to filter products viewed by a user.
- Updated API routes to include endpoint for retrieving viewed products.
- Commented out SMS notification logic in SendOrderCreatedNotification.
- Removed CreateOrderServiceTest as it is no longer needed.
2026-02-08 02:02:30 +05:00

213 lines
4.6 KiB
PHP

<?php
namespace App\Models;
use App\Models\Concerns\HasEcommerceChannels;
use App\Models\Concerns\HasSchemalessAttributes;
use App\Models\Concerns\InteractsWithNova;
use App\Models\Concerns\InteractsWithRoles;
use App\Models\Ecommerce\Product\Cart\CartItem;
use App\Models\Ecommerce\Product\Favorite\Favorite;
use App\Models\Ecommerce\Product\Order\Order;
use App\Models\Ecommerce\Product\ProductView\ProductView;
use App\Models\Ecommerce\Product\Review\Review;
use App\Models\Post\User\UserDoc;
use App\Models\System\Settings\Location\UserAddress;
use App\Repositories\System\Cache\CacheRepository;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\DeletedModels\Models\Concerns\KeepsDeletedModels;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasApiTokens;
use HasEcommerceChannels;
use HasFactory;
use HasRoles;
use HasSchemalessAttributes;
use InteractsWithNova;
use InteractsWithRoles;
use KeepsDeletedModels;
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'first_name',
'last_name',
'email',
'phone_number',
'verified',
'password',
'options',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
/**
* Get fullname of user
*
* @return string
*/
public function fullname(): Attribute
{
return Attribute::make(
get: fn () => sprintf('%s %s', $this->first_name, $this->last_name)
);
}
/**
* Get users locale
*/
public function locale(): Attribute
{
return Attribute::make(
get: fn () => $this->options->get('locale'),
);
}
/**
* Get all role names
*
* @return string
*/
public function roleNames(): Attribute
{
return Attribute::make(
get: fn () => $this->getRoleNames()->implode(', ')
);
}
/**
* Addresses
*/
public function addresses(): HasMany
{
return $this->hasMany(UserAddress::class);
}
/**
* User's carts
*/
public function carts(): HasMany
{
return $this->hasMany(CartItem::class);
}
/**
* User's favorite products
*/
public function favorites(): HasMany
{
return $this->hasMany(Favorite::class);
}
/**
* User's viewed products
*/
public function productViews(): HasMany
{
return $this->hasMany(ProductView::class);
}
/**
* User's favorite products
*/
public function reviews(): HasMany
{
return $this->hasMany(Review::class);
}
/**
* Orders
*/
public function orders(): HasMany
{
return $this->hasMany(Order::class);
}
/**
* Documents
*/
public function documents(): HasOne
{
return $this->hasOne(UserDoc::class);
}
/**
* Get Company name
*/
public function companyName(): ?string
{
$this->loadMissing('documents');
return $this->documents?->formattedCorparationName;
}
/**
* User owns product
*/
public function ownsProduct($product): bool
{
return true;
// return CacheRepository::make();
}
/**
* Owns inventory
*/
public function ownsInventory($inventory): bool
{
return $this->channel()->id === $inventory->channel_id;
}
/**
* Check if user does not own any inventories
*/
public function doesntOwnInventory(): bool
{
return $this->inventoryCount() === 0;
}
/**
* Inventory count
*/
public function inventoryCount(): int
{
$inventoryCount = cache()->store('array')->get('user_inventory_count');
if (! $inventoryCount) {
cache()->store('array')->put('user_inventory_count', $this->channel()->inventories()->count());
}
return cache()->store('array')->get('user_inventory_count');
}
}