@cedarjs/cli 9.0.0-canary.1784 → 9.0.0-canary.3124
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 +7 -10
- package/dist/cfw.js +10 -6
- package/dist/commands/build/buildHandler.js +220 -45
- package/dist/commands/build/buildPackagesTask.js +21 -20
- package/dist/commands/build.js +13 -2
- package/dist/commands/check.js +4 -6
- package/dist/commands/console.js +6 -4
- package/dist/commands/deploy/baremetal/SshExecutor.js +13 -4
- package/dist/commands/deploy/baremetal/baremetalHandler.js +116 -50
- package/dist/commands/deploy/baremetal.js +11 -4
- package/dist/commands/deploy/flightcontrolHandler.js +28 -15
- package/dist/commands/deploy/helpers/deployHandler.js +11 -6
- package/dist/commands/deploy/netlify.js +1 -1
- package/dist/commands/deploy/packing/nft.js +1 -2
- package/dist/commands/deploy/render.js +1 -1
- package/dist/commands/deploy/renderHandler.js +24 -13
- package/dist/commands/deploy/serverlessHandler.js +69 -27
- package/dist/commands/deploy/vercel.js +1 -1
- package/dist/commands/destroy/component/component.js +2 -3
- package/dist/commands/destroy/directive/directive.js +1 -3
- package/dist/commands/destroy/directive/directiveHandler.js +2 -4
- package/dist/commands/destroy/function/function.js +1 -3
- package/dist/commands/destroy/handlerHelpers.js +17 -17
- package/dist/commands/destroy/helpers.js +8 -2
- package/dist/commands/destroy/layout/layout.js +1 -2
- package/dist/commands/destroy/page/pageHandler.js +3 -3
- package/dist/commands/destroy/scaffold/scaffoldHandler.js +31 -12
- package/dist/commands/destroy/sdl/sdlHandler.js +3 -3
- package/dist/commands/dev/apiDebugFlag.js +5 -4
- package/dist/commands/dev/devHandler.js +219 -68
- package/dist/commands/dev/packageWatchCommands.js +8 -3
- package/dist/commands/dev.js +16 -6
- package/dist/commands/execHandler.js +49 -29
- package/dist/commands/experimental/live-queries/liveQueries.js +32 -0
- package/dist/commands/experimental/live-queries/liveQueriesHandler.js +448 -0
- package/dist/commands/experimental/live-queries/templates/liveQueriesListener.ts.template +153 -0
- package/dist/commands/experimental/live-queries/templates/migration.sql.template +100 -0
- package/dist/commands/experimental/setupInngest.js +2 -2
- package/dist/commands/experimental/setupInngestHandler.js +13 -12
- package/dist/commands/experimental/setupOpentelemetry.js +2 -2
- package/dist/commands/experimental/setupOpentelemetryHandler.js +14 -10
- package/dist/commands/experimental/setupReactCompiler.js +2 -2
- package/dist/commands/experimental/setupReactCompilerHandler.js +48 -33
- package/dist/commands/experimental/setupRsc.js +1 -1
- package/dist/commands/experimental/setupRscHandler.js +20 -16
- package/dist/commands/experimental/setupStreamingSsr.js +2 -2
- package/dist/commands/experimental/setupStreamingSsrHandler.js +49 -34
- package/dist/commands/experimental/templates/opentelemetry.ts.template +2 -1
- package/dist/commands/experimental/templates/rsc/Document.tsx.template +1 -1
- package/dist/commands/experimental/templates/rsc/entry.client.tsx.template +8 -8
- package/dist/commands/experimental/templates/streamingSsr/Document.tsx.template +1 -1
- package/dist/commands/experimental/templates/streamingSsr/entry.client.tsx.template +6 -6
- package/dist/commands/experimental/templates/streamingSsr/tsconfig.json.template +6 -6
- package/dist/commands/experimental/util.js +27 -25
- package/dist/commands/experimental.js +3 -2
- package/dist/commands/generate/cell/cell.js +20 -0
- package/dist/commands/generate/cell/cellHandler.js +89 -40
- package/dist/commands/generate/cell/templates/cell.tsx.template +21 -2
- package/dist/commands/generate/cell/templates/cellFragment.tsx.template +43 -0
- package/dist/commands/generate/cell/templates/cellList.tsx.template +21 -2
- package/dist/commands/generate/cell/templates/storiesFragment.tsx.template +23 -0
- package/dist/commands/generate/cell/templates/test.js.template +1 -1
- package/dist/commands/generate/cell/templates/testFragment.js.template +30 -0
- package/dist/commands/generate/component/componentHandler.js +14 -16
- package/dist/commands/generate/dataMigration/dataMigration.js +36 -18
- package/dist/commands/generate/dataMigration/templates/dataMigration.js.template +1 -1
- package/dist/commands/generate/dataMigration/templates/dataMigration.ts.template +1 -1
- package/dist/commands/generate/dbAuth/dbAuth.js +1 -0
- package/dist/commands/generate/dbAuth/dbAuthHandler.js +63 -34
- package/dist/commands/generate/dbAuth/templates/login.oauth.tsx.template +164 -0
- package/dist/commands/generate/dbAuth/templates/signup.oauth.tsx.template +147 -0
- package/dist/commands/generate/directive/directiveHandler.js +14 -16
- package/dist/commands/generate/function/function.js +1 -0
- package/dist/commands/generate/function/functionHandler.js +14 -19
- package/dist/commands/generate/helpers.js +34 -18
- package/dist/commands/generate/job/job.js +1 -0
- package/dist/commands/generate/job/jobHandler.js +20 -24
- package/dist/commands/generate/layout/layoutHandler.js +13 -16
- package/dist/commands/generate/model/model.js +1 -0
- package/dist/commands/generate/model/modelHandler.js +11 -5
- package/dist/commands/generate/ogImage/ogImage.js +1 -0
- package/dist/commands/generate/ogImage/ogImageHandler.js +18 -11
- package/dist/commands/generate/package/filesTask.js +9 -0
- package/dist/commands/generate/package/packageHandler.js +50 -61
- package/dist/commands/generate/package/templates/test.ts.template +2 -2
- package/dist/commands/generate/package/templates/vitest.config.ts.template +9 -0
- package/dist/commands/generate/page/pageHandler.js +27 -24
- package/dist/commands/generate/realtime/realtime.js +2 -2
- package/dist/commands/generate/realtime/realtimeHandler.js +51 -32
- package/dist/commands/generate/realtime/templates/subscriptions/blank/blank.ts.template +1 -1
- package/dist/commands/generate/scaffold/scaffold.js +1 -0
- package/dist/commands/generate/scaffold/scaffoldHandler.js +176 -49
- package/dist/commands/generate/scaffold/templates/lib/formatters.tsx.template +1 -1
- package/dist/commands/generate/script/script.js +1 -0
- package/dist/commands/generate/script/scriptHandler.js +10 -6
- package/dist/commands/generate/script/templates/script.ts.template +9 -4
- package/dist/commands/generate/script/templates/tsconfig.json.template +16 -2
- package/dist/commands/generate/sdl/sdl.js +4 -3
- package/dist/commands/generate/sdl/sdlHandler.js +135 -46
- package/dist/commands/generate/sdl/stubFiles.js +132 -0
- package/dist/commands/generate/sdl/templates/sdl.js.template +2 -1
- package/dist/commands/generate/sdl/templates/sdl.ts.template +2 -1
- package/dist/commands/generate/secret/secret.js +20 -20
- package/dist/commands/generate/service/service.js +5 -5
- package/dist/commands/generate/service/serviceHandler.js +126 -61
- package/dist/commands/generate/service/templates/scenarios.ts.template +4 -2
- package/dist/commands/generate/service/templates/service.ts.template +22 -8
- package/dist/commands/generate/service/templates/test.ts.template +2 -2
- package/dist/commands/generate/yargsCommandHelpers.js +7 -2
- package/dist/commands/generate/yargsHandlerHelpers.js +20 -18
- package/dist/commands/generate.js +4 -4
- package/dist/commands/info.js +2 -2
- package/dist/commands/jobsHandler.js +7 -4
- package/dist/commands/lint.js +26 -69
- package/dist/commands/prerenderHandler.js +95 -58
- package/dist/commands/prismaHandler.js +51 -26
- package/dist/commands/record/init.js +2 -2
- package/dist/commands/record.js +4 -3
- package/dist/commands/serve.js +245 -31
- package/dist/commands/serveApiHandler.js +7 -6
- package/dist/commands/serveBothHandler.js +22 -13
- package/dist/commands/serveWebHandler.js +3 -2
- package/dist/commands/setup/auth/auth.js +15 -11
- package/dist/commands/setup/cache/cache.js +1 -1
- package/dist/commands/setup/cache/cacheHandler.js +15 -9
- package/dist/commands/setup/database/database.js +15 -0
- package/dist/commands/setup/database/postgres.js +19 -0
- package/dist/commands/setup/database/postgresHandler.js +196 -0
- package/dist/commands/setup/database/templates/db.ts.template +32 -0
- package/dist/commands/setup/deploy/deploy.js +4 -3
- package/dist/commands/setup/deploy/helpers/helpers.js +5 -1
- package/dist/commands/setup/deploy/helpers/index.js +132 -6
- package/dist/commands/setup/deploy/providers/baremetalHandler.js +28 -6
- package/dist/commands/setup/deploy/providers/coherence.js +1 -1
- package/dist/commands/setup/deploy/providers/coherenceHandler.js +60 -31
- package/dist/commands/setup/deploy/providers/flightcontrolHandler.js +69 -51
- package/dist/commands/setup/deploy/providers/netlify.js +6 -0
- package/dist/commands/setup/deploy/providers/netlifyHandler.js +114 -13
- package/dist/commands/setup/deploy/providers/renderHandler.js +44 -41
- package/dist/commands/setup/deploy/providers/serverlessHandler.js +7 -5
- package/dist/commands/setup/deploy/providers/universal-deploy.js +11 -0
- package/dist/commands/setup/deploy/providers/universalDeployHandler.js +186 -0
- package/dist/commands/setup/deploy/providers/vercel.js +6 -0
- package/dist/commands/setup/deploy/providers/vercelHandler.js +115 -14
- package/dist/commands/setup/deploy/templates/baremetal.js +3 -2
- package/dist/commands/setup/deploy/templates/flightcontrol.js +60 -49
- package/dist/commands/setup/deploy/templates/netlify.js +2 -1
- package/dist/commands/setup/deploy/templates/netlifyUD.js +31 -0
- package/dist/commands/setup/deploy/templates/render.js +32 -17
- package/dist/commands/setup/docker/docker.js +2 -2
- package/dist/commands/setup/docker/dockerHandler.js +74 -39
- package/dist/commands/setup/docker/templates/Dockerfile.npm +119 -0
- package/dist/commands/setup/docker/templates/Dockerfile.pnpm +112 -0
- package/dist/commands/setup/docker/templates/{Dockerfile → Dockerfile.yarn} +4 -7
- package/dist/commands/setup/docker/templates/docker-compose.dev.yml +3 -3
- package/dist/commands/setup/docker/templates/docker-compose.prod.yml +2 -5
- package/dist/commands/setup/docker/templates/dockerignore +1 -0
- package/dist/commands/setup/generator/generator.js +1 -1
- package/dist/commands/setup/generator/generatorHandler.js +5 -4
- package/dist/commands/setup/graphql/features/fragments/appGqlConfigTransform.js +6 -3
- package/dist/commands/setup/graphql/features/fragments/fragments.js +1 -1
- package/dist/commands/setup/graphql/features/fragments/fragmentsHandler.js +5 -7
- package/dist/commands/setup/graphql/features/trustedDocuments/trustedDocuments.js +1 -1
- package/dist/commands/setup/graphql/features/trustedDocuments/trustedDocumentsHandler.js +4 -3
- package/dist/commands/setup/i18n/i18n.js +1 -1
- package/dist/commands/setup/i18n/i18nHandler.js +21 -17
- package/dist/commands/setup/jobs/jobs.js +1 -1
- package/dist/commands/setup/jobs/jobsHandler.js +17 -12
- package/dist/commands/setup/mailer/mailer.js +1 -1
- package/dist/commands/setup/mailer/mailerHandler.js +13 -11
- package/dist/commands/setup/middleware/ogImage/ogImage.js +1 -1
- package/dist/commands/setup/middleware/ogImage/ogImageHandler.js +2 -1
- package/dist/commands/setup/monitoring/sentry/sentry.js +1 -1
- package/dist/commands/setup/monitoring/sentry/sentryHandler.js +18 -12
- package/dist/commands/setup/neon/neon.js +35 -0
- package/dist/commands/setup/neon/neonHandler.js +264 -0
- package/dist/commands/setup/package/package.js +1 -1
- package/dist/commands/setup/package/packageHandler.js +18 -14
- package/dist/commands/setup/realtime/addRealtimeToGraphql.js +1 -1
- package/dist/commands/setup/realtime/realtime.js +1 -1
- package/dist/commands/setup/realtime/realtimeHandler.js +26 -37
- package/dist/commands/setup/realtime/templates/realtime.ts.template +1 -1
- package/dist/commands/setup/realtime/templates/subscriptions/countdown/countdown.ts.template +1 -1
- package/dist/commands/setup/realtime/templates/subscriptions/newMessage/newMessage.ts.template +1 -1
- package/dist/commands/setup/server-file/serverFile.js +1 -1
- package/dist/commands/setup/server-file/serverFileHandler.js +18 -10
- package/dist/commands/setup/tenancy/authCodemod.js +66 -0
- package/dist/commands/setup/tenancy/dbCodemod.js +64 -0
- package/dist/commands/setup/tenancy/graphqlCodemod.js +88 -0
- package/dist/commands/setup/tenancy/preflight.js +49 -0
- package/dist/commands/setup/tenancy/schemaPrisma.js +81 -0
- package/dist/commands/setup/tenancy/signupCodemod.js +153 -0
- package/dist/commands/setup/tenancy/templates/InvitePage.tsx.template +63 -0
- package/dist/commands/setup/tenancy/templates/OrgScope.tsx.template +40 -0
- package/dist/commands/setup/tenancy/templates/ensureDefaultOrganizations.dataMigration.ts.template +39 -0
- package/dist/commands/setup/tenancy/templates/organizations.scenarios.ts.template +59 -0
- package/dist/commands/setup/tenancy/templates/organizations.sdl.ts.template +89 -0
- package/dist/commands/setup/tenancy/templates/organizations.test.ts.template +389 -0
- package/dist/commands/setup/tenancy/templates/organizations.ts.template +394 -0
- package/dist/commands/setup/tenancy/templates/requireMembership.test.ts.template +35 -0
- package/dist/commands/setup/tenancy/templates/requireMembership.ts.template +25 -0
- package/dist/commands/setup/tenancy/tenancy.js +30 -0
- package/dist/commands/setup/tenancy/tenancyHandler.js +425 -0
- package/dist/commands/setup/ui/helpers/helpers.js +5 -1
- package/dist/commands/setup/ui/libraries/chakra-uiHandler.js +16 -8
- package/dist/commands/setup/ui/libraries/mantineHandler.js +26 -16
- package/dist/commands/setup/ui/libraries/tailwindcssHandler.js +40 -29
- package/dist/commands/setup/uploads/schemaPrisma.js +41 -0
- package/dist/commands/setup/uploads/serverFile.js +151 -0
- package/dist/commands/setup/uploads/templates/requireUploadToken.ts.template +15 -0
- package/dist/commands/setup/uploads/templates/srcLibUploads.ts.template +23 -24
- package/dist/commands/setup/uploads/templates/uploads.sdl.ts.template +79 -0
- package/dist/commands/setup/uploads/templates/uploads.ts.template +89 -0
- package/dist/commands/setup/uploads/templates/withDataUri.ts.template +14 -0
- package/dist/commands/setup/uploads/templates/withSignedUrl.ts.template +16 -0
- package/dist/commands/setup/uploads/uploads.js +49 -7
- package/dist/commands/setup/uploads/uploadsHandler.js +266 -72
- package/dist/commands/setup.js +5 -4
- package/dist/commands/studio.js +1 -1
- package/dist/commands/studioHandler.js +2 -2
- package/dist/commands/test/datasourceWarning.js +42 -0
- package/dist/commands/test/testHandler.js +104 -0
- package/dist/commands/test/viteVersionCheck.js +75 -0
- package/dist/commands/test.js +16 -19
- package/dist/commands/type-check.js +2 -1
- package/dist/commands/type-checkHandler.js +25 -18
- package/dist/commands/upgrade/preUpgradeScripts.js +11 -11
- package/dist/commands/upgrade/upgrade.js +4 -2
- package/dist/commands/upgrade/upgradeHandler.js +97 -73
- package/dist/index.js +15 -11
- package/dist/lib/background.js +22 -1
- package/dist/lib/configureStorybook.js +2 -2
- package/dist/lib/exec.js +68 -63
- package/dist/lib/exit.js +20 -5
- package/dist/lib/extendFile.js +23 -13
- package/dist/lib/generatePrismaClient.js +48 -12
- package/dist/lib/index.js +137 -106
- package/dist/lib/loadEnvFiles.js +1 -1
- package/dist/lib/locking.js +8 -4
- package/dist/lib/merge/algorithms.js +4 -1
- package/dist/lib/merge/index.js +78 -41
- package/dist/lib/merge/semanticIdentity.js +18 -6
- package/dist/lib/merge/strategy.js +86 -36
- package/dist/lib/packages.js +23 -13
- package/dist/lib/plugin.js +13 -7
- package/dist/lib/pluralHelpers.js +6 -2
- package/dist/lib/project.js +3 -1
- package/dist/lib/rollback.js +7 -7
- package/dist/lib/schemaHelpers.js +16 -19
- package/dist/lib/test.js +20 -5
- package/dist/lib/updateCheck.js +47 -18
- package/dist/middleware/checkNodeVersion.js +9 -4
- package/dist/middleware/{detectProjectRxVersion.js → detectProjectCedarVersion.js} +2 -2
- package/dist/plugin.js +18 -12
- package/dist/telemetry/exporter.js +12 -5
- package/dist/telemetry/index.js +4 -3
- package/dist/telemetry/resource.js +9 -12
- package/dist/telemetry/send.js +14 -10
- package/dist/testLib/cells.js +5 -3
- package/package.json +44 -41
- package/LICENSE +0 -21
- package/dist/commands/consoleHandler.js +0 -68
- package/dist/commands/setup/tsconfig/tsconfig.js +0 -25
- package/dist/commands/setup/tsconfig/tsconfigHandler.js +0 -59
- package/dist/commands/setup/uploads/dbCodemod.js +0 -28
- package/dist/commands/setup/uploads/templates/signedUrl.ts.template +0 -21
- package/dist/commands/setup/vite/templates/vite.config.ts.template +0 -19
- package/dist/commands/setup/vite/vite.js +0 -38
- package/dist/commands/setup/vite/viteHandler.js +0 -83
- package/dist/commands/testEsm.js +0 -36
- package/dist/commands/testHandler.js +0 -137
- package/dist/commands/testHandlerEsm.js +0 -85
- package/dist/commands/ts-to-js.js +0 -20
- package/dist/lib/cedarPluralize.js +0 -56
- package/dist/lib/colors.js +0 -18
- package/dist/testLib/getFilesWithPattern.js +0 -21
- /package/dist/{index.d.js → global.d.js} +0 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ScenarioData } from '@cedarjs/testing/api'
|
|
2
|
+
|
|
3
|
+
import type { Membership, Organization, Prisma } from 'src/lib/db'
|
|
4
|
+
|
|
5
|
+
// This scenario assumes a dbAuth-style `User` model (`email`,
|
|
6
|
+
// `hashedPassword`, `salt`). Adjust the `user` fixture below to match your
|
|
7
|
+
// app's actual `User` fields if you use a different auth provider.
|
|
8
|
+
export const standard = defineScenario<
|
|
9
|
+
Prisma.OrganizationCreateArgs | Prisma.MembershipCreateArgs
|
|
10
|
+
>({
|
|
11
|
+
organization: {
|
|
12
|
+
acme: {
|
|
13
|
+
data: {
|
|
14
|
+
name: 'Acme Inc',
|
|
15
|
+
slug: 'acme-inc',
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
membership: {
|
|
20
|
+
owner: {
|
|
21
|
+
data: {
|
|
22
|
+
role: 'owner',
|
|
23
|
+
organization: { connect: { slug: 'acme-inc' } },
|
|
24
|
+
user: {
|
|
25
|
+
create: {
|
|
26
|
+
email: 'owner@example.com',
|
|
27
|
+
hashedPassword: 'x',
|
|
28
|
+
salt: 'x',
|
|
29
|
+
fullName: 'Org Owner',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
admin: {
|
|
35
|
+
data: {
|
|
36
|
+
role: 'admin',
|
|
37
|
+
organization: { connect: { slug: 'acme-inc' } },
|
|
38
|
+
user: {
|
|
39
|
+
create: {
|
|
40
|
+
email: 'admin@example.com',
|
|
41
|
+
hashedPassword: 'x',
|
|
42
|
+
salt: 'x',
|
|
43
|
+
fullName: 'Org Admin',
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
pending: {
|
|
49
|
+
data: {
|
|
50
|
+
role: 'admin',
|
|
51
|
+
invitationToken: 'invite-token',
|
|
52
|
+
organization: { connect: { slug: 'acme-inc' } },
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
export type StandardScenario = ScenarioData<Organization, 'organization', 'acme'> &
|
|
59
|
+
ScenarioData<Membership, 'membership', 'owner' | 'admin' | 'pending'>
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
export const schema = gql`
|
|
2
|
+
type Organization {
|
|
3
|
+
id: String!
|
|
4
|
+
name: String!
|
|
5
|
+
slug: String!
|
|
6
|
+
createdAt: DateTime!
|
|
7
|
+
updatedAt: DateTime!
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
type Membership {
|
|
11
|
+
id: String!
|
|
12
|
+
role: String!
|
|
13
|
+
userId: String
|
|
14
|
+
organizationId: String!
|
|
15
|
+
invitedById: String
|
|
16
|
+
createdAt: DateTime!
|
|
17
|
+
updatedAt: DateTime!
|
|
18
|
+
organization: Organization!
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
"""
|
|
22
|
+
inviteMember's result. invitationToken is deliberately not a Membership
|
|
23
|
+
field: Membership is readable by any member (myMemberships, removeMember),
|
|
24
|
+
and a selectable invitationToken there would let any member read a
|
|
25
|
+
pending invitation's token and claim it themselves. Invitation exists only
|
|
26
|
+
as inviteMember's return type, so the token reaches only the caller who
|
|
27
|
+
just created the invitation -- the one the generated flow expects to email
|
|
28
|
+
it onward.
|
|
29
|
+
"""
|
|
30
|
+
type Invitation {
|
|
31
|
+
membership: Membership!
|
|
32
|
+
invitationToken: String!
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
input CreateOrganizationInput {
|
|
36
|
+
name: String!
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
input InviteMemberInput {
|
|
40
|
+
email: String!
|
|
41
|
+
role: String!
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type Query {
|
|
45
|
+
"Every membership belonging to the signed-in user, across all organizations they belong to."
|
|
46
|
+
myMemberships: [Membership!]! @requireAuth
|
|
47
|
+
|
|
48
|
+
"The current organization, resolved by resolveCurrentOrg for this request."
|
|
49
|
+
organization(id: String!): Organization @requireMembership
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type Mutation {
|
|
53
|
+
"Creates a new organization with the caller as its owner."
|
|
54
|
+
createOrganization(input: CreateOrganizationInput!): Organization!
|
|
55
|
+
@requireAuth
|
|
56
|
+
|
|
57
|
+
"""
|
|
58
|
+
Creates a pending membership (an invitation) in the current organization,
|
|
59
|
+
for a role the caller's own role may grant (see ROLE_GRANTS in
|
|
60
|
+
organizations.ts) -- an admin can invite an admin or a member, but not an
|
|
61
|
+
owner. The app is responsible for emailing input.email a link built from
|
|
62
|
+
the returned invitationToken; Membership has no email column of its own.
|
|
63
|
+
Web callers must call reauthenticate() from useAuth() in onCompleted so
|
|
64
|
+
the invite shows up if the inviter is also the invitee.
|
|
65
|
+
"""
|
|
66
|
+
inviteMember(input: InviteMemberInput!): Invitation!
|
|
67
|
+
@requireMembership(roles: ["owner", "admin"])
|
|
68
|
+
|
|
69
|
+
"""
|
|
70
|
+
Attaches the pending membership matching token to the signed-in user, or
|
|
71
|
+
is a no-op if it is already attached to them. Throws if the token is
|
|
72
|
+
invalid or was already claimed by someone else.
|
|
73
|
+
Web callers must call reauthenticate() from useAuth() in onCompleted so
|
|
74
|
+
the new membership is reflected in currentUser.memberships.
|
|
75
|
+
"""
|
|
76
|
+
acceptInvitation(token: String!): Organization! @requireAuth
|
|
77
|
+
|
|
78
|
+
"""
|
|
79
|
+
Removes a membership from the current organization, for a role the
|
|
80
|
+
caller's own role may grant (the same rule inviteMember uses) -- an
|
|
81
|
+
admin can never remove an owner, and an organization's last owner can
|
|
82
|
+
never be removed.
|
|
83
|
+
Web callers must call reauthenticate() from useAuth() in onCompleted when
|
|
84
|
+
removing their own membership.
|
|
85
|
+
"""
|
|
86
|
+
removeMember(membershipId: String!): Membership!
|
|
87
|
+
@requireMembership(roles: ["owner", "admin"])
|
|
88
|
+
}
|
|
89
|
+
`
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import { setContext } from '@cedarjs/context'
|
|
2
|
+
|
|
3
|
+
import { db } from 'src/lib/db'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
myMemberships,
|
|
7
|
+
organization,
|
|
8
|
+
inviteMember,
|
|
9
|
+
acceptInvitation,
|
|
10
|
+
removeMember,
|
|
11
|
+
ensureDefaultOrganization,
|
|
12
|
+
} from './organizations.js'
|
|
13
|
+
import type { StandardScenario } from './organizations.scenarios.js'
|
|
14
|
+
|
|
15
|
+
// Generated boilerplate tests do not account for all circumstances and can
|
|
16
|
+
// fail without adjustments, e.g. a `User` model shaped by a different auth
|
|
17
|
+
// provider than the dbAuth one this scenario assumes. Please refer to the
|
|
18
|
+
// CedarJS Testing Docs:
|
|
19
|
+
// https://cedarjs.com/docs/testing#testing-services
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* `context.currentUser` is typed from this app's `getCurrentUser`, which
|
|
23
|
+
* selects more fields than these tests need.
|
|
24
|
+
*/
|
|
25
|
+
const asCurrentUser = (user: { id: string; memberships?: unknown[] }) =>
|
|
26
|
+
user as unknown as typeof context.currentUser
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Sets `context.currentUser` and `context.currentOrg` the way the GraphQL
|
|
30
|
+
* `context` function does for an authenticated, in-organization request.
|
|
31
|
+
*/
|
|
32
|
+
const asMember = (
|
|
33
|
+
scenario: StandardScenario,
|
|
34
|
+
membership: { id: string; userId: string | null; role: string },
|
|
35
|
+
) => {
|
|
36
|
+
setContext({
|
|
37
|
+
currentUser: asCurrentUser({ id: membership.userId! }),
|
|
38
|
+
currentOrg: {
|
|
39
|
+
id: scenario.organization.acme.id,
|
|
40
|
+
slug: scenario.organization.acme.slug,
|
|
41
|
+
role: membership.role,
|
|
42
|
+
membershipId: membership.id,
|
|
43
|
+
},
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
describe('organizations service', () => {
|
|
48
|
+
scenario(
|
|
49
|
+
"myMemberships returns the signed-in user's memberships",
|
|
50
|
+
async (scenario: StandardScenario) => {
|
|
51
|
+
setContext({
|
|
52
|
+
currentUser: asCurrentUser({ id: scenario.membership.owner.userId! }),
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
const result = await myMemberships()
|
|
56
|
+
|
|
57
|
+
expect(result).toHaveLength(1)
|
|
58
|
+
expect(result[0]).toMatchObject({ id: scenario.membership.owner.id })
|
|
59
|
+
},
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
scenario(
|
|
63
|
+
'organization returns the current organization',
|
|
64
|
+
async (scenario: StandardScenario) => {
|
|
65
|
+
asMember(scenario, scenario.membership.owner)
|
|
66
|
+
|
|
67
|
+
const result = await organization({ id: scenario.organization.acme.id })
|
|
68
|
+
|
|
69
|
+
expect(result?.id).toEqual(scenario.organization.acme.id)
|
|
70
|
+
},
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
scenario(
|
|
74
|
+
'organization refuses an id other than the current organization',
|
|
75
|
+
async (scenario: StandardScenario) => {
|
|
76
|
+
asMember(scenario, scenario.membership.owner)
|
|
77
|
+
|
|
78
|
+
await expect(organization({ id: 'some-other-org' })).rejects.toThrow()
|
|
79
|
+
},
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
describe('inviteMember', () => {
|
|
83
|
+
scenario(
|
|
84
|
+
'an owner can invite a member',
|
|
85
|
+
async (scenario: StandardScenario) => {
|
|
86
|
+
asMember(scenario, scenario.membership.owner)
|
|
87
|
+
|
|
88
|
+
const result = await inviteMember({
|
|
89
|
+
input: { email: 'new-member@example.com', role: 'member' },
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
expect(result.invitationToken).toBeTruthy()
|
|
93
|
+
|
|
94
|
+
// `Invitation.membership` is resolver-backed like any relation
|
|
95
|
+
// field, so its generated type allows a promise; read the row back
|
|
96
|
+
// from `db` instead of asserting on `result.membership` directly.
|
|
97
|
+
const created = await db.membership.findFirst({
|
|
98
|
+
where: { invitedById: scenario.membership.owner.id },
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
expect(created?.role).toEqual('member')
|
|
102
|
+
expect(created?.invitationToken).toEqual(result.invitationToken)
|
|
103
|
+
},
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
scenario(
|
|
107
|
+
'an admin cannot invite an owner',
|
|
108
|
+
async (scenario: StandardScenario) => {
|
|
109
|
+
asMember(scenario, scenario.membership.admin)
|
|
110
|
+
|
|
111
|
+
await expect(
|
|
112
|
+
inviteMember({
|
|
113
|
+
input: { email: 'new-owner@example.com', role: 'owner' },
|
|
114
|
+
}),
|
|
115
|
+
).rejects.toThrow()
|
|
116
|
+
},
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
scenario(
|
|
120
|
+
'an unknown role is rejected',
|
|
121
|
+
async (scenario: StandardScenario) => {
|
|
122
|
+
asMember(scenario, scenario.membership.owner)
|
|
123
|
+
|
|
124
|
+
await expect(
|
|
125
|
+
inviteMember({
|
|
126
|
+
input: { email: 'typo@example.com', role: 'superadmin' },
|
|
127
|
+
}),
|
|
128
|
+
).rejects.toThrow()
|
|
129
|
+
},
|
|
130
|
+
)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
describe('removeMember', () => {
|
|
134
|
+
scenario(
|
|
135
|
+
'an owner can remove an admin',
|
|
136
|
+
async (scenario: StandardScenario) => {
|
|
137
|
+
asMember(scenario, scenario.membership.owner)
|
|
138
|
+
|
|
139
|
+
await removeMember({ membershipId: scenario.membership.admin.id })
|
|
140
|
+
|
|
141
|
+
const remaining = await db.membership.findUnique({
|
|
142
|
+
where: { id: scenario.membership.admin.id },
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
expect(remaining).toBeNull()
|
|
146
|
+
},
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
scenario(
|
|
150
|
+
'an admin cannot remove an owner',
|
|
151
|
+
async (scenario: StandardScenario) => {
|
|
152
|
+
asMember(scenario, scenario.membership.admin)
|
|
153
|
+
|
|
154
|
+
await expect(
|
|
155
|
+
removeMember({ membershipId: scenario.membership.owner.id }),
|
|
156
|
+
).rejects.toThrow()
|
|
157
|
+
},
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
scenario(
|
|
161
|
+
"the organization's last owner cannot be removed",
|
|
162
|
+
async (scenario: StandardScenario) => {
|
|
163
|
+
asMember(scenario, scenario.membership.owner)
|
|
164
|
+
|
|
165
|
+
await expect(
|
|
166
|
+
removeMember({ membershipId: scenario.membership.owner.id }),
|
|
167
|
+
).rejects.toThrow()
|
|
168
|
+
},
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
scenario(
|
|
172
|
+
'an unclaimed owner invitation does not count as the remaining owner',
|
|
173
|
+
async (scenario: StandardScenario) => {
|
|
174
|
+
asMember(scenario, scenario.membership.owner)
|
|
175
|
+
|
|
176
|
+
// Nobody has accepted this one yet, so it leaves the organization
|
|
177
|
+
// with no owner who can administer it.
|
|
178
|
+
await db.membership.create({
|
|
179
|
+
data: {
|
|
180
|
+
role: 'owner',
|
|
181
|
+
organizationId: scenario.organization.acme.id,
|
|
182
|
+
invitationToken: 'pending-owner-token',
|
|
183
|
+
},
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
await expect(
|
|
187
|
+
removeMember({ membershipId: scenario.membership.owner.id }),
|
|
188
|
+
).rejects.toThrow()
|
|
189
|
+
},
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
scenario(
|
|
193
|
+
'an owner can be removed once another owner has accepted',
|
|
194
|
+
async (scenario: StandardScenario) => {
|
|
195
|
+
asMember(scenario, scenario.membership.owner)
|
|
196
|
+
|
|
197
|
+
const second = await db.user.create({
|
|
198
|
+
data: {
|
|
199
|
+
email: 'second-owner@example.com',
|
|
200
|
+
hashedPassword: 'x',
|
|
201
|
+
salt: 'x',
|
|
202
|
+
fullName: 'Second Owner',
|
|
203
|
+
},
|
|
204
|
+
})
|
|
205
|
+
await db.membership.create({
|
|
206
|
+
data: {
|
|
207
|
+
role: 'owner',
|
|
208
|
+
organizationId: scenario.organization.acme.id,
|
|
209
|
+
userId: second.id,
|
|
210
|
+
},
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
const result = await removeMember({
|
|
214
|
+
membershipId: scenario.membership.owner.id,
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
expect(result.id).toEqual(scenario.membership.owner.id)
|
|
218
|
+
await expect(
|
|
219
|
+
db.membership.findUnique({
|
|
220
|
+
where: { id: scenario.membership.owner.id },
|
|
221
|
+
}),
|
|
222
|
+
).resolves.toBeNull()
|
|
223
|
+
},
|
|
224
|
+
)
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
describe('acceptInvitation', () => {
|
|
228
|
+
scenario(
|
|
229
|
+
'attaches a pending invitation to the signed-in user',
|
|
230
|
+
async (scenario: StandardScenario) => {
|
|
231
|
+
const invitee = await db.user.create({
|
|
232
|
+
data: {
|
|
233
|
+
email: 'invitee@example.com',
|
|
234
|
+
hashedPassword: 'x',
|
|
235
|
+
salt: 'x',
|
|
236
|
+
fullName: 'Invitee',
|
|
237
|
+
},
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
setContext({ currentUser: asCurrentUser({ id: invitee.id }) })
|
|
241
|
+
|
|
242
|
+
const result = await acceptInvitation({
|
|
243
|
+
token: scenario.membership.pending.invitationToken!,
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
expect(result.id).toEqual(scenario.organization.acme.id)
|
|
247
|
+
},
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
scenario(
|
|
251
|
+
'a repeat call from the same user still succeeds',
|
|
252
|
+
async (scenario: StandardScenario) => {
|
|
253
|
+
const invitee = await db.user.create({
|
|
254
|
+
data: {
|
|
255
|
+
email: 'invitee@example.com',
|
|
256
|
+
hashedPassword: 'x',
|
|
257
|
+
salt: 'x',
|
|
258
|
+
fullName: 'Invitee',
|
|
259
|
+
},
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
setContext({ currentUser: asCurrentUser({ id: invitee.id }) })
|
|
263
|
+
|
|
264
|
+
const token = scenario.membership.pending.invitationToken!
|
|
265
|
+
|
|
266
|
+
const first = await acceptInvitation({ token })
|
|
267
|
+
const second = await acceptInvitation({ token })
|
|
268
|
+
|
|
269
|
+
expect(second.id).toEqual(first.id)
|
|
270
|
+
},
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
scenario(
|
|
274
|
+
'a second user cannot claim an already-claimed token',
|
|
275
|
+
async (scenario: StandardScenario) => {
|
|
276
|
+
const firstUser = await db.user.create({
|
|
277
|
+
data: {
|
|
278
|
+
email: 'first@example.com',
|
|
279
|
+
hashedPassword: 'x',
|
|
280
|
+
salt: 'x',
|
|
281
|
+
fullName: 'First User',
|
|
282
|
+
},
|
|
283
|
+
})
|
|
284
|
+
const secondUser = await db.user.create({
|
|
285
|
+
data: {
|
|
286
|
+
email: 'second@example.com',
|
|
287
|
+
hashedPassword: 'x',
|
|
288
|
+
salt: 'x',
|
|
289
|
+
fullName: 'Second User',
|
|
290
|
+
},
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
const token = scenario.membership.pending.invitationToken!
|
|
294
|
+
|
|
295
|
+
setContext({ currentUser: asCurrentUser({ id: firstUser.id }) })
|
|
296
|
+
await acceptInvitation({ token })
|
|
297
|
+
|
|
298
|
+
setContext({ currentUser: asCurrentUser({ id: secondUser.id }) })
|
|
299
|
+
await expect(acceptInvitation({ token })).rejects.toThrow()
|
|
300
|
+
},
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
scenario(
|
|
304
|
+
'clears the invitation token once claimed',
|
|
305
|
+
async (scenario: StandardScenario) => {
|
|
306
|
+
const invitee = await db.user.create({
|
|
307
|
+
data: {
|
|
308
|
+
email: 'invitee@example.com',
|
|
309
|
+
hashedPassword: 'x',
|
|
310
|
+
salt: 'x',
|
|
311
|
+
fullName: 'Invitee',
|
|
312
|
+
},
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
const token = scenario.membership.pending.invitationToken!
|
|
316
|
+
|
|
317
|
+
setContext({ currentUser: asCurrentUser({ id: invitee.id }) })
|
|
318
|
+
await acceptInvitation({ token })
|
|
319
|
+
|
|
320
|
+
const claimed = await db.membership.findUnique({
|
|
321
|
+
where: { id: scenario.membership.pending.id },
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
expect(claimed?.userId).toEqual(invitee.id)
|
|
325
|
+
expect(claimed?.invitationToken).not.toEqual(token)
|
|
326
|
+
},
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
it('rejects a token that never matched a pending invitation', async () => {
|
|
330
|
+
setContext({ currentUser: asCurrentUser({ id: 'some-user-id' }) })
|
|
331
|
+
|
|
332
|
+
await expect(
|
|
333
|
+
acceptInvitation({ token: 'not-a-real-token' }),
|
|
334
|
+
).rejects.toThrow()
|
|
335
|
+
})
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
it('ensureDefaultOrganization is idempotent when the user already has memberships', async () => {
|
|
339
|
+
const existingMemberships = [
|
|
340
|
+
{ id: 'membership-1', organizationId: 'org-1', role: 'owner' },
|
|
341
|
+
]
|
|
342
|
+
|
|
343
|
+
const result = await ensureDefaultOrganization({
|
|
344
|
+
currentUser: { id: 'user-1', memberships: existingMemberships },
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
expect(result).toBe(existingMemberships)
|
|
348
|
+
})
|
|
349
|
+
|
|
350
|
+
scenario(
|
|
351
|
+
'ensureDefaultOrganization attaches a pending invitation',
|
|
352
|
+
async (scenario: StandardScenario) => {
|
|
353
|
+
const invitee = await db.user.create({
|
|
354
|
+
data: {
|
|
355
|
+
email: 'invitee@example.com',
|
|
356
|
+
hashedPassword: 'x',
|
|
357
|
+
salt: 'x',
|
|
358
|
+
fullName: 'Invitee',
|
|
359
|
+
},
|
|
360
|
+
})
|
|
361
|
+
|
|
362
|
+
const result = await ensureDefaultOrganization({
|
|
363
|
+
currentUser: { id: invitee.id, memberships: [] },
|
|
364
|
+
invitationToken: scenario.membership.pending.invitationToken,
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
expect(result).toHaveLength(1)
|
|
368
|
+
expect(result[0].organizationId).toEqual(scenario.organization.acme.id)
|
|
369
|
+
},
|
|
370
|
+
)
|
|
371
|
+
|
|
372
|
+
it('ensureDefaultOrganization creates a new organization when there is no invitation', async () => {
|
|
373
|
+
const newUser = await db.user.create({
|
|
374
|
+
data: {
|
|
375
|
+
email: 'brand-new@example.com',
|
|
376
|
+
hashedPassword: 'x',
|
|
377
|
+
salt: 'x',
|
|
378
|
+
fullName: 'Brand New',
|
|
379
|
+
},
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
const result = await ensureDefaultOrganization({
|
|
383
|
+
currentUser: { id: newUser.id, memberships: [] },
|
|
384
|
+
})
|
|
385
|
+
|
|
386
|
+
expect(result).toHaveLength(1)
|
|
387
|
+
expect(result[0].role).toEqual('owner')
|
|
388
|
+
})
|
|
389
|
+
})
|