Spaces:
Sleeping
Sleeping
File size: 1,273 Bytes
097eaf0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import { supabase_rr_service } from "../supabase";
import { WebhookConfig } from "./types";
import { config } from "../../config";
export async function getWebhookConfig(
teamId: string,
jobId: string,
webhook?: WebhookConfig,
): Promise<{ config: WebhookConfig; secret?: string } | null> {
// priority:
// - webhook
// - self-hosted environment variable
if (webhook) {
return { config: webhook, secret: await getHmacSecret(teamId) };
}
const selfHostedUrl = config.SELF_HOSTED_WEBHOOK_URL?.replace(
"{{JOB_ID}}",
jobId,
);
if (selfHostedUrl) {
return {
config: {
url: selfHostedUrl,
headers: {},
metadata: {},
events: ["completed", "failed", "page", "started"],
},
secret: config.SELF_HOSTED_WEBHOOK_HMAC_SECRET,
};
}
return null;
}
async function getHmacSecret(teamId: string): Promise<string | undefined> {
if (config.USE_DB_AUTHENTICATION !== true) {
return config.SELF_HOSTED_WEBHOOK_HMAC_SECRET;
}
try {
const { data, error } = await supabase_rr_service
.from("teams")
.select("hmac_secret")
.eq("id", teamId)
.limit(1)
.single();
return error ? undefined : data?.hmac_secret;
} catch {
return undefined;
}
}
|