@@ -10,6 +10,7 @@ import { chunkArray, sleep } from '@sim/utils/helpers'
1010import { generateId } from '@sim/utils/id'
1111import { backoffWithJitter } from '@sim/utils/retry'
1212import { and , desc , eq , notInArray , sql } from 'drizzle-orm'
13+ import { runWithOutboundOrganization } from '@/lib/core/network/context.server'
1314import { listSlackSearchMembers , type SlackSearchMember } from '@/lib/internal/slack/search-client'
1415import { loadSlackSearchCredential } from '@/lib/knowledge/application/slack-search/repository'
1516import { isPeopleEnabled } from '@/lib/people/feature-flag'
@@ -102,7 +103,11 @@ async function readMembers(
102103 * the bot is checked again a day later. A list is written only while its bot is still the
103104 * organization's enabled one.
104105 */
105- async function refreshOrganization ( lease : Lease , deadline : number , signal : AbortSignal ) {
106+ async function refreshOrganization (
107+ lease : Lease ,
108+ deadline : number ,
109+ signal : AbortSignal
110+ ) : Promise < boolean > {
106111 const { organizationId } = lease
107112 const [ bot ] = await db
108113 . select ( {
@@ -116,7 +121,7 @@ async function refreshOrganization(lease: Lease, deadline: number, signal: Abort
116121 . orderBy ( desc ( installation . updatedAt ) )
117122 . limit ( 1 )
118123 if ( ! bot || ! ( await isPeopleEnabled ( organizationId ) ) ) {
119- await db . transaction ( async ( tx ) => {
124+ return db . transaction ( async ( tx ) => {
120125 const [ held ] = bot
121126 ? await tx
122127 . update ( refresh )
@@ -129,11 +134,14 @@ async function refreshOrganization(lease: Lease, deadline: number, signal: Abort
129134 . returning ( )
130135 : await tx . delete ( refresh ) . where ( holds ( lease ) ) . returning ( )
131136 if ( held ) await tx . delete ( identity ) . where ( eq ( identity . organizationId , organizationId ) )
137+ return Boolean ( held )
132138 } )
133- return
134139 }
135140 const { botToken } = await loadSlackSearchCredential ( bot . credentialId , organizationId )
136- const members = await readMembers ( botToken , bot . teamId , deadline , signal )
141+ /** Slack is reached through the organization's own outbound route, like any of its requests. */
142+ const members = await runWithOutboundOrganization ( organizationId , ( ) =>
143+ readMembers ( botToken , bot . teamId , deadline , signal )
144+ )
137145 const rows = [ ...new Map ( members . map ( ( member ) => [ member . email , member ] ) ) . values ( ) ] . map (
138146 ( member ) => ( {
139147 organizationId,
@@ -142,7 +150,7 @@ async function refreshOrganization(lease: Lease, deadline: number, signal: Abort
142150 timezone : directoryTimeZone ( member . timezone ) ,
143151 } )
144152 )
145- await db . transaction ( async ( tx ) => {
153+ return db . transaction ( async ( tx ) => {
146154 const [ current ] = await tx
147155 . select ( { id : installation . id } )
148156 . from ( installation )
@@ -165,7 +173,7 @@ async function refreshOrganization(lease: Lease, deadline: number, signal: Abort
165173 } )
166174 . where ( holds ( lease ) )
167175 . returning ( )
168- if ( ! held ) return
176+ if ( ! held ) return false
169177 for ( const chunk of chunkArray ( rows , UPSERT_CHUNK ) )
170178 await tx
171179 . insert ( identity )
@@ -186,6 +194,7 @@ async function refreshOrganization(lease: Lease, deadline: number, signal: Abort
186194 : undefined
187195 )
188196 )
197+ return true
189198 } )
190199}
191200
@@ -199,8 +208,7 @@ export async function refreshDueSlackIdentities() {
199208 let refreshed = 0
200209 for ( const lease of await leaseDue ( 2 ) ) {
201210 try {
202- await refreshOrganization ( lease , deadline , signal )
203- refreshed ++
211+ if ( await refreshOrganization ( lease , deadline , signal ) ) refreshed ++
204212 } catch ( error ) {
205213 logger . warn ( 'Could not refresh Slack identities' , {
206214 organizationId : lease . organizationId ,
0 commit comments