@@ -227,19 +227,13 @@ export async function safeAccountInsert(
227227}
228228
229229/**
230- * Get a credential by ID and verify it belongs to the user
230+ * Get a credential by resolved account ID and verify it belongs to the user.
231231 */
232- export async function getCredential ( requestId : string , credentialId : string , userId : string ) {
233- const resolved = await resolveOAuthAccountId ( credentialId )
234- if ( ! resolved ) {
235- logger . warn ( `[${ requestId } ] Credential is not an OAuth credential` )
236- return undefined
237- }
238-
232+ async function getCredentialByAccountId ( requestId : string , accountId : string , userId : string ) {
239233 const credentials = await db
240234 . select ( )
241235 . from ( account )
242- . where ( and ( eq ( account . id , resolved . accountId ) , eq ( account . userId , userId ) ) )
236+ . where ( and ( eq ( account . id , accountId ) , eq ( account . userId , userId ) ) )
243237 . limit ( 1 )
244238
245239 if ( ! credentials . length ) {
@@ -249,8 +243,20 @@ export async function getCredential(requestId: string, credentialId: string, use
249243
250244 return {
251245 ...credentials [ 0 ] ,
252- resolvedCredentialId : resolved . accountId ,
246+ resolvedCredentialId : accountId ,
247+ }
248+ }
249+
250+ /**
251+ * Get a credential by ID and verify it belongs to the user.
252+ */
253+ export async function getCredential ( requestId : string , credentialId : string , userId : string ) {
254+ const resolved = await resolveOAuthAccountId ( credentialId )
255+ if ( ! resolved ) {
256+ logger . warn ( `[${ requestId } ] Credential is not an OAuth credential` )
257+ return undefined
253258 }
259+ return getCredentialByAccountId ( requestId , resolved . accountId , userId )
254260}
255261
256262export async function getOAuthToken ( userId : string , providerId : string ) : Promise < string | null > {
@@ -370,8 +376,8 @@ export async function refreshAccessTokenIfNeeded(
370376 return getServiceAccountToken ( resolved . credentialId , scopes , impersonateEmail )
371377 }
372378
373- // Get the credential directly using the getCredential helper
374- const credential = await getCredential ( requestId , credentialId , userId )
379+ // Use the already-resolved account ID to avoid a redundant resolveOAuthAccountId query
380+ const credential = await getCredentialByAccountId ( requestId , resolved . accountId , userId )
375381
376382 if ( ! credential ) {
377383 return null
0 commit comments