Skip to content

Commit

Permalink
fix: too many enterprise license checks on the ee server (formbricks#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gupta-piyush19 authored Oct 7, 2024
1 parent 4c594fa commit 7207a4d
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions packages/ee/lib/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "server-only";
import { HttpsProxyAgent } from "https-proxy-agent";
import fetch from "node-fetch";
import { cache as reactCache } from "react";
import { prisma } from "@formbricks/database";
import { cache, revalidateTag } from "@formbricks/lib/cache";
import {
Expand Down Expand Up @@ -184,53 +185,55 @@ export const getLicenseFeatures = async (): Promise<TEnterpriseLicenseFeatures |
}
};

export const fetchLicense = async (): Promise<TEnterpriseLicenseDetails | null> =>
await cache(
async () => {
if (!env.ENTERPRISE_LICENSE_KEY) return null;
try {
const now = new Date();
const startOfYear = new Date(now.getFullYear(), 0, 1); // January 1st of the current year
const endOfYear = new Date(now.getFullYear() + 1, 0, 0); // December 31st of the current year

const responseCount = await prisma.response.count({
where: {
createdAt: {
gte: startOfYear,
lt: endOfYear,
export const fetchLicense = reactCache(
(): Promise<TEnterpriseLicenseDetails | null> =>
cache(
async () => {
if (!env.ENTERPRISE_LICENSE_KEY) return null;
try {
const now = new Date();
const startOfYear = new Date(now.getFullYear(), 0, 1); // January 1st of the current year
const endOfYear = new Date(now.getFullYear() + 1, 0, 0); // December 31st of the current year

const responseCount = await prisma.response.count({
where: {
createdAt: {
gte: startOfYear,
lt: endOfYear,
},
},
},
});

const proxyUrl = env.HTTPS_PROXY || env.HTTP_PROXY;
const agent = proxyUrl ? new HttpsProxyAgent(proxyUrl) : undefined;

const res = await fetch("https://ee.formbricks.com/api/licenses/check", {
body: JSON.stringify({
licenseKey: ENTERPRISE_LICENSE_KEY,
usage: { responseCount: responseCount },
}),
headers: { "Content-Type": "application/json" },
method: "POST",
agent,
});

if (res.ok) {
const responseJson = (await res.json()) as {
data: TEnterpriseLicenseDetails;
};
return responseJson.data;
});

const proxyUrl = env.HTTPS_PROXY || env.HTTP_PROXY;
const agent = proxyUrl ? new HttpsProxyAgent(proxyUrl) : undefined;

const res = await fetch("https://ee.formbricks.com/api/licenses/check", {
body: JSON.stringify({
licenseKey: ENTERPRISE_LICENSE_KEY,
usage: { responseCount: responseCount },
}),
headers: { "Content-Type": "application/json" },
method: "POST",
agent,
});

if (res.ok) {
const responseJson = (await res.json()) as {
data: TEnterpriseLicenseDetails;
};
return responseJson.data;
}

return null;
} catch (error) {
console.error("Error while checking license: ", error);
return null;
}

return null;
} catch (error) {
console.error("Error while checking license: ", error);
return null;
}
},
[`fetchLicense-${hashedKey}`],
{ revalidate: 60 * 60 * 24 }
)();
},
[`fetchLicense-${hashedKey}`],
{ revalidate: 60 * 60 * 24 }
)()
);

export const getRemoveInAppBrandingPermission = (organization: TOrganization): boolean => {
if (IS_FORMBRICKS_CLOUD) return organization.billing.plan !== PRODUCT_FEATURE_KEYS.FREE;
Expand Down

0 comments on commit 7207a4d

Please sign in to comment.