mirror of
https://github.com/formbricks/formbricks.git
synced 2025-12-29 18:00:26 -06:00
chore: optimize sync endpoint performance (#2342)
This commit is contained in:
@@ -2635,7 +2635,7 @@ export const minimalSurvey: TSurvey = {
|
||||
languages: [],
|
||||
};
|
||||
|
||||
export const getFirstSurvey = (webAppUrl: string) => ({
|
||||
export const getExampleSurveyTemplate = (webAppUrl: string) => ({
|
||||
...customSurvey.preset,
|
||||
questions: customSurvey.preset.questions.map(
|
||||
(question) =>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { getExampleSurveyTemplate } from "@/app/(app)/environments/[environmentId]/surveys/templates/templates";
|
||||
import { sendFreeLimitReachedEventToPosthogBiWeekly } from "@/app/api/v1/client/[environmentId]/in-app/sync/lib/posthog";
|
||||
import { responses } from "@/app/lib/api/response";
|
||||
import { transformErrorToDetails } from "@/app/lib/api/validator";
|
||||
@@ -8,12 +9,13 @@ import {
|
||||
IS_FORMBRICKS_CLOUD,
|
||||
PRICING_APPSURVEYS_FREE_RESPONSES,
|
||||
PRICING_USERTARGETING_FREE_MTU,
|
||||
WEBAPP_URL,
|
||||
} from "@formbricks/lib/constants";
|
||||
import { getEnvironment, updateEnvironment } from "@formbricks/lib/environment/service";
|
||||
import { createPerson, getIsPersonMonthlyActive, getPersonByUserId } from "@formbricks/lib/person/service";
|
||||
import { getProductByEnvironmentId } from "@formbricks/lib/product/service";
|
||||
import { COLOR_DEFAULTS } from "@formbricks/lib/styling/constants";
|
||||
import { getSyncSurveys, transformToLegacySurvey } from "@formbricks/lib/survey/service";
|
||||
import { createSurvey, getSyncSurveys, transformToLegacySurvey } from "@formbricks/lib/survey/service";
|
||||
import {
|
||||
getMonthlyActiveTeamPeopleCount,
|
||||
getMonthlyTeamResponseCount,
|
||||
@@ -70,9 +72,11 @@ export async function GET(
|
||||
throw new Error("Environment does not exist");
|
||||
}
|
||||
if (!environment?.widgetSetupCompleted) {
|
||||
const firstSurvey = getExampleSurveyTemplate(WEBAPP_URL);
|
||||
await createSurvey(environmentId, firstSurvey);
|
||||
await updateEnvironment(environment.id, { widgetSetupCompleted: true });
|
||||
}
|
||||
// check team subscriptons
|
||||
// check team subscriptions
|
||||
const team = await getTeamByEnvironmentId(environmentId);
|
||||
|
||||
if (!team) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getFirstSurvey } from "@/app/(app)/environments/[environmentId]/surveys/templates/templates";
|
||||
import { getExampleSurveyTemplate } from "@/app/(app)/environments/[environmentId]/surveys/templates/templates";
|
||||
import { sendFreeLimitReachedEventToPosthogBiWeekly } from "@/app/api/v1/client/[environmentId]/in-app/sync/lib/posthog";
|
||||
import { responses } from "@/app/lib/api/response";
|
||||
import { transformErrorToDetails } from "@/app/lib/api/validator";
|
||||
@@ -77,7 +77,7 @@ export async function GET(
|
||||
}
|
||||
|
||||
if (!environment?.widgetSetupCompleted) {
|
||||
const firstSurvey = getFirstSurvey(WEBAPP_URL);
|
||||
const firstSurvey = getExampleSurveyTemplate(WEBAPP_URL);
|
||||
await createSurvey(environmentId, firstSurvey);
|
||||
await updateEnvironment(environment.id, { widgetSetupCompleted: true });
|
||||
}
|
||||
|
||||
@@ -200,7 +200,7 @@ model Display {
|
||||
status DisplayStatus?
|
||||
|
||||
@@index([surveyId])
|
||||
@@index([personId])
|
||||
@@index([personId, createdAt])
|
||||
}
|
||||
|
||||
model SurveyTrigger {
|
||||
@@ -313,7 +313,7 @@ model Survey {
|
||||
displayPercentage Int?
|
||||
languages SurveyLanguage[]
|
||||
|
||||
@@index([environmentId])
|
||||
@@index([environmentId, updatedAt])
|
||||
@@index([segmentId])
|
||||
}
|
||||
|
||||
@@ -339,6 +339,7 @@ model ActionClass {
|
||||
actions Action[]
|
||||
|
||||
@@unique([name, environmentId])
|
||||
@@index([environmentId, createdAt])
|
||||
}
|
||||
|
||||
model Action {
|
||||
@@ -356,6 +357,7 @@ model Action {
|
||||
@@index([personId, actionClassId, createdAt])
|
||||
@@index([actionClassId, createdAt])
|
||||
@@index([personId])
|
||||
@@index([createdAt])
|
||||
}
|
||||
|
||||
enum EnvironmentType {
|
||||
|
||||
@@ -15,8 +15,7 @@ import { actionClassCache } from "../actionClass/cache";
|
||||
import { createActionClass, getActionClassByEnvironmentIdAndName } from "../actionClass/service";
|
||||
import { ITEMS_PER_PAGE, SERVICES_REVALIDATION_INTERVAL } from "../constants";
|
||||
import { activePersonCache } from "../person/cache";
|
||||
import { createPerson, getIsPersonMonthlyActive, getPersonByUserId } from "../person/service";
|
||||
import { surveyCache } from "../survey/cache";
|
||||
import { getIsPersonMonthlyActive } from "../person/service";
|
||||
import { formatDateFields } from "../utils/datetime";
|
||||
import { validateInputs } from "../utils/validate";
|
||||
import { actionCache } from "./cache";
|
||||
@@ -127,13 +126,6 @@ export const createAction = async (data: TActionInput): Promise<TAction> => {
|
||||
actionType = "automatic";
|
||||
}
|
||||
|
||||
let person = await getPersonByUserId(environmentId, userId);
|
||||
|
||||
if (!person) {
|
||||
// create person if it does not exist
|
||||
person = await createPerson(environmentId, userId);
|
||||
}
|
||||
|
||||
let actionClass = await getActionClassByEnvironmentIdAndName(environmentId, name);
|
||||
|
||||
if (!actionClass) {
|
||||
@@ -148,7 +140,10 @@ export const createAction = async (data: TActionInput): Promise<TAction> => {
|
||||
data: {
|
||||
person: {
|
||||
connect: {
|
||||
id: person.id,
|
||||
environmentId_userId: {
|
||||
environmentId,
|
||||
userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
actionClass: {
|
||||
@@ -159,18 +154,14 @@ export const createAction = async (data: TActionInput): Promise<TAction> => {
|
||||
},
|
||||
});
|
||||
|
||||
const isPersonMonthlyActive = await getIsPersonMonthlyActive(person.id);
|
||||
const isPersonMonthlyActive = await getIsPersonMonthlyActive(action.personId);
|
||||
if (!isPersonMonthlyActive) {
|
||||
activePersonCache.revalidate({ id: person.id });
|
||||
activePersonCache.revalidate({ id: action.personId });
|
||||
}
|
||||
|
||||
actionCache.revalidate({
|
||||
environmentId,
|
||||
personId: person.id,
|
||||
});
|
||||
|
||||
surveyCache.revalidate({
|
||||
environmentId,
|
||||
personId: action.personId,
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
@@ -23,13 +23,13 @@ export const personCache = {
|
||||
revalidateTag(this.tag.byId(id));
|
||||
}
|
||||
|
||||
if (environmentId && userId) {
|
||||
revalidateTag(this.tag.byEnvironmentIdAndUserId(environmentId, userId));
|
||||
}
|
||||
|
||||
if (environmentId) {
|
||||
revalidateTag(this.tag.byEnvironmentId(environmentId));
|
||||
}
|
||||
|
||||
if (environmentId && userId) {
|
||||
revalidateTag(this.tag.byEnvironmentIdAndUserId(environmentId, userId));
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -754,12 +754,13 @@ export const getSyncSurveys = async (
|
||||
const surveys = await unstable_cache(
|
||||
async () => {
|
||||
const product = await getProductByEnvironmentId(environmentId);
|
||||
const person = personId === "legacy" ? ({ id: "legacy" } as TPerson) : await getPerson(personId);
|
||||
|
||||
if (!product) {
|
||||
throw new Error("Product not found");
|
||||
}
|
||||
|
||||
const person = personId === "legacy" ? ({ id: "legacy" } as TPerson) : await getPerson(personId);
|
||||
|
||||
if (!person) {
|
||||
throw new Error("Person not found");
|
||||
}
|
||||
@@ -769,6 +770,11 @@ export const getSyncSurveys = async (
|
||||
// filtered surveys for running and web
|
||||
surveys = surveys.filter((survey) => survey.status === "inProgress" && survey.type === "web");
|
||||
|
||||
// if no surveys are left, return an empty array
|
||||
if (surveys.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const displays = await getDisplaysByPersonId(person.id);
|
||||
|
||||
// filter surveys that meet the displayOption criteria
|
||||
@@ -806,6 +812,11 @@ export const getSyncSurveys = async (
|
||||
}
|
||||
});
|
||||
|
||||
// if no surveys are left, return an empty array
|
||||
if (surveys.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// if no surveys have segment filters, return the surveys
|
||||
if (!anySurveyHasFilters(surveys)) {
|
||||
return surveys;
|
||||
|
||||
@@ -339,7 +339,6 @@ export const getMonthlyActiveTeamPeopleCount = async (teamId: string): Promise<n
|
||||
},
|
||||
[`getMonthlyActiveTeamPeopleCount-${teamId}`],
|
||||
{
|
||||
tags: [],
|
||||
revalidate: SERVICES_REVALIDATION_INTERVAL,
|
||||
}
|
||||
)();
|
||||
@@ -376,7 +375,6 @@ export const getMonthlyTeamResponseCount = async (teamId: string): Promise<numbe
|
||||
},
|
||||
[`getMonthlyTeamResponseCount-${teamId}`],
|
||||
{
|
||||
tags: [],
|
||||
revalidate: SERVICES_REVALIDATION_INTERVAL,
|
||||
}
|
||||
)();
|
||||
|
||||
Reference in New Issue
Block a user