Open Source Team Metrics based on PRs
at main 5.2 kB view raw
1import { auth } from '@/auth'; 2import { findUserWithOrganizations } from '@/lib/repositories/user-repository'; 3// Note: This file is largely obsolete after hexagonal architecture migration 4// Only kept for legacy routes that haven't been migrated to withAuth middleware 5 6// Request-level cache to avoid repeated user queries 7const requestCache = new WeakMap<Request, { 8 userWithOrganizations?: { user: any; organizations: any[] }; 9}>(); 10 11/** 12 * Gets the authenticated user from the session and database 13 * @param request Optional request object for caching optimization 14 * @returns The authenticated user object from the database 15 * @throws Error if user is not authenticated or not found in database 16 */ 17export async function getAuthenticatedUser(request?: Request) { 18 const session = await auth(); 19 20 if (!session || !session.user) { 21 throw new Error('Not authenticated'); 22 } 23 24 // Use request-level caching if request object is provided 25 if (request) { 26 let cache = requestCache.get(request); 27 if (!cache) { 28 cache = {}; 29 requestCache.set(request, cache); 30 } 31 32 // Return cached user if available 33 if (cache.userWithOrganizations) { 34 return cache.userWithOrganizations.user; 35 } 36 37 // Fetch and cache user with organizations in a single query 38 const result = await findUserWithOrganizations(session.user.id); 39 if (!result) { 40 throw new Error('User not found in database'); 41 } 42 43 cache.userWithOrganizations = result; 44 return result.user; 45 } 46 47 // Fallback to optimized query (for cases where request is not available) 48 const result = await findUserWithOrganizations(session.user.id); 49 if (!result) { 50 throw new Error('User not found in database'); 51 } 52 53 return result.user; 54} 55 56export async function getUserOrganizations(request?: Request) { 57 const session = await auth(); 58 59 if (!session || !session.user) { 60 throw new Error('Not authenticated'); 61 } 62 63 // Use request-level caching 64 if (request) { 65 let cache = requestCache.get(request); 66 if (!cache) { 67 cache = {}; 68 requestCache.set(request, cache); 69 } 70 71 // Return cached organizations if available 72 if (cache.userWithOrganizations) { 73 const organizations = cache.userWithOrganizations.organizations; 74 if (!organizations || organizations.length === 0) { 75 throw new Error('No organizations found'); 76 } 77 return organizations; 78 } 79 80 // Fetch and cache user with organizations in a single query 81 const result = await findUserWithOrganizations(session.user.id); 82 if (!result) { 83 throw new Error('User not found in database'); 84 } 85 86 if (!result.organizations || result.organizations.length === 0) { 87 throw new Error('No organizations found'); 88 } 89 90 cache.userWithOrganizations = result; 91 return result.organizations; 92 } 93 94 // Fallback to optimized query 95 const result = await findUserWithOrganizations(session.user.id); 96 if (!result) { 97 throw new Error('User not found in database'); 98 } 99 100 if (!result.organizations || result.organizations.length === 0) { 101 throw new Error('No organizations found'); 102 } 103 104 return result.organizations; 105} 106 107/** 108 * Gets the authenticated user along with their organizations in a single optimized query 109 * @param request Optional request object for caching optimization 110 * @returns Object containing user, organizations array, and primary organization 111 * @throws Error if user is not authenticated, not found, or has no organizations 112 */ 113export async function getUserWithOrganizations(request?: Request) { 114 const session = await auth(); 115 116 if (!session || !session.user) { 117 throw new Error('Not authenticated'); 118 } 119 120 // Use request-level caching 121 if (request) { 122 let cache = requestCache.get(request); 123 if (!cache) { 124 cache = {}; 125 requestCache.set(request, cache); 126 } 127 128 // Return cached data if available 129 if (cache.userWithOrganizations) { 130 const { user, organizations } = cache.userWithOrganizations; 131 if (!organizations || organizations.length === 0) { 132 throw new Error('No organizations found'); 133 } 134 return { 135 user, 136 organizations, 137 primaryOrganization: organizations[0] // Use first org as primary 138 }; 139 } 140 141 // Fetch and cache in a single query 142 const result = await findUserWithOrganizations(session.user.id); 143 if (!result) { 144 throw new Error('User not found in database'); 145 } 146 147 if (!result.organizations || result.organizations.length === 0) { 148 throw new Error('No organizations found'); 149 } 150 151 cache.userWithOrganizations = result; 152 return { 153 user: result.user, 154 organizations: result.organizations, 155 primaryOrganization: result.organizations[0] 156 }; 157 } 158 159 // Fallback to optimized query 160 const result = await findUserWithOrganizations(session.user.id); 161 if (!result) { 162 throw new Error('User not found in database'); 163 } 164 165 if (!result.organizations || result.organizations.length === 0) { 166 throw new Error('No organizations found'); 167 } 168 169 return { 170 user: result.user, 171 organizations: result.organizations, 172 primaryOrganization: result.organizations[0] 173 }; 174}