d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
103 lines
2.7 KiB
TypeScript
103 lines
2.7 KiB
TypeScript
import {
|
|
account,
|
|
credential,
|
|
db,
|
|
webhook,
|
|
webhookCredentialIdExpression,
|
|
workflow,
|
|
workflowDeploymentVersion,
|
|
} from '@sim/db'
|
|
import { createLogger } from '@sim/logger'
|
|
import { and, eq, isNull, like, or } from 'drizzle-orm'
|
|
|
|
const logger = createLogger('TikTokWebhookTargets')
|
|
const ACCOUNT_ID_UUID_SUFFIX = /-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
|
|
export interface TikTokWebhookTarget {
|
|
webhook: typeof webhook.$inferSelect
|
|
workflow: typeof workflow.$inferSelect
|
|
}
|
|
|
|
function escapeLikePattern(value: string): string {
|
|
return value.replace(/\\/g, '\\\\').replace(/%/g, '\\%').replace(/_/g, '\\_')
|
|
}
|
|
|
|
function openIdFromAccountId(accountId: string): string {
|
|
return accountId.replace(ACCOUNT_ID_UUID_SUFFIX, '')
|
|
}
|
|
|
|
/**
|
|
* Resolves a TikTok user_openid to active webhook targets through the credential ID persisted in
|
|
* providerConfig. The workflow-workspace equality prevents cross-tenant event routing.
|
|
*/
|
|
export async function findTikTokWebhookTargets(
|
|
userOpenId: string,
|
|
requestId: string
|
|
): Promise<TikTokWebhookTarget[]> {
|
|
if (!userOpenId) return []
|
|
|
|
const rows = await db
|
|
.select({
|
|
accountId: account.accountId,
|
|
webhook,
|
|
workflow,
|
|
})
|
|
.from(account)
|
|
.innerJoin(
|
|
credential,
|
|
and(
|
|
eq(credential.accountId, account.id),
|
|
eq(credential.type, 'oauth'),
|
|
eq(credential.providerId, 'tiktok')
|
|
)
|
|
)
|
|
.innerJoin(
|
|
webhook,
|
|
and(
|
|
eq(webhookCredentialIdExpression(webhook.providerConfig), credential.id),
|
|
eq(webhook.provider, 'tiktok'),
|
|
eq(webhook.isActive, true),
|
|
isNull(webhook.archivedAt)
|
|
)
|
|
)
|
|
.innerJoin(
|
|
workflow,
|
|
and(
|
|
eq(workflow.id, webhook.workflowId),
|
|
eq(workflow.workspaceId, credential.workspaceId),
|
|
isNull(workflow.archivedAt)
|
|
)
|
|
)
|
|
.leftJoin(
|
|
workflowDeploymentVersion,
|
|
and(
|
|
eq(workflowDeploymentVersion.workflowId, workflow.id),
|
|
eq(workflowDeploymentVersion.isActive, true)
|
|
)
|
|
)
|
|
.where(
|
|
and(
|
|
eq(account.providerId, 'tiktok'),
|
|
like(account.accountId, `${escapeLikePattern(userOpenId)}-%`),
|
|
or(
|
|
eq(webhook.deploymentVersionId, workflowDeploymentVersion.id),
|
|
and(isNull(workflowDeploymentVersion.id), isNull(webhook.deploymentVersionId))
|
|
)
|
|
)
|
|
)
|
|
|
|
const targets = rows
|
|
.filter((row) => openIdFromAccountId(row.accountId) === userOpenId)
|
|
.map(({ webhook: webhookRecord, workflow: workflowRecord }) => ({
|
|
webhook: webhookRecord,
|
|
workflow: workflowRecord,
|
|
}))
|
|
|
|
logger.info(`[${requestId}] Resolved TikTok webhook targets`, {
|
|
userOpenIdPrefix: userOpenId.slice(0, 12),
|
|
webhookCount: targets.length,
|
|
})
|
|
|
|
return targets
|
|
}
|