UNPKG

better-auth

Version:

The most comprehensive authentication framework for TypeScript.

1 lines • 40 kB
{"version":3,"file":"crud-org.mjs","names":["options","member:\n\t\t\t\t| (Member & InferAdditionalFieldsFromPluginOptions<\"member\", O, false>)\n\t\t\t\t| undefined","teamMember: TeamMember | null","organization"],"sources":["../../../../src/plugins/organization/routes/crud-org.ts"],"sourcesContent":["import { createAuthEndpoint } from \"@better-auth/core/api\";\nimport { APIError } from \"better-call\";\nimport * as z from \"zod\";\nimport { getSessionFromCtx, requestOnlySessionMiddleware } from \"../../../api\";\nimport { setSessionCookie } from \"../../../cookies\";\nimport type { InferAdditionalFieldsFromPluginOptions } from \"../../../db\";\nimport { toZodSchema } from \"../../../db\";\nimport { getOrgAdapter } from \"../adapter\";\nimport { orgMiddleware, orgSessionMiddleware } from \"../call\";\nimport { ORGANIZATION_ERROR_CODES } from \"../error-codes\";\nimport { hasPermission } from \"../has-permission\";\nimport type {\n\tInferInvitation,\n\tInferMember,\n\tInferOrganization,\n\tInferTeam,\n\tMember,\n\tTeamMember,\n} from \"../schema\";\nimport type { OrganizationOptions } from \"../types\";\n\nconst baseOrganizationSchema = z.object({\n\tname: z.string().min(1).meta({\n\t\tdescription: \"The name of the organization\",\n\t}),\n\tslug: z.string().min(1).meta({\n\t\tdescription: \"The slug of the organization\",\n\t}),\n\tuserId: z.coerce\n\t\t.string()\n\t\t.meta({\n\t\t\tdescription:\n\t\t\t\t'The user id of the organization creator. If not provided, the current user will be used. Should only be used by admins or when called by the server. server-only. Eg: \"user-id\"',\n\t\t})\n\t\t.optional(),\n\tlogo: z\n\t\t.string()\n\t\t.meta({\n\t\t\tdescription: \"The logo of the organization\",\n\t\t})\n\t\t.optional(),\n\tmetadata: z\n\t\t.record(z.string(), z.any())\n\t\t.meta({\n\t\t\tdescription: \"The metadata of the organization\",\n\t\t})\n\t\t.optional(),\n\tkeepCurrentActiveOrganization: z\n\t\t.boolean()\n\t\t.meta({\n\t\t\tdescription:\n\t\t\t\t\"Whether to keep the current active organization active after creating a new one. Eg: true\",\n\t\t})\n\t\t.optional(),\n});\n\nexport const createOrganization = <O extends OrganizationOptions>(\n\toptions?: O | undefined,\n) => {\n\tconst additionalFieldsSchema = toZodSchema({\n\t\tfields: options?.schema?.organization?.additionalFields || {},\n\t\tisClientSide: true,\n\t});\n\n\ttype Body = InferAdditionalFieldsFromPluginOptions<\"organization\", O> &\n\t\tz.infer<typeof baseOrganizationSchema>;\n\n\treturn createAuthEndpoint(\n\t\t\"/organization/create\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tbody: z.object({\n\t\t\t\t...baseOrganizationSchema.shape,\n\t\t\t\t...additionalFieldsSchema.shape,\n\t\t\t}),\n\t\t\tuse: [orgMiddleware],\n\t\t\tmetadata: {\n\t\t\t\t$Infer: {\n\t\t\t\t\tbody: {} as Body,\n\t\t\t\t},\n\t\t\t\topenapi: {\n\t\t\t\t\tdescription: \"Create an organization\",\n\t\t\t\t\tresponses: {\n\t\t\t\t\t\t\"200\": {\n\t\t\t\t\t\t\tdescription: \"Success\",\n\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\tdescription: \"The organization that was created\",\n\t\t\t\t\t\t\t\t\t\t$ref: \"#/components/schemas/Organization\",\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\tif (!session && (ctx.request || ctx.headers)) {\n\t\t\t\tthrow new APIError(\"UNAUTHORIZED\");\n\t\t\t}\n\t\t\tlet user = session?.user || null;\n\t\t\tif (!user) {\n\t\t\t\tif (!ctx.body.userId) {\n\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\");\n\t\t\t\t}\n\t\t\t\tuser = await ctx.context.internalAdapter.findUserById(ctx.body.userId);\n\t\t\t}\n\t\t\tif (!user) {\n\t\t\t\treturn ctx.json(null, {\n\t\t\t\t\tstatus: 401,\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst options = ctx.context.orgOptions;\n\t\t\tconst canCreateOrg =\n\t\t\t\ttypeof options?.allowUserToCreateOrganization === \"function\"\n\t\t\t\t\t? await options.allowUserToCreateOrganization(user)\n\t\t\t\t\t: options?.allowUserToCreateOrganization === undefined\n\t\t\t\t\t\t? true\n\t\t\t\t\t\t: options.allowUserToCreateOrganization;\n\n\t\t\tconst isSystemAction = !session && ctx.body.userId;\n\n\t\t\tif (!canCreateOrg && !isSystemAction) {\n\t\t\t\tthrow new APIError(\"FORBIDDEN\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\tORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_CREATE_A_NEW_ORGANIZATION,\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst adapter = getOrgAdapter<O>(ctx.context, options as O);\n\n\t\t\tconst userOrganizations = await adapter.listOrganizations(user.id);\n\t\t\tconst hasReachedOrgLimit =\n\t\t\t\ttypeof options.organizationLimit === \"number\"\n\t\t\t\t\t? userOrganizations.length >= options.organizationLimit\n\t\t\t\t\t: typeof options.organizationLimit === \"function\"\n\t\t\t\t\t\t? await options.organizationLimit(user)\n\t\t\t\t\t\t: false;\n\n\t\t\tif (hasReachedOrgLimit) {\n\t\t\t\tthrow new APIError(\"FORBIDDEN\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\tORGANIZATION_ERROR_CODES.YOU_HAVE_REACHED_THE_MAXIMUM_NUMBER_OF_ORGANIZATIONS,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst existingOrganization = await adapter.findOrganizationBySlug(\n\t\t\t\tctx.body.slug,\n\t\t\t);\n\t\t\tif (existingOrganization) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: ORGANIZATION_ERROR_CODES.ORGANIZATION_ALREADY_EXISTS,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tlet {\n\t\t\t\tkeepCurrentActiveOrganization: _,\n\t\t\t\tuserId: __,\n\t\t\t\t...orgData\n\t\t\t} = ctx.body;\n\n\t\t\tif (options.organizationCreation?.beforeCreate) {\n\t\t\t\tconst response = await options.organizationCreation.beforeCreate(\n\t\t\t\t\t{\n\t\t\t\t\t\torganization: {\n\t\t\t\t\t\t\t...orgData,\n\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tuser,\n\t\t\t\t\t},\n\t\t\t\t\tctx.request,\n\t\t\t\t);\n\t\t\t\tif (response && typeof response === \"object\" && \"data\" in response) {\n\t\t\t\t\torgData = {\n\t\t\t\t\t\t...ctx.body,\n\t\t\t\t\t\t...response.data,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (options?.organizationHooks?.beforeCreateOrganization) {\n\t\t\t\tconst response =\n\t\t\t\t\tawait options?.organizationHooks.beforeCreateOrganization({\n\t\t\t\t\t\torganization: orgData,\n\t\t\t\t\t\tuser,\n\t\t\t\t\t});\n\t\t\t\tif (response && typeof response === \"object\" && \"data\" in response) {\n\t\t\t\t\torgData = {\n\t\t\t\t\t\t...ctx.body,\n\t\t\t\t\t\t...response.data,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst organization = await adapter.createOrganization({\n\t\t\t\torganization: {\n\t\t\t\t\t...orgData,\n\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tlet member:\n\t\t\t\t| (Member & InferAdditionalFieldsFromPluginOptions<\"member\", O, false>)\n\t\t\t\t| undefined;\n\t\t\tlet teamMember: TeamMember | null = null;\n\t\t\tlet data = {\n\t\t\t\tuserId: user.id,\n\t\t\t\torganizationId: organization.id,\n\t\t\t\trole: ctx.context.orgOptions.creatorRole || \"owner\",\n\t\t\t};\n\t\t\tif (options?.organizationHooks?.beforeAddMember) {\n\t\t\t\tconst response = await options?.organizationHooks.beforeAddMember({\n\t\t\t\t\tmember: {\n\t\t\t\t\t\tuserId: user.id,\n\t\t\t\t\t\torganizationId: organization.id,\n\t\t\t\t\t\trole: ctx.context.orgOptions.creatorRole || \"owner\",\n\t\t\t\t\t},\n\t\t\t\t\tuser,\n\t\t\t\t\torganization,\n\t\t\t\t});\n\t\t\t\tif (response && typeof response === \"object\" && \"data\" in response) {\n\t\t\t\t\tdata = {\n\t\t\t\t\t\t...data,\n\t\t\t\t\t\t...response.data,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t\tmember = await adapter.createMember(data);\n\t\t\tif (options?.organizationHooks?.afterAddMember) {\n\t\t\t\tawait options?.organizationHooks.afterAddMember({\n\t\t\t\t\tmember,\n\t\t\t\t\tuser,\n\t\t\t\t\torganization,\n\t\t\t\t});\n\t\t\t}\n\t\t\tif (\n\t\t\t\toptions?.teams?.enabled &&\n\t\t\t\toptions.teams.defaultTeam?.enabled !== false\n\t\t\t) {\n\t\t\t\tlet teamData = {\n\t\t\t\t\torganizationId: organization.id,\n\t\t\t\t\tname: `${organization.name}`,\n\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t};\n\t\t\t\tif (options?.organizationHooks?.beforeCreateTeam) {\n\t\t\t\t\tconst response = await options?.organizationHooks.beforeCreateTeam({\n\t\t\t\t\t\tteam: {\n\t\t\t\t\t\t\torganizationId: organization.id,\n\t\t\t\t\t\t\tname: `${organization.name}`,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tuser,\n\t\t\t\t\t\torganization,\n\t\t\t\t\t});\n\t\t\t\t\tif (response && typeof response === \"object\" && \"data\" in response) {\n\t\t\t\t\t\tteamData = {\n\t\t\t\t\t\t\t...teamData,\n\t\t\t\t\t\t\t...response.data,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tconst defaultTeam =\n\t\t\t\t\t(await options.teams.defaultTeam?.customCreateDefaultTeam?.(\n\t\t\t\t\t\torganization,\n\t\t\t\t\t\tctx,\n\t\t\t\t\t)) || (await adapter.createTeam(teamData));\n\n\t\t\t\tteamMember = await adapter.findOrCreateTeamMember({\n\t\t\t\t\tteamId: defaultTeam.id,\n\t\t\t\t\tuserId: user.id,\n\t\t\t\t});\n\n\t\t\t\tif (options?.organizationHooks?.afterCreateTeam) {\n\t\t\t\t\tawait options?.organizationHooks.afterCreateTeam({\n\t\t\t\t\t\tteam: defaultTeam,\n\t\t\t\t\t\tuser,\n\t\t\t\t\t\torganization,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (options.organizationCreation?.afterCreate) {\n\t\t\t\tawait options.organizationCreation.afterCreate(\n\t\t\t\t\t{\n\t\t\t\t\t\torganization,\n\t\t\t\t\t\tuser,\n\t\t\t\t\t\tmember,\n\t\t\t\t\t},\n\t\t\t\t\tctx.request,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (options?.organizationHooks?.afterCreateOrganization) {\n\t\t\t\tawait options?.organizationHooks.afterCreateOrganization({\n\t\t\t\t\torganization,\n\t\t\t\t\tuser,\n\t\t\t\t\tmember,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (ctx.context.session && !ctx.body.keepCurrentActiveOrganization) {\n\t\t\t\tawait adapter.setActiveOrganization(\n\t\t\t\t\tctx.context.session.session.token,\n\t\t\t\t\torganization.id,\n\t\t\t\t\tctx,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tteamMember &&\n\t\t\t\tctx.context.session &&\n\t\t\t\t!ctx.body.keepCurrentActiveOrganization\n\t\t\t) {\n\t\t\t\tawait adapter.setActiveTeam(\n\t\t\t\t\tctx.context.session.session.token,\n\t\t\t\t\tteamMember.teamId,\n\t\t\t\t\tctx,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn ctx.json({\n\t\t\t\t...organization,\n\t\t\t\tmetadata:\n\t\t\t\t\torganization.metadata && typeof organization.metadata === \"string\"\n\t\t\t\t\t\t? JSON.parse(organization.metadata)\n\t\t\t\t\t\t: organization.metadata,\n\t\t\t\tmembers: [member],\n\t\t\t});\n\t\t},\n\t);\n};\n\nconst checkOrganizationSlugBodySchema = z.object({\n\tslug: z.string().meta({\n\t\tdescription: 'The organization slug to check. Eg: \"my-org\"',\n\t}),\n});\n\nexport const checkOrganizationSlug = <O extends OrganizationOptions>(\n\toptions: O,\n) =>\n\tcreateAuthEndpoint(\n\t\t\"/organization/check-slug\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tbody: checkOrganizationSlugBodySchema,\n\t\t\tuse: [requestOnlySessionMiddleware, orgMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst orgAdapter = getOrgAdapter<O>(ctx.context, options);\n\t\t\tconst org = await orgAdapter.findOrganizationBySlug(ctx.body.slug);\n\t\t\tif (!org) {\n\t\t\t\treturn ctx.json({\n\t\t\t\t\tstatus: true,\n\t\t\t\t});\n\t\t\t}\n\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\tmessage: \"slug is taken\",\n\t\t\t});\n\t\t},\n\t);\n\nconst baseUpdateOrganizationSchema = z.object({\n\tname: z\n\t\t.string()\n\t\t.min(1)\n\t\t.meta({\n\t\t\tdescription: \"The name of the organization\",\n\t\t})\n\t\t.optional(),\n\tslug: z\n\t\t.string()\n\t\t.min(1)\n\t\t.meta({\n\t\t\tdescription: \"The slug of the organization\",\n\t\t})\n\t\t.optional(),\n\tlogo: z\n\t\t.string()\n\t\t.meta({\n\t\t\tdescription: \"The logo of the organization\",\n\t\t})\n\t\t.optional(),\n\tmetadata: z\n\t\t.record(z.string(), z.any())\n\t\t.meta({\n\t\t\tdescription: \"The metadata of the organization\",\n\t\t})\n\t\t.optional(),\n});\n\nexport const updateOrganization = <O extends OrganizationOptions>(\n\toptions?: O | undefined,\n) => {\n\tconst additionalFieldsSchema = toZodSchema({\n\t\tfields: options?.schema?.organization?.additionalFields || {},\n\t\tisClientSide: true,\n\t});\n\ttype Body = {\n\t\tdata: {\n\t\t\tname?: string | undefined;\n\t\t\tslug?: string | undefined;\n\t\t\tlogo?: string | undefined;\n\t\t\tmetadata?: Record<string, any> | undefined;\n\t\t} & Partial<InferAdditionalFieldsFromPluginOptions<\"organization\", O>>;\n\t\torganizationId?: string | undefined;\n\t};\n\treturn createAuthEndpoint(\n\t\t\"/organization/update\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tbody: z.object({\n\t\t\t\tdata: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\t...additionalFieldsSchema.shape,\n\t\t\t\t\t\t...baseUpdateOrganizationSchema.shape,\n\t\t\t\t\t})\n\t\t\t\t\t.partial(),\n\t\t\t\torganizationId: z\n\t\t\t\t\t.string()\n\t\t\t\t\t.meta({\n\t\t\t\t\t\tdescription: 'The organization ID. Eg: \"org-id\"',\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t}),\n\t\t\trequireHeaders: true,\n\t\t\tuse: [orgMiddleware],\n\t\t\tmetadata: {\n\t\t\t\t$Infer: {\n\t\t\t\t\tbody: {} as Body,\n\t\t\t\t},\n\t\t\t\topenapi: {\n\t\t\t\t\tdescription: \"Update an organization\",\n\t\t\t\t\tresponses: {\n\t\t\t\t\t\t\"200\": {\n\t\t\t\t\t\t\tdescription: \"Success\",\n\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\tdescription: \"The updated organization\",\n\t\t\t\t\t\t\t\t\t\t$ref: \"#/components/schemas/Organization\",\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst session = await ctx.context.getSession(ctx);\n\t\t\tif (!session) {\n\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst organizationId =\n\t\t\t\tctx.body.organizationId || session.session.activeOrganizationId;\n\t\t\tif (!organizationId) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: ORGANIZATION_ERROR_CODES.ORGANIZATION_NOT_FOUND,\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst adapter = getOrgAdapter<O>(ctx.context, options);\n\t\t\tconst member = await adapter.findMemberByOrgId({\n\t\t\t\tuserId: session.user.id,\n\t\t\t\torganizationId: organizationId,\n\t\t\t});\n\t\t\tif (!member) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\tORGANIZATION_ERROR_CODES.USER_IS_NOT_A_MEMBER_OF_THE_ORGANIZATION,\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst canUpdateOrg = await hasPermission(\n\t\t\t\t{\n\t\t\t\t\tpermissions: {\n\t\t\t\t\t\torganization: [\"update\"],\n\t\t\t\t\t},\n\t\t\t\t\trole: member.role,\n\t\t\t\t\toptions: ctx.context.orgOptions,\n\t\t\t\t\torganizationId,\n\t\t\t\t},\n\t\t\t\tctx,\n\t\t\t);\n\t\t\tif (!canUpdateOrg) {\n\t\t\t\tthrow new APIError(\"FORBIDDEN\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\tORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_UPDATE_THIS_ORGANIZATION,\n\t\t\t\t});\n\t\t\t}\n\t\t\t// Check if slug is being updated and validate uniqueness\n\t\t\tif (typeof ctx.body.data.slug === \"string\") {\n\t\t\t\tconst existingOrganization = await adapter.findOrganizationBySlug(\n\t\t\t\t\tctx.body.data.slug,\n\t\t\t\t);\n\t\t\t\tif (\n\t\t\t\t\texistingOrganization &&\n\t\t\t\t\texistingOrganization.id !== organizationId\n\t\t\t\t) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: ORGANIZATION_ERROR_CODES.ORGANIZATION_SLUG_ALREADY_TAKEN,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (options?.organizationHooks?.beforeUpdateOrganization) {\n\t\t\t\tconst response =\n\t\t\t\t\tawait options.organizationHooks.beforeUpdateOrganization({\n\t\t\t\t\t\torganization: ctx.body.data,\n\t\t\t\t\t\tuser: session.user,\n\t\t\t\t\t\tmember,\n\t\t\t\t\t});\n\t\t\t\tif (response && typeof response === \"object\" && \"data\" in response) {\n\t\t\t\t\tctx.body.data = {\n\t\t\t\t\t\t...ctx.body.data,\n\t\t\t\t\t\t...response.data,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst updatedOrg = await adapter.updateOrganization(\n\t\t\t\torganizationId,\n\t\t\t\tctx.body.data,\n\t\t\t);\n\t\t\tif (options?.organizationHooks?.afterUpdateOrganization) {\n\t\t\t\tawait options.organizationHooks.afterUpdateOrganization({\n\t\t\t\t\torganization: updatedOrg,\n\t\t\t\t\tuser: session.user,\n\t\t\t\t\tmember,\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn ctx.json(updatedOrg);\n\t\t},\n\t);\n};\n\nconst deleteOrganizationBodySchema = z.object({\n\torganizationId: z.string().meta({\n\t\tdescription: \"The organization id to delete\",\n\t}),\n});\n\nexport const deleteOrganization = <O extends OrganizationOptions>(\n\toptions: O,\n) => {\n\treturn createAuthEndpoint(\n\t\t\"/organization/delete\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tbody: deleteOrganizationBodySchema,\n\t\t\trequireHeaders: true,\n\t\t\tuse: [orgMiddleware],\n\t\t\tmetadata: {\n\t\t\t\topenapi: {\n\t\t\t\t\tdescription: \"Delete an organization\",\n\t\t\t\t\tresponses: {\n\t\t\t\t\t\t\"200\": {\n\t\t\t\t\t\t\tdescription: \"Success\",\n\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\tdescription: \"The organization id that was deleted\",\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst disableOrganizationDeletion =\n\t\t\t\tctx.context.orgOptions.organizationDeletion?.disabled ||\n\t\t\t\tctx.context.orgOptions.disableOrganizationDeletion;\n\t\t\tif (disableOrganizationDeletion) {\n\t\t\t\tif (ctx.context.orgOptions.organizationDeletion?.disabled) {\n\t\t\t\t\tctx.context.logger.info(\n\t\t\t\t\t\t\"`organizationDeletion.disabled` is deprecated. Use `disableOrganizationDeletion` instead\",\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\tmessage: \"Organization deletion is disabled\",\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst session = await ctx.context.getSession(ctx);\n\t\t\tif (!session) {\n\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", { status: 401 });\n\t\t\t}\n\n\t\t\tconst organizationId = ctx.body.organizationId;\n\t\t\tif (!organizationId) {\n\t\t\t\treturn ctx.json(null, {\n\t\t\t\t\tstatus: 400,\n\t\t\t\t\tbody: {\n\t\t\t\t\t\tmessage: ORGANIZATION_ERROR_CODES.ORGANIZATION_NOT_FOUND,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst adapter = getOrgAdapter<O>(ctx.context, options);\n\t\t\tconst member = await adapter.findMemberByOrgId({\n\t\t\t\tuserId: session.user.id,\n\t\t\t\torganizationId: organizationId,\n\t\t\t});\n\t\t\tif (!member) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\tORGANIZATION_ERROR_CODES.USER_IS_NOT_A_MEMBER_OF_THE_ORGANIZATION,\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst canDeleteOrg = await hasPermission(\n\t\t\t\t{\n\t\t\t\t\trole: member.role,\n\t\t\t\t\tpermissions: {\n\t\t\t\t\t\torganization: [\"delete\"],\n\t\t\t\t\t},\n\t\t\t\t\torganizationId,\n\t\t\t\t\toptions: ctx.context.orgOptions,\n\t\t\t\t},\n\t\t\t\tctx,\n\t\t\t);\n\t\t\tif (!canDeleteOrg) {\n\t\t\t\tthrow new APIError(\"FORBIDDEN\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\tORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_DELETE_THIS_ORGANIZATION,\n\t\t\t\t});\n\t\t\t}\n\t\t\tif (organizationId === session.session.activeOrganizationId) {\n\t\t\t\t/**\n\t\t\t\t * If the organization is deleted, we set the active organization to null\n\t\t\t\t */\n\t\t\t\tawait adapter.setActiveOrganization(session.session.token, null, ctx);\n\t\t\t}\n\n\t\t\tconst org = await adapter.findOrganizationById(organizationId);\n\t\t\tif (!org) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\");\n\t\t\t}\n\t\t\tif (options?.organizationHooks?.beforeDeleteOrganization) {\n\t\t\t\tawait options.organizationHooks.beforeDeleteOrganization({\n\t\t\t\t\torganization: org,\n\t\t\t\t\tuser: session.user,\n\t\t\t\t});\n\t\t\t}\n\t\t\tawait adapter.deleteOrganization(organizationId);\n\t\t\tif (options?.organizationHooks?.afterDeleteOrganization) {\n\t\t\t\tawait options.organizationHooks.afterDeleteOrganization({\n\t\t\t\t\torganization: org,\n\t\t\t\t\tuser: session.user,\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn ctx.json(org);\n\t\t},\n\t);\n};\n\nconst getFullOrganizationQuerySchema = z.optional(\n\tz.object({\n\t\torganizationId: z\n\t\t\t.string()\n\t\t\t.meta({\n\t\t\t\tdescription: \"The organization id to get\",\n\t\t\t})\n\t\t\t.optional(),\n\t\torganizationSlug: z\n\t\t\t.string()\n\t\t\t.meta({\n\t\t\t\tdescription: \"The organization slug to get\",\n\t\t\t})\n\t\t\t.optional(),\n\t\tmembersLimit: z\n\t\t\t.number()\n\t\t\t.or(z.string().transform((val) => parseInt(val)))\n\t\t\t.meta({\n\t\t\t\tdescription:\n\t\t\t\t\t\"The limit of members to get. By default, it uses the membershipLimit option which defaults to 100.\",\n\t\t\t})\n\t\t\t.optional(),\n\t}),\n);\n\nexport const getFullOrganization = <O extends OrganizationOptions>(\n\toptions: O,\n) =>\n\tcreateAuthEndpoint(\n\t\t\"/organization/get-full-organization\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tquery: getFullOrganizationQuerySchema,\n\t\t\trequireHeaders: true,\n\t\t\tuse: [orgMiddleware, orgSessionMiddleware],\n\t\t\tmetadata: {\n\t\t\t\topenapi: {\n\t\t\t\t\toperationId: \"getOrganization\",\n\t\t\t\t\tdescription: \"Get the full organization\",\n\t\t\t\t\tresponses: {\n\t\t\t\t\t\t\"200\": {\n\t\t\t\t\t\t\tdescription: \"Success\",\n\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\tdescription: \"The organization\",\n\t\t\t\t\t\t\t\t\t\t$ref: \"#/components/schemas/Organization\",\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst session = ctx.context.session;\n\t\t\tconst organizationId =\n\t\t\t\tctx.query?.organizationSlug ||\n\t\t\t\tctx.query?.organizationId ||\n\t\t\t\tsession.session.activeOrganizationId;\n\t\t\t// return null if no organization is found to avoid erroring since this is a usual scenario\n\t\t\tif (!organizationId) {\n\t\t\t\treturn ctx.json(null, {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst adapter = getOrgAdapter<O>(ctx.context, options);\n\t\t\tconst organization = await adapter.findFullOrganization({\n\t\t\t\torganizationId,\n\t\t\t\tisSlug: !!ctx.query?.organizationSlug,\n\t\t\t\tincludeTeams: ctx.context.orgOptions.teams?.enabled,\n\t\t\t\tmembersLimit: ctx.query?.membersLimit,\n\t\t\t});\n\t\t\tif (!organization) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: ORGANIZATION_ERROR_CODES.ORGANIZATION_NOT_FOUND,\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst isMember = await adapter.checkMembership({\n\t\t\t\tuserId: session.user.id,\n\t\t\t\torganizationId: organization.id,\n\t\t\t});\n\t\t\tif (!isMember) {\n\t\t\t\tawait adapter.setActiveOrganization(session.session.token, null, ctx);\n\t\t\t\tthrow new APIError(\"FORBIDDEN\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\tORGANIZATION_ERROR_CODES.USER_IS_NOT_A_MEMBER_OF_THE_ORGANIZATION,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttype OrganizationReturn = O[\"teams\"] extends { enabled: true }\n\t\t\t\t? {\n\t\t\t\t\t\tmembers: InferMember<O>[];\n\t\t\t\t\t\tinvitations: InferInvitation<O>[];\n\t\t\t\t\t\tteams: InferTeam<O>[];\n\t\t\t\t\t} & InferOrganization<O>\n\t\t\t\t: {\n\t\t\t\t\t\tmembers: InferMember<O>[];\n\t\t\t\t\t\tinvitations: InferInvitation<O>[];\n\t\t\t\t\t} & InferOrganization<O>;\n\t\t\treturn ctx.json(organization as unknown as OrganizationReturn);\n\t\t},\n\t);\n\nconst setActiveOrganizationBodySchema = z.object({\n\torganizationId: z\n\t\t.string()\n\t\t.meta({\n\t\t\tdescription:\n\t\t\t\t'The organization id to set as active. It can be null to unset the active organization. Eg: \"org-id\"',\n\t\t})\n\t\t.nullable()\n\t\t.optional(),\n\torganizationSlug: z\n\t\t.string()\n\t\t.meta({\n\t\t\tdescription:\n\t\t\t\t'The organization slug to set as active. It can be null to unset the active organization if organizationId is not provided. Eg: \"org-slug\"',\n\t\t})\n\t\t.optional(),\n});\n\nexport const setActiveOrganization = <O extends OrganizationOptions>(\n\toptions: O,\n) => {\n\treturn createAuthEndpoint(\n\t\t\"/organization/set-active\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tbody: setActiveOrganizationBodySchema,\n\t\t\tuse: [orgSessionMiddleware, orgMiddleware],\n\t\t\trequireHeaders: true,\n\t\t\tmetadata: {\n\t\t\t\topenapi: {\n\t\t\t\t\toperationId: \"setActiveOrganization\",\n\t\t\t\t\tdescription: \"Set the active organization\",\n\t\t\t\t\tresponses: {\n\t\t\t\t\t\t\"200\": {\n\t\t\t\t\t\t\tdescription: \"Success\",\n\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\tdescription: \"The organization\",\n\t\t\t\t\t\t\t\t\t\t$ref: \"#/components/schemas/Organization\",\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst adapter = getOrgAdapter<O>(ctx.context, options);\n\t\t\tconst session = ctx.context.session;\n\t\t\tlet organizationId = ctx.body.organizationId;\n\t\t\tconst organizationSlug = ctx.body.organizationSlug;\n\n\t\t\tif (organizationId === null) {\n\t\t\t\tconst sessionOrgId = session.session.activeOrganizationId;\n\t\t\t\tif (!sessionOrgId) {\n\t\t\t\t\treturn ctx.json(null);\n\t\t\t\t}\n\t\t\t\tconst updatedSession = await adapter.setActiveOrganization(\n\t\t\t\t\tsession.session.token,\n\t\t\t\t\tnull,\n\t\t\t\t\tctx,\n\t\t\t\t);\n\t\t\t\tawait setSessionCookie(ctx, {\n\t\t\t\t\tsession: updatedSession,\n\t\t\t\t\tuser: session.user,\n\t\t\t\t});\n\t\t\t\treturn ctx.json(null);\n\t\t\t}\n\n\t\t\tif (!organizationId && !organizationSlug) {\n\t\t\t\tconst sessionOrgId = session.session.activeOrganizationId;\n\t\t\t\tif (!sessionOrgId) {\n\t\t\t\t\treturn ctx.json(null);\n\t\t\t\t}\n\t\t\t\torganizationId = sessionOrgId;\n\t\t\t}\n\n\t\t\tif (organizationSlug && !organizationId) {\n\t\t\t\tconst organization =\n\t\t\t\t\tawait adapter.findOrganizationBySlug(organizationSlug);\n\t\t\t\tif (!organization) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: ORGANIZATION_ERROR_CODES.ORGANIZATION_NOT_FOUND,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\torganizationId = organization.id;\n\t\t\t}\n\n\t\t\tif (!organizationId) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: ORGANIZATION_ERROR_CODES.ORGANIZATION_NOT_FOUND,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst isMember = await adapter.checkMembership({\n\t\t\t\tuserId: session.user.id,\n\t\t\t\torganizationId,\n\t\t\t});\n\t\t\tif (!isMember) {\n\t\t\t\tawait adapter.setActiveOrganization(session.session.token, null, ctx);\n\t\t\t\tthrow new APIError(\"FORBIDDEN\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\tORGANIZATION_ERROR_CODES.USER_IS_NOT_A_MEMBER_OF_THE_ORGANIZATION,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst organization = await adapter.findOrganizationById(organizationId);\n\t\t\tif (!organization) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: ORGANIZATION_ERROR_CODES.ORGANIZATION_NOT_FOUND,\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst updatedSession = await adapter.setActiveOrganization(\n\t\t\t\tsession.session.token,\n\t\t\t\torganization.id,\n\t\t\t\tctx,\n\t\t\t);\n\t\t\tawait setSessionCookie(ctx, {\n\t\t\t\tsession: updatedSession,\n\t\t\t\tuser: session.user,\n\t\t\t});\n\t\t\ttype OrganizationReturn = O[\"teams\"] extends { enabled: true }\n\t\t\t\t? {\n\t\t\t\t\t\tmembers: InferMember<O>[];\n\t\t\t\t\t\tinvitations: InferInvitation<O>[];\n\t\t\t\t\t\tteams: InferTeam<O>[];\n\t\t\t\t\t} & InferOrganization<O>\n\t\t\t\t: {\n\t\t\t\t\t\tmembers: InferMember<O>[];\n\t\t\t\t\t\tinvitations: InferInvitation<O>[];\n\t\t\t\t\t} & InferOrganization<O>;\n\t\t\treturn ctx.json(organization as unknown as OrganizationReturn);\n\t\t},\n\t);\n};\n\nexport const listOrganizations = <O extends OrganizationOptions>(options: O) =>\n\tcreateAuthEndpoint(\n\t\t\"/organization/list\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [orgMiddleware, orgSessionMiddleware],\n\t\t\trequireHeaders: true,\n\t\t\tmetadata: {\n\t\t\t\topenapi: {\n\t\t\t\t\tdescription: \"List all organizations\",\n\t\t\t\t\tresponses: {\n\t\t\t\t\t\t\"200\": {\n\t\t\t\t\t\t\tdescription: \"Success\",\n\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\ttype: \"array\",\n\t\t\t\t\t\t\t\t\t\titems: {\n\t\t\t\t\t\t\t\t\t\t\t$ref: \"#/components/schemas/Organization\",\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst adapter = getOrgAdapter<O>(ctx.context, options);\n\t\t\tconst organizations = await adapter.listOrganizations(\n\t\t\t\tctx.context.session.user.id,\n\t\t\t);\n\t\t\treturn ctx.json(organizations);\n\t\t},\n\t);\n"],"mappings":";;;;;;;;;;;;;;AAqBA,MAAM,yBAAyB,EAAE,OAAO;CACvC,MAAM,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,EAC5B,aAAa,gCACb,CAAC;CACF,MAAM,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,EAC5B,aAAa,gCACb,CAAC;CACF,QAAQ,EAAE,OACR,QAAQ,CACR,KAAK,EACL,aACC,qLACD,CAAC,CACD,UAAU;CACZ,MAAM,EACJ,QAAQ,CACR,KAAK,EACL,aAAa,gCACb,CAAC,CACD,UAAU;CACZ,UAAU,EACR,OAAO,EAAE,QAAQ,EAAE,EAAE,KAAK,CAAC,CAC3B,KAAK,EACL,aAAa,oCACb,CAAC,CACD,UAAU;CACZ,+BAA+B,EAC7B,SAAS,CACT,KAAK,EACL,aACC,6FACD,CAAC,CACD,UAAU;CACZ,CAAC;AAEF,MAAa,sBACZ,YACI;CACJ,MAAM,yBAAyB,YAAY;EAC1C,QAAQ,SAAS,QAAQ,cAAc,oBAAoB,EAAE;EAC7D,cAAc;EACd,CAAC;AAKF,QAAO,mBACN,wBACA;EACC,QAAQ;EACR,MAAM,EAAE,OAAO;GACd,GAAG,uBAAuB;GAC1B,GAAG,uBAAuB;GAC1B,CAAC;EACF,KAAK,CAAC,cAAc;EACpB,UAAU;GACT,QAAQ,EAC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