Files
formbricks-formbricks/packages/lib/webhook/auth.ts
Dhruwang Jariwala ab80bc1bf2 feat: language switch (#2692)
Co-authored-by: Johannes <johannes@formbricks.com>
Co-authored-by: Matti Nannt <mail@matthiasnannt.com>
2024-06-12 14:10:22 +00:00

30 lines
923 B
TypeScript

import { ZId } from "@formbricks/types/environment";
import { cache } from "../cache";
import { hasUserEnvironmentAccess } from "../environment/auth";
import { validateInputs } from "../utils/validate";
import { webhookCache } from "./cache";
import { getWebhook } from "./service";
export const canUserAccessWebhook = async (userId: string, webhookId: string): Promise<boolean> =>
cache(
async () => {
validateInputs([userId, ZId], [webhookId, ZId]);
try {
const webhook = await getWebhook(webhookId);
if (!webhook) return false;
const hasAccessToEnvironment = await hasUserEnvironmentAccess(userId, webhook.environmentId);
if (!hasAccessToEnvironment) return false;
return true;
} catch (error) {
throw error;
}
},
[`canUserAccessWebhook-${userId}-${webhookId}`],
{
tags: [webhookCache.tag.byId(webhookId)],
}
)();