From ed6fc1b40c98d94f1f96507bbe958fa84862dfd0 Mon Sep 17 00:00:00 2001 From: Rotimi Best Date: Tue, 3 Oct 2023 13:01:26 +0100 Subject: [PATCH] chore: move revalidation interval to a constant (#921) --- packages/lib/actionClass/auth.ts | 5 +++-- packages/lib/actionClass/service.ts | 7 +++---- packages/lib/apiKey/auth.ts | 5 +++-- packages/lib/constants.ts | 1 + packages/lib/environment/auth.ts | 5 +++-- packages/lib/response/auth.ts | 5 +++-- packages/lib/services/attributeClass.ts | 3 ++- packages/lib/services/environment.ts | 5 +++-- packages/lib/services/person.ts | 3 ++- packages/lib/services/product.ts | 5 +++-- packages/lib/services/profile.ts | 5 +++-- packages/lib/services/team.ts | 5 +++-- packages/lib/tag/auth.ts | 3 ++- packages/lib/tagOnResponse/auth.ts | 3 ++- 14 files changed, 36 insertions(+), 24 deletions(-) diff --git a/packages/lib/actionClass/auth.ts b/packages/lib/actionClass/auth.ts index df7d40ae872..381d981b460 100644 --- a/packages/lib/actionClass/auth.ts +++ b/packages/lib/actionClass/auth.ts @@ -5,6 +5,7 @@ import { validateInputs } from "../utils/validate"; import { hasUserEnvironmentAccess } from "../environment/auth"; import { getActionClass } from "./service"; import { unstable_cache } from "next/cache"; +import { SERVICES_REVALIDATION_INTERVAL } from "../constants"; export const canUserAccessActionClass = async (userId: string, actionClassId: string): Promise => await unstable_cache( @@ -22,5 +23,5 @@ export const canUserAccessActionClass = async (userId: string, actionClassId: st }, [`users-${userId}-actionClasses-${actionClassId}`], - { revalidate: 30 * 60, tags: [`actionClasses-${actionClassId}`] } - )(); // 30 minutes + { revalidate: SERVICES_REVALIDATION_INTERVAL, tags: [`actionClasses-${actionClassId}`] } + )(); diff --git a/packages/lib/actionClass/service.ts b/packages/lib/actionClass/service.ts index f69cb1bb056..2631636ce06 100644 --- a/packages/lib/actionClass/service.ts +++ b/packages/lib/actionClass/service.ts @@ -2,14 +2,13 @@ import "server-only"; import { prisma } from "@formbricks/database"; +import { SERVICES_REVALIDATION_INTERVAL } from "../constants"; import { TActionClass, TActionClassInput, ZActionClassInput } from "@formbricks/types/v1/actionClasses"; import { ZId } from "@formbricks/types/v1/environment"; import { DatabaseError, ResourceNotFoundError } from "@formbricks/types/v1/errors"; import { revalidateTag, unstable_cache } from "next/cache"; import { validateInputs } from "../utils/validate"; -const halfHourInSeconds = 60 * 30; - export const getActionClassCacheTag = (name: string, environmentId: string): string => `environments-${environmentId}-actionClass-${name}`; @@ -50,7 +49,7 @@ export const getActionClasses = (environmentId: string): Promise [`environments-${environmentId}-actionClasses`], { tags: [getActionClassesCacheTag(environmentId)], - revalidate: halfHourInSeconds, + revalidate: SERVICES_REVALIDATION_INTERVAL, } )(); @@ -168,6 +167,6 @@ export const getActionClassCached = async (name: string, environmentId: string) [`environments-${environmentId}-actionClasses-${name}`], { tags: [getActionClassesCacheTag(environmentId)], - revalidate: halfHourInSeconds, + revalidate: SERVICES_REVALIDATION_INTERVAL, } )(); diff --git a/packages/lib/apiKey/auth.ts b/packages/lib/apiKey/auth.ts index 5e3140e93e8..1ba3027163c 100644 --- a/packages/lib/apiKey/auth.ts +++ b/packages/lib/apiKey/auth.ts @@ -5,6 +5,7 @@ import { validateInputs } from "../utils/validate"; import { hasUserEnvironmentAccess } from "../environment/auth"; import { getApiKey } from "./service"; import { unstable_cache } from "next/cache"; +import { SERVICES_REVALIDATION_INTERVAL } from "../constants"; export const canUserAccessApiKey = async (userId: string, apiKeyId: string): Promise => await unstable_cache( @@ -21,5 +22,5 @@ export const canUserAccessApiKey = async (userId: string, apiKeyId: string): Pro }, [`users-${userId}-apiKeys-${apiKeyId}`], - { revalidate: 30 * 60, tags: [`apiKeys-${apiKeyId}`] } - )(); // 30 minutes + { revalidate: SERVICES_REVALIDATION_INTERVAL, tags: [`apiKeys-${apiKeyId}`] } + )(); diff --git a/packages/lib/constants.ts b/packages/lib/constants.ts index b7a8ae1a36c..b2c8a7c0a6c 100644 --- a/packages/lib/constants.ts +++ b/packages/lib/constants.ts @@ -4,6 +4,7 @@ import { env } from "@/env.mjs"; export const RESPONSES_LIMIT_FREE = 100; export const IS_FORMBRICKS_CLOUD = env.IS_FORMBRICKS_CLOUD === "1"; export const REVALIDATION_INTERVAL = 0; //TODO: find a good way to cache and revalidate data when it changes +export const SERVICES_REVALIDATION_INTERVAL = 60 * 30; // 30 minutes export const MAU_LIMIT = IS_FORMBRICKS_CLOUD ? 5000 : 1000000; // URLs diff --git a/packages/lib/environment/auth.ts b/packages/lib/environment/auth.ts index 4a693e6ed09..b9186ef8125 100644 --- a/packages/lib/environment/auth.ts +++ b/packages/lib/environment/auth.ts @@ -2,6 +2,7 @@ import { prisma } from "@formbricks/database"; import { ZId } from "@formbricks/types/v1/environment"; import { unstable_cache } from "next/cache"; import { validateInputs } from "../utils/validate"; +import { SERVICES_REVALIDATION_INTERVAL } from "../constants"; export const hasUserEnvironmentAccess = async (userId: string, environmentId: string) => { return await unstable_cache( @@ -31,6 +32,6 @@ export const hasUserEnvironmentAccess = async (userId: string, environmentId: st return environmentUsers.includes(userId); }, [`users-${userId}-environments-${environmentId}`], - { revalidate: 30 * 60, tags: [`environments-${environmentId}`] } - )(); // 30 minutes + { revalidate: SERVICES_REVALIDATION_INTERVAL, tags: [`environments-${environmentId}`] } + )(); }; diff --git a/packages/lib/response/auth.ts b/packages/lib/response/auth.ts index 15268c9598d..f4adfe0858b 100644 --- a/packages/lib/response/auth.ts +++ b/packages/lib/response/auth.ts @@ -6,6 +6,7 @@ import { hasUserEnvironmentAccess } from "../environment/auth"; import { getResponse, getResponseCacheTag } from "./service"; import { unstable_cache } from "next/cache"; import { getSurvey } from "../services/survey"; +import { SERVICES_REVALIDATION_INTERVAL } from "../constants"; export const canUserAccessResponse = async (userId: string, responseId: string): Promise => await unstable_cache( @@ -26,5 +27,5 @@ export const canUserAccessResponse = async (userId: string, responseId: string): return true; }, [`users-${userId}-responses-${responseId}`], - { revalidate: 30 * 60, tags: [getResponseCacheTag(responseId)] } - )(); // 30 minutes + { revalidate: SERVICES_REVALIDATION_INTERVAL, tags: [getResponseCacheTag(responseId)] } + )(); diff --git a/packages/lib/services/attributeClass.ts b/packages/lib/services/attributeClass.ts index 621c50a5b35..2a75547f9f1 100644 --- a/packages/lib/services/attributeClass.ts +++ b/packages/lib/services/attributeClass.ts @@ -13,6 +13,7 @@ import { validateInputs } from "../utils/validate"; import { DatabaseError } from "@formbricks/types/v1/errors"; import { cache } from "react"; import { revalidateTag, unstable_cache } from "next/cache"; +import { SERVICES_REVALIDATION_INTERVAL } from "../constants"; const attributeClassesCacheTag = (environmentId: string): string => `environments-${environmentId}-attributeClasses`; @@ -100,7 +101,7 @@ export const getAttributeClassByNameCached = async (environmentId: string, name: getAttributeClassesCacheKey(environmentId), { tags: getAttributeClassesCacheKey(environmentId), - revalidate: 30 * 60, // 30 minutes + revalidate: SERVICES_REVALIDATION_INTERVAL, } )(); diff --git a/packages/lib/services/environment.ts b/packages/lib/services/environment.ts index 6c23f9d3f9e..87714c5f19f 100644 --- a/packages/lib/services/environment.ts +++ b/packages/lib/services/environment.ts @@ -9,6 +9,7 @@ import { populateEnvironment } from "../utils/createDemoProductHelpers"; import { ZEnvironment, ZEnvironmentUpdateInput, ZId } from "@formbricks/types/v1/environment"; import { validateInputs } from "../utils/validate"; import { unstable_cache, revalidateTag } from "next/cache"; +import { SERVICES_REVALIDATION_INTERVAL } from "../constants"; export const getEnvironmentCacheTag = (environmentId: string) => `environments-${environmentId}`; export const getEnvironmentsCacheTag = (productId: string) => `products-${productId}-environments`; @@ -46,7 +47,7 @@ export const getEnvironment = (environmentId: string) => [`environments-${environmentId}`], { tags: [getEnvironmentCacheTag(environmentId)], - revalidate: 30 * 60, // 30 minutes + revalidate: SERVICES_REVALIDATION_INTERVAL, } )(); @@ -93,7 +94,7 @@ export const getEnvironments = async (productId: string): Promise getPersonCacheKey(personId), { tags: getPersonCacheKey(personId), - revalidate: 30 * 60, // 30 minutes + revalidate: SERVICES_REVALIDATION_INTERVAL, } )(); diff --git a/packages/lib/services/product.ts b/packages/lib/services/product.ts index fff3ef30543..8072f3a88e4 100644 --- a/packages/lib/services/product.ts +++ b/packages/lib/services/product.ts @@ -13,6 +13,7 @@ import { validateInputs } from "../utils/validate"; import { EnvironmentType } from "@prisma/client"; import { EventType } from "@prisma/client"; import { getEnvironmentCacheTag, getEnvironmentsCacheTag } from "./environment"; +import { SERVICES_REVALIDATION_INTERVAL } from "../constants"; export const getProductsCacheTag = (teamId: string): string => `teams-${teamId}-products`; const getProductCacheTag = (environmentId: string): string => `environments-${environmentId}-product`; @@ -86,7 +87,7 @@ export const getProducts = async (teamId: string): Promise => [`teams-${teamId}-products`], { tags: [getProductsCacheTag(teamId)], - revalidate: 30 * 60, // 30 minutes + revalidate: SERVICES_REVALIDATION_INTERVAL, } )(); @@ -125,7 +126,7 @@ export const getProductByEnvironmentIdCached = (environmentId: string) => getProductCacheKey(environmentId), { tags: getProductCacheKey(environmentId), - revalidate: 30 * 60, // 30 minutes + revalidate: SERVICES_REVALIDATION_INTERVAL, } )(); diff --git a/packages/lib/services/profile.ts b/packages/lib/services/profile.ts index c32fa92f3e3..b176dff86f5 100644 --- a/packages/lib/services/profile.ts +++ b/packages/lib/services/profile.ts @@ -10,6 +10,7 @@ import { unstable_cache, revalidateTag } from "next/cache"; import { validateInputs } from "../utils/validate"; import { deleteTeam } from "./team"; import { z } from "zod"; +import { SERVICES_REVALIDATION_INTERVAL } from "../constants"; const responseSelection = { id: true, @@ -52,7 +53,7 @@ export const getProfile = async (userId: string): Promise => [`profiles-${userId}`], { tags: [getProfileByEmailCacheTag(userId)], - revalidate: 30 * 60, // 30 minutes + revalidate: SERVICES_REVALIDATION_INTERVAL, } )(); @@ -84,7 +85,7 @@ export const getProfileByEmail = async (email: string): Promise [`profiles-${email}`], { tags: [getProfileCacheTag(email)], - revalidate: 30 * 60, // 30 minutes + revalidate: SERVICES_REVALIDATION_INTERVAL, } )(); diff --git a/packages/lib/services/team.ts b/packages/lib/services/team.ts index 2c733684163..a9417cd90a1 100644 --- a/packages/lib/services/team.ts +++ b/packages/lib/services/team.ts @@ -26,6 +26,7 @@ import { updateEnvironmentArgs, } from "../utils/createDemoProductHelpers"; import { validateInputs } from "../utils/validate"; +import { SERVICES_REVALIDATION_INTERVAL } from "../constants"; export const select = { id: true, @@ -66,7 +67,7 @@ export const getTeamsByUserId = async (userId: string): Promise => [`users-${userId}-teams`], { tags: [getTeamsByUserIdCacheTag(userId)], - revalidate: 30 * 60, // 30 minutes + revalidate: SERVICES_REVALIDATION_INTERVAL, } )(); @@ -102,7 +103,7 @@ export const getTeamByEnvironmentId = async (environmentId: string): Promise => await unstable_cache( @@ -21,6 +22,6 @@ export const canUserAccessTag = async (userId: string, tagId: string): Promise