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\Controllers\Controller;
|
||||||
use App\Http\Requests\Api\V1\Product\ProductIndexRequest;
|
use App\Http\Requests\Api\V1\Product\ProductIndexRequest;
|
||||||
use App\Models\Ecommerce\Product\Product\Product;
|
use App\Models\Ecommerce\Product\Product\Product;
|
||||||
|
use App\Models\Ecommerce\Product\ProductView\ProductView;
|
||||||
use App\Repositories\Ecommerce\Product\ProductRepository;
|
use App\Repositories\Ecommerce\Product\ProductRepository;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
|
|
||||||
@@ -33,6 +34,15 @@ class ProductController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function show(Product $product): JsonResponse
|
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([
|
$product->load([
|
||||||
'channels:id,name',
|
'channels:id,name',
|
||||||
'properties',
|
'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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->sendSMSToClient($event->order);
|
// $this->sendSMSToClient($event->order);
|
||||||
$this->sendSMSToStaff($event->order);
|
// $this->sendSMSToStaff($event->order);
|
||||||
$this->sendSMSToVendors($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\Cart\CartItem;
|
||||||
use App\Models\Ecommerce\Product\Favorite\Favorite;
|
use App\Models\Ecommerce\Product\Favorite\Favorite;
|
||||||
use App\Models\Ecommerce\Product\Order\Order;
|
use App\Models\Ecommerce\Product\Order\Order;
|
||||||
|
use App\Models\Ecommerce\Product\ProductView\ProductView;
|
||||||
use App\Models\Ecommerce\Product\Review\Review;
|
use App\Models\Ecommerce\Product\Review\Review;
|
||||||
use App\Models\Post\User\UserDoc;
|
use App\Models\Post\User\UserDoc;
|
||||||
use App\Models\System\Settings\Location\UserAddress;
|
use App\Models\System\Settings\Location\UserAddress;
|
||||||
@@ -128,6 +129,14 @@ class User extends Authenticatable
|
|||||||
return $this->hasMany(Favorite::class);
|
return $this->hasMany(Favorite::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User's viewed products
|
||||||
|
*/
|
||||||
|
public function productViews(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(ProductView::class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User's favorite products
|
* User's favorite products
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -67,13 +67,13 @@ class OrderFieldsForIndex
|
|||||||
DateTime::make(__('Created at'), 'created_at')
|
DateTime::make(__('Created at'), 'created_at')
|
||||||
->turkmenDate(),
|
->turkmenDate(),
|
||||||
|
|
||||||
Badge::make('Status')
|
// Badge::make('Status')
|
||||||
->map(OrderStatus::classes())
|
// ->map(OrderStatus::classes())
|
||||||
->labels(OrderStatus::values())
|
// ->labels(OrderStatus::values())
|
||||||
->addTypes([
|
// ->addTypes([
|
||||||
'primary' => 'dark:bg-gray-900 bg-gray-600 text-white',
|
// 'primary' => 'dark:bg-gray-900 bg-gray-600 text-white',
|
||||||
])
|
// ])
|
||||||
->sortable(),
|
// ->sortable(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -150,6 +150,20 @@ class ProductRepository
|
|||||||
return $this;
|
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
|
* "Where IN" clause
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('product_views', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('product_views');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -21,6 +21,7 @@ use App\Http\Controllers\Api\V1\Product\ProductController;
|
|||||||
use App\Http\Controllers\Api\V1\Product\ProductRelatedController;
|
use App\Http\Controllers\Api\V1\Product\ProductRelatedController;
|
||||||
use App\Http\Controllers\Api\V1\Product\ProductReviewController;
|
use App\Http\Controllers\Api\V1\Product\ProductReviewController;
|
||||||
use App\Http\Controllers\Api\V1\Product\Search\ProductSearchController;
|
use App\Http\Controllers\Api\V1\Product\Search\ProductSearchController;
|
||||||
|
use App\Http\Controllers\Api\V1\Product\ViewedProductController;
|
||||||
use App\Http\Controllers\Api\V1\Profile\ProfileController;
|
use App\Http\Controllers\Api\V1\Profile\ProfileController;
|
||||||
use App\Http\Controllers\Api\V1\Province\ProvinceController;
|
use App\Http\Controllers\Api\V1\Province\ProvinceController;
|
||||||
use App\Http\Controllers\Api\V1\ReviewController;
|
use App\Http\Controllers\Api\V1\ReviewController;
|
||||||
@@ -115,6 +116,9 @@ Route::middleware(['auth:sanctum', 'banned'])->group(function () {
|
|||||||
Route::get('favorites', [FavoriteController::class, 'index']);
|
Route::get('favorites', [FavoriteController::class, 'index']);
|
||||||
Route::post('favorites', [FavoriteController::class, 'store']);
|
Route::post('favorites', [FavoriteController::class, 'store']);
|
||||||
|
|
||||||
|
// Viewed products...
|
||||||
|
Route::get('products/viewed', [ViewedProductController::class, 'index']);
|
||||||
|
|
||||||
// Reviews...
|
// Reviews...
|
||||||
Route::get('reviews', [ReviewController::class, 'index']);
|
Route::get('reviews', [ReviewController::class, 'index']);
|
||||||
Route::patch('reviews/{review}', [ReviewController::class, 'update'])->where(['review' => '[0-9]+']);
|
Route::patch('reviews/{review}', [ReviewController::class, 'update'])->where(['review' => '[0-9]+']);
|
||||||
|
|||||||
170
tests/Feature/Api/V1/Product/ViewedProductTest.php
Normal file
170
tests/Feature/Api/V1/Product/ViewedProductTest.php
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Models\Ecommerce\Product\Product\Product;
|
||||||
|
use App\Models\Ecommerce\Product\ProductView\ProductView;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
uses(RefreshDatabase::class);
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
Config::set('ecommerce.api.token', 'test-token');
|
||||||
|
|
||||||
|
$this->user = User::factory()->create([
|
||||||
|
'password' => 'password',
|
||||||
|
'phone_number' => 61929248,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->createProduct = function (array $attributes = []) {
|
||||||
|
$name = $attributes['name'] ?? 'Test Product ' . Str::random(5);
|
||||||
|
return Product::create(array_merge([
|
||||||
|
'name' => $name,
|
||||||
|
'slug' => Str::slug($name),
|
||||||
|
'is_visible' => true,
|
||||||
|
'price_amount' => 100.00,
|
||||||
|
'stock' => 10,
|
||||||
|
'parent_id' => null,
|
||||||
|
], $attributes));
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
test('authenticated user viewing a product records the view', function () {
|
||||||
|
$product = ($this->createProduct)();
|
||||||
|
|
||||||
|
$this->actingAs($this->user, 'sanctum')
|
||||||
|
->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson("/api/v1/products/{$product->id}")
|
||||||
|
->assertOk();
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('product_views', [
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'product_id' => $product->id,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('unauthenticated user viewing a product does not record the view', function () {
|
||||||
|
$product = ($this->createProduct)();
|
||||||
|
|
||||||
|
$this->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson("/api/v1/products/{$product->id}")
|
||||||
|
->assertOk();
|
||||||
|
|
||||||
|
$this->assertDatabaseMissing('product_views', [
|
||||||
|
'product_id' => $product->id,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('viewing the same product again updates the timestamp', function () {
|
||||||
|
$product = ($this->createProduct)();
|
||||||
|
|
||||||
|
// First view
|
||||||
|
$this->actingAs($this->user, 'sanctum')
|
||||||
|
->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson("/api/v1/products/{$product->id}");
|
||||||
|
|
||||||
|
$firstView = ProductView::where('user_id', $this->user->id)
|
||||||
|
->where('product_id', $product->id)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
// Travel into the future
|
||||||
|
$this->travel(1)->hour();
|
||||||
|
|
||||||
|
// Second view
|
||||||
|
$this->actingAs($this->user, 'sanctum')
|
||||||
|
->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson("/api/v1/products/{$product->id}");
|
||||||
|
|
||||||
|
$secondView = ProductView::where('user_id', $this->user->id)
|
||||||
|
->where('product_id', $product->id)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
expect($secondView->updated_at->gt($firstView->updated_at))->toBeTrue();
|
||||||
|
// Ensure no duplicate records
|
||||||
|
expect(ProductView::where('user_id', $this->user->id)->count())->toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('authenticated user can list viewed products', function () {
|
||||||
|
$product1 = ($this->createProduct)(['name' => 'Product 1']);
|
||||||
|
$product2 = ($this->createProduct)(['name' => 'Product 2']);
|
||||||
|
|
||||||
|
// View products
|
||||||
|
$this->actingAs($this->user, 'sanctum')
|
||||||
|
->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson("/api/v1/products/{$product1->id}");
|
||||||
|
|
||||||
|
$this->actingAs($this->user, 'sanctum')
|
||||||
|
->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson("/api/v1/products/{$product2->id}");
|
||||||
|
|
||||||
|
// List viewed products
|
||||||
|
$response = $this->actingAs($this->user, 'sanctum')
|
||||||
|
->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson('/api/v1/products/viewed');
|
||||||
|
|
||||||
|
$response->assertOk()
|
||||||
|
->assertJsonStructure([
|
||||||
|
'data' => [
|
||||||
|
'*' => [
|
||||||
|
'id',
|
||||||
|
'name',
|
||||||
|
'slug',
|
||||||
|
'price_amount',
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertCount(2, $response->json('data'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('viewed products are sorted by most recently viewed', function () {
|
||||||
|
$product1 = ($this->createProduct)(['name' => 'First Viewed']);
|
||||||
|
$product2 = ($this->createProduct)(['name' => 'Second Viewed']);
|
||||||
|
$product3 = ($this->createProduct)(['name' => 'Third Viewed']);
|
||||||
|
|
||||||
|
// View sequence: 1 -> 2 -> 3 -> 1
|
||||||
|
// Expected order: 1, 3, 2
|
||||||
|
|
||||||
|
// View 1
|
||||||
|
$this->actingAs($this->user, 'sanctum')
|
||||||
|
->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson("/api/v1/products/{$product1->id}");
|
||||||
|
|
||||||
|
$this->travel(1)->minute();
|
||||||
|
|
||||||
|
// View 2
|
||||||
|
$this->actingAs($this->user, 'sanctum')
|
||||||
|
->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson("/api/v1/products/{$product2->id}");
|
||||||
|
|
||||||
|
$this->travel(1)->minute();
|
||||||
|
|
||||||
|
// View 3
|
||||||
|
$this->actingAs($this->user, 'sanctum')
|
||||||
|
->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson("/api/v1/products/{$product3->id}");
|
||||||
|
|
||||||
|
$this->travel(1)->minute();
|
||||||
|
|
||||||
|
// View 1 again
|
||||||
|
$this->actingAs($this->user, 'sanctum')
|
||||||
|
->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson("/api/v1/products/{$product1->id}");
|
||||||
|
|
||||||
|
$response = $this->actingAs($this->user, 'sanctum')
|
||||||
|
->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson('/api/v1/products/viewed');
|
||||||
|
|
||||||
|
$data = $response->json('data');
|
||||||
|
|
||||||
|
expect($data[0]['id'])->toBe($product1->id)
|
||||||
|
->and($data[1]['id'])->toBe($product3->id)
|
||||||
|
->and($data[2]['id'])->toBe($product2->id);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('unauthenticated user cannot list viewed products', function () {
|
||||||
|
$this->withHeaders(['Api-Token' => 'test-token'])
|
||||||
|
->getJson('/api/v1/products/viewed')
|
||||||
|
->assertStatus(401);
|
||||||
|
});
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Tests\Feature\Services\Order;
|
|
||||||
|
|
||||||
use App\Models\Ecommerce\Product\Cart\Cart;
|
|
||||||
use App\Models\Ecommerce\Product\Product\Product;
|
|
||||||
use App\Models\User;
|
|
||||||
use App\Services\Order\CreateOrderService;
|
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
||||||
use Illuminate\Support\Facades\Event;
|
|
||||||
use Tests\TestCase;
|
|
||||||
use App\Events\Ecommerce\Product\Order\OrderCreated;
|
|
||||||
|
|
||||||
class CreateOrderServiceTest extends TestCase
|
|
||||||
{
|
|
||||||
use RefreshDatabase;
|
|
||||||
|
|
||||||
public function test_it_creates_order_successfully()
|
|
||||||
{
|
|
||||||
Event::fake();
|
|
||||||
|
|
||||||
// 1. Arrange
|
|
||||||
$user = User::factory()->create();
|
|
||||||
$product = Product::factory()->create([
|
|
||||||
'stock' => 10,
|
|
||||||
'price_amount' => 100,
|
|
||||||
'cost_amount' => 50,
|
|
||||||
'name' => 'Test Product'
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Create Cart Item
|
|
||||||
Cart::factory()->create([
|
|
||||||
'user_id' => $user->id,
|
|
||||||
'product_id' => $product->id,
|
|
||||||
'product_quantity' => 2
|
|
||||||
]);
|
|
||||||
|
|
||||||
$service = new CreateOrderService();
|
|
||||||
$orderData = [
|
|
||||||
'user_id' => $user->id,
|
|
||||||
'status' => 'pending',
|
|
||||||
'total_amount' => 200
|
|
||||||
];
|
|
||||||
|
|
||||||
// 2. Act
|
|
||||||
$order = $service->execute($user, $orderData);
|
|
||||||
|
|
||||||
// 3. Assert
|
|
||||||
|
|
||||||
// Check Order Created
|
|
||||||
$this->assertDatabaseHas('orders', [
|
|
||||||
'id' => $order->id,
|
|
||||||
'user_id' => $user->id,
|
|
||||||
'total_amount' => 200
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Check Order Items Created
|
|
||||||
$this->assertDatabaseHas('order_items', [
|
|
||||||
'order_id' => $order->id,
|
|
||||||
'product_id' => $product->id,
|
|
||||||
'quantity' => 2,
|
|
||||||
'product_name' => 'Test Product'
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Check Stock Deducted (10 - 2 = 8)
|
|
||||||
$this->assertEquals(8, $product->fresh()->stock);
|
|
||||||
|
|
||||||
// Check Cart Cleared
|
|
||||||
$this->assertDatabaseMissing('carts', [
|
|
||||||
'user_id' => $user->id
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Check Event Dispatched
|
|
||||||
Event::assertDispatched(OrderCreated::class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user