create-ej-app 25.0.0 → 27.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +125 -226
- package/cli.ts +83 -28
- package/package.json +2 -2
- package/template/api/.vscode/settings.json +1 -1
- package/template/api/{.github/copilot-instructions.md → AGENTS.md} +0 -2
- package/template/api/CLAUDE.md +1 -0
- package/template/api/package.json +10 -10
- package/template/nextjs/.dockerignore +1 -0
- package/template/nextjs/AGENTS.md +260 -0
- package/template/nextjs/CLAUDE.md +1 -0
- package/template/nextjs/README.md +109 -0
- package/template/nextjs/bunfig.toml +3 -0
- package/template/nextjs/eslint.config.mjs +6 -5
- package/template/nextjs/gitignore +6 -0
- package/template/nextjs/next.config.ts +8 -1
- package/template/nextjs/package.json +47 -40
- package/template/nextjs/public/logo-dark.svg +4 -0
- package/template/nextjs/public/logo.svg +4 -0
- package/template/nextjs/src/app/(admin)/actions/users.ts +183 -0
- package/template/nextjs/src/app/(admin)/admin/_components/admin-header.tsx +24 -0
- package/template/nextjs/src/app/(admin)/admin/_components/admin-search.tsx +16 -0
- package/template/nextjs/src/app/(admin)/admin/_components/admin-sidebar.tsx +42 -0
- package/template/nextjs/src/app/(admin)/admin/layout.tsx +53 -0
- package/template/nextjs/src/app/(admin)/admin/page.tsx +11 -2
- package/template/nextjs/src/app/(admin)/admin/settings/layout.tsx +24 -0
- package/template/nextjs/src/app/(admin)/admin/settings/users/[userId]/page.tsx +43 -0
- package/template/nextjs/src/app/(admin)/admin/settings/users/new/page.tsx +16 -0
- package/template/nextjs/src/app/(admin)/admin/settings/users/page.tsx +377 -0
- package/template/nextjs/src/app/(admin)/admin/settings/users/user-form.tsx +138 -0
- package/template/nextjs/src/app/(admin)/admin/settings/users/validation.ts +23 -0
- package/template/nextjs/src/app/(app)/app/layout.tsx +44 -0
- package/template/nextjs/src/app/(app)/app/page.tsx +14 -2
- package/template/nextjs/src/app/(auth)/(account)/layout.tsx +64 -0
- package/template/nextjs/src/app/(auth)/(account)/profile/page.tsx +16 -0
- package/template/nextjs/src/app/(auth)/(account)/profile/profile-form.tsx +247 -0
- package/template/nextjs/src/app/(auth)/login/email-login-form.tsx +56 -29
- package/template/nextjs/src/app/(auth)/login/page.tsx +45 -12
- package/template/nextjs/src/app/(auth)/login/social-login-buttons.tsx +85 -17
- package/template/nextjs/src/app/(marketing)/layout.tsx +35 -4
- package/template/nextjs/src/app/(marketing)/legal.tsx +56 -0
- package/template/nextjs/src/app/(marketing)/page.tsx +20 -2
- package/template/nextjs/src/app/(marketing)/privacy/page.tsx +43 -2
- package/template/nextjs/src/app/(marketing)/terms/page.tsx +33 -2
- package/template/nextjs/src/app/error.tsx +37 -0
- package/template/nextjs/src/app/fonts/geist-mono/GeistMono-Variable.woff2 +0 -0
- package/template/nextjs/src/app/fonts/geist-sans/Geist-Variable.woff2 +0 -0
- package/template/nextjs/src/app/fonts/index.ts +60 -0
- package/template/nextjs/src/app/global-error.tsx +38 -0
- package/template/nextjs/src/app/globals.css +29 -0
- package/template/nextjs/src/app/layout.tsx +45 -18
- package/template/nextjs/src/app/not-found.tsx +25 -3
- package/template/nextjs/src/app/robots.ts +29 -0
- package/template/nextjs/src/app/sitemap.ts +21 -0
- package/template/nextjs/src/components/confirm-dialog.tsx +79 -0
- package/template/nextjs/src/components/dashboard-header.tsx +70 -0
- package/template/nextjs/src/components/data-table/enum.ts +5 -0
- package/template/nextjs/src/components/data-table/features.ts +49 -0
- package/template/nextjs/src/components/data-table/index.tsx +961 -0
- package/template/nextjs/src/components/data-table/types.ts +76 -0
- package/template/nextjs/src/components/error.tsx +71 -0
- package/template/nextjs/src/components/loader.tsx +18 -2
- package/template/nextjs/src/components/logo.tsx +49 -0
- package/template/nextjs/src/components/navigation-search.tsx +140 -0
- package/template/nextjs/src/components/navigation-sidebar.tsx +241 -0
- package/template/nextjs/src/components/page-heading.tsx +38 -0
- package/template/nextjs/src/components/react-query-provider.tsx +41 -2
- package/template/nextjs/src/components/route-error.tsx +55 -0
- package/template/nextjs/src/components/screen-size.tsx +36 -0
- package/template/nextjs/src/components/site-footer.tsx +20 -4
- package/template/nextjs/src/components/theme-provider.tsx +48 -0
- package/template/nextjs/src/components/theme-toggle.tsx +58 -5
- package/template/nextjs/src/components/ui/alert-dialog.tsx +199 -0
- package/template/nextjs/src/components/ui/avatar.tsx +112 -0
- package/template/nextjs/src/components/ui/badge.tsx +54 -0
- package/template/nextjs/src/components/ui/button.tsx +3 -1
- package/template/nextjs/src/components/ui/card.tsx +4 -4
- package/template/nextjs/src/components/ui/collapsible.tsx +33 -0
- package/template/nextjs/src/components/ui/command.tsx +192 -0
- package/template/nextjs/src/components/ui/dialog.tsx +164 -0
- package/template/nextjs/src/components/ui/dropdown-menu.tsx +273 -0
- package/template/nextjs/src/components/ui/input-group.tsx +156 -0
- package/template/nextjs/src/components/ui/select.tsx +195 -0
- package/template/nextjs/src/components/ui/sheet.tsx +143 -0
- package/template/nextjs/src/components/ui/sidebar.tsx +702 -0
- package/template/nextjs/src/components/ui/skeleton.tsx +13 -0
- package/template/nextjs/src/components/ui/table.tsx +116 -0
- package/template/nextjs/src/components/ui/textarea.tsx +18 -0
- package/template/nextjs/src/components/ui/tooltip.tsx +57 -0
- package/template/nextjs/src/components/user-menu.tsx +206 -0
- package/template/nextjs/src/db/common.ts +10 -0
- package/template/nextjs/src/db/index.ts +16 -6
- package/template/nextjs/src/db/relations.ts +21 -0
- package/template/nextjs/src/db/schema.ts +85 -46
- package/template/nextjs/src/emails/components/email-layout.tsx +27 -11
- package/template/nextjs/src/emails/magic-link.tsx +15 -13
- package/template/nextjs/src/emails/test.tsx +11 -9
- package/template/nextjs/src/emails/welcome.tsx +12 -9
- package/template/nextjs/src/env.ts +1 -1
- package/template/nextjs/src/hooks/use-mobile.ts +14 -12
- package/template/nextjs/src/instrumentation.ts +20 -2
- package/template/nextjs/src/lib/actions.ts +55 -0
- package/template/nextjs/src/lib/admin-navigation.ts +43 -0
- package/template/nextjs/src/lib/auth-client.ts +20 -9
- package/template/nextjs/src/lib/auth-permissions.ts +61 -0
- package/template/nextjs/src/lib/auth.ts +33 -3
- package/template/nextjs/src/lib/callback-url.ts +28 -0
- package/template/nextjs/src/lib/constants.ts +21 -0
- package/template/nextjs/src/lib/delay.ts +10 -0
- package/template/nextjs/src/lib/email-service.ts +21 -0
- package/template/nextjs/src/lib/errors.ts +74 -0
- package/template/nextjs/src/lib/format-currency.ts +22 -24
- package/template/nextjs/src/lib/format-date.ts +69 -6
- package/template/nextjs/src/lib/nodemailer.ts +27 -3
- package/template/nextjs/src/lib/otel.ts +92 -2
- package/template/nextjs/src/lib/safe-action-client.ts +60 -9
- package/template/nextjs/src/lib/safe-action.ts +152 -16
- package/template/nextjs/src/lib/search.ts +16 -0
- package/template/nextjs/src/lib/seo.ts +7 -0
- package/template/nextjs/src/lib/siteConfig.ts +17 -1
- package/template/nextjs/src/lib/toast-message.ts +87 -1
- package/template/nextjs/src/lib/validations.ts +30 -0
- package/template/nextjs/src/proxy.ts +48 -0
- package/template/nextjs/src/scripts/reset-db.ts +22 -0
- package/template/nextjs/src/scripts/seed.ts +59 -8
- package/template/nextjs/src/scripts/test-mail.ts +28 -20
- package/template/nextjs/tests/admin/users.test.ts +64 -0
- package/template/nextjs/tests/db/fk-indexes.test.ts +62 -0
- package/template/nextjs/tests/db/seed.test.ts +49 -0
- package/template/nextjs/tests/helpers/action.ts +71 -0
- package/template/nextjs/tests/helpers/fixtures.ts +78 -0
- package/template/nextjs/tests/helpers/index.ts +2 -0
- package/template/nextjs/tests/lib/callback-url.test.ts +48 -0
- package/template/nextjs/tests/lib/client-server-boundary.test.ts +57 -0
- package/template/nextjs/tests/lib/errors.test.ts +10 -0
- package/template/nextjs/tests/lib/search.test.ts +23 -0
- package/template/nextjs/tests/setup/pglite-db.ts +63 -0
- package/template/nextjs/tests/setup/preload.ts +95 -0
- package/template/nextjs/tsconfig.json +1 -0
- package/template/saas-starter/.dockerignore +8 -0
- package/template/saas-starter/.env.example +25 -0
- package/template/saas-starter/.vscode/extensions.json +11 -0
- package/template/saas-starter/.vscode/settings.json +3 -0
- package/template/saas-starter/AGENTS.md +302 -0
- package/template/saas-starter/CLAUDE.md +1 -0
- package/template/saas-starter/Dockerfile +15 -0
- package/template/saas-starter/README.md +125 -0
- package/template/saas-starter/bunfig.toml +3 -0
- package/template/saas-starter/components.json +25 -0
- package/template/saas-starter/drizzle.config.ts +10 -0
- package/template/saas-starter/eslint.config.mjs +24 -0
- package/template/saas-starter/gitignore +14 -0
- package/template/saas-starter/next.config.ts +32 -0
- package/template/saas-starter/package.json +97 -0
- package/template/saas-starter/postcss.config.mjs +7 -0
- package/template/saas-starter/prettier.config.mjs +11 -0
- package/template/saas-starter/public/logo-dark.svg +4 -0
- package/template/saas-starter/public/logo.svg +4 -0
- package/template/saas-starter/public/placeholder.svg +1 -0
- package/template/saas-starter/src/app/(admin)/actions/contact-leads.ts +51 -0
- package/template/saas-starter/src/app/(admin)/actions/feedbacks.ts +91 -0
- package/template/saas-starter/src/app/(admin)/actions/users.ts +183 -0
- package/template/saas-starter/src/app/(admin)/admin/_components/admin-header.tsx +24 -0
- package/template/saas-starter/src/app/(admin)/admin/_components/admin-search.tsx +16 -0
- package/template/saas-starter/src/app/(admin)/admin/_components/admin-sidebar.tsx +42 -0
- package/template/saas-starter/src/app/(admin)/admin/contact-leads/page.tsx +123 -0
- package/template/saas-starter/src/app/(admin)/admin/feedbacks/page.tsx +161 -0
- package/template/saas-starter/src/app/(admin)/admin/layout.tsx +53 -0
- package/template/saas-starter/src/app/(admin)/admin/page.tsx +60 -0
- package/template/saas-starter/src/app/(admin)/admin/settings/layout.tsx +24 -0
- package/template/saas-starter/src/app/(admin)/admin/settings/users/[userId]/page.tsx +43 -0
- package/template/saas-starter/src/app/(admin)/admin/settings/users/new/page.tsx +16 -0
- package/template/saas-starter/src/app/(admin)/admin/settings/users/page.tsx +377 -0
- package/template/saas-starter/src/app/(admin)/admin/settings/users/user-form.tsx +138 -0
- package/template/saas-starter/src/app/(admin)/admin/settings/users/validation.ts +23 -0
- package/template/saas-starter/src/app/(admin)/admin/support-tickets/[ticketId]/page.tsx +17 -0
- package/template/saas-starter/src/app/(admin)/admin/support-tickets/page.tsx +5 -0
- package/template/saas-starter/src/app/(app)/actions/billing.ts +216 -0
- package/template/saas-starter/src/app/(app)/actions/feedbacks.ts +28 -0
- package/template/saas-starter/src/app/(app)/actions/feedbacks.validation.ts +12 -0
- package/template/saas-starter/src/app/(app)/actions/members.ts +189 -0
- package/template/saas-starter/src/app/(app)/actions/organizations.ts +230 -0
- package/template/saas-starter/src/app/(app)/actions/organizations.validation.ts +17 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/dashboard/page.tsx +17 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/layout.tsx +31 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/settings/_components/billing-email-form.tsx +92 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/settings/_components/danger-zone.tsx +357 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/settings/_components/general-settings-form.tsx +130 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/settings/_components/plans-section.tsx +222 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/settings/billing/page.tsx +115 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/settings/layout.tsx +24 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/settings/members/_components/invite-member-dialog.tsx +168 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/settings/members/_components/members-tabs.tsx +604 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/settings/members/page.tsx +33 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/settings/page.tsx +55 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/support-tickets/[ticketId]/page.tsx +25 -0
- package/template/saas-starter/src/app/(app)/app/[orgId]/support-tickets/page.tsx +25 -0
- package/template/saas-starter/src/app/(app)/app/_components/app-header.tsx +31 -0
- package/template/saas-starter/src/app/(app)/app/_components/app-search.tsx +26 -0
- package/template/saas-starter/src/app/(app)/app/_components/app-sidebar.tsx +30 -0
- package/template/saas-starter/src/app/(app)/app/_components/feedback-dialog.tsx +108 -0
- package/template/saas-starter/src/app/(app)/app/_components/organization-context.tsx +37 -0
- package/template/saas-starter/src/app/(app)/app/_components/organization-switcher.tsx +200 -0
- package/template/saas-starter/src/app/(app)/app/layout.tsx +64 -0
- package/template/saas-starter/src/app/(app)/app/onboarding/_components/create-organization-form.tsx +117 -0
- package/template/saas-starter/src/app/(app)/app/onboarding/page.tsx +40 -0
- package/template/saas-starter/src/app/(app)/app/page.tsx +76 -0
- package/template/saas-starter/src/app/(auth)/(account)/layout.tsx +64 -0
- package/template/saas-starter/src/app/(auth)/(account)/profile/page.tsx +16 -0
- package/template/saas-starter/src/app/(auth)/(account)/profile/profile-form.tsx +247 -0
- package/template/saas-starter/src/app/(auth)/(account)/support/[ticketId]/page.tsx +19 -0
- package/template/saas-starter/src/app/(auth)/(account)/support/page.tsx +9 -0
- package/template/saas-starter/src/app/(auth)/login/email-login-form.tsx +131 -0
- package/template/saas-starter/src/app/(auth)/login/page.tsx +105 -0
- package/template/saas-starter/src/app/(auth)/login/social-login-buttons.tsx +137 -0
- package/template/saas-starter/src/app/(invitation)/accept-invitation/[id]/accept-invitation-form.tsx +94 -0
- package/template/saas-starter/src/app/(invitation)/accept-invitation/[id]/page.tsx +153 -0
- package/template/saas-starter/src/app/(invitation)/actions/invitations.ts +32 -0
- package/template/saas-starter/src/app/(marketing)/contact-us/actions.ts +23 -0
- package/template/saas-starter/src/app/(marketing)/contact-us/contact-form.tsx +118 -0
- package/template/saas-starter/src/app/(marketing)/contact-us/page.tsx +20 -0
- package/template/saas-starter/src/app/(marketing)/contact-us/validation.ts +22 -0
- package/template/saas-starter/src/app/(marketing)/layout.tsx +48 -0
- package/template/saas-starter/src/app/(marketing)/legal.tsx +66 -0
- package/template/saas-starter/src/app/(marketing)/page.tsx +24 -0
- package/template/saas-starter/src/app/(marketing)/privacy/page.tsx +63 -0
- package/template/saas-starter/src/app/(marketing)/terms/page.tsx +41 -0
- package/template/saas-starter/src/app/actions/support-tickets.ts +510 -0
- package/template/saas-starter/src/app/actions/support-tickets.validation.ts +112 -0
- package/template/saas-starter/src/app/actions/uploads.ts +36 -0
- package/template/saas-starter/src/app/actions/uploads.validation.ts +24 -0
- package/template/saas-starter/src/app/api/auth/[...all]/route.ts +4 -0
- package/template/saas-starter/src/app/api/webhooks/stripe/route.ts +94 -0
- package/template/saas-starter/src/app/error.tsx +37 -0
- package/template/saas-starter/src/app/favicon.ico +0 -0
- package/template/saas-starter/src/app/fonts/geist-mono/GeistMono-Variable.woff2 +0 -0
- package/template/saas-starter/src/app/fonts/geist-sans/Geist-Variable.woff2 +0 -0
- package/template/saas-starter/src/app/fonts/index.ts +60 -0
- package/template/saas-starter/src/app/global-error.tsx +38 -0
- package/template/saas-starter/src/app/globals.css +269 -0
- package/template/saas-starter/src/app/layout.tsx +77 -0
- package/template/saas-starter/src/app/not-found.tsx +49 -0
- package/template/saas-starter/src/app/robots.ts +36 -0
- package/template/saas-starter/src/app/sitemap.ts +21 -0
- package/template/saas-starter/src/components/confirm-dialog.tsx +79 -0
- package/template/saas-starter/src/components/dashboard-header.tsx +70 -0
- package/template/saas-starter/src/components/data-table/enum.ts +5 -0
- package/template/saas-starter/src/components/data-table/features.ts +49 -0
- package/template/saas-starter/src/components/data-table/index.tsx +961 -0
- package/template/saas-starter/src/components/data-table/types.ts +76 -0
- package/template/saas-starter/src/components/error.tsx +71 -0
- package/template/saas-starter/src/components/file-upload.tsx +254 -0
- package/template/saas-starter/src/components/loader.tsx +41 -0
- package/template/saas-starter/src/components/logo.tsx +49 -0
- package/template/saas-starter/src/components/navigation-search.tsx +140 -0
- package/template/saas-starter/src/components/navigation-sidebar.tsx +241 -0
- package/template/saas-starter/src/components/page-heading.tsx +38 -0
- package/template/saas-starter/src/components/react-query-provider.tsx +51 -0
- package/template/saas-starter/src/components/route-error.tsx +55 -0
- package/template/saas-starter/src/components/screen-size.tsx +76 -0
- package/template/saas-starter/src/components/site-footer.tsx +39 -0
- package/template/saas-starter/src/components/support/create-support-ticket-dialog.tsx +122 -0
- package/template/saas-starter/src/components/support/support-ticket-conversation.tsx +236 -0
- package/template/saas-starter/src/components/support/support-ticket-shared.tsx +58 -0
- package/template/saas-starter/src/components/support/support-tickets-table.tsx +281 -0
- package/template/saas-starter/src/components/theme-provider.tsx +115 -0
- package/template/saas-starter/src/components/theme-toggle.tsx +75 -0
- package/template/saas-starter/src/components/ui/alert-dialog.tsx +199 -0
- package/template/saas-starter/src/components/ui/avatar.tsx +112 -0
- package/template/saas-starter/src/components/ui/badge.tsx +54 -0
- package/template/saas-starter/src/components/ui/button.tsx +69 -0
- package/template/saas-starter/src/components/ui/card.tsx +103 -0
- package/template/saas-starter/src/components/ui/checkbox.tsx +32 -0
- package/template/saas-starter/src/components/ui/collapsible.tsx +33 -0
- package/template/saas-starter/src/components/ui/command.tsx +192 -0
- package/template/saas-starter/src/components/ui/dialog.tsx +164 -0
- package/template/saas-starter/src/components/ui/dropdown-menu.tsx +273 -0
- package/template/saas-starter/src/components/ui/field.tsx +238 -0
- package/template/saas-starter/src/components/ui/input-group.tsx +156 -0
- package/template/saas-starter/src/components/ui/input.tsx +19 -0
- package/template/saas-starter/src/components/ui/label.tsx +24 -0
- package/template/saas-starter/src/components/ui/popover.tsx +89 -0
- package/template/saas-starter/src/components/ui/select.tsx +195 -0
- package/template/saas-starter/src/components/ui/separator.tsx +28 -0
- package/template/saas-starter/src/components/ui/sheet.tsx +143 -0
- package/template/saas-starter/src/components/ui/sidebar.tsx +702 -0
- package/template/saas-starter/src/components/ui/skeleton.tsx +13 -0
- package/template/saas-starter/src/components/ui/sonner.tsx +45 -0
- package/template/saas-starter/src/components/ui/switch.tsx +33 -0
- package/template/saas-starter/src/components/ui/table.tsx +116 -0
- package/template/saas-starter/src/components/ui/tabs.tsx +90 -0
- package/template/saas-starter/src/components/ui/textarea.tsx +18 -0
- package/template/saas-starter/src/components/ui/tooltip.tsx +57 -0
- package/template/saas-starter/src/components/user-menu.tsx +213 -0
- package/template/saas-starter/src/db/common.ts +63 -0
- package/template/saas-starter/src/db/enums.ts +50 -0
- package/template/saas-starter/src/db/index.ts +60 -0
- package/template/saas-starter/src/db/relations.ts +112 -0
- package/template/saas-starter/src/db/schema.ts +342 -0
- package/template/saas-starter/src/emails/components/email-layout.tsx +79 -0
- package/template/saas-starter/src/emails/invitation.tsx +69 -0
- package/template/saas-starter/src/emails/magic-link.tsx +51 -0
- package/template/saas-starter/src/emails/test.tsx +34 -0
- package/template/saas-starter/src/emails/welcome.tsx +37 -0
- package/template/saas-starter/src/env.ts +66 -0
- package/template/saas-starter/src/hooks/use-mobile.ts +21 -0
- package/template/saas-starter/src/instrumentation.ts +44 -0
- package/template/saas-starter/src/lib/actions.ts +55 -0
- package/template/saas-starter/src/lib/admin-navigation.ts +63 -0
- package/template/saas-starter/src/lib/app-navigation.ts +63 -0
- package/template/saas-starter/src/lib/auth-client.ts +34 -0
- package/template/saas-starter/src/lib/auth-permissions.ts +61 -0
- package/template/saas-starter/src/lib/auth.ts +312 -0
- package/template/saas-starter/src/lib/billing.ts +152 -0
- package/template/saas-starter/src/lib/callback-url.ts +28 -0
- package/template/saas-starter/src/lib/cn.ts +4 -0
- package/template/saas-starter/src/lib/constants.ts +91 -0
- package/template/saas-starter/src/lib/delay.ts +12 -0
- package/template/saas-starter/src/lib/email-service.ts +118 -0
- package/template/saas-starter/src/lib/errors.ts +74 -0
- package/template/saas-starter/src/lib/format-currency.ts +25 -0
- package/template/saas-starter/src/lib/format-date.ts +75 -0
- package/template/saas-starter/src/lib/image-url.ts +54 -0
- package/template/saas-starter/src/lib/nodemailer.ts +67 -0
- package/template/saas-starter/src/lib/organization-access.ts +73 -0
- package/template/saas-starter/src/lib/otel.ts +117 -0
- package/template/saas-starter/src/lib/plan-limits.ts +37 -0
- package/template/saas-starter/src/lib/plans.ts +90 -0
- package/template/saas-starter/src/lib/rbac.ts +44 -0
- package/template/saas-starter/src/lib/safe-action-client.ts +126 -0
- package/template/saas-starter/src/lib/safe-action.ts +187 -0
- package/template/saas-starter/src/lib/search.ts +16 -0
- package/template/saas-starter/src/lib/seo.ts +7 -0
- package/template/saas-starter/src/lib/siteConfig.ts +20 -0
- package/template/saas-starter/src/lib/storage.ts +311 -0
- package/template/saas-starter/src/lib/stripe.ts +9 -0
- package/template/saas-starter/src/lib/toast-message.ts +101 -0
- package/template/saas-starter/src/lib/validations.ts +95 -0
- package/template/saas-starter/src/proxy.ts +48 -0
- package/template/saas-starter/src/scripts/cleanup-temp-uploads.ts +24 -0
- package/template/saas-starter/src/scripts/reset-db.ts +22 -0
- package/template/saas-starter/src/scripts/seed.ts +85 -0
- package/template/saas-starter/src/scripts/setup-db.ts +72 -0
- package/template/saas-starter/src/scripts/setup-stripe-plans.ts +206 -0
- package/template/saas-starter/src/scripts/test-mail.ts +43 -0
- package/template/saas-starter/tests/admin/users.test.ts +64 -0
- package/template/saas-starter/tests/db/fk-indexes.test.ts +62 -0
- package/template/saas-starter/tests/db/seed.test.ts +57 -0
- package/template/saas-starter/tests/helpers/action.ts +71 -0
- package/template/saas-starter/tests/helpers/fixtures.ts +151 -0
- package/template/saas-starter/tests/helpers/index.ts +2 -0
- package/template/saas-starter/tests/lib/callback-url.test.ts +48 -0
- package/template/saas-starter/tests/lib/client-server-boundary.test.ts +57 -0
- package/template/saas-starter/tests/lib/errors.test.ts +10 -0
- package/template/saas-starter/tests/lib/image-url.test.ts +31 -0
- package/template/saas-starter/tests/lib/search.test.ts +23 -0
- package/template/saas-starter/tests/marketing/contact-and-feedback.test.ts +86 -0
- package/template/saas-starter/tests/organizations/organizations.test.ts +175 -0
- package/template/saas-starter/tests/setup/pglite-db.ts +67 -0
- package/template/saas-starter/tests/setup/preload.ts +108 -0
- package/template/saas-starter/tests/support/support-tickets.test.ts +145 -0
- package/template/saas-starter/tsconfig.json +52 -0
- package/cli.js +0 -143
- package/template/nextjs/src/app/(admin)/layout.tsx +0 -20
- package/template/nextjs/src/app/(app)/layout.tsx +0 -20
- package/template/nextjs/terraform/artifact-registry.tf +0 -5
- package/template/nextjs/terraform/backend-service.tf +0 -13
- package/template/nextjs/terraform/cleanup.sh +0 -9
- package/template/nextjs/terraform/cloudbuild.tf +0 -70
- package/template/nextjs/terraform/cloudrun.tf +0 -68
- package/template/nextjs/terraform/database.tf +0 -60
- package/template/nextjs/terraform/forwarding-rule.tf +0 -43
- package/template/nextjs/terraform/gitignore +0 -4
- package/template/nextjs/terraform/ip-address.tf +0 -11
- package/template/nextjs/terraform/network-endpoint-group.tf +0 -11
- package/template/nextjs/terraform/network.tf +0 -21
- package/template/nextjs/terraform/pre.sh +0 -3
- package/template/nextjs/terraform/providers.tf +0 -18
- package/template/nextjs/terraform/secret.txt +0 -11
- package/template/nextjs/terraform/setup.sh +0 -25
- package/template/nextjs/terraform/ssl-certs.tf +0 -32
- package/template/nextjs/terraform/target-https-proxy copy.tf +0 -14
- package/template/nextjs/terraform/target-https-proxy.tf +0 -14
- package/template/nextjs/terraform/terraform.tfvars +0 -4
- package/template/nextjs/terraform/url-map.tf +0 -23
- package/template/nextjs/terraform/variables.tf +0 -21
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
'use server'
|
|
2
|
+
|
|
3
|
+
import { eq } from 'drizzle-orm'
|
|
4
|
+
import { z } from 'zod'
|
|
5
|
+
import { db } from '~/db'
|
|
6
|
+
import { organizationSubscriptionsTable } from '~/db/schema'
|
|
7
|
+
import { env } from '~/env'
|
|
8
|
+
import { canStartTrial, ensureStripeCustomer } from '~/lib/billing'
|
|
9
|
+
import { TRIAL_DAYS } from '~/lib/constants'
|
|
10
|
+
import { AppError, AppErrorCode } from '~/lib/errors'
|
|
11
|
+
import { assertCanManageOrganization } from '~/lib/organization-access'
|
|
12
|
+
import { getOrganizationPlan } from '~/lib/plan-limits'
|
|
13
|
+
import { PLANS, hasLiveSubscription } from '~/lib/plans'
|
|
14
|
+
import { authActionClient, withWideEvent } from '~/lib/safe-action'
|
|
15
|
+
import { stripeClient } from '~/lib/stripe'
|
|
16
|
+
import { emailValidation, stringValidation } from '~/lib/validations'
|
|
17
|
+
|
|
18
|
+
const billingReturnUrl = (organizationId: string) =>
|
|
19
|
+
`${env.BETTER_AUTH_URL}/app/${organizationId}/settings/billing`
|
|
20
|
+
|
|
21
|
+
async function getSubscription(organizationId: string) {
|
|
22
|
+
const sub = await db.query.organizationSubscriptionsTable.findFirst({
|
|
23
|
+
where: eq(organizationSubscriptionsTable.organizationId, organizationId)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
if (!sub) {
|
|
27
|
+
throw new AppError(
|
|
28
|
+
AppErrorCode.NOT_FOUND,
|
|
29
|
+
'This organization has no billing record.'
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return sub
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Everything the billing page shows: the current plan and subscription, and
|
|
38
|
+
* each plan's public prices. Stripe price IDs stay on the server.
|
|
39
|
+
*/
|
|
40
|
+
export const getBillingOverview = authActionClient
|
|
41
|
+
.inputSchema(stringValidation)
|
|
42
|
+
.action(
|
|
43
|
+
withWideEvent(
|
|
44
|
+
'getBillingOverview',
|
|
45
|
+
async ({ parsedInput: organizationId, ctx }) => {
|
|
46
|
+
await assertCanManageOrganization(organizationId, ctx.user.id)
|
|
47
|
+
const { plan, sub } = await getOrganizationPlan(organizationId)
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
currentPlan: { id: plan.id, name: plan.name },
|
|
51
|
+
subscription: {
|
|
52
|
+
status: sub.status,
|
|
53
|
+
billingEmail: sub.billingEmail,
|
|
54
|
+
trialEndsAt: sub.trialEndsAt,
|
|
55
|
+
currentPeriodEnd: sub.currentPeriodEnd,
|
|
56
|
+
cancelAtPeriodEnd: sub.cancelAtPeriodEnd,
|
|
57
|
+
canStartTrial: canStartTrial(sub),
|
|
58
|
+
hasLiveSubscription: hasLiveSubscription(sub.status)
|
|
59
|
+
},
|
|
60
|
+
plans: PLANS.map(item => ({
|
|
61
|
+
id: item.id,
|
|
62
|
+
name: item.name,
|
|
63
|
+
maxMembers: item.maxMembers,
|
|
64
|
+
canUseTrial: Boolean(item.canUseTrial),
|
|
65
|
+
prices: item.stripePrice
|
|
66
|
+
? {
|
|
67
|
+
monthly: {
|
|
68
|
+
amount: item.stripePrice.monthly.amount,
|
|
69
|
+
currency: item.stripePrice.monthly.currency
|
|
70
|
+
},
|
|
71
|
+
annual: {
|
|
72
|
+
amount: item.stripePrice.annual.amount,
|
|
73
|
+
currency: item.stripePrice.annual.currency
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
: null
|
|
77
|
+
}))
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
export const updateBillingEmailAction = authActionClient
|
|
84
|
+
.inputSchema(
|
|
85
|
+
z.object({
|
|
86
|
+
organizationId: stringValidation,
|
|
87
|
+
billingEmail: emailValidation
|
|
88
|
+
})
|
|
89
|
+
)
|
|
90
|
+
.action(
|
|
91
|
+
withWideEvent(
|
|
92
|
+
'updateBillingEmailAction',
|
|
93
|
+
async ({ parsedInput: { organizationId, billingEmail }, ctx }) => {
|
|
94
|
+
await assertCanManageOrganization(organizationId, ctx.user.id)
|
|
95
|
+
const sub = await getSubscription(organizationId)
|
|
96
|
+
|
|
97
|
+
if (sub.stripeCustomerId) {
|
|
98
|
+
await stripeClient.customers.update(sub.stripeCustomerId, {
|
|
99
|
+
email: billingEmail
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
await db
|
|
104
|
+
.update(organizationSubscriptionsTable)
|
|
105
|
+
.set({ billingEmail })
|
|
106
|
+
.where(eq(organizationSubscriptionsTable.id, sub.id))
|
|
107
|
+
|
|
108
|
+
return true
|
|
109
|
+
}
|
|
110
|
+
)
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
export const createCheckoutSessionAction = authActionClient
|
|
114
|
+
.inputSchema(
|
|
115
|
+
z.object({
|
|
116
|
+
organizationId: stringValidation,
|
|
117
|
+
plan: z.enum(['pro', 'ultimate']),
|
|
118
|
+
interval: z.enum(['monthly', 'annual']),
|
|
119
|
+
withTrial: z.boolean().default(false)
|
|
120
|
+
})
|
|
121
|
+
)
|
|
122
|
+
.action(
|
|
123
|
+
withWideEvent(
|
|
124
|
+
'createCheckoutSessionAction',
|
|
125
|
+
async ({
|
|
126
|
+
parsedInput: { organizationId, plan, interval, withTrial },
|
|
127
|
+
ctx
|
|
128
|
+
}) => {
|
|
129
|
+
await assertCanManageOrganization(organizationId, ctx.user.id)
|
|
130
|
+
const sub = await getSubscription(organizationId)
|
|
131
|
+
|
|
132
|
+
if (hasLiveSubscription(sub.status) || sub.stripeSubscriptionId) {
|
|
133
|
+
throw new AppError(
|
|
134
|
+
AppErrorCode.CONFLICT,
|
|
135
|
+
'This organization already has a subscription. Change plans from Manage Billing.'
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const planDef = PLANS.find(item => item.id === plan)
|
|
140
|
+
const price = planDef?.stripePrice?.[interval]
|
|
141
|
+
if (!price) {
|
|
142
|
+
throw new AppError(
|
|
143
|
+
AppErrorCode.BAD_REQUEST,
|
|
144
|
+
'That plan is not available. Choose another plan.'
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// The webhook marks the trial as used once Stripe starts it, so an
|
|
149
|
+
// abandoned checkout does not use it up.
|
|
150
|
+
const trialAllowed =
|
|
151
|
+
withTrial && Boolean(planDef?.canUseTrial) && canStartTrial(sub)
|
|
152
|
+
|
|
153
|
+
const customerId = await ensureStripeCustomer(sub)
|
|
154
|
+
|
|
155
|
+
const session = await stripeClient.checkout.sessions.create({
|
|
156
|
+
mode: 'subscription',
|
|
157
|
+
customer: customerId,
|
|
158
|
+
line_items: [{ price: price.id, quantity: 1 }],
|
|
159
|
+
client_reference_id: organizationId,
|
|
160
|
+
allow_promotion_codes: true,
|
|
161
|
+
success_url: `${billingReturnUrl(organizationId)}?checkout=success`,
|
|
162
|
+
cancel_url: `${billingReturnUrl(organizationId)}?checkout=cancel`,
|
|
163
|
+
subscription_data: {
|
|
164
|
+
metadata: { organizationId },
|
|
165
|
+
...(trialAllowed
|
|
166
|
+
? {
|
|
167
|
+
trial_period_days: TRIAL_DAYS,
|
|
168
|
+
trial_settings: {
|
|
169
|
+
end_behavior: { missing_payment_method: 'cancel' }
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
: {})
|
|
173
|
+
},
|
|
174
|
+
...(trialAllowed
|
|
175
|
+
? { payment_method_collection: 'always' as const }
|
|
176
|
+
: {})
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
if (!session.url) {
|
|
180
|
+
throw new AppError(
|
|
181
|
+
AppErrorCode.INTERNAL,
|
|
182
|
+
'Stripe did not return a checkout page. Try again.'
|
|
183
|
+
)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return { url: session.url }
|
|
187
|
+
}
|
|
188
|
+
)
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
export const createPortalSessionAction = authActionClient
|
|
192
|
+
.inputSchema(z.object({ organizationId: stringValidation }))
|
|
193
|
+
.action(
|
|
194
|
+
withWideEvent(
|
|
195
|
+
'createPortalSessionAction',
|
|
196
|
+
async ({ parsedInput: { organizationId }, ctx }) => {
|
|
197
|
+
await assertCanManageOrganization(organizationId, ctx.user.id)
|
|
198
|
+
const sub = await getSubscription(organizationId)
|
|
199
|
+
|
|
200
|
+
if (!sub.stripeCustomerId) {
|
|
201
|
+
throw new AppError(
|
|
202
|
+
AppErrorCode.BAD_REQUEST,
|
|
203
|
+
'This organization has no billing account yet. Choose a paid plan first.'
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const session = await stripeClient.billingPortal.sessions.create({
|
|
208
|
+
customer: sub.stripeCustomerId,
|
|
209
|
+
return_url: billingReturnUrl(organizationId),
|
|
210
|
+
configuration: env.STRIPE_PORTAL_CONFIGURATION_ID
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
return { url: session.url }
|
|
214
|
+
}
|
|
215
|
+
)
|
|
216
|
+
)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use server'
|
|
2
|
+
|
|
3
|
+
import { db } from '~/db'
|
|
4
|
+
import { feedbacksTable } from '~/db/schema'
|
|
5
|
+
import { assertMember } from '~/lib/organization-access'
|
|
6
|
+
import { authActionClient, withWideEvent } from '~/lib/safe-action'
|
|
7
|
+
import { createFeedbackSchema } from './feedbacks.validation'
|
|
8
|
+
|
|
9
|
+
/** Any member can rate the product from inside an organization. */
|
|
10
|
+
export const createFeedbackAction = authActionClient
|
|
11
|
+
.inputSchema(createFeedbackSchema)
|
|
12
|
+
.action(
|
|
13
|
+
withWideEvent('createFeedbackAction', async ({ parsedInput, ctx }) => {
|
|
14
|
+
await assertMember(parsedInput.organizationId, ctx.user.id)
|
|
15
|
+
|
|
16
|
+
const [feedback] = await db
|
|
17
|
+
.insert(feedbacksTable)
|
|
18
|
+
.values({
|
|
19
|
+
userId: ctx.user.id,
|
|
20
|
+
organizationId: parsedInput.organizationId,
|
|
21
|
+
rating: parsedInput.rating,
|
|
22
|
+
comment: parsedInput.comment
|
|
23
|
+
})
|
|
24
|
+
.returning()
|
|
25
|
+
|
|
26
|
+
return feedback
|
|
27
|
+
})
|
|
28
|
+
)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import {
|
|
3
|
+
ratingValidation,
|
|
4
|
+
requiredCommentValidation,
|
|
5
|
+
stringValidation
|
|
6
|
+
} from '~/lib/validations'
|
|
7
|
+
|
|
8
|
+
export const createFeedbackSchema = z.object({
|
|
9
|
+
organizationId: stringValidation,
|
|
10
|
+
rating: ratingValidation,
|
|
11
|
+
comment: requiredCommentValidation
|
|
12
|
+
})
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
'use server'
|
|
2
|
+
|
|
3
|
+
import { and, asc, count, desc, eq, ilike, inArray, or } from 'drizzle-orm'
|
|
4
|
+
import { z } from 'zod'
|
|
5
|
+
import { SortOrderEnum } from '~/components/data-table/enum'
|
|
6
|
+
import { db } from '~/db'
|
|
7
|
+
import { invitationsTable, membersTable, usersTable } from '~/db/schema'
|
|
8
|
+
import { getPaginationSchema, getSortSchema } from '~/lib/actions'
|
|
9
|
+
import { MAX_FILTER_VALUES } from '~/lib/constants'
|
|
10
|
+
import { assertCanManageOrganization } from '~/lib/organization-access'
|
|
11
|
+
import { getOrganizationPlan } from '~/lib/plan-limits'
|
|
12
|
+
import { authActionClient, withWideEvent } from '~/lib/safe-action'
|
|
13
|
+
import { likeContains } from '~/lib/search'
|
|
14
|
+
import { optionalStringValidation, stringValidation } from '~/lib/validations'
|
|
15
|
+
|
|
16
|
+
export const getMembers = authActionClient
|
|
17
|
+
.inputSchema(
|
|
18
|
+
z.object({
|
|
19
|
+
organizationId: stringValidation,
|
|
20
|
+
...getPaginationSchema(),
|
|
21
|
+
...getSortSchema(['name', 'email', 'role', 'createdAt']),
|
|
22
|
+
search: optionalStringValidation,
|
|
23
|
+
filters: z
|
|
24
|
+
.object({
|
|
25
|
+
role: z.array(z.string()).max(MAX_FILTER_VALUES).optional()
|
|
26
|
+
})
|
|
27
|
+
.optional()
|
|
28
|
+
})
|
|
29
|
+
)
|
|
30
|
+
.action(
|
|
31
|
+
withWideEvent(
|
|
32
|
+
'getMembers',
|
|
33
|
+
async ({
|
|
34
|
+
parsedInput: {
|
|
35
|
+
organizationId,
|
|
36
|
+
page,
|
|
37
|
+
limit,
|
|
38
|
+
sortBy,
|
|
39
|
+
sortOrder,
|
|
40
|
+
search,
|
|
41
|
+
filters
|
|
42
|
+
},
|
|
43
|
+
ctx
|
|
44
|
+
}) => {
|
|
45
|
+
await assertCanManageOrganization(organizationId, ctx.user.id)
|
|
46
|
+
|
|
47
|
+
const where = and(
|
|
48
|
+
eq(membersTable.organizationId, organizationId),
|
|
49
|
+
search
|
|
50
|
+
? or(
|
|
51
|
+
ilike(usersTable.name, likeContains(search)),
|
|
52
|
+
ilike(usersTable.email, likeContains(search))
|
|
53
|
+
)
|
|
54
|
+
: undefined,
|
|
55
|
+
filters?.role?.length
|
|
56
|
+
? inArray(membersTable.role, filters.role)
|
|
57
|
+
: undefined
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
const sortColumn =
|
|
61
|
+
sortBy === 'name'
|
|
62
|
+
? usersTable.name
|
|
63
|
+
: sortBy === 'email'
|
|
64
|
+
? usersTable.email
|
|
65
|
+
: sortBy === 'role'
|
|
66
|
+
? membersTable.role
|
|
67
|
+
: membersTable.createdAt
|
|
68
|
+
|
|
69
|
+
const direction = sortOrder === SortOrderEnum.ASC ? asc : desc
|
|
70
|
+
|
|
71
|
+
const [rows, total] = await Promise.all([
|
|
72
|
+
db
|
|
73
|
+
.select({
|
|
74
|
+
id: membersTable.id,
|
|
75
|
+
userId: usersTable.id,
|
|
76
|
+
name: usersTable.name,
|
|
77
|
+
email: usersTable.email,
|
|
78
|
+
image: usersTable.image,
|
|
79
|
+
role: membersTable.role,
|
|
80
|
+
createdAt: membersTable.createdAt
|
|
81
|
+
})
|
|
82
|
+
.from(membersTable)
|
|
83
|
+
.innerJoin(usersTable, eq(membersTable.userId, usersTable.id))
|
|
84
|
+
.where(where)
|
|
85
|
+
.orderBy(direction(sortColumn))
|
|
86
|
+
.limit(limit)
|
|
87
|
+
.offset((page - 1) * limit),
|
|
88
|
+
db
|
|
89
|
+
.select({ count: count() })
|
|
90
|
+
.from(membersTable)
|
|
91
|
+
.innerJoin(usersTable, eq(membersTable.userId, usersTable.id))
|
|
92
|
+
.where(where)
|
|
93
|
+
])
|
|
94
|
+
|
|
95
|
+
return [rows, total[0]?.count ?? 0] as const
|
|
96
|
+
}
|
|
97
|
+
)
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
export const getInvitations = authActionClient
|
|
101
|
+
.inputSchema(
|
|
102
|
+
z.object({
|
|
103
|
+
organizationId: stringValidation,
|
|
104
|
+
...getPaginationSchema(),
|
|
105
|
+
...getSortSchema(['email', 'status', 'createdAt', 'expiresAt']),
|
|
106
|
+
search: optionalStringValidation,
|
|
107
|
+
filters: z
|
|
108
|
+
.object({
|
|
109
|
+
status: z.array(z.string()).max(MAX_FILTER_VALUES).optional()
|
|
110
|
+
})
|
|
111
|
+
.optional()
|
|
112
|
+
})
|
|
113
|
+
)
|
|
114
|
+
.action(
|
|
115
|
+
withWideEvent(
|
|
116
|
+
'getInvitations',
|
|
117
|
+
async ({
|
|
118
|
+
parsedInput: {
|
|
119
|
+
organizationId,
|
|
120
|
+
page,
|
|
121
|
+
limit,
|
|
122
|
+
sortBy,
|
|
123
|
+
sortOrder,
|
|
124
|
+
search,
|
|
125
|
+
filters
|
|
126
|
+
},
|
|
127
|
+
ctx
|
|
128
|
+
}) => {
|
|
129
|
+
await assertCanManageOrganization(organizationId, ctx.user.id)
|
|
130
|
+
|
|
131
|
+
const where = and(
|
|
132
|
+
eq(invitationsTable.organizationId, organizationId),
|
|
133
|
+
search
|
|
134
|
+
? ilike(invitationsTable.email, likeContains(search))
|
|
135
|
+
: undefined,
|
|
136
|
+
filters?.status?.length
|
|
137
|
+
? inArray(invitationsTable.status, filters.status)
|
|
138
|
+
: undefined
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
const direction = sortOrder === SortOrderEnum.ASC ? asc : desc
|
|
142
|
+
|
|
143
|
+
const [rows, total] = await Promise.all([
|
|
144
|
+
db.query.invitationsTable.findMany({
|
|
145
|
+
where,
|
|
146
|
+
with: { inviter: { columns: { name: true } } },
|
|
147
|
+
limit,
|
|
148
|
+
offset: (page - 1) * limit,
|
|
149
|
+
orderBy: [direction(invitationsTable[sortBy ?? 'createdAt'])]
|
|
150
|
+
}),
|
|
151
|
+
db.select({ count: count() }).from(invitationsTable).where(where)
|
|
152
|
+
])
|
|
153
|
+
|
|
154
|
+
const data = rows.map(row => ({
|
|
155
|
+
id: row.id,
|
|
156
|
+
email: row.email,
|
|
157
|
+
role: row.role ?? 'member',
|
|
158
|
+
status: row.status,
|
|
159
|
+
expiresAt: row.expiresAt,
|
|
160
|
+
createdAt: row.createdAt,
|
|
161
|
+
inviterName: row.inviter?.name ?? null
|
|
162
|
+
}))
|
|
163
|
+
|
|
164
|
+
return [data, total[0]?.count ?? 0] as const
|
|
165
|
+
}
|
|
166
|
+
)
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
/** Member count and the plan's member limit, for the invite button. */
|
|
170
|
+
export const getMemberUsage = authActionClient
|
|
171
|
+
.inputSchema(stringValidation)
|
|
172
|
+
.action(
|
|
173
|
+
withWideEvent(
|
|
174
|
+
'getMemberUsage',
|
|
175
|
+
async ({ parsedInput: organizationId, ctx }) => {
|
|
176
|
+
await assertCanManageOrganization(organizationId, ctx.user.id)
|
|
177
|
+
|
|
178
|
+
const [[row], { plan }] = await Promise.all([
|
|
179
|
+
db
|
|
180
|
+
.select({ count: count() })
|
|
181
|
+
.from(membersTable)
|
|
182
|
+
.where(eq(membersTable.organizationId, organizationId)),
|
|
183
|
+
getOrganizationPlan(organizationId)
|
|
184
|
+
])
|
|
185
|
+
|
|
186
|
+
return { memberCount: row?.count ?? 0, maxMembers: plan.maxMembers }
|
|
187
|
+
}
|
|
188
|
+
)
|
|
189
|
+
)
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
'use server'
|
|
2
|
+
|
|
3
|
+
import { and, desc, eq, ne } from 'drizzle-orm'
|
|
4
|
+
import { cache } from 'react'
|
|
5
|
+
import { z } from 'zod'
|
|
6
|
+
import { db } from '~/db'
|
|
7
|
+
import { membersTable, organizationsTable, usersTable } from '~/db/schema'
|
|
8
|
+
import { AppError, AppErrorCode, rethrowUniqueViolation } from '~/lib/errors'
|
|
9
|
+
import {
|
|
10
|
+
assertCanManageOrganization,
|
|
11
|
+
assertRole
|
|
12
|
+
} from '~/lib/organization-access'
|
|
13
|
+
import { authActionClient, withWideEvent } from '~/lib/safe-action'
|
|
14
|
+
import {
|
|
15
|
+
assertOwnedR2Key,
|
|
16
|
+
assertPersistableImageValue,
|
|
17
|
+
confirmUpload,
|
|
18
|
+
deleteOwnedFileQuietly,
|
|
19
|
+
isR2Key,
|
|
20
|
+
resolveImageUrl
|
|
21
|
+
} from '~/lib/storage'
|
|
22
|
+
import { stringValidation } from '~/lib/validations'
|
|
23
|
+
import { updateOrganizationSchema } from './organizations.validation'
|
|
24
|
+
|
|
25
|
+
/** Every organization the caller belongs to, newest membership first. */
|
|
26
|
+
export const getOrganizations = authActionClient.action(
|
|
27
|
+
withWideEvent('getOrganizations', async ({ ctx: { user } }) => {
|
|
28
|
+
const rows = await db
|
|
29
|
+
.select({
|
|
30
|
+
id: organizationsTable.id,
|
|
31
|
+
name: organizationsTable.name,
|
|
32
|
+
slug: organizationsTable.slug,
|
|
33
|
+
logo: organizationsTable.logo,
|
|
34
|
+
role: membersTable.role,
|
|
35
|
+
memberId: membersTable.id
|
|
36
|
+
})
|
|
37
|
+
.from(membersTable)
|
|
38
|
+
.innerJoin(
|
|
39
|
+
organizationsTable,
|
|
40
|
+
eq(membersTable.organizationId, organizationsTable.id)
|
|
41
|
+
)
|
|
42
|
+
.where(eq(membersTable.userId, user.id))
|
|
43
|
+
.orderBy(desc(membersTable.createdAt))
|
|
44
|
+
|
|
45
|
+
return rows.map(row => ({ ...row, logoUrl: resolveImageUrl(row.logo) }))
|
|
46
|
+
})
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
/** Per-request cache, so layouts and pages share one query. */
|
|
50
|
+
export const getOrganizationsCached = cache(getOrganizations)
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* One organization with the caller's role in it. Returns no data when the
|
|
54
|
+
* caller is not a member, so pages can respond with `notFound()`.
|
|
55
|
+
*/
|
|
56
|
+
export const getOrganization = authActionClient
|
|
57
|
+
.inputSchema(stringValidation)
|
|
58
|
+
.action(
|
|
59
|
+
withWideEvent(
|
|
60
|
+
'getOrganization',
|
|
61
|
+
async ({ parsedInput: organizationId, ctx: { user } }) => {
|
|
62
|
+
const [row] = await db
|
|
63
|
+
.select({
|
|
64
|
+
id: organizationsTable.id,
|
|
65
|
+
name: organizationsTable.name,
|
|
66
|
+
slug: organizationsTable.slug,
|
|
67
|
+
logo: organizationsTable.logo,
|
|
68
|
+
role: membersTable.role,
|
|
69
|
+
memberId: membersTable.id
|
|
70
|
+
})
|
|
71
|
+
.from(membersTable)
|
|
72
|
+
.innerJoin(
|
|
73
|
+
organizationsTable,
|
|
74
|
+
eq(membersTable.organizationId, organizationsTable.id)
|
|
75
|
+
)
|
|
76
|
+
.where(
|
|
77
|
+
and(
|
|
78
|
+
eq(membersTable.organizationId, organizationId),
|
|
79
|
+
eq(membersTable.userId, user.id)
|
|
80
|
+
)
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
return row ? { ...row, logoUrl: resolveImageUrl(row.logo) } : null
|
|
84
|
+
}
|
|
85
|
+
)
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
export const getOrganizationCached = cache(getOrganization)
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Saves the organization's name, slug, and logo. The logo must be an upload
|
|
92
|
+
* this organization owns (or a data URL while R2 is off); the replaced file
|
|
93
|
+
* is deleted.
|
|
94
|
+
*/
|
|
95
|
+
export const updateOrganizationAction = authActionClient
|
|
96
|
+
.inputSchema(updateOrganizationSchema)
|
|
97
|
+
.action(
|
|
98
|
+
withWideEvent(
|
|
99
|
+
'updateOrganizationAction',
|
|
100
|
+
async ({ parsedInput, ctx: { user } }) => {
|
|
101
|
+
const { organizationId } = parsedInput
|
|
102
|
+
await assertCanManageOrganization(organizationId, user.id)
|
|
103
|
+
|
|
104
|
+
const org = await db.query.organizationsTable.findFirst({
|
|
105
|
+
where: eq(organizationsTable.id, organizationId),
|
|
106
|
+
columns: { logo: true }
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
if (!org) {
|
|
110
|
+
throw new AppError(AppErrorCode.NOT_FOUND, 'Organization not found.')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const logo = parsedInput.logo || null
|
|
114
|
+
const logoChanged = logo !== org.logo
|
|
115
|
+
// An unchanged logo is already stored, even a data URL saved before R2
|
|
116
|
+
// was configured, so only a new value has to pass the storage rules.
|
|
117
|
+
if (logoChanged) assertPersistableImageValue(logo)
|
|
118
|
+
|
|
119
|
+
// Check ownership before saving, and delete the old file only after
|
|
120
|
+
// the save succeeds, so a failed save never loses the current logo.
|
|
121
|
+
if (logoChanged && isR2Key(logo)) {
|
|
122
|
+
await assertOwnedR2Key(logo, organizationId)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
await db
|
|
126
|
+
.update(organizationsTable)
|
|
127
|
+
.set({ name: parsedInput.name, slug: parsedInput.slug, logo })
|
|
128
|
+
.where(eq(organizationsTable.id, organizationId))
|
|
129
|
+
.catch(error =>
|
|
130
|
+
rethrowUniqueViolation(
|
|
131
|
+
error,
|
|
132
|
+
'Another organization already uses this slug. Choose a different one.'
|
|
133
|
+
)
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
if (logoChanged && isR2Key(logo)) {
|
|
137
|
+
await confirmUpload(logo, organizationId, org.logo)
|
|
138
|
+
} else if (logoChanged && org.logo) {
|
|
139
|
+
await deleteOwnedFileQuietly(org.logo, organizationId)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return true
|
|
143
|
+
}
|
|
144
|
+
)
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
/** Members the owner can hand ownership to: everyone except themselves. */
|
|
148
|
+
export const getTransferTargets = authActionClient
|
|
149
|
+
.inputSchema(stringValidation)
|
|
150
|
+
.action(
|
|
151
|
+
withWideEvent(
|
|
152
|
+
'getTransferTargets',
|
|
153
|
+
async ({ parsedInput: organizationId, ctx: { user } }) => {
|
|
154
|
+
const owner = await assertRole(organizationId, user.id, ['owner'])
|
|
155
|
+
|
|
156
|
+
return db
|
|
157
|
+
.select({
|
|
158
|
+
memberId: membersTable.id,
|
|
159
|
+
name: usersTable.name,
|
|
160
|
+
email: usersTable.email,
|
|
161
|
+
role: membersTable.role
|
|
162
|
+
})
|
|
163
|
+
.from(membersTable)
|
|
164
|
+
.innerJoin(usersTable, eq(membersTable.userId, usersTable.id))
|
|
165
|
+
.where(
|
|
166
|
+
and(
|
|
167
|
+
eq(membersTable.organizationId, organizationId),
|
|
168
|
+
ne(membersTable.id, owner.id)
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
.orderBy(usersTable.name)
|
|
172
|
+
}
|
|
173
|
+
)
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
/** Makes another member the owner. The current owner becomes an admin. */
|
|
177
|
+
export const transferOrganizationOwnershipAction = authActionClient
|
|
178
|
+
.inputSchema(
|
|
179
|
+
z.object({
|
|
180
|
+
organizationId: stringValidation,
|
|
181
|
+
targetMemberId: stringValidation
|
|
182
|
+
})
|
|
183
|
+
)
|
|
184
|
+
.action(
|
|
185
|
+
withWideEvent(
|
|
186
|
+
'transferOrganizationOwnershipAction',
|
|
187
|
+
async ({
|
|
188
|
+
parsedInput: { organizationId, targetMemberId },
|
|
189
|
+
ctx: { user }
|
|
190
|
+
}) => {
|
|
191
|
+
const owner = await assertRole(organizationId, user.id, ['owner'])
|
|
192
|
+
|
|
193
|
+
if (targetMemberId === owner.id) {
|
|
194
|
+
throw new AppError(
|
|
195
|
+
AppErrorCode.BAD_REQUEST,
|
|
196
|
+
'You already own this organization. Choose another member.'
|
|
197
|
+
)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const target = await db.query.membersTable.findFirst({
|
|
201
|
+
where: and(
|
|
202
|
+
eq(membersTable.id, targetMemberId),
|
|
203
|
+
eq(membersTable.organizationId, organizationId)
|
|
204
|
+
),
|
|
205
|
+
columns: { id: true }
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
if (!target) {
|
|
209
|
+
throw new AppError(
|
|
210
|
+
AppErrorCode.NOT_FOUND,
|
|
211
|
+
'That member is no longer in this organization. Refresh and choose again.'
|
|
212
|
+
)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
await db.transaction(async tx => {
|
|
216
|
+
await tx
|
|
217
|
+
.update(membersTable)
|
|
218
|
+
.set({ role: 'owner' })
|
|
219
|
+
.where(eq(membersTable.id, target.id))
|
|
220
|
+
|
|
221
|
+
await tx
|
|
222
|
+
.update(membersTable)
|
|
223
|
+
.set({ role: 'admin' })
|
|
224
|
+
.where(eq(membersTable.id, owner.id))
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
return true
|
|
228
|
+
}
|
|
229
|
+
)
|
|
230
|
+
)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import {
|
|
3
|
+
imageUrlValidation,
|
|
4
|
+
slugValidation,
|
|
5
|
+
stringValidation
|
|
6
|
+
} from '~/lib/validations'
|
|
7
|
+
|
|
8
|
+
export const organizationDetailsSchema = z.object({
|
|
9
|
+
name: stringValidation,
|
|
10
|
+
slug: slugValidation
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
export const updateOrganizationSchema = organizationDetailsSchema.extend({
|
|
14
|
+
organizationId: stringValidation,
|
|
15
|
+
/** Empty string clears the logo. */
|
|
16
|
+
logo: imageUrlValidation
|
|
17
|
+
})
|