Skip to content

Commit 1e2e927

Browse files
Bill Leoutsakoscursoragent
authored andcommitted
fix(tiktok): only count actually queued webhook executions
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 83841a3 commit 1e2e927

1 file changed

Lines changed: 41 additions & 9 deletions

File tree

  • apps/sim/app/api/webhooks/tiktok

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ import {
2727
import { verifyTikTokSignature } from '@/lib/webhooks/providers/tiktok'
2828
import { findTikTokWebhooksForOpenId } from '@/lib/webhooks/tiktok-fanout'
2929
import { blockExistsInDeployment } from '@/lib/workflows/persistence/utils'
30+
import { isTikTokEventMatch } from '@/triggers/tiktok/utils'
31+
32+
/**
33+
* queueWebhookExecution returns 200 for both real queues and event mismatches.
34+
* Only count responses that indicate work was actually enqueued.
35+
*/
36+
async function didQueueWebhookExecution(response: NextResponse): Promise<boolean> {
37+
if (!response.ok) return false
38+
try {
39+
const payload = (await response.clone().json()) as Record<string, unknown>
40+
return payload.message === 'Webhook processed'
41+
} catch {
42+
return false
43+
}
44+
}
3045

3146
const logger = createLogger('TikTokWebhookIngress')
3247

@@ -169,15 +184,32 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
169184
continue
170185
}
171186

172-
await queueWebhookExecution(webhookRecord, foundWorkflow, envelope, request, {
173-
requestId,
174-
path: webhookRecord.path,
175-
actorUserId: preprocessResult.actorUserId,
176-
executionId: preprocessResult.executionId,
177-
correlation: preprocessResult.correlation,
178-
receivedAt,
179-
})
180-
processed += 1
187+
const triggerId = webhookRecord.providerConfig?.triggerId
188+
if (
189+
typeof triggerId === 'string' &&
190+
triggerId.length > 0 &&
191+
!isTikTokEventMatch(triggerId, envelope.event)
192+
) {
193+
continue
194+
}
195+
196+
const queueResponse = await queueWebhookExecution(
197+
webhookRecord,
198+
foundWorkflow,
199+
envelope,
200+
request,
201+
{
202+
requestId,
203+
path: webhookRecord.path,
204+
actorUserId: preprocessResult.actorUserId,
205+
executionId: preprocessResult.executionId,
206+
correlation: preprocessResult.correlation,
207+
receivedAt,
208+
}
209+
)
210+
if (await didQueueWebhookExecution(queueResponse)) {
211+
processed += 1
212+
}
181213
}
182214

183215
if (processed === 0 && matches.length > 0) {

0 commit comments

Comments
 (0)