Skip to content

Commit e799c5a

Browse files
authored
Merge pull request #186 from fleetbase/dev-v1.6.34
feat: fix basic api paging + offset + page params + added meta/option…
2 parents 5ccf94e + 55b990b commit e799c5a

File tree

11 files changed

+228
-76
lines changed

11 files changed

+228
-76
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.33",
3+
"version": "1.6.34",
44
"description": "Core Framework and Resources for Fleetbase API",
55
"keywords": [
66
"fleetbase",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
/**
9+
* Run the migrations.
10+
*/
11+
public function up(): void
12+
{
13+
// Add options column to users table
14+
Schema::table('users', function (Blueprint $table) {
15+
if (!Schema::hasColumn('users', 'options')) {
16+
$table->json('options')->nullable()->after('meta');
17+
}
18+
});
19+
20+
// Add meta column to companies table
21+
Schema::table('companies', function (Blueprint $table) {
22+
if (!Schema::hasColumn('companies', 'meta')) {
23+
$table->json('meta')->nullable()->after('options');
24+
}
25+
});
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*/
31+
public function down(): void
32+
{
33+
// Remove options column from users table
34+
Schema::table('users', function (Blueprint $table) {
35+
if (Schema::hasColumn('users', 'options')) {
36+
$table->dropColumn('options');
37+
}
38+
});
39+
40+
// Remove meta column from companies table
41+
Schema::table('companies', function (Blueprint $table) {
42+
if (Schema::hasColumn('companies', 'meta')) {
43+
$table->dropColumn('meta');
44+
}
45+
});
46+
}
47+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
/**
9+
* Run the migrations.
10+
*/
11+
public function up(): void
12+
{
13+
Schema::table('companies', function (Blueprint $table) {
14+
$table->timestamp('onboarding_completed_at')->nullable()->after('updated_at');
15+
$table->string('onboarding_completed_by_uuid')->nullable()->after('onboarding_completed_at');
16+
$table->index('onboarding_completed_at');
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*/
23+
public function down(): void
24+
{
25+
Schema::table('companies', function (Blueprint $table) {
26+
$table->dropIndex(['onboarding_completed_at']);
27+
$table->dropColumn(['onboarding_completed_at', 'onboarding_completed_by_uuid']);
28+
});
29+
}
30+
};

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ public function createAccount(OnboardRequest $request)
5757
'last_login' => $isAdmin ? now() : null,
5858
]);
5959

60+
// create company FIRST (required for billing resource tracking)
61+
$company = Company::create(['name' => $request->input('organization_name')]);
62+
63+
// set company_uuid before creating user (required for billing resource tracking)
64+
$attributes['company_uuid'] = $company->uuid;
65+
6066
// create user account
6167
$user = User::create($attributes);
6268

@@ -66,8 +72,7 @@ public function createAccount(OnboardRequest $request)
6672
// set the user type
6773
$user->setUserType($isAdmin ? 'admin' : 'user');
6874

69-
// create company
70-
$company = new Company(['name' => $request->input('organization_name')]);
75+
// set company owner
7176
$company->setOwner($user)->save();
7277

7378
// assign user to organization
@@ -224,6 +229,15 @@ public function verifyEmail(Request $request)
224229
$user->updateLastLogin();
225230
$token = $user->createToken($user->uuid);
226231

232+
// Mark company onboarding as complete (email verification is final step)
233+
$company = $user->company;
234+
if ($company && $company->onboarding_completed_at === null) {
235+
$company->update([
236+
'onboarding_completed_at' => $verifiedAt,
237+
'onboarding_completed_by_uuid' => $user->uuid,
238+
]);
239+
}
240+
227241
return response()->json([
228242
'status' => 'ok',
229243
'verified_at' => $verifiedAt,

src/Http/Resources/Organization.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,27 @@ class Organization extends FleetbaseResource
1818
public function toArray($request)
1919
{
2020
return [
21-
'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
22-
'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
23-
'owner_uuid' => $this->when(Http::isInternalRequest(), $this->owner_uuid),
24-
'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
25-
'name' => $this->name,
26-
'description' => $this->description,
27-
'phone' => $this->phone,
28-
'type' => $this->when(Http::isInternalRequest(), $this->type),
29-
'users_count' => $this->when(Http::isInternalRequest(), $this->companyUsers()->count()),
30-
'timezone' => $this->timezone,
31-
'country' => $this->country,
32-
'currency' => $this->currency,
33-
'logo_url' => $this->logo_url,
34-
'backdrop_url' => $this->backdrop_url,
35-
'branding' => Setting::getBranding(),
36-
'options' => $this->options ?? Utils::createObject([]),
37-
'owner' => $this->owner ? new User($this->owner) : null,
38-
'slug' => $this->slug,
39-
'status' => $this->status,
40-
'joined_at' => $this->when(Http::isInternalRequest() && $request->hasSession() && $request->session()->has('user'), function () {
21+
'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
22+
'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
23+
'owner_uuid' => $this->when(Http::isInternalRequest(), $this->owner_uuid),
24+
'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
25+
'name' => $this->name,
26+
'description' => $this->description,
27+
'phone' => $this->phone,
28+
'type' => $this->when(Http::isInternalRequest(), $this->type),
29+
'users_count' => $this->when(Http::isInternalRequest(), $this->companyUsers()->count()),
30+
'timezone' => $this->timezone,
31+
'country' => $this->country,
32+
'currency' => $this->currency,
33+
'logo_url' => $this->logo_url,
34+
'backdrop_url' => $this->backdrop_url,
35+
'branding' => Setting::getBranding(),
36+
'options' => $this->options ?? Utils::createObject([]),
37+
'owner' => $this->owner ? new User($this->owner) : null,
38+
'slug' => $this->slug,
39+
'status' => $this->status,
40+
'onboarding_completed' => $this->when(Http::isInternalRequest(), $this->onboarding_completed_at !== null),
41+
'joined_at' => $this->when(Http::isInternalRequest() && $request->hasSession() && $request->session()->has('user'), function () {
4142
if ($this->resource->joined_at) {
4243
return $this->resource->joined_at;
4344
}

src/Http/Resources/User.php

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,41 @@ class User extends FleetbaseResource
1818
public function toArray($request)
1919
{
2020
$data = [
21-
'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
22-
'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
23-
'company_uuid' => $this->when(Http::isInternalRequest(), $this->company_uuid),
24-
'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
25-
'company' => $this->when(Http::isPublicRequest(), $this->company ? $this->company->public_id : null),
26-
'name' => $this->name,
27-
'username' => $this->username,
28-
'email' => $this->email,
29-
'phone' => $this->phone,
30-
'country' => $this->country,
31-
'timezone' => $this->timezone,
32-
'avatar_url' => $this->avatar_url,
33-
'meta' => data_get($this, 'meta', Utils::createObject()),
34-
'role' => $this->when(Http::isInternalRequest(), new Role($this->role), null),
35-
'policies' => $this->when(Http::isInternalRequest(), Policy::collection($this->policies), []),
36-
'permissions' => $this->when(Http::isInternalRequest(), $this->serializePermissions($this->permissions), []),
37-
'role_name' => $this->when(Http::isInternalRequest(), $this->role ? $this->role->name : null),
38-
'type' => $this->type,
39-
'locale' => $this->getLocale(),
40-
'types' => $this->when(Http::isInternalRequest(), $this->types ?? []),
41-
'company_name' => $this->when(Http::isInternalRequest(), $this->company_name),
42-
'session_status' => $this->when(Http::isInternalRequest(), $this->session_status),
43-
'is_admin' => $this->when(Http::isInternalRequest(), $this->is_admin),
44-
'is_online' => $this->is_online,
45-
'ip_address' => $this->ip_address,
46-
'date_of_birth' => $this->date_of_birth,
47-
'email_verified_at' => $this->email_verified_at,
48-
'phone_verified_at' => $this->phone_verified_at,
49-
'last_seen_at' => $this->last_seen_at,
50-
'last_login' => $this->last_login,
51-
'status' => $this->status,
52-
'slug' => $this->slug,
53-
'updated_at' => $this->updated_at,
54-
'created_at' => $this->created_at,
21+
'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
22+
'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
23+
'company_uuid' => $this->when(Http::isInternalRequest(), $this->company_uuid),
24+
'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
25+
'company' => $this->when(Http::isPublicRequest(), $this->company ? $this->company->public_id : null),
26+
'name' => $this->name,
27+
'username' => $this->username,
28+
'email' => $this->email,
29+
'phone' => $this->phone,
30+
'country' => $this->country,
31+
'timezone' => $this->timezone,
32+
'avatar_url' => $this->avatar_url,
33+
'meta' => data_get($this, 'meta', Utils::createObject()),
34+
'role' => $this->when(Http::isInternalRequest(), new Role($this->role), null),
35+
'policies' => $this->when(Http::isInternalRequest(), Policy::collection($this->policies), []),
36+
'permissions' => $this->when(Http::isInternalRequest(), $this->serializePermissions($this->permissions), []),
37+
'role_name' => $this->when(Http::isInternalRequest(), $this->role ? $this->role->name : null),
38+
'type' => $this->type,
39+
'locale' => $this->getLocale(),
40+
'types' => $this->when(Http::isInternalRequest(), $this->types ?? []),
41+
'company_name' => $this->when(Http::isInternalRequest(), $this->company_name),
42+
'company_onboarding_completed' => $this->when(Http::isInternalRequest(), $this->company_onboarding_completed),
43+
'session_status' => $this->when(Http::isInternalRequest(), $this->session_status),
44+
'is_admin' => $this->when(Http::isInternalRequest(), $this->is_admin),
45+
'is_online' => $this->is_online,
46+
'ip_address' => $this->ip_address,
47+
'date_of_birth' => $this->date_of_birth,
48+
'email_verified_at' => $this->email_verified_at,
49+
'phone_verified_at' => $this->phone_verified_at,
50+
'last_seen_at' => $this->last_seen_at,
51+
'last_login' => $this->last_login,
52+
'status' => $this->status,
53+
'slug' => $this->slug,
54+
'updated_at' => $this->updated_at,
55+
'created_at' => $this->created_at,
5556
];
5657

5758
return ResourceTransformerRegistry::transform($this->resource, $data);

src/Models/Company.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Fleetbase\Casts\Json;
66
use Fleetbase\FleetOps\Models\Driver;
77
use Fleetbase\Traits\HasApiModelBehavior;
8+
use Fleetbase\Traits\HasMetaAttributes;
89
use Fleetbase\Traits\HasOptionsAttributes;
910
use Fleetbase\Traits\HasPublicId;
1011
use Fleetbase\Traits\HasUuid;
@@ -28,6 +29,7 @@ class Company extends Model
2829
use TracksApiCredential;
2930
use HasApiModelBehavior;
3031
use HasOptionsAttributes;
32+
use HasMetaAttributes;
3133
use HasSlug;
3234
use Searchable;
3335
use SendsWebhooks;
@@ -86,6 +88,7 @@ class Company extends Model
8688
'website_url',
8789
'description',
8890
'options',
91+
'meta',
8992
'type',
9093
'currency',
9194
'country',
@@ -95,6 +98,8 @@ class Company extends Model
9598
'status',
9699
'slug',
97100
'trial_ends_at',
101+
'onboarding_completed_at',
102+
'onboarding_completed_by_uuid',
98103
];
99104

100105
/**
@@ -117,8 +122,10 @@ class Company extends Model
117122
* @var array
118123
*/
119124
protected $casts = [
120-
'options' => Json::class,
121-
'trial_ends_at' => 'datetime',
125+
'options' => Json::class,
126+
'meta' => Json::class,
127+
'trial_ends_at' => 'datetime',
128+
'onboarding_completed_at' => 'datetime',
122129
];
123130

124131
/**

src/Models/User.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Fleetbase\Traits\HasApiModelBehavior;
1515
use Fleetbase\Traits\HasCacheableAttributes;
1616
use Fleetbase\Traits\HasMetaAttributes;
17+
use Fleetbase\Traits\HasOptionsAttributes;
1718
use Fleetbase\Traits\HasPresence;
1819
use Fleetbase\Traits\HasPublicId;
1920
use Fleetbase\Traits\HasSessionAttributes;
@@ -53,6 +54,7 @@ class User extends Authenticatable
5354
use HasApiModelBehavior;
5455
use HasCacheableAttributes;
5556
use HasMetaAttributes;
57+
use HasOptionsAttributes;
5658
use HasTimestamps;
5759
use LogsActivity;
5860
use CausesActivity;
@@ -149,6 +151,7 @@ class User extends Authenticatable
149151
'date_of_birth',
150152
'timezone',
151153
'meta',
154+
'options',
152155
'country',
153156
'ip_address',
154157
'last_login',
@@ -181,6 +184,7 @@ class User extends Authenticatable
181184
'avatar_url',
182185
'session_status',
183186
'company_name',
187+
'company_onboarding_completed',
184188
'is_admin',
185189
'is_online',
186190
'last_seen_at',
@@ -192,10 +196,11 @@ class User extends Authenticatable
192196
* @var array
193197
*/
194198
protected $casts = [
195-
'meta' => Json::class,
196-
'email_verified_at' => 'datetime',
197-
'phone_verified_at' => 'datetime',
198-
'last_login' => 'datetime',
199+
'meta' => Json::class,
200+
'options' => Json::class,
201+
'email_verified_at' => 'datetime',
202+
'phone_verified_at' => 'datetime',
203+
'last_login' => 'datetime',
199204
];
200205

201206
/**
@@ -749,6 +754,14 @@ public function getCompanyNameAttribute(): ?string
749754
return data_get($this, 'company.name');
750755
}
751756

757+
/**
758+
* Get the users's company onboard completed.
759+
*/
760+
public function getCompanyOnboardingCompletedAttribute(): bool
761+
{
762+
return data_get($this, 'company.onboarding_completed_at') !== null;
763+
}
764+
752765
/**
753766
* Get the users's company name.
754767
*/

src/Services/UserCacheService.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ public static function invalidateCompany(string $companyId): void
197197
*/
198198
public static function generateETag(User $user): string
199199
{
200-
return '"user-' . $user->uuid . '-' . $user->updated_at->timestamp . '"';
200+
$userMeta = json_encode($user->meta);
201+
$userOptions = json_encode($user->options);
202+
203+
return '"user-' . $user->uuid . '-' . $user->updated_at->timestamp . '-' . strlen($userMeta) . '-' . strlen($userOptions) . '"';
201204
}
202205

203206
/**

0 commit comments

Comments
 (0)