Skip to content

Commit f7614c6

Browse files
committed
Test and workflow
1 parent 5ead329 commit f7614c6

File tree

125 files changed

+1227
-1093
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+1227
-1093
lines changed

.github/workflows/lint.yml

Lines changed: 0 additions & 45 deletions
This file was deleted.

.github/workflows/tests.yml

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,49 @@
1-
name: tests
1+
name: Tests
22

33
on:
44
push:
5-
branches:
6-
- develop
7-
- main
5+
branches: [ main ]
86
pull_request:
9-
branches:
10-
- develop
11-
- main
7+
branches: [ main ]
128

139
jobs:
14-
ci:
10+
test:
1511
runs-on: ubuntu-latest
1612

1713
steps:
18-
- name: Checkout
19-
uses: actions/checkout@v4
14+
- uses: actions/checkout@v4
2015

21-
- name: Setup PHP
22-
uses: shivammathur/setup-php@v2
23-
with:
24-
php-version: 8.4
25-
tools: composer:v2
26-
coverage: xdebug
16+
- name: Setup PHP
17+
uses: shivammathur/setup-php@v2
18+
with:
19+
php-version: '8.4'
20+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv
2721

28-
- name: Setup Node
29-
uses: actions/setup-node@v4
30-
with:
31-
node-version: '22'
32-
cache: 'npm'
22+
- name: Setup Node.js
23+
uses: actions/setup-node@v4
24+
with:
25+
node-version: '20'
3326

34-
- name: Install Node Dependencies
35-
run: npm ci
27+
- name: Copy .env
28+
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
3629

37-
- name: Install Dependencies
38-
run: composer install --no-interaction --prefer-dist --optimize-autoloader
30+
- name: Install PHP Dependencies
31+
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
3932

40-
- name: Copy Environment File
41-
run: cp .env.example .env
33+
- name: Install Node Dependencies
34+
run: npm ci
4235

43-
- name: Generate Application Key
44-
run: php artisan key:generate
36+
- name: Generate key
37+
run: php artisan key:generate
4538

46-
- name: Publish Ziggy Configuration
47-
run: php artisan ziggy:generate
39+
- name: Run Laravel Pint
40+
run: vendor/bin/pint --test
4841

49-
- name: Build Assets
50-
run: npm run build
42+
- name: PHP Lint
43+
run: find app tests -name "*.php" -print0 | xargs -0 -n1 php -l
5144

52-
- name: Tests
53-
run: ./vendor/bin/pest
45+
- name: Run ESLint
46+
run: npm run lint
47+
48+
- name: Run Tests
49+
run: php artisan test

app/Http/Controllers/Auth/ConfirmablePasswordController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ public function store(Request $request): RedirectResponse
3636

3737
$request->session()->put('auth.password_confirmed_at', time());
3838

39-
return redirect()->intended(route('dashboard', absolute: false));
39+
return redirect()->intended(route('app.dashboard', absolute: false));
4040
}
4141
}

app/Http/Controllers/Public/HomeController.php

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,21 @@
55
use App\Http\Controllers\Controller;
66
use App\Models\Category;
77
use App\Models\Equipment;
8+
use App\Services\LocationPreferencesService;
89
use Illuminate\Http\Request;
9-
use Illuminate\Support\Facades\Cache;
1010
use Inertia\Inertia;
1111

1212
class HomeController extends Controller
1313
{
14+
public function __construct(
15+
private readonly LocationPreferencesService $locationService
16+
) {}
17+
1418
public function index(Request $request)
1519
{
16-
17-
if ($request->user()) {
18-
$cacheKey = 'user_preferences_'.$request->user()->id;
19-
$userPreferences = Cache::get($cacheKey);
20-
$locationData = $request->only(['coordinates', 'radius', 'city', 'postcode']);
21-
22-
if ($this->hasValidLocationData($request) && $this->shouldUpdatePreferences($userPreferences, $locationData)) {
23-
$request->user()->update(['preferences' => $locationData]);
24-
Cache::put($cacheKey, $locationData, now()->addDays(30));
25-
$userPreferences = $locationData;
26-
} else {
27-
$userPreferences = null;
28-
}
29-
} else {
30-
$userPreferences = $this->hasValidLocationData($request) ? $request->only(['coordinates', 'radius', 'city', 'postcode']) : null;
31-
}
20+
// Handle location preferences
21+
$locationPreferences = $this->locationService->updateLocationPreferences($request)
22+
?? $this->locationService->getLocationPreferences($request);
3223

3324
$query = Equipment::query()
3425
->select([
@@ -55,63 +46,47 @@ public function index(Request $request)
5546
$query->where('name', 'like', '%'.$request->search.'%');
5647
}
5748

58-
if ($request->filled('category')) {
59-
if ($request->category !== 'all') {
60-
$query->where('category_id', $request->category);
61-
}
49+
if ($request->filled('category') && $request->category !== 'all') {
50+
$query->where('category_id', $request->category);
6251
}
6352

64-
if ($userPreferences && $userPreferences['coordinates'] && $userPreferences['radius']) {
65-
$location = $userPreferences['coordinates'];
66-
$query->whereHas('depot', function ($query) use ($location, $userPreferences) {
53+
// Apply location filter if preferences exist
54+
if ($locationPreferences && isset($locationPreferences['coordinates'], $locationPreferences['radius'])) {
55+
$location = $locationPreferences['coordinates'];
56+
$query->whereHas('depot', function ($query) use ($location, $locationPreferences) {
6757
$query->whereRaw('ST_Distance_Sphere(
6858
POINT(depots.longitude, depots.latitude),
6959
POINT(?, ?)
7060
) <= ?', [
7161
$location['lng'],
7262
$location['lat'],
73-
$userPreferences['radius'] * 1000, // Convert km to meters
63+
$locationPreferences['radius'] * 1000, // Convert km to meters
7464
]);
7565
});
7666
}
7767

78-
// Availability
68+
// Availability filter
7969
if ($request->filled('start_date') && $request->filled('end_date')) {
8070
$query->whereDoesntHave('reservations', function ($query) use ($request) {
8171
$query->whereBetween('start_date', [$request->start_date, $request->end_date]);
8272
});
8373
}
8474

85-
$equipments = $query->paginate(12);
86-
87-
// Get stats for filters
88-
$stats = [
89-
'categories' => Category::select(['id', 'name'])->orderBy('name')->get(),
90-
];
91-
9275
return Inertia::render('Public/Home', [
93-
'equipments' => $equipments,
76+
'equipments' => $query->paginate(12),
9477
'filters' => [
9578
'search' => $request->search,
9679
'category' => $request->category,
9780
'start_date' => $request->start_date,
9881
'end_date' => $request->end_date,
99-
'coordinates' => $userPreferences['coordinates'] ?? null,
100-
'radius' => $userPreferences['radius'] ?? null,
101-
'city' => $userPreferences['city'] ?? null,
102-
'postcode' => $userPreferences['postcode'] ?? null,
82+
'coordinates' => $locationPreferences['coordinates'] ?? null,
83+
'radius' => $locationPreferences['radius'] ?? null,
84+
'city' => $locationPreferences['city'] ?? null,
85+
'postcode' => $locationPreferences['postcode'] ?? null,
86+
],
87+
'stats' => [
88+
'categories' => Category::select(['id', 'name'])->orderBy('name')->get(),
10389
],
104-
'stats' => $stats,
10590
]);
10691
}
107-
108-
private function hasValidLocationData(Request $request)
109-
{
110-
return $request->filled('coordinates') && $request->filled('radius') && $request->filled('city') && $request->filled('postcode');
111-
}
112-
113-
private function shouldUpdatePreferences($userPreferences, $locationData)
114-
{
115-
return ! $userPreferences || $userPreferences['coordinates'] !== $locationData['coordinates'] || $userPreferences['radius'] !== $locationData['radius'] || $userPreferences['city'] !== $locationData['city'] || $userPreferences['postcode'] !== $locationData['postcode'];
116-
}
11792
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use App\Models\User;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Support\Facades\Cache;
8+
9+
class LocationPreferencesService
10+
{
11+
private const GUEST_CACHE_TTL = 86400; // 24 hours
12+
13+
private const USER_CACHE_TTL = 2592000; // 30 days
14+
15+
private const REQUIRED_LOCATION_FIELDS = ['coordinates', 'radius', 'city', 'postcode'];
16+
17+
public function getLocationPreferences(Request $request): ?array
18+
{
19+
if ($request->user()) {
20+
return $this->getAuthenticatedUserPreferences($request->user());
21+
}
22+
23+
return $this->getGuestPreferences($request);
24+
}
25+
26+
public function updateLocationPreferences(Request $request): ?array
27+
{
28+
$locationData = $request->only(self::REQUIRED_LOCATION_FIELDS);
29+
30+
if (! $this->hasValidLocationData($locationData)) {
31+
return null;
32+
}
33+
34+
if ($request->user()) {
35+
return $this->updateAuthenticatedUserPreferences($request->user(), $locationData);
36+
}
37+
38+
return $this->updateGuestPreferences($locationData);
39+
}
40+
41+
private function getAuthenticatedUserPreferences(User $user): ?array
42+
{
43+
$cacheKey = $this->getUserCacheKey($user);
44+
45+
return Cache::remember($cacheKey, self::USER_CACHE_TTL, function () use ($user) {
46+
return $user->preferences;
47+
});
48+
}
49+
50+
private function getGuestPreferences(Request $request): ?array
51+
{
52+
$cacheKey = $this->getGuestCacheKey($request);
53+
54+
return Cache::get($cacheKey);
55+
}
56+
57+
private function updateAuthenticatedUserPreferences(User $user, array $locationData): array
58+
{
59+
$user->update(['preferences' => $locationData]);
60+
61+
$cacheKey = $this->getUserCacheKey($user);
62+
Cache::put($cacheKey, $locationData, self::USER_CACHE_TTL);
63+
64+
return $locationData;
65+
}
66+
67+
private function updateGuestPreferences(array $locationData): array
68+
{
69+
$cacheKey = $this->getGuestCacheKey(request());
70+
Cache::put($cacheKey, $locationData, self::GUEST_CACHE_TTL);
71+
72+
return $locationData;
73+
}
74+
75+
private function hasValidLocationData(?array $locationData): bool
76+
{
77+
if (! $locationData) {
78+
return false;
79+
}
80+
// check if latitude and longitude exists and are not null
81+
if (! isset($locationData['coordinates']['lat']) || ! isset($locationData['coordinates']['lng'])) {
82+
return false;
83+
}
84+
85+
return collect(self::REQUIRED_LOCATION_FIELDS)
86+
->every(fn ($field) => ! empty($locationData[$field]));
87+
}
88+
89+
private function getUserCacheKey(User $user): string
90+
{
91+
return "user_location_preferences_{$user->id}";
92+
}
93+
94+
private function getGuestCacheKey(Request $request): string
95+
{
96+
return 'guest_location_preferences_'.md5($request->ip());
97+
}
98+
}

database/factories/CategoryFactory.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use App\Models\Category;
66
use Illuminate\Database\Eloquent\Factories\Factory;
7-
use Illuminate\Support\Str;
87

98
/**
109
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Category>
@@ -28,8 +27,7 @@ public function definition(): array
2827
$name = $this->faker->unique()->words(rand(1, 3), true);
2928

3029
return [
31-
'name' => ucwords($name),
32-
'slug' => Str::slug($name),
30+
'name' => $name,
3331
'description' => $this->faker->optional(0.7)->sentence(),
3432
'parent_id' => null,
3533
'order' => $this->faker->numberBetween(0, 100),

0 commit comments

Comments
 (0)