MCPcopy Index your code
hub / github.com/simstudioai/sim / syncWorkspaceOAuthCredentialsForUser

Function syncWorkspaceOAuthCredentialsForUser

apps/sim/lib/credentials/oauth.ts:30–169  ·  view source on GitHub ↗
(
  params: SyncWorkspaceOAuthCredentialsForUserParams
)

Source from the content-addressed store, hash-verified

28 * credential creation is handled by the draft-based OAuth connect flow.
29 */
30export async function syncWorkspaceOAuthCredentialsForUser(
31 params: SyncWorkspaceOAuthCredentialsForUserParams
32): Promise<SyncWorkspaceOAuthCredentialsForUserResult> {
33 const { workspaceId, userId } = params
34
35 const userAccounts = await db
36 .select({
37 id: account.id,
38 providerId: account.providerId,
39 accountId: account.accountId,
40 })
41 .from(account)
42 .where(
43 and(eq(account.userId, userId), notInArray(account.providerId, [...NON_OAUTH_PROVIDER_IDS]))
44 )
45
46 if (userAccounts.length === 0) {
47 return { updatedMemberships: 0 }
48 }
49
50 const accountIds = userAccounts.map((row) => row.id)
51 const existingCredentials = await db
52 .select({
53 id: credential.id,
54 displayName: credential.displayName,
55 providerId: credential.providerId,
56 accountId: credential.accountId,
57 })
58 .from(credential)
59 .where(
60 and(
61 eq(credential.workspaceId, workspaceId),
62 eq(credential.type, 'oauth'),
63 inArray(credential.accountId, accountIds)
64 )
65 )
66
67 const now = new Date()
68 const userAccountById = new Map(userAccounts.map((row) => [row.id, row]))
69 for (const existingCredential of existingCredentials) {
70 if (!existingCredential.accountId) continue
71 const linkedAccount = userAccountById.get(existingCredential.accountId)
72 if (!linkedAccount) continue
73
74 const normalizedLabel =
75 getServiceConfigByProviderId(linkedAccount.providerId)?.name || linkedAccount.providerId
76 const shouldNormalizeDisplayName =
77 existingCredential.displayName === linkedAccount.accountId ||
78 existingCredential.displayName === linkedAccount.providerId
79
80 if (!shouldNormalizeDisplayName || existingCredential.displayName === normalizedLabel) {
81 continue
82 }
83
84 await db
85 .update(credential)
86 .set({
87 displayName: normalizedLabel,

Callers 2

route.tsFile · 0.90
route.tsFile · 0.90

Calls 6

generateIdFunction · 0.90
getPostgresErrorCodeFunction · 0.70
getMethod · 0.65
setMethod · 0.65
eqFunction · 0.50

Tested by

no test coverage detected