Skip to content

Commit bb1d8a4

Browse files
committed
Ran linter and fixed invite flow
1 parent d5ffc7a commit bb1d8a4

18 files changed

Lines changed: 124 additions & 149 deletions

migrations/2026_04_05_000001_add_scheduled_status_to_schedule_items_table.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
* 'pending' is retained for backwards compatibility (e.g. manually-created items
1414
* that have not yet been confirmed).
1515
*/
16-
return new class extends Migration
17-
{
16+
return new class extends Migration {
1817
public function up(): void
1918
{
2019
DB::statement("

migrations/2026_04_06_000002_add_company_uuid_to_schedule_items_table.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
5-
use Illuminate\Support\Facades\Schema;
65
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\Facades\Schema;
77

88
return new class extends Migration {
99
public function up(): void
@@ -13,13 +13,13 @@ public function up(): void
1313
});
1414

1515
// Backfill from the parent schedule
16-
DB::statement("
16+
DB::statement('
1717
UPDATE schedule_items si
1818
JOIN schedules s ON s.uuid = si.schedule_uuid
1919
SET si.company_uuid = s.company_uuid
2020
WHERE si.company_uuid IS NULL
2121
AND si.schedule_uuid IS NOT NULL
22-
");
22+
');
2323
}
2424

2525
public function down(): void

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ class ScheduleExceptionController extends FleetbaseController
1919

2020
/**
2121
* The ScheduleService instance.
22-
*
23-
* @var ScheduleService
2422
*/
2523
protected ScheduleService $scheduleService;
2624

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class ScheduleTemplateController extends FleetbaseController
2020

2121
/**
2222
* The ScheduleService instance.
23-
*
24-
* @var ScheduleService
2523
*/
2624
protected ScheduleService $scheduleService;
2725

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,17 @@ public function inviteUser(InviteUserRequest $request)
372372

373373
$user = User::create($data);
374374

375+
// Set user type
376+
$user->setUserType('user');
377+
378+
// Assign to user
379+
$user->assignCompany($company, $request->input('user.role_uuid'));
380+
381+
// Assign role if set
382+
if ($request->filled('user.role_uuid')) {
383+
$user->assignSingleRole($request->input('user.role_uuid'));
384+
}
385+
375386
$invitation = Invite::create([
376387
'company_uuid' => $company->uuid,
377388
'created_by_uuid' => session('user'),
@@ -381,6 +392,7 @@ public function inviteUser(InviteUserRequest $request)
381392
'recipients' => [$user->email],
382393
'reason' => 'join_company',
383394
'meta' => array_filter(['role_uuid' => $request->input('user.role_uuid') ?? $request->input('user.role')]),
395+
'expires_at' => now()->addHours(48),
384396
]);
385397

386398
$user->notify(new UserInvited($invitation));
@@ -397,10 +409,8 @@ public function inviteUser(InviteUserRequest $request)
397409
* (the dedicated invite endpoint). Keeping the logic in one place ensures
398410
* both paths behave identically.
399411
*
400-
* @param User $user The existing user to invite.
401-
* @param Request $request The originating HTTP request.
402-
*
403-
* @return \Illuminate\Http\JsonResponse
412+
* @param User $user the existing user to invite
413+
* @param Request $request the originating HTTP request
404414
*/
405415
private function inviteExistingUser(User $user, Request $request): \Illuminate\Http\JsonResponse
406416
{
@@ -424,6 +434,7 @@ private function inviteExistingUser(User $user, Request $request): \Illuminate\H
424434
'recipients' => [$user->email],
425435
'reason' => 'join_company',
426436
'meta' => array_filter(['role_uuid' => $request->input('user.role_uuid') ?? $request->input('user.role')]),
437+
'expires_at' => now()->addHours(48),
427438
]);
428439

429440
$user->notify(new UserInvited($invitation));
@@ -456,6 +467,7 @@ public function resendInvitation(ResendUserInvite $request)
456467
'protocol' => 'email',
457468
'recipients' => [$user->email],
458469
'reason' => 'join_company',
470+
'expires_at' => now()->addHours(48),
459471
]);
460472

461473
// notify user
@@ -520,6 +532,9 @@ public function acceptCompanyInvite(AcceptCompanyInvite $request)
520532
}
521533
}
522534

535+
// Delete the invite
536+
$invite->delete();
537+
523538
// Switch the user's active company to the one they just joined.
524539
// This ensures that subsequent calls to /users/me resolve the
525540
// companyUser relationship (and therefore role/policies) against

src/Http/Filter/ScheduleExceptionFilter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function subjectType(?string $type)
6060

6161
if (Str::contains($type, '\\')) {
6262
$this->builder->where('subject_type', $type);
63+
6364
return;
6465
}
6566

src/Http/Filter/ScheduleFilter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function subjectType(?string $type)
3636
// If it already looks like a fully-qualified class name, use as-is
3737
if (Str::contains($type, '\\')) {
3838
$this->builder->where('subject_type', $type);
39+
3940
return;
4041
}
4142

src/Http/Filter/ScheduleItemFilter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function assigneeType(?string $type)
6767

6868
if (Str::contains($type, '\\')) {
6969
$this->builder->where('assignee_type', $type);
70+
7071
return;
7172
}
7273

src/Http/Filter/ScheduleTemplateFilter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function subjectType(?string $type)
3333

3434
if (Str::contains($type, '\\')) {
3535
$this->builder->where('subject_type', $type);
36+
3637
return;
3738
}
3839

src/Http/Filter/UserFilter.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,27 @@ public function queryForInternal()
88
{
99
$companyUuid = $this->session->get('company');
1010

11-
$this->builder->whereHas(
12-
'companyUsers',
13-
function ($q) use ($companyUuid) {
14-
$q->where('company_uuid', $companyUuid);
11+
$this->builder->where(
12+
function ($query) use ($companyUuid) {
13+
// Include users who are already members of the company.
14+
$query->whereHas(
15+
'companyUsers',
16+
function ($q) use ($companyUuid) {
17+
$q->where('company_uuid', $companyUuid);
18+
}
19+
)
20+
// Also include users who have a pending invite to join the company
21+
// but have not yet accepted (no CompanyUser row exists yet).
22+
// The invites table has no user_uuid; the link is via the user's
23+
// email stored in the JSON recipients column.
24+
->orWhereExists(function ($q) use ($companyUuid) {
25+
$q->selectRaw(1)
26+
->from('invites')
27+
->whereRaw('JSON_CONTAINS(invites.recipients, JSON_QUOTE(users.email))')
28+
->where('invites.company_uuid', $companyUuid)
29+
->where('invites.reason', 'join_company')
30+
->whereNull('invites.deleted_at');
31+
});
1532
}
1633
);
1734
}

0 commit comments

Comments
 (0)