mirror of
https://github.com/formbricks/formbricks.git
synced 2026-01-05 16:19:55 -06:00
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { ZId } from "@formbricks/types/environment";
|
|
import { validateInputs } from "../utils/validate";
|
|
import { getProduct } from "./service";
|
|
import { unstable_cache } from "next/cache";
|
|
import { getTeamsByUserId } from "../team/service";
|
|
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";
|
|
import { productCache } from "./cache";
|
|
import { getMembershipByUserIdTeamId } from "../../lib/membership/service";
|
|
import { getAccessFlags } from "../../lib/membership/utils";
|
|
|
|
export const canUserAccessProduct = async (userId: string, productId: string): Promise<boolean> =>
|
|
await unstable_cache(
|
|
async () => {
|
|
validateInputs([userId, ZId], [productId, ZId]);
|
|
|
|
if (!userId || !productId) return false;
|
|
|
|
const product = await getProduct(productId);
|
|
if (!product) return false;
|
|
|
|
const teamIds = (await getTeamsByUserId(userId)).map((team) => team.id);
|
|
return teamIds.includes(product.teamId);
|
|
},
|
|
[`canUserAccessProduct-${userId}-${productId}`],
|
|
{
|
|
revalidate: SERVICES_REVALIDATION_INTERVAL,
|
|
tags: [productCache.tag.byId(productId), productCache.tag.byUserId(userId)],
|
|
}
|
|
)();
|
|
|
|
export const verifyUserRoleAccess = async (
|
|
teamId: string,
|
|
userId: string
|
|
): Promise<{
|
|
hasCreateOrUpdateAccess: boolean;
|
|
hasDeleteAccess: boolean;
|
|
}> => {
|
|
const accessObject = {
|
|
hasCreateOrUpdateAccess: true,
|
|
hasDeleteAccess: true,
|
|
};
|
|
|
|
if (!teamId) {
|
|
throw new Error("Team not found");
|
|
}
|
|
|
|
const currentUserMembership = await getMembershipByUserIdTeamId(userId, teamId);
|
|
const { isDeveloper, isViewer } = getAccessFlags(currentUserMembership?.role);
|
|
|
|
if (isDeveloper || isViewer) {
|
|
accessObject.hasCreateOrUpdateAccess = false;
|
|
accessObject.hasDeleteAccess = false;
|
|
}
|
|
|
|
return accessObject;
|
|
};
|