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.
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Http\Controllers\Api\V1\Product\Resources\ProductShowResource;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\Product\ProductIndexRequest;
|
||||
use App\Models\Ecommerce\Product\Product\Product;
|
||||
use App\Models\Ecommerce\Product\ProductView\ProductView;
|
||||
use App\Repositories\Ecommerce\Product\ProductRepository;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
@@ -33,6 +34,15 @@ class ProductController extends Controller
|
||||
*/
|
||||
public function show(Product $product): JsonResponse
|
||||
{
|
||||
if (auth('sanctum')->check()) {
|
||||
ProductView::updateOrCreate([
|
||||
'user_id' => auth('sanctum')->id(),
|
||||
'product_id' => $product->id,
|
||||
], [
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
$product->load([
|
||||
'channels:id,name',
|
||||
'properties',
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\ProductIndexResource;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Repositories\Ecommerce\Product\ProductRepository;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ViewedProductController extends Controller
|
||||
{
|
||||
/**
|
||||
* Viewed Products (index)
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
return response()->rest_paginate(
|
||||
ProductIndexResource::collection(
|
||||
ProductRepository::make($request)
|
||||
->applyBasicQueries()
|
||||
->applyViewedBy(auth('sanctum')->user())
|
||||
->simplePaginate()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -31,9 +31,9 @@ class SendOrderCreatedNotification implements ShouldQueue
|
||||
return;
|
||||
}
|
||||
|
||||
$this->sendSMSToClient($event->order);
|
||||
$this->sendSMSToStaff($event->order);
|
||||
$this->sendSMSToVendors($event->order);
|
||||
// $this->sendSMSToClient($event->order);
|
||||
// $this->sendSMSToStaff($event->order);
|
||||
// $this->sendSMSToVendors($event->order);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
46
app/Models/Ecommerce/Product/ProductView/ProductView.php
Normal file
46
app/Models/Ecommerce/Product/ProductView/ProductView.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Ecommerce\Product\ProductView;
|
||||
|
||||
use App\Models\Ecommerce\Product\Product\Product;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class ProductView extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected $table = 'product_views';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'product_id',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* Product
|
||||
*/
|
||||
public function product(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* User
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ 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;
|
||||
@@ -128,6 +129,14 @@ class User extends Authenticatable
|
||||
return $this->hasMany(Favorite::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* User's viewed products
|
||||
*/
|
||||
public function productViews(): HasMany
|
||||
{
|
||||
return $this->hasMany(ProductView::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* User's favorite products
|
||||
*/
|
||||
|
||||
@@ -67,13 +67,13 @@ class OrderFieldsForIndex
|
||||
DateTime::make(__('Created at'), 'created_at')
|
||||
->turkmenDate(),
|
||||
|
||||
Badge::make('Status')
|
||||
->map(OrderStatus::classes())
|
||||
->labels(OrderStatus::values())
|
||||
->addTypes([
|
||||
'primary' => 'dark:bg-gray-900 bg-gray-600 text-white',
|
||||
])
|
||||
->sortable(),
|
||||
// Badge::make('Status')
|
||||
// ->map(OrderStatus::classes())
|
||||
// ->labels(OrderStatus::values())
|
||||
// ->addTypes([
|
||||
// 'primary' => 'dark:bg-gray-900 bg-gray-600 text-white',
|
||||
// ])
|
||||
// ->sortable(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,6 +150,20 @@ class ProductRepository
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by viewed by user
|
||||
*/
|
||||
public function applyViewedBy($user): self
|
||||
{
|
||||
$this->queryBuilder
|
||||
->join('product_views', 'products.id', '=', 'product_views.product_id')
|
||||
->where('product_views.user_id', $user->id)
|
||||
->orderBy('product_views.updated_at', 'desc')
|
||||
->select('products.*');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* "Where IN" clause
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user