Skip to content

Modify getAll bookings to allow drivers#63

Open
JustinTan-1 wants to merge 2 commits intomainfrom
SANC-87-change-driver-permissions-to-see-all-bookings
Open

Modify getAll bookings to allow drivers#63
JustinTan-1 wants to merge 2 commits intomainfrom
SANC-87-change-driver-permissions-to-see-all-bookings

Conversation

@JustinTan-1
Copy link
Contributor

@JustinTan-1 JustinTan-1 commented Mar 4, 2026

Changed getAll api endpoint to get all bookings if role is driver. Changed query in driver-dashboard.tsx to getAll instead of getDriverTrip.

Summary by CodeRabbit

  • Bug Fixes
    • Drivers can now reliably view their assigned bookings and trips in the driver dashboard; data is correctly returned for driver accounts.
  • Improvements
    • Survey buttons now retrieve the authenticated session to enable driver-specific behavior and future personalization of survey requests.

@vercel
Copy link

vercel bot commented Mar 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
salvationarmy Ready Ready Preview, Comment Mar 5, 2026 7:10pm

@coderabbitai
Copy link

coderabbitai bot commented Mar 4, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 36a9fbca-3d9b-4b4d-af0d-bfd84c301b7e

📥 Commits

Reviewing files that changed from the base of the PR and between c71207b and 9a7b6ae.

📒 Files selected for processing (1)
  • src/app/_components/drivercomponents/surveyButtons/surveyButtons.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/app/_components/drivercomponents/surveyButtons/surveyButtons.tsx

📝 Walkthrough

Walkthrough

Replaced a driver-specific trips query with the shared bookings.getAll endpoint in the driver dashboard, added session retrieval in survey buttons, and relaxed the backend getAll authorization to allow users with the driver role alongside admin.

Changes

Cohort / File(s) Summary
Driver dashboard query
src/app/_components/drivercomponents/driver-dashboard.tsx
Switched trips fetch from getDriverTrip to bookings.getAll, preserving same request params and enabled condition.
Survey buttons session
src/app/_components/drivercomponents/surveyButtons/surveyButtons.tsx
Imported authClient and added authClient.useSession() to obtain session data (session currently retrieved but not yet used).
Bookings API authorization
src/server/api/routers/bookings.ts
Expanded getAll role check to allow `role === "admin"

Sequence Diagram

sequenceDiagram
    actor Driver
    participant Dashboard
    participant BookingsAPI
    participant SurveyButtons
    participant AuthClient

    Driver->>Dashboard: Open dashboard
    Dashboard->>BookingsAPI: call bookings.getAll(startDate,endDate)
    BookingsAPI->>BookingsAPI: authorize (role === "admin" || role === "driver")
    BookingsAPI-->>Dashboard: return bookings

    Dashboard->>SurveyButtons: mount
    SurveyButtons->>AuthClient: useSession()
    AuthClient-->>SurveyButtons: session data
    SurveyButtons->>SurveyButtons: (optionally) filter bookings by session.user.id
    SurveyButtons-->>Driver: render surveys
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • Lujarios
  • burtonjong
  • themaxboucher
  • promatty
  • Yemyam
  • wesleylui

Poem

🐰 I hopped through code with nimble feet,
Swapped a query so drivers can meet,
Session saved, backend gates ajar,
Trips now shared — near and far! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Modify getAll bookings to allow drivers' directly and accurately summarizes the main change: updating the getAll API endpoint to allow drivers to access all bookings, as evidenced by both the server-side permission change and the client-side query update.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch SANC-87-change-driver-permissions-to-see-all-bookings

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/app/_components/drivercomponents/driver-dashboard.tsx (1)

21-34: ⚠️ Potential issue | 🟠 Major

Drivers will see all bookings, not just their own.

Unlike surveyButtons.tsx which filters rawBookings by session?.user?.id, this component assigns all results from getAll directly to driverTrips without filtering. Drivers will see every booking in the system on their calendar.

At minimum, apply the same client-side filter used in surveyButtons.tsx. However, the proper fix is server-side filtering in the getAll procedure (see comment on bookings.ts).

Proposed client-side filter (interim fix)
+ import { authClient } from "@/lib/auth-client";
  
  export const DriverDashboard = () => {
+   const { data: session } = authClient.useSession();
    const [dateJSON, setDateJSON] = useState<CalendarDates>({
      startDate: "",
      endDate: "",
    });

    let driverTrips = [{}] as Booking[];

    const tripQuery = api.bookings.getAll.useQuery(
      {
        startDate: dateJSON.startDate,
        endDate: dateJSON.endDate,
      },
      { enabled: dateJSON.startDate !== "" && dateJSON.endDate !== "" },
    );

    if (tripQuery.data) {
-     driverTrips = tripQuery.data as Booking[];
+     driverTrips = (tripQuery.data as Booking[]).filter(
+       (b) => b.driverId === session?.user?.id
+     );
    }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/app/_components/drivercomponents/driver-dashboard.tsx` around lines 21 -
34, The component currently assigns all results from
api.bookings.getAll.useQuery directly to driverTrips, exposing every booking to
drivers; update the assignment to filter tripQuery.data by the current user id
(same logic used in surveyButtons.tsx that filters rawBookings by
session?.user?.id) so driverTrips only includes bookings where booking.driverId
(or the appropriate booking user id field) === session?.user?.id; locate the
tripQuery and driverTrips variables in driver-dashboard.tsx and apply the
client-side filter as an interim fix until you implement server-side filtering
in the getAll procedure.
src/server/api/routers/bookings.ts (1)

151-157: ⚠️ Potential issue | 🔴 Critical

Security issue: Drivers can access all bookings without server-side filtering.

Allowing role === "driver" into the admin branch grants drivers access to ALL bookings in the system. The getDriverTrip procedure (lines 279-336) correctly filters by eq(bookings.driverId, user.id), but this change bypasses that protection.

Drivers should only see bookings assigned to them. Apply server-side filtering for the driver role.

Proposed fix: Add server-side filtering for drivers
-     if (role === "admin" || role === "driver") {
+     if (role === "admin") {
        return ctx.db
          .select()
          .from(bookings)
          .where(and(...conditions))
          .orderBy(desc(bookings.createdAt));
      }

+     if (role === "driver") {
+       return ctx.db
+         .select()
+         .from(bookings)
+         .where(and(eq(bookings.driverId, userId), ...conditions))
+         .orderBy(desc(bookings.createdAt));
+     }
+
      return ctx.db
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/server/api/routers/bookings.ts` around lines 151 - 157, The current
branch in bookings.ts that returns all bookings when role === "admin" || role
=== "driver" lets drivers see every booking; change the logic so admin continues
to get unfiltered results but drivers get an added server-side filter
eq(bookings.driverId, user.id). In the bookings router code block (the if
checking role === "admin" || role === "driver"), split the branch into admin and
driver cases: for admin return the existing query, for driver add
and(eq(bookings.driverId, user.id), ...conditions) (use ctx.session.user.id or
user.id as in this file) so drivers only receive bookings assigned to them;
mirror the same server-side restriction used by getDriverTrip to locate the
correct filtering approach.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/app/_components/drivercomponents/surveyButtons/surveyButtons.tsx`:
- Around line 30-37: The client is calling api.bookings.getAll.useQuery and then
filtering rawBookings by session?.user?.id (bookings variable), which leaks all
bookings to the client; move the primary authorization into the server-side
getAll procedure so it only returns bookings for the authenticated driver, e.g.
check ctx.session.user.id inside the getAll resolver and apply a query filter
(or require a driverId param validated against ctx.session) before fetching from
the DB; keep the client-side filter in surveyButtons.tsx as defense-in-depth but
ensure getAll enforces the authorization so no other drivers' bookings are sent
to the client.

---

Outside diff comments:
In `@src/app/_components/drivercomponents/driver-dashboard.tsx`:
- Around line 21-34: The component currently assigns all results from
api.bookings.getAll.useQuery directly to driverTrips, exposing every booking to
drivers; update the assignment to filter tripQuery.data by the current user id
(same logic used in surveyButtons.tsx that filters rawBookings by
session?.user?.id) so driverTrips only includes bookings where booking.driverId
(or the appropriate booking user id field) === session?.user?.id; locate the
tripQuery and driverTrips variables in driver-dashboard.tsx and apply the
client-side filter as an interim fix until you implement server-side filtering
in the getAll procedure.

In `@src/server/api/routers/bookings.ts`:
- Around line 151-157: The current branch in bookings.ts that returns all
bookings when role === "admin" || role === "driver" lets drivers see every
booking; change the logic so admin continues to get unfiltered results but
drivers get an added server-side filter eq(bookings.driverId, user.id). In the
bookings router code block (the if checking role === "admin" || role ===
"driver"), split the branch into admin and driver cases: for admin return the
existing query, for driver add and(eq(bookings.driverId, user.id),
...conditions) (use ctx.session.user.id or user.id as in this file) so drivers
only receive bookings assigned to them; mirror the same server-side restriction
used by getDriverTrip to locate the correct filtering approach.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: aa081775-64a1-45e3-b345-af5d3bef8acf

📥 Commits

Reviewing files that changed from the base of the PR and between 04278c3 and c71207b.

📒 Files selected for processing (3)
  • src/app/_components/drivercomponents/driver-dashboard.tsx
  • src/app/_components/drivercomponents/surveyButtons/surveyButtons.tsx
  • src/server/api/routers/bookings.ts

Comment on lines +30 to +37
const { data: rawBookings = [] } = api.bookings.getAll.useQuery(
{ surveyCompleted: false },
{
initialData: initialBookings,
refetchInterval: 60000,
},
);
const bookings = rawBookings.filter((b) => b.driverId === session?.user?.id);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Client-side filtering is insufficient for authorization.

While filtering by session?.user?.id prevents displaying other drivers' bookings in the UI, all booking data is still fetched and sent to the client. A driver could inspect network traffic or call the API directly to see all bookings.

Authorization should be enforced server-side in the getAll procedure. This client-side filter can remain as defense-in-depth but should not be the primary access control mechanism.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/app/_components/drivercomponents/surveyButtons/surveyButtons.tsx` around
lines 30 - 37, The client is calling api.bookings.getAll.useQuery and then
filtering rawBookings by session?.user?.id (bookings variable), which leaks all
bookings to the client; move the primary authorization into the server-side
getAll procedure so it only returns bookings for the authenticated driver, e.g.
check ctx.session.user.id inside the getAll resolver and apply a query filter
(or require a driverId param validated against ctx.session) before fetching from
the DB; keep the client-side filter in surveyButtons.tsx as defense-in-depth but
ensure getAll enforces the authorization so no other drivers' bookings are sent
to the client.

refetchInterval: 60000,
},
);
const bookings = rawBookings.filter((b) => b.driverId === session?.user?.id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intended...? If we want the driver to see all bookings then why are we filtering here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants