import { SmrtClassOptions } from '@happyvertical/smrt-core'; import { Membership } from '../models/Membership.js'; import { Tenant } from '../models/Tenant.js'; import { TenantPolicy } from '../types/index.js'; /** * Result of tenant creation with ownership */ export interface TenantWithOwnershipResult { tenant: Tenant; membership: Membership; } /** * Result of ensureTenantForUser */ export interface EnsureTenantResult { /** The tenant (null if flexible mode and no existing tenant) */ tenant: Tenant | null; /** The membership (null if no tenant) */ membership: Membership | null; /** Whether a new tenant was created */ created: boolean; } /** * TenantService manages tenant creation with configurable policies. * * Policies: * - `flexible`: No tenant created on signup, user can have zero tenants * - `personal`: Auto-create personal tenant on first login, can delete all * - `required`: Auto-create personal tenant, must keep at least one * * @example * ```typescript * const tenantService = new TenantService(options, { * mode: 'personal', * maxTenants: 5, * defaultName: 'My Workspace', * }); * await tenantService.initialize(); * * // During OIDC login * const { tenant, membership, created } = await tenantService.ensureTenantForUser( * user.id, * { email: user.email, name: user.name } * ); * * // Creating additional tenants * if (await tenantService.canCreateTenant(user.id)) { * const { tenant } = await tenantService.createTenantWithOwnership( * user.id, * 'New Organization' * ); * } * ``` */ export declare class TenantService { private options; private policy; private tenantCollection; private membershipCollection; private roleCollection; constructor(options: SmrtClassOptions, policy?: TenantPolicy); /** * Initialize collections */ initialize(): Promise; /** * Get the current policy */ getPolicy(): TenantPolicy; /** * Create a tenant and make the user the owner * * @param userId - The user to make owner * @param name - Tenant name * @param options - Optional slug override * @returns The created tenant and membership */ createTenantWithOwnership(userId: string, name: string, options?: { slug?: string; }): Promise; /** * Check if a user can create a new tenant * * Returns false if maxTenants limit is reached (0 = unlimited) */ canCreateTenant(userId: string): Promise; /** * Get a specific error message explaining why a tenant cannot be deleted. * * @returns Error message if deletion is not allowed, or null if allowed. */ private getDeleteTenantError; /** * Check if a user can delete a specific tenant * * Returns false if: * - User is not an active member * - User is not the owner * - Policy is 'required' and this is the last tenant */ canDeleteTenant(userId: string, tenantId: string): Promise; /** * Delete a tenant * * Note: This does not cascade delete related records (memberships, etc.). * The caller should handle cleanup of related data if needed. * * @throws Error if user cannot delete the tenant (with specific reason) */ deleteTenant(userId: string, tenantId: string): Promise; /** * Ensure a tenant exists for the user based on policy * * Called during OIDC login to apply tenant policy: * - `flexible`: Returns first existing tenant or null (no auto-create) * - `personal`/`required`: Creates default tenant if none exists * * @param userId - The user ID * @param userInfo - User info for naming the auto-created tenant * @returns Tenant and membership (may be null in flexible mode) */ ensureTenantForUser(userId: string, userInfo: { email?: string; name?: string; }): Promise; /** * Get all tenants for a user (where they are owner) */ getOwnedTenants(userId: string): Promise; /** * Static factory method */ static create(options: SmrtClassOptions, policy?: TenantPolicy): Promise; } //# sourceMappingURL=TenantService.d.ts.map