SANC-94: Remap bookings.agency_id to organization.id#72
SANC-94: Remap bookings.agency_id to organization.id#72jason-duong4509 wants to merge 4 commits intomainfrom
Conversation
- Made schema changes in the bookings table so that bookings.agency_id references the right column (organization.id not user.id) - Propagated the schema changes to affected files - Fixed a bug where agency users could not see bookings made by their agency
…s.agency_id-to-organization.id # Conflicts: # src/server/api/routers/trip.ts
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThis PR refactors the booking system's agency semantics by reassociating Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@drizzle/seed.ts`:
- Around line 343-349: Replace the ad-hoc membership lookup with a single helper
(e.g., getSingleUserMembership(userId)) that centralizes logic used at the three
places (the userOrg lookups around the current diff). Implement the helper to
query all memberships for the user (select from member where member.userId =
userId), then: return null if none; if exactly one, return it; if more than one,
either throw an explicit error (fail fast) or apply a deterministic selector
(for example orderBy(member.orgId) and pick the first) — pick one policy and
document it in the helper. Replace each occurrence (the current
db.select(...).limit(1).then(r => r[0]) calls) with calls to this helper so
behavior is consistent and deterministic across the file.
In `@src/server/api/routers/bookings.ts`:
- Around line 252-253: The code currently uses
ctx.session.session.activeOrganizationId ?? "" to populate agencyId which
creates an ambiguous empty-string sentinel; replace these fallbacks with
explicit null-handling: in mutation handlers (where you create/update bookings)
check ctx.session.session.activeOrganizationId and if missing throw a clear
TRPCError (bad_request/unauthorized) instead of using "", and in query/debug
paths return null or omit the agencyId field rather than "" so FK operations are
never passed an empty string; update the agencyId assignments and the three
other occurrences noted (the other agencyId assignments at the locations
referenced) to perform this guard and use TRPCError for mutations.
In `@src/server/api/routers/trip.ts`:
- Line 35: The code uses ctx.session.session.activeOrganizationId ?? "" when
setting agencyId which masks missing orgs and can cause FK errors; update the
router handler (the function setting agencyId in src/server/api/routers/trip.ts)
to explicitly read const activeOrg = ctx.session.session.activeOrganizationId
and if (!activeOrg) throw new TRPCError({ code: "BAD_REQUEST", message: "No
active organization set" }) (or "FORBIDDEN" if preferred) instead of falling
back to an empty string, then use agencyId: activeOrg in the payload.
In `@src/server/db/booking-schema.ts`:
- Around line 54-57: The relationName "agencyBookings" is defined on the wrong
side of the relation: in booking-schema.ts the agency field uses relationName:
"agencyBookings" with bookings.agencyId referencing organization.id, but the
reverse many(...) was placed in userRelations; move the reverse relation
declaration agencyBookings: many(bookings, { relationName: "agencyBookings" })
out of userRelations in auth-schema.ts and add it under organizationRelations
instead so the relation is owned by organization and matches bookings.agencyId
-> organization.id.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 16dae3c5-c6e1-44f9-bf20-cae08feb3d1d
📒 Files selected for processing (5)
drizzle/seed.tssrc/app/debug/bookings/page.tsxsrc/server/api/routers/bookings.tssrc/server/api/routers/trip.tssrc/server/db/booking-schema.ts
- Moved relation declaration from userRelations to organizationRelations since the bookings.agency_id FK moved from users to organizations - Added error handling for when a user does not belong to an organization
promatty
left a comment
There was a problem hiding this comment.
a few easy fixes. also, you are now requiring activeOrganizationId. because the admin and driver calendars call getAll, is there assurance that admins and drivers will always have an activeOrganizationId? ie. we dont want to fail to load bookings into their calendars
There is assurance, in the form of a database hook which sets a user's |
- Some error messages were spelt wrong. They have been corrected - A comment was outdated due to changes made in this ticket. That has been corrected
Remapped the
bookings.agency_idcolumn to be a foreign key oforganization.idrather thanuser.idsince the old foreign key caused discrepancy issues between implementation and design. Also made changes to various files within the repository to ensure that any affected code was fixed.In addition to this change, a bug was fixed during the process of propagating the schema changes. Before, agency members could only view bookings that they created. Now, they can see bookings created by their agency.
Summary by CodeRabbit