11import { db } from '@sim/db'
2- import { credentialSet , credentialSetMember , member , user } from '@sim/db/schema'
2+ import { account , credentialSet , credentialSetMember , member , user } from '@sim/db/schema'
33import { createLogger } from '@sim/logger'
4- import { and , eq } from 'drizzle-orm'
4+ import { and , eq , inArray } from 'drizzle-orm'
55import { type NextRequest , NextResponse } from 'next/server'
66import { getSession } from '@/lib/auth'
7+ import { syncAllWebhooksForCredentialSet } from '@/lib/webhooks/utils.server'
78
89const logger = createLogger ( 'CredentialSetMembers' )
910
@@ -12,6 +13,8 @@ async function getCredentialSetWithAccess(credentialSetId: string, userId: strin
1213 . select ( {
1314 id : credentialSet . id ,
1415 organizationId : credentialSet . organizationId ,
16+ type : credentialSet . type ,
17+ providerId : credentialSet . providerId ,
1518 } )
1619 . from ( credentialSet )
1720 . where ( eq ( credentialSet . id , credentialSetId ) )
@@ -59,7 +62,58 @@ export async function GET(req: NextRequest, { params }: { params: Promise<{ id:
5962 . leftJoin ( user , eq ( credentialSetMember . userId , user . id ) )
6063 . where ( eq ( credentialSetMember . credentialSetId , id ) )
6164
62- return NextResponse . json ( { members } )
65+ // Get credentials for all active members
66+ const activeMembers = members . filter ( ( m ) => m . status === 'active' )
67+ const memberUserIds = activeMembers . map ( ( m ) => m . userId )
68+
69+ let credentials : { userId : string ; providerId : string ; accountId : string } [ ] = [ ]
70+ if ( memberUserIds . length > 0 ) {
71+ // If credential set is for a specific provider, filter by that provider
72+ if ( result . set . type === 'specific' && result . set . providerId ) {
73+ credentials = await db
74+ . select ( {
75+ userId : account . userId ,
76+ providerId : account . providerId ,
77+ accountId : account . accountId ,
78+ } )
79+ . from ( account )
80+ . where (
81+ and ( inArray ( account . userId , memberUserIds ) , eq ( account . providerId , result . set . providerId ) )
82+ )
83+ } else {
84+ credentials = await db
85+ . select ( {
86+ userId : account . userId ,
87+ providerId : account . providerId ,
88+ accountId : account . accountId ,
89+ } )
90+ . from ( account )
91+ . where ( inArray ( account . userId , memberUserIds ) )
92+ }
93+ }
94+
95+ // Group credentials by userId
96+ const credentialsByUser = credentials . reduce (
97+ ( acc , cred ) => {
98+ if ( ! acc [ cred . userId ] ) {
99+ acc [ cred . userId ] = [ ]
100+ }
101+ acc [ cred . userId ] . push ( {
102+ providerId : cred . providerId ,
103+ accountId : cred . accountId ,
104+ } )
105+ return acc
106+ } ,
107+ { } as Record < string , { providerId : string ; accountId : string } [ ] >
108+ )
109+
110+ // Attach credentials to members
111+ const membersWithCredentials = members . map ( ( m ) => ( {
112+ ...m ,
113+ credentials : credentialsByUser [ m . userId ] || [ ] ,
114+ } ) )
115+
116+ return NextResponse . json ( { members : membersWithCredentials } )
63117}
64118
65119export async function DELETE ( req : NextRequest , { params } : { params : Promise < { id : string } > } ) {
@@ -106,6 +160,17 @@ export async function DELETE(req: NextRequest, { params }: { params: Promise<{ i
106160 userId : session . user . id ,
107161 } )
108162
163+ try {
164+ const requestId = crypto . randomUUID ( ) . slice ( 0 , 8 )
165+ const syncResult = await syncAllWebhooksForCredentialSet ( id , requestId )
166+ logger . info ( 'Synced webhooks after member removed' , {
167+ credentialSetId : id ,
168+ ...syncResult ,
169+ } )
170+ } catch ( syncError ) {
171+ logger . error ( 'Error syncing webhooks after member removed' , syncError )
172+ }
173+
109174 return NextResponse . json ( { success : true } )
110175 } catch ( error ) {
111176 logger . error ( 'Error removing member from credential set' , error )
0 commit comments