Skip to content

Commit df6065e

Browse files
Bill LeoutsakosBill Leoutsakos
authored andcommitted
refactor(tiktok): remove rollout compatibility
1 parent a1d8c79 commit df6065e

7 files changed

Lines changed: 20 additions & 252 deletions

File tree

apps/sim/app/api/webhooks/tiktok/route.test.ts

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,19 @@ import { requestUtilsMockFns, resetEnvMock, setEnv } from '@sim/testing'
77
import { NextRequest } from 'next/server'
88
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
99

10-
const {
11-
mockDispatchResolvedWebhookTarget,
12-
mockFindLegacyTikTokWebhooks,
13-
mockFindWebhooksByRoutingKey,
14-
mockRelease,
15-
} = vi.hoisted(() => ({
16-
mockDispatchResolvedWebhookTarget: vi.fn(),
17-
mockFindLegacyTikTokWebhooks: vi.fn(),
18-
mockFindWebhooksByRoutingKey: vi.fn(),
19-
mockRelease: vi.fn(),
20-
}))
10+
const { mockDispatchResolvedWebhookTarget, mockFindWebhooksByRoutingKey, mockRelease } = vi.hoisted(
11+
() => ({
12+
mockDispatchResolvedWebhookTarget: vi.fn(),
13+
mockFindWebhooksByRoutingKey: vi.fn(),
14+
mockRelease: vi.fn(),
15+
})
16+
)
2117

2218
vi.mock('@/lib/webhooks/processor', () => ({
2319
dispatchResolvedWebhookTarget: mockDispatchResolvedWebhookTarget,
2420
findWebhooksByRoutingKey: mockFindWebhooksByRoutingKey,
2521
}))
2622

27-
vi.mock('@/lib/webhooks/tiktok-legacy-routing', () => ({
28-
findLegacyTikTokWebhooks: mockFindLegacyTikTokWebhooks,
29-
}))
30-
3123
vi.mock('@/lib/core/admission/gate', () => ({
3224
admissionRejectedResponse: vi.fn(() => new Response(null, { status: 503 })),
3325
tryAdmit: vi.fn(() => ({ release: mockRelease })),
@@ -76,7 +68,6 @@ describe('TikTok app webhook route', () => {
7668
setEnv({ TIKTOK_CLIENT_ID: 'client-key', TIKTOK_CLIENT_SECRET: 'client-secret' })
7769
requestUtilsMockFns.mockGenerateRequestId.mockReturnValue('request-1')
7870
mockFindWebhooksByRoutingKey.mockResolvedValue([])
79-
mockFindLegacyTikTokWebhooks.mockResolvedValue([])
8071
mockDispatchResolvedWebhookTarget.mockResolvedValue({ outcome: 'queued', reason: 'queued' })
8172
})
8273

@@ -113,22 +104,6 @@ describe('TikTok app webhook route', () => {
113104
expect(mockDispatchResolvedWebhookTarget).not.toHaveBeenCalled()
114105
})
115106

116-
it('also dispatches legacy null-routing-key registrations during rolling deployment', async () => {
117-
mockFindLegacyTikTokWebhooks.mockResolvedValue([target('legacy-webhook')])
118-
119-
const response = await POST(signedRequest({ userOpenId: 'legacy-user' }))
120-
121-
expect(response.status).toBe(200)
122-
expect(mockFindLegacyTikTokWebhooks).toHaveBeenCalledWith('legacy-user')
123-
expect(mockDispatchResolvedWebhookTarget).toHaveBeenCalledWith(
124-
expect.objectContaining({ id: 'legacy-webhook' }),
125-
expect.objectContaining({ id: 'workflow-legacy-webhook' }),
126-
expect.any(Object),
127-
expect.any(NextRequest),
128-
expect.any(Object)
129-
)
130-
})
131-
132107
it('dispatches matching workflows sequentially', async () => {
133108
mockFindWebhooksByRoutingKey.mockResolvedValue([target('webhook-1'), target('webhook-2')])
134109
const order: string[] = []

apps/sim/app/api/webhooks/tiktok/route.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
1414
import { WEBHOOK_MAX_BODY_BYTES } from '@/lib/webhooks/constants'
1515
import { dispatchResolvedWebhookTarget, findWebhooksByRoutingKey } from '@/lib/webhooks/processor'
1616
import { verifyTikTokSignature } from '@/lib/webhooks/providers/tiktok'
17-
import { findLegacyTikTokWebhooks } from '@/lib/webhooks/tiktok-legacy-routing'
1817

1918
const logger = createLogger('TikTokAppWebhookAPI')
2019

@@ -94,9 +93,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
9493
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
9594
}
9695

97-
const routedWebhooks = await findWebhooksByRoutingKey(envelope.user_openid, requestId, 'tiktok')
98-
const legacyWebhooks = await findLegacyTikTokWebhooks(envelope.user_openid)
99-
const webhooks = [...routedWebhooks, ...legacyWebhooks]
96+
const webhooks = await findWebhooksByRoutingKey(envelope.user_openid, requestId, 'tiktok')
10097
let dispatched = 0
10198
let failed = 0
10299
for (const { webhook, workflow } of webhooks) {

apps/sim/lib/webhooks/tiktok-legacy-routing.test.ts

Lines changed: 0 additions & 102 deletions
This file was deleted.

apps/sim/lib/webhooks/tiktok-legacy-routing.ts

Lines changed: 0 additions & 83 deletions
This file was deleted.

packages/db/migrations/0282_tiktok_routing_key.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,11 @@ END $$;
5959
-- migration-safe: remove the session-local scratch table after the backfill completes.
6060
DROP TABLE IF EXISTS "_tiktok_webhook_routing_backfill";
6161
--> statement-breakpoint
62+
-- The routing-key index replaces the old TikTok credential-expression lookup.
6263
COMMIT;
64+
--> statement-breakpoint
65+
SET lock_timeout = 0;
66+
--> statement-breakpoint
67+
DROP INDEX CONCURRENTLY IF EXISTS "webhook_tiktok_credential_id_idx";
68+
--> statement-breakpoint
69+
SET lock_timeout = '5s';

packages/db/migrations/meta/0282_snapshot.json

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13618,22 +13618,6 @@
1361813618
"method": "btree",
1361913619
"with": {}
1362013620
},
13621-
"webhook_tiktok_credential_id_idx": {
13622-
"name": "webhook_tiktok_credential_id_idx",
13623-
"columns": [
13624-
{
13625-
"expression": "((\"provider_config\")::jsonb ->> 'credentialId')",
13626-
"asc": true,
13627-
"isExpression": true,
13628-
"nulls": "last"
13629-
}
13630-
],
13631-
"isUnique": false,
13632-
"where": "\"webhook\".\"provider\" = 'tiktok' AND \"webhook\".\"is_active\" = true AND \"webhook\".\"archived_at\" IS NULL",
13633-
"concurrently": false,
13634-
"method": "btree",
13635-
"with": {}
13636-
},
1363713621
"idx_webhook_on_workflow_id_block_id_updated_at_desc": {
1363813622
"name": "idx_webhook_on_workflow_id_block_id_updated_at_desc",
1363913623
"columns": [

packages/db/schema.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -833,11 +833,6 @@ export const jobExecutionLogs = pgTable(
833833
})
834834
)
835835

836-
/** Extracts the canonical credential ID persisted in webhook provider configuration. */
837-
export function webhookCredentialIdExpression(column: AnyPgColumn): SQL<string> {
838-
return sql<string>`((${column})::jsonb ->> 'credentialId')`
839-
}
840-
841836
export const webhook = pgTable(
842837
'webhook',
843838
{
@@ -856,15 +851,15 @@ export const webhook = pgTable(
856851
blockId: text('block_id'),
857852
/**
858853
* URL-addressable webhook path. NULL for shared-app providers (e.g. the
859-
* native Slack OAuth trigger) whose events arrive on a single shared
854+
* native Slack and TikTok triggers) whose events arrive on a single shared
860855
* endpoint and route by `routingKey` instead of a per-workflow path.
861856
*/
862857
path: text('path'),
863858
/**
864-
* Tenant routing key for shared-app providers. For `provider='slack_app'`
865-
* this is the Slack `team_id`, derived server-side from the connected
866-
* credential at deploy time — never user input. Inbound events match on
867-
* this after HMAC verification.
859+
* Tenant routing key for shared-app providers, such as Slack `team_id` or
860+
* TikTok `open_id`, derived server-side from the connected credential at
861+
* deploy time — never user input. Inbound events match on this after HMAC
862+
* verification.
868863
*/
869864
routingKey: text('routing_key'),
870865
provider: text('provider'), // e.g., "whatsapp", "github", etc.
@@ -896,11 +891,6 @@ export const webhook = pgTable(
896891
providerActiveWorkflowDeploymentIdx: index(
897892
'idx_webhook_on_provider_is_active_workflow_id_deploym_bdeed5468'
898893
).on(table.provider, table.isActive, table.workflowId, table.deploymentVersionId),
899-
tiktokCredentialIdIdx: index('webhook_tiktok_credential_id_idx')
900-
.on(webhookCredentialIdExpression(table.providerConfig))
901-
.where(
902-
sql`${table.provider} = 'tiktok' AND ${table.isActive} = true AND ${table.archivedAt} IS NULL`
903-
),
904894
workflowBlockUpdatedDescIdx: index('idx_webhook_on_workflow_id_block_id_updated_at_desc').on(
905895
table.workflowId,
906896
table.blockId,

0 commit comments

Comments
 (0)