@@ -65,19 +65,10 @@ async function ensureWorkspaceCredentialMemberships(
6565 credentialId : string ,
6666 memberUserIds : string [ ] ,
6767 ownerUserId : string ,
68- workspaceId : string
68+ wsPermissionByUser : Map < string , string >
6969) {
7070 if ( ! memberUserIds . length ) return
7171
72- const workspacePermissionRows = await db
73- . select ( { userId : permissions . userId , permissionType : permissions . permissionType } )
74- . from ( permissions )
75- . where ( and ( eq ( permissions . entityType , 'workspace' ) , eq ( permissions . entityId , workspaceId ) ) )
76-
77- const wsPermissionByUser = new Map (
78- workspacePermissionRows . map ( ( row ) => [ row . userId , row . permissionType ] )
79- )
80-
8172 const existingMemberships = await db
8273 . select ( {
8374 id : credentialMember . id ,
@@ -137,17 +128,25 @@ export async function syncWorkspaceEnvCredentials(params: {
137128 actingUserId : string
138129} ) {
139130 const { workspaceId, envKeys, actingUserId } = params
140- const [ [ workspaceRow ] , memberUserIds ] = await Promise . all ( [
131+ const [ [ workspaceRow ] , memberUserIds , wsPermissionRows ] = await Promise . all ( [
141132 db
142133 . select ( { ownerId : workspace . ownerId } )
143134 . from ( workspace )
144135 . where ( eq ( workspace . id , workspaceId ) )
145136 . limit ( 1 ) ,
146137 getWorkspaceMemberUserIds ( workspaceId ) ,
138+ db
139+ . select ( { userId : permissions . userId , permissionType : permissions . permissionType } )
140+ . from ( permissions )
141+ . where ( and ( eq ( permissions . entityType , 'workspace' ) , eq ( permissions . entityId , workspaceId ) ) ) ,
147142 ] )
148143
149144 if ( ! workspaceRow ) return
150145
146+ const wsPermissionByUser = new Map (
147+ wsPermissionRows . map ( ( row ) => [ row . userId , row . permissionType ] )
148+ )
149+
151150 const normalizedKeys = Array . from ( new Set ( envKeys . filter ( Boolean ) ) )
152151 const existingCredentials = await db
153152 . select ( {
@@ -197,7 +196,7 @@ export async function syncWorkspaceEnvCredentials(params: {
197196 credentialId ,
198197 memberUserIds ,
199198 workspaceRow . ownerId ,
200- workspaceId
199+ wsPermissionByUser
201200 )
202201 }
203202
@@ -232,18 +231,25 @@ export async function createWorkspaceEnvCredentials(params: {
232231 const keys = Array . from ( new Set ( newKeys . filter ( Boolean ) ) )
233232 if ( keys . length === 0 ) return
234233
235- const [ [ workspaceRow ] , memberUserIds ] = await Promise . all ( [
234+ const [ [ workspaceRow ] , memberUserIds , wsPermissionRows ] = await Promise . all ( [
236235 db
237236 . select ( { ownerId : workspace . ownerId } )
238237 . from ( workspace )
239238 . where ( eq ( workspace . id , workspaceId ) )
240239 . limit ( 1 ) ,
241240 getWorkspaceMemberUserIds ( workspaceId ) ,
241+ db
242+ . select ( { userId : permissions . userId , permissionType : permissions . permissionType } )
243+ . from ( permissions )
244+ . where ( and ( eq ( permissions . entityType , 'workspace' ) , eq ( permissions . entityId , workspaceId ) ) ) ,
242245 ] )
243246
244247 if ( ! workspaceRow ) return
245248
246249 const ownerUserId = workspaceRow . ownerId
250+ const wsPermissionByUser = new Map (
251+ wsPermissionRows . map ( ( row ) => [ row . userId , row . permissionType ] )
252+ )
247253 const now = new Date ( )
248254 const createdIds : string [ ] = [ ]
249255
@@ -269,15 +275,6 @@ export async function createWorkspaceEnvCredentials(params: {
269275
270276 if ( createdIds . length === 0 || memberUserIds . length === 0 ) return
271277
272- const wsPermissionRows = await db
273- . select ( { userId : permissions . userId , permissionType : permissions . permissionType } )
274- . from ( permissions )
275- . where ( and ( eq ( permissions . entityType , 'workspace' ) , eq ( permissions . entityId , workspaceId ) ) )
276-
277- const wsPermissionByUser = new Map (
278- wsPermissionRows . map ( ( row ) => [ row . userId , row . permissionType ] )
279- )
280-
281278 // Bulk-insert memberships for all new credentials × all workspace members in one query
282279 const membershipValues = createdIds . flatMap ( ( credentialId ) =>
283280 memberUserIds . map ( ( memberUserId ) => {
0 commit comments