mirror of
https://github.com/formbricks/formbricks.git
synced 2026-02-05 02:58:36 -06:00
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import { unstable_cache } from "next/cache";
|
|
|
|
import { prisma } from "@formbricks/database";
|
|
import { ZId } from "@formbricks/types/environment";
|
|
|
|
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";
|
|
import { teamCache } from "../team/cache";
|
|
import { validateInputs } from "../utils/validate";
|
|
|
|
export const hasUserEnvironmentAccess = async (userId: string, environmentId: string) => {
|
|
return await unstable_cache(
|
|
async (): Promise<boolean> => {
|
|
validateInputs([userId, ZId], [environmentId, ZId]);
|
|
|
|
const environment = await prisma.environment.findUnique({
|
|
where: {
|
|
id: environmentId,
|
|
},
|
|
select: {
|
|
product: {
|
|
select: {
|
|
team: {
|
|
select: {
|
|
memberships: {
|
|
select: {
|
|
userId: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const environmentUsers = environment?.product.team.memberships.map((member) => member.userId) || [];
|
|
return environmentUsers.includes(userId);
|
|
},
|
|
[`hasUserEnvironmentAccess-${userId}-${environmentId}`],
|
|
{
|
|
revalidate: SERVICES_REVALIDATION_INTERVAL,
|
|
tags: [teamCache.tag.byEnvironmentId(environmentId), teamCache.tag.byUserId(userId)],
|
|
}
|
|
)();
|
|
};
|