fix: caching for license key check

This commit is contained in:
pandeymangg
2024-05-02 15:03:58 +05:30
parent 1891f286e7
commit b27314cec6
3 changed files with 61 additions and 17 deletions

View File

@@ -37,7 +37,7 @@ export default async function EnterpriseLicensePage({ params }) {
notFound();
}
const isEnterpriseEdition = await getIsEnterpriseEdition(team.id);
const isEnterpriseEdition = await getIsEnterpriseEdition();
const paidFeatures = [
{

View File

@@ -1,20 +1,48 @@
import "server-only";
import { cache } from "@formbricks/lib/cache";
import { cache, revalidateTag } from "@formbricks/lib/cache";
import { ENTERPRISE_LICENSE_KEY, IS_FORMBRICKS_CLOUD } from "@formbricks/lib/constants";
import { hashString } from "@formbricks/lib/hashString";
import { TTeam } from "@formbricks/types/teams";
import { prisma } from "../../database/src";
export const getIsEnterpriseEdition = async (teamId: string): Promise<boolean> => {
const getPreviousResult = (): Promise<{ active: boolean | null; lastChecked: Date }> =>
cache(
async () => ({
active: null,
lastChecked: new Date(0),
}),
["getPreviousResult"],
{
tags: [`getPreviousResult`],
}
)();
const setPreviousResult = async (previousResult: { active: boolean | null; lastChecked: Date }) => {
revalidateTag("getPreviousResult");
const { lastChecked, active } = previousResult;
await cache(
async () => ({
active,
lastChecked,
}),
["getPreviousResult"],
{
tags: [`getPreviousResult`],
}
)();
};
export const getIsEnterpriseEdition = async (): Promise<boolean> => {
if (!ENTERPRISE_LICENSE_KEY || ENTERPRISE_LICENSE_KEY.length === 0) {
return false;
}
const hashedKey = hashString(ENTERPRISE_LICENSE_KEY);
const isValid = await cache(
const isValid: boolean | null = await cache(
async () => {
try {
const now = new Date();
@@ -27,13 +55,6 @@ export const getIsEnterpriseEdition = async (teamId: string): Promise<boolean> =
gte: startOfYear,
lt: endOfYear,
},
survey: {
environment: {
product: {
teamId,
},
},
},
},
});
@@ -52,17 +73,38 @@ export const getIsEnterpriseEdition = async (teamId: string): Promise<boolean> =
return responseJson.data.status === "active";
}
return false;
return null;
} catch (error) {
console.error("Error while checking license: ", error);
return false;
return null;
}
},
[`getIsEnterpriseEdition-${hashedKey}`],
{ revalidate: 60 * 60 * 24 }
)();
return isValid;
const previousResult = await getPreviousResult();
if (previousResult.active === null) {
if (isValid === null) {
await setPreviousResult({ active: false, lastChecked: new Date() });
return false;
}
}
if (isValid !== null) {
await setPreviousResult({ active: isValid, lastChecked: new Date() });
return isValid;
} else {
// if result is undefined -> error
// if the last check was less than 24 hours, return the previous value:
if (new Date().getTime() - previousResult.lastChecked.getTime() <= 24 * 60 * 60 * 1000) {
return previousResult.active !== null ? previousResult.active : false;
}
// if the last check was more than 24 hours, throw an error
throw new Error("Error while checking license");
}
};
export const getRemoveInAppBrandingPermission = (team: TTeam): boolean => {
@@ -79,18 +121,18 @@ export const getRemoveLinkBrandingPermission = (team: TTeam): boolean => {
export const getRoleManagementPermission = async (team: TTeam): Promise<boolean> => {
if (IS_FORMBRICKS_CLOUD) return team.billing.features.inAppSurvey.status !== "inactive";
else if (!IS_FORMBRICKS_CLOUD) return await getIsEnterpriseEdition(team.id);
else if (!IS_FORMBRICKS_CLOUD) return await getIsEnterpriseEdition();
else return false;
};
export const getAdvancedTargetingPermission = async (team: TTeam): Promise<boolean> => {
if (IS_FORMBRICKS_CLOUD) return team.billing.features.userTargeting.status !== "inactive";
else if (!IS_FORMBRICKS_CLOUD) return await getIsEnterpriseEdition(team.id);
else if (!IS_FORMBRICKS_CLOUD) return await getIsEnterpriseEdition();
else return false;
};
export const getMultiLanguagePermission = async (team: TTeam): Promise<boolean> => {
if (IS_FORMBRICKS_CLOUD) return team.billing.features.inAppSurvey.status !== "inactive";
else if (!IS_FORMBRICKS_CLOUD) return await getIsEnterpriseEdition(team.id);
else if (!IS_FORMBRICKS_CLOUD) return await getIsEnterpriseEdition();
else return false;
};

View File

@@ -4,6 +4,8 @@
import { unstable_cache } from "next/cache";
import { parse, stringify } from "superjson";
export { revalidateTag } from "next/cache";
export const cache = <T, P extends unknown[]>(
fn: (...params: P) => Promise<T>,
keys: Parameters<typeof unstable_cache>[1],