@@ -27,6 +27,21 @@ import {
2727import { verifyTikTokSignature } from '@/lib/webhooks/providers/tiktok'
2828import { findTikTokWebhooksForOpenId } from '@/lib/webhooks/tiktok-fanout'
2929import { 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
3146const 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