Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 1 addition & 24 deletions app/Http/Controllers/WidgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Controllers;

use App\Models\Item;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Settings\WidgetSettings;
Expand Down Expand Up @@ -102,36 +101,14 @@ public function submit(Request $request): JsonResponse
return response()->json(['errors' => $validator->errors()], 422);
}

// Only create/find user if email is provided
$user = null;
if ($request->filled('email')) {
$user = User::firstOrCreate(
['email' => $request->input('email')],
[
'name' => $request->input('name', 'Widget User'),
'password' => bcrypt(str()->random(32)),
]
);
}

// Temporarily set the user for this request without triggering login events
if ($user) {
auth()->setUser($user);
}

// Create item
$item = Item::create([
'title' => $request->input('title'),
'content' => $request->input('content'),
'user_id' => $user?->id,
'user_id' => null,
'private' => false,
]);

// Automatically upvote the item for the user
if ($user) {
$item->toggleUpvote($user);
}

return response()->json([
'success' => true,
'message' => 'Feedback submitted successfully',
Expand Down
16 changes: 9 additions & 7 deletions tests/Feature/Widget/WidgetTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Models\Item;
use App\Models\User;
use App\Settings\WidgetSettings;
use function Pest\Laravel\assertDatabaseHas;

Expand Down Expand Up @@ -182,7 +183,9 @@
->toContain("document.documentElement.classList.contains('dark')");
});

test('widget submission automatically upvotes item for user', function () {
test('widget email does not assign an existing user or create a vote', function () {
$user = User::factory()->create(['email' => '[email protected]']);

$response = $this->postJson('/api/widget/submit', [
'title' => 'Test Feedback with Vote',
'content' => 'This feedback should have an automatic upvote',
Expand All @@ -195,11 +198,12 @@
$item = Item::where('title', 'Test Feedback with Vote')->first();

expect($item)->not->toBeNull()
->and($item->votes()->count())->toBe(1)
->and($item->votes()->first()->user->email)->toBe('[email protected]');
->and($item->user_id)->toBeNull()
->and($item->votes()->count())->toBe(0)
->and(User::find($user->id))->not->toBeNull();
});

test('widget submission creates activity log with correct user', function () {
test('widget email does not assign an activity causer', function () {
$response = $this->postJson('/api/widget/submit', [
'title' => 'Test Activity Log',
'content' => 'This should have correct user in activity log',
Expand All @@ -214,7 +218,5 @@

expect($item)->not->toBeNull()
->and($activity)->not->toBeNull()
->and($activity->causer)->not->toBeNull()
->and($activity->causer->email)->toBe('[email protected]')
->and($activity->causer->name)->toBe('Activity User');
->and($activity->causer)->toBeNull();
});
Loading