Skip to content

Commit d964db3

Browse files
authored
Merge pull request #177 from fleetbase/dev-v1.6.28
v1.6.28 ~ Added ability to send verification SMS via org alpha numeri…
2 parents 831600c + 0dde589 commit d964db3

File tree

13 files changed

+199
-102
lines changed

13 files changed

+199
-102
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fleetbase/core-api",
3-
"version": "1.6.27",
3+
"version": "1.6.28",
44
"description": "Core Framework and Resources for Fleetbase API",
55
"keywords": [
66
"fleetbase",

migrations/2024_11_28_000001_add_performance_indexes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected function indexExists($table, $index)
8888
->listTableIndexes($table);
8989

9090
return isset($indexes[$index]);
91-
} catch (\Exception $e) {
91+
} catch (Exception $e) {
9292
return false;
9393
}
9494
}

src/Expansions/Builder.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ function ($query) use ($column, $search, $strict) {
4343
foreach ($column as $c) {
4444
if (Str::contains($c, '->')) {
4545
$jsonQueryPath = explode('->', $c);
46-
if (count($jsonQueryPath) !== 2) continue;
46+
if (count($jsonQueryPath) !== 2) {
47+
continue;
48+
}
4749

4850
[$column, $property] = $jsonQueryPath;
4951
$query->orWhere(DB::raw("lower(json_unquote(json_extract($column, '$.$property')))"), 'LIKE', '%' . str_replace('.', '%', str_replace(',', '%', $search)) . '%');
@@ -62,9 +64,12 @@ function ($query) use ($column, $search, $strict) {
6264

6365
if (Str::contains($column, '->')) {
6466
$jsonQueryPath = explode('->', $column);
65-
if (count($jsonQueryPath) !== 2) return;
67+
if (count($jsonQueryPath) !== 2) {
68+
return;
69+
}
6670

6771
[$column, $property] = $jsonQueryPath;
72+
6873
return $this->where(DB::raw("lower(json_unquote(json_extract($column, '$.$property')))"), 'LIKE', '%' . str_replace('.', '%', str_replace(',', '%', $search)) . '%');
6974
}
7075

src/Http/Controllers/Internal/v1/AuthController.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function login(LoginRequest $request)
103103
*/
104104
public function session(Request $request)
105105
{
106-
$token = $request->bearerToken();
106+
$token = $request->bearerToken();
107107
$cacheKey = "session_validation_{$token}";
108108

109109
// Cache session validation for 5 minutes
@@ -154,8 +154,6 @@ public function logout(Request $request)
154154
/**
155155
* Bootstrap endpoint - combines session, organizations, and installer status.
156156
*
157-
* @param Request $request
158-
*
159157
* @return \Illuminate\Http\Response
160158
*/
161159
public function bootstrap(Request $request)
@@ -630,11 +628,16 @@ public function getUserOrganizations(Request $request)
630628
$cacheKey = "user_organizations_{$user->uuid}";
631629

632630
// Cache for 30 minutes
633-
$companies = Cache::remember($cacheKey, now()->addMinutes(30), function () use ($user) {
634-
// Optimized query: use join instead of whereHas
631+
$companies = Cache::remember($cacheKey, 60 * 30, function () use ($user) {
635632
return Company::select([
636633
'companies.uuid',
637634
'companies.name',
635+
'companies.phone',
636+
'companies.options',
637+
'companies.currency',
638+
'companies.timezone',
639+
'companies.status',
640+
'companies.type',
638641
'companies.owner_uuid',
639642
'companies.created_at',
640643
'companies.updated_at',
@@ -643,21 +646,37 @@ public function getUserOrganizations(Request $request)
643646
->where('company_users.user_uuid', $user->uuid)
644647
->whereNull('company_users.deleted_at')
645648
->whereNotNull('companies.owner_uuid')
646-
->with(['owner:uuid,company_uuid,name,email', 'owner.companyUser:uuid,user_uuid,company_uuid'])
649+
->with([
650+
'owner:uuid,company_uuid,name,email',
651+
'owner.companyUser:uuid,user_uuid,company_uuid',
652+
])
647653
->distinct()
648654
->get();
649655
});
650656

657+
/**
658+
* Generate a full ETag representing:
659+
* - all org UUIDs
660+
* - all org updated_at timestamps
661+
* - count of organizations
662+
*/
663+
$etagPayload = $companies->map(function ($company) {
664+
return $company->uuid . ':' . $company->updated_at;
665+
})->join('|');
666+
667+
// Add count to ETag (if orgs added/removed)
668+
$etagPayload .= '|count:' . $companies->count();
669+
$etag = sha1($etagPayload);
670+
651671
return Organization::collection($companies)
652672
->response()
653-
->header('Cache-Control', 'private, max-age=1800'); // 30 minutes
673+
->setEtag($etag)
674+
->header('Cache-Control', 'private, max-age=1800, must-revalidate');
654675
}
655676

656677
/**
657678
* Clear user organizations cache (call when org changes).
658679
*
659-
* @param string $userUuid
660-
*
661680
* @return void
662681
*/
663682
public static function clearUserOrganizationsCache(string $userUuid)

src/Http/Controllers/Internal/v1/InstallerController.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ public function initialize()
3434

3535
/**
3636
* Check installation status.
37-
*
38-
* @return array
3937
*/
4038
protected function checkInstallationStatus(): array
4139
{

src/Http/Controllers/Internal/v1/LookupController.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@ public function country($code, Request $request)
182182
/**
183183
* Pull the Fleetbase.io blog RSS feed with aggressive caching.
184184
*
185-
* @param Request $request
186-
*
187185
* @return \Illuminate\Http\Response
188186
*/
189187
public function fleetbaseBlog(Request $request)
@@ -211,10 +209,6 @@ public function fleetbaseBlog(Request $request)
211209

212210
/**
213211
* Fetch blog posts from RSS feed.
214-
*
215-
* @param int $limit
216-
*
217-
* @return array
218212
*/
219213
protected function fetchBlogPosts(int $limit): array
220214
{

src/Http/Middleware/PerformanceMonitoring.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@
22

33
namespace Fleetbase\Http\Middleware;
44

5-
use Closure;
65
use Illuminate\Http\Request;
76
use Illuminate\Support\Facades\Log;
87

98
class PerformanceMonitoring
109
{
1110
/**
1211
* Handle an incoming request and log performance metrics.
13-
*
14-
* @param \Illuminate\Http\Request $request
15-
* @param \Closure $next
16-
*
17-
* @return mixed
1812
*/
19-
public function handle(Request $request, Closure $next)
13+
public function handle(Request $request, \Closure $next)
2014
{
2115
$startTime = microtime(true);
2216
$startMemory = memory_get_usage();

src/Http/Resources/Organization.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Fleetbase\Models\Setting;
66
use Fleetbase\Support\Http;
7+
use Fleetbase\Support\Utils;
78

89
class Organization extends FleetbaseResource
910
{
@@ -32,7 +33,7 @@ public function toArray($request)
3233
'logo_url' => $this->logo_url,
3334
'backdrop_url' => $this->backdrop_url,
3435
'branding' => Setting::getBranding(),
35-
'options' => $this->options,
36+
'options' => $this->options ?? Utils::createObject([]),
3637
'owner' => $this->owner ? new User($this->owner) : null,
3738
'slug' => $this->slug,
3839
'status' => $this->status,

src/Models/VerificationCode.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,24 @@ public static function generateSmsVerificationFor($subject, $for = 'phone_verifi
180180
$message = $messageCallback($verificationCode);
181181
}
182182

183+
// Twilio params
184+
$twilioParams = [];
185+
186+
if ($companyUuid = session('company')) {
187+
$company = Company::select(['uuid', 'options'])->find($companyUuid);
188+
189+
if ($company) {
190+
$enabled = Utils::castBoolean($company->getOption('alpha_numeric_sender_id_enabled'));
191+
$senderId = $company->getOption('alpha_numeric_sender_id');
192+
193+
if ($enabled && !empty($senderId)) {
194+
$twilioParams['from'] = $senderId;
195+
}
196+
}
197+
}
198+
183199
if ($subject->phone) {
184-
Twilio::message($subject->phone, $message);
200+
Twilio::message($subject->phone, $message, [], $twilioParams);
185201
}
186202

187203
return $verificationCode;

src/Observers/CompanyObserver.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Fleetbase\Observers;
4+
5+
use Fleetbase\Models\Company;
6+
use Illuminate\Support\Facades\Cache;
7+
8+
class CompanyObserver
9+
{
10+
/**
11+
* Clear org caches for all users of this company.
12+
*
13+
* @param \App\Models\Company $company
14+
*/
15+
protected function clearUserOrganizationCache(Company $company)
16+
{
17+
// Ensure company_users relationship is loaded
18+
$company->loadMissing(['users:uuid']);
19+
20+
foreach ($company->users as $user) {
21+
Cache::forget("user_organizations_{$user->uuid}");
22+
}
23+
}
24+
25+
/**
26+
* Handle the Company "created" event.
27+
*/
28+
public function created(Company $company)
29+
{
30+
$this->clearUserOrganizationCache($company);
31+
}
32+
33+
/**
34+
* Handle the Company "updated" event.
35+
*/
36+
public function updated(Company $company)
37+
{
38+
$this->clearUserOrganizationCache($company);
39+
}
40+
41+
/**
42+
* Handle the Company "deleted" event.
43+
*/
44+
public function deleted(Company $company)
45+
{
46+
$this->clearUserOrganizationCache($company);
47+
}
48+
49+
/**
50+
* Handle the Company "restored" event.
51+
*/
52+
public function restored(Company $company)
53+
{
54+
$this->clearUserOrganizationCache($company);
55+
}
56+
57+
/**
58+
* Handle the Company "force deleted" event.
59+
*/
60+
public function forceDeleted(Company $company)
61+
{
62+
$this->clearUserOrganizationCache($company);
63+
}
64+
}

0 commit comments

Comments
 (0)