-
Notifications
You must be signed in to change notification settings - Fork 10.5k
feat: (PBAC) Add db seed #22704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: (PBAC) Add db seed #22704
Conversation
Hey there and thank you for opening this pull request! 👋🏼 We require pull request titles to follow the Conventional Commits specification and it looks like your proposed title needs to be adjusted. Details:
|
WalkthroughThis update introduces three new TypeScript scripts within the Prisma package: Estimated code review effort🎯 3 (Moderate) | ⏱️ ~45 minutes Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
packages/prisma/seed-pbac-organization.tsOops! Something went wrong! :( ESLint: 8.57.1 ESLint couldn't find the plugin "eslint-plugin-playwright". (The package "eslint-plugin-playwright" was not found when loaded as a Node module from the directory "".) It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:
The plugin "eslint-plugin-playwright" was referenced from the config file in ".eslintrc.js". If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎ |
Graphite Automations"Add consumer team as reviewer" took an action on this PR • (07/23/25)1 reviewer was added to this PR based on Keith Williams's automation. "Add ready-for-e2e label" took an action on this PR • (07/24/25)1 label was added to this PR based on Keith Williams's automation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
🧹 Nitpick comments (2)
packages/prisma/cleanup-pbac.ts (1)
82-89
: Make redirect cleanup more specificThe current query for cleaning up redirects might catch unintended records. The
from
field check only looks for specific usernames without considering the organization context.await prisma.tempOrgRedirect.deleteMany({ where: { OR: [ - { from: { in: ["owner", "events", "analytics", "coordinator", "support"] } }, + { + from: { in: ["owner", "events", "analytics", "coordinator", "support"] }, + fromOrgId: 0, // Ensure these are the redirects created by the seed + }, { toUrl: { contains: "pbac-demo-org" } }, ], }, });packages/prisma/seed-pbac-only.ts (1)
22-26
: Display actual demo passwords for better usabilityThe current output shows "password" as a placeholder, but users need the actual passwords to log in. Since these are demo credentials as mentioned in the PR objectives, consider showing the actual passwords.
console.log("\n🔐 Login Credentials:"); result.users.forEach(({ user, role, customRole }) => { const roleText = customRole ? `${role} + ${customRole}` : role; - console.log(` - ${user.name}: ${user.email} / password (${roleText})`); + // Map emails to their demo passwords + const passwordMap: Record<string, string> = { + "owner@pbac-demo.com": "pbac-owner-2024!", + "events@pbac-demo.com": "events-2024!", + "analytics@pbac-demo.com": "analytics-2024!", + "coordinator@pbac-demo.com": "coordinator-2024!", + "support@pbac-demo.com": "support-2024!", + }; + const password = passwordMap[user.email] || "unknown"; + console.log(` - ${user.name}: ${user.email} / ${password} (${roleText})`); });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
packages/prisma/cleanup-pbac.ts
(1 hunks)packages/prisma/seed-pbac-only.ts
(1 hunks)packages/prisma/seed-pbac-organization.ts
(1 hunks)packages/prisma/seed.ts
(3 hunks)
📓 Path-based instructions (1)
**/*.{js,jsx,ts,tsx}
📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)
Flag excessive Day.js use in performance-critical code. Functions like .add, .diff, .isBefore, and .isAfter are slow, especially in timezone mode. Prefer .utc() for better performance. Where possible, replace with native Date and direct .valueOf() comparisons for faster execution. Recommend using native methods or Day.js .utc() consistently in hot paths like loops.
Files:
packages/prisma/cleanup-pbac.ts
packages/prisma/seed-pbac-organization.ts
packages/prisma/seed-pbac-only.ts
packages/prisma/seed.ts
🧠 Learnings (1)
📓 Common learnings
Learnt from: CR
PR: calcom/cal.com#0
File: .cursor/rules/review.mdc:0-0
Timestamp: 2025-07-21T13:54:11.770Z
Learning: Applies to backend/**/*.{ts,tsx} : For Prisma queries: Select selects only the fields you specify explicitly.
Learnt from: CR
PR: calcom/cal.com#0
File: .cursor/rules/review.mdc:0-0
Timestamp: 2025-07-21T13:54:11.770Z
Learning: Applies to backend/**/*.{ts,tsx} : For Prisma queries: Only select data you need.
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{js,jsx,ts,tsx}
📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)
Flag excessive Day.js use in performance-critical code. Functions like .add, .diff, .isBefore, and .isAfter are slow, especially in timezone mode. Prefer .utc() for better performance. Where possible, replace with native Date and direct .valueOf() comparisons for faster execution. Recommend using native methods or Day.js .utc() consistently in hot paths like loops.
Files:
packages/prisma/cleanup-pbac.ts
packages/prisma/seed-pbac-organization.ts
packages/prisma/seed-pbac-only.ts
packages/prisma/seed.ts
🧠 Learnings (1)
📓 Common learnings
Learnt from: CR
PR: calcom/cal.com#0
File: .cursor/rules/review.mdc:0-0
Timestamp: 2025-07-21T13:54:11.770Z
Learning: Applies to backend/**/*.{ts,tsx} : For Prisma queries: Select selects only the fields you specify explicitly.
Learnt from: CR
PR: calcom/cal.com#0
File: .cursor/rules/review.mdc:0-0
Timestamp: 2025-07-21T13:54:11.770Z
Learning: Applies to backend/**/*.{ts,tsx} : For Prisma queries: Only select data you need.
🔇 Additional comments (2)
packages/prisma/seed-pbac-organization.ts (1)
294-296
: Security consideration: Hardcoded passwords in source codeThe passwords are hardcoded directly in the source code. While this is acceptable for demo/seed data as mentioned in the PR objectives, ensure these credentials are well-documented and users are aware they should change them in any non-demo environment.
Consider adding a comment to highlight these are demo credentials:
+ // Demo credentials - CHANGE THESE IN PRODUCTION const owner = await createUser({ email: "owner@pbac-demo.com", username: "pbac-owner", name: "Organization Owner", password: "pbac-owner-2024!", });
Also applies to: 318-319, 341-342, 364-365, 387-388
packages/prisma/seed.ts (1)
346-364
: Good improvement: Using upsert to handle duplicate redirectsThe change from
create
toupsert
prevents duplicate key errors when re-running the seed script. This makes the seeding process more robust and idempotent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not planning to put these behind yarn commands like we have for the others?
e.g. "seed-app-store": "ts-node --transpile-only ./seed-app-store.ts"
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/prisma/package.json (1)
21-22
: Add an execution-guard or naming convention to prevent accidental prod executionThe new
seed-pbac
script parallels other ad-hoc seeders, but unlike the default Prismaseed
hook it can be invoked in any environment. Consider adding a guard (e.g., checkprocess.env.NODE_ENV !== 'production'
) insideseed-pbac-organization.ts
or prefixing the script withNODE_ENV=development
to avoid seeding PBAC demo data in production by mistake.- "seed-pbac": "ts-node --transpile-only ./seed-pbac-organization.ts" + "seed-pbac": "NODE_ENV=development ts-node --transpile-only ./seed-pbac-organization.ts"Alternatively, rename to
seed-pbac:dev
to signal its intent.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/prisma/package.json
(1 hunks)packages/prisma/seed.ts
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/prisma/seed.ts
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: CR
PR: calcom/cal.com#0
File: .cursor/rules/review.mdc:0-0
Timestamp: 2025-07-21T13:54:11.770Z
Learning: Applies to backend/**/*.{ts,tsx} : For Prisma queries: Select selects only the fields you specify explicitly.
Learnt from: CR
PR: calcom/cal.com#0
File: .cursor/rules/review.mdc:0-0
Timestamp: 2025-07-21T13:54:11.770Z
Learning: Applies to backend/**/*.{ts,tsx} : For Prisma queries: Only select data you need.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Install dependencies / Yarn install & cache
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
♻️ Duplicate comments (3)
packages/prisma/seed-pbac-organization.ts (3)
24-32
: Return existing data when organization already existsWhen the organization exists, returning empty
customRoles
andusers
arrays is misleading. The existing organization likely has roles and users that should be fetched and returned for consistency.
316-323
: Existing users returned with incorrect default rolesWhen existing users are found, they're returned with hardcoded
MembershipRole.MEMBER
andcustomRole: null
, which may not reflect their actual roles. Consider fetching the actual membership data to return accurate role information.
468-478
: Hardcoded role assignment logic needs refactoringThe role assignment uses hardcoded email checks and assigns "owner_role" which wasn't created in
createCustomRoles
. This logic is fragile and should use dynamic role mapping based on user data structure.
🧹 Nitpick comments (1)
packages/prisma/seed-pbac-organization.ts (1)
617-691
: Consider consolidating hardcoded data to reduce duplicationThe password mapping and permission definitions are duplicated across the file. Consider creating centralized data structures to define user data and role permissions, then reference them in both creation and utility functions to improve maintainability.
+// Centralized user and role data +const DEMO_USERS = { + owner: { email: "owner@pbac-demo.com", password: "pbac-owner-2024!", role: "owner" }, + events: { email: "events@pbac-demo.com", password: "events-2024!", role: "Event Manager" }, + // ... other users +}; + +const ROLE_PERMISSIONS = { + "Event Manager": [ + { resource: "eventType", action: "create" }, + // ... other permissions + ], + // ... other roles +};
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/prisma/seed-pbac-organization.ts
(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{js,jsx,ts,tsx}
📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)
Flag excessive Day.js use in performance-critical code. Functions like .add, .diff, .isBefore, and .isAfter are slow, especially in timezone mode. Prefer .utc() for better performance. Where possible, replace with native Date and direct .valueOf() comparisons for faster execution. Recommend using native methods or Day.js .utc() consistently in hot paths like loops.
Files:
packages/prisma/seed-pbac-organization.ts
🧠 Learnings (2)
📓 Common learnings
Learnt from: CR
PR: calcom/cal.com#0
File: .cursor/rules/review.mdc:0-0
Timestamp: 2025-07-21T13:54:11.770Z
Learning: Applies to backend/**/*.{ts,tsx} : For Prisma queries: Select selects only the fields you specify explicitly.
Learnt from: CR
PR: calcom/cal.com#0
File: .cursor/rules/review.mdc:0-0
Timestamp: 2025-07-21T13:54:11.770Z
Learning: Applies to backend/**/*.{ts,tsx} : For Prisma queries: Only select data you need.
packages/prisma/seed-pbac-organization.ts (1)
Learnt from: CR
PR: calcom/cal.com#0
File: .cursor/rules/review.mdc:0-0
Timestamp: 2025-07-21T13:54:11.770Z
Learning: Applies to backend/**/*.{ts,tsx} : For Prisma queries: Only select data you need.
🧬 Code Graph Analysis (1)
packages/prisma/seed-pbac-organization.ts (2)
packages/platform/libraries/index.ts (1)
MembershipRole
(98-98)apps/web/playwright/fixtures/users.ts (1)
hashPassword
(26-29)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Install dependencies / Yarn install & cache
🔇 Additional comments (3)
packages/prisma/seed-pbac-organization.ts (3)
113-287
: Well-structured custom role creation with comprehensive permissionsThe function implements proper idempotency checks, creates roles with detailed permission sets, and maintains consistent patterns. The permission mappings are thorough and appropriate for each role type.
527-612
: Well-implemented helper functions with proper separation of concernsThe helper functions handle user creation, membership management, and profile creation effectively. Password hashing follows security best practices, and the Prisma operations are correctly structured.
693-706
: Proper script execution pattern with clean resource managementThe direct execution block follows best practices with proper error handling, resource cleanup via Prisma disconnect, and appropriate exit codes.
What does this PR do?
Adds a DB seed for PBAC for a org with a few custom roles and the feature flag enabeld
Users with Roles