Skip to content

Commit 702b415

Browse files
committed
fix(people): clear Slack identities on the next sweep once an organization's last bot is disabled
1 parent 3bcffe4 commit 702b415

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

‎apps/sim/lib/people/slack-identities.integration.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ it('clears the list once the Slack Search bot is disabled', async () => {
184184
.update(slackSearchInstallation)
185185
.set({ enabled: false })
186186
.where(eq(slackSearchInstallation.id, installationId))
187-
expect(await sweepNow()).toEqual({ refreshed: 1 })
187+
expect(await refreshDueSlackIdentities()).toEqual({ refreshed: 1 })
188188
expect(listMembers).not.toHaveBeenCalled()
189189
expect(await stored()).toEqual([])
190190
expect(

‎apps/sim/lib/people/slack-identities.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getErrorMessage } from '@sim/utils/errors'
99
import { chunkArray, sleep } from '@sim/utils/helpers'
1010
import { generateId } from '@sim/utils/id'
1111
import { backoffWithJitter } from '@sim/utils/retry'
12-
import { and, desc, eq, notInArray, sql } from 'drizzle-orm'
12+
import { and, desc, eq, notInArray, type SQL, sql } from 'drizzle-orm'
1313
import { runWithOutboundOrganization } from '@/lib/core/network/context.server'
1414
import { listSlackSearchMembers, type SlackSearchMember } from '@/lib/internal/slack/search-client'
1515
import { loadSlackSearchCredential } from '@/lib/knowledge/application/slack-search/repository'
@@ -38,10 +38,16 @@ type Lease = {
3838
}
3939

4040
/**
41-
* Leases due organizations: those with an enabled Slack Search bot, and those still holding
42-
* identities from a bot since disabled. Each lease is fenced by its token, so a sweep that
41+
* Leases due organizations: those with an enabled Slack Search bot, and, at once, those whose
42+
* last bot was disabled or removed, so their identities go on the next sweep. Each lease is fenced by its token, so a sweep that
4343
* outlives its lease cannot overwrite the next one's work.
4444
*/
45+
/** Whether the refresh row's organization has no enabled Slack Search bot left. */
46+
function botless(organizationId: SQL) {
47+
return sql`NOT EXISTS (SELECT 1 FROM ${installation} i
48+
WHERE i.organization_id = ${organizationId} AND i.enabled)`
49+
}
50+
4551
async function leaseDue(limit: number): Promise<Lease[]> {
4652
const leaseToken = generateId()
4753
return db.execute<Lease>(sql`
@@ -50,14 +56,16 @@ async function leaseDue(limit: number): Promise<Lease[]> {
5056
LEFT JOIN ${refresh} r ON r.organization_id = i.organization_id
5157
WHERE i.enabled AND (r.organization_id IS NULL OR r.next_refresh_at <= now())
5258
UNION
53-
SELECT r.organization_id FROM ${refresh} r WHERE r.next_refresh_at <= now()
59+
SELECT r.organization_id FROM ${refresh} r
60+
WHERE r.next_refresh_at <= now() OR (r.lease_token IS NULL AND ${botless(sql`r.organization_id`)})
5461
LIMIT ${limit}
5562
)
5663
INSERT INTO ${refresh} (organization_id, next_refresh_at, lease_token)
5764
SELECT organization_id, now() + make_interval(secs => ${LEASE_MS / 1000}), ${leaseToken} FROM due
5865
ON CONFLICT (organization_id) DO UPDATE
5966
SET next_refresh_at = excluded.next_refresh_at, lease_token = excluded.lease_token
6067
WHERE ${refresh.nextRefreshAt} <= now()
68+
OR (${refresh.leaseToken} IS NULL AND ${botless(sql`${refresh.organizationId}`)})
6169
RETURNING organization_id AS "organizationId", lease_token AS "leaseToken"
6270
`)
6371
}

0 commit comments

Comments
 (0)