mirror of
https://github.com/formbricks/formbricks.git
synced 2026-03-15 11:20:58 -05:00
* feat: caching in surveys * fix: fixes unstable_cache date parsing error * fix: fixes survey revalidation in displays and responses * fix: fixes survey cache * fix: adds comments * fix: response cache tag * fix cache validation and tag naming * move TSurveysWithAnalytics to TSurveys * add caching to more services --------- Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
185 lines
5.2 KiB
TypeScript
185 lines
5.2 KiB
TypeScript
"use server";
|
|
import "server-only";
|
|
|
|
import { prisma } from "@formbricks/database";
|
|
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 { cache } from "react";
|
|
import { validateInputs } from "../utils/validate";
|
|
|
|
const halfHourInSeconds = 60 * 30;
|
|
|
|
export const getActionClassCacheTag = (name: string, environmentId: string): string =>
|
|
`environments-${environmentId}-actionClass-${name}`;
|
|
const getActionClassCacheKey = (name: string, environmentId: string): string[] => [
|
|
getActionClassCacheTag(name, environmentId),
|
|
];
|
|
|
|
const getActionClassesCacheTag = (environmentId: string): string =>
|
|
`environments-${environmentId}-actionClasses`;
|
|
const getActionClassesCacheKey = (environmentId: string): string[] => [
|
|
getActionClassesCacheTag(environmentId),
|
|
];
|
|
|
|
const select = {
|
|
id: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
name: true,
|
|
description: true,
|
|
type: true,
|
|
noCodeConfig: true,
|
|
environmentId: true,
|
|
};
|
|
|
|
export const getActionClasses = cache(async (environmentId: string): Promise<TActionClass[]> => {
|
|
validateInputs([environmentId, ZId]);
|
|
try {
|
|
let actionClasses = await prisma.eventClass.findMany({
|
|
where: {
|
|
environmentId: environmentId,
|
|
},
|
|
select,
|
|
orderBy: {
|
|
createdAt: "asc",
|
|
},
|
|
});
|
|
|
|
return actionClasses;
|
|
} catch (error) {
|
|
throw new DatabaseError(`Database error when fetching actions for environment ${environmentId}`);
|
|
}
|
|
});
|
|
|
|
export const getActionClassesCached = (environmentId: string) =>
|
|
unstable_cache(
|
|
async () => {
|
|
return await getActionClasses(environmentId);
|
|
},
|
|
getActionClassesCacheKey(environmentId),
|
|
{
|
|
tags: getActionClassesCacheKey(environmentId),
|
|
revalidate: halfHourInSeconds,
|
|
}
|
|
)();
|
|
|
|
export const getActionClass = async (actionClassId: string): Promise<TActionClass | null> => {
|
|
validateInputs([actionClassId, ZId]);
|
|
try {
|
|
let actionClass = await prisma.eventClass.findUnique({
|
|
where: {
|
|
id: actionClassId,
|
|
},
|
|
select,
|
|
});
|
|
|
|
return actionClass;
|
|
} catch (error) {
|
|
throw new DatabaseError(`Database error when fetching action`);
|
|
}
|
|
};
|
|
|
|
export const deleteActionClass = async (
|
|
environmentId: string,
|
|
actionClassId: string
|
|
): Promise<TActionClass> => {
|
|
validateInputs([environmentId, ZId], [actionClassId, ZId]);
|
|
try {
|
|
const result = await prisma.eventClass.delete({
|
|
where: {
|
|
id: actionClassId,
|
|
},
|
|
select,
|
|
});
|
|
if (result === null) throw new ResourceNotFoundError("Action", actionClassId);
|
|
|
|
// revalidate cache
|
|
revalidateTag(getActionClassesCacheTag(environmentId));
|
|
|
|
return result;
|
|
} catch (error) {
|
|
throw new DatabaseError(
|
|
`Database error when deleting an action with id ${actionClassId} for environment ${environmentId}`
|
|
);
|
|
}
|
|
};
|
|
|
|
export const createActionClass = async (
|
|
environmentId: string,
|
|
actionClass: TActionClassInput
|
|
): Promise<TActionClass> => {
|
|
validateInputs([environmentId, ZId], [actionClass, ZActionClassInput]);
|
|
try {
|
|
const result = await prisma.eventClass.create({
|
|
data: {
|
|
name: actionClass.name,
|
|
description: actionClass.description,
|
|
type: actionClass.type,
|
|
noCodeConfig: actionClass.noCodeConfig
|
|
? JSON.parse(JSON.stringify(actionClass.noCodeConfig))
|
|
: undefined,
|
|
environment: { connect: { id: environmentId } },
|
|
},
|
|
select,
|
|
});
|
|
|
|
// revalidate cache
|
|
revalidateTag(getActionClassesCacheTag(environmentId));
|
|
|
|
return result;
|
|
} catch (error) {
|
|
throw new DatabaseError(`Database error when creating an action for environment ${environmentId}`);
|
|
}
|
|
};
|
|
|
|
export const updateActionClass = async (
|
|
environmentId: string,
|
|
actionClassId: string,
|
|
inputActionClass: Partial<TActionClassInput>
|
|
): Promise<TActionClass> => {
|
|
validateInputs([environmentId, ZId], [actionClassId, ZId], [inputActionClass, ZActionClassInput.partial()]);
|
|
try {
|
|
const result = await prisma.eventClass.update({
|
|
where: {
|
|
id: actionClassId,
|
|
},
|
|
data: {
|
|
name: inputActionClass.name,
|
|
description: inputActionClass.description,
|
|
type: inputActionClass.type,
|
|
noCodeConfig: inputActionClass.noCodeConfig
|
|
? JSON.parse(JSON.stringify(inputActionClass.noCodeConfig))
|
|
: undefined,
|
|
},
|
|
select,
|
|
});
|
|
|
|
// revalidate cache
|
|
revalidateTag(getActionClassCacheTag(result.name, environmentId));
|
|
revalidateTag(getActionClassesCacheTag(environmentId));
|
|
|
|
return result;
|
|
} catch (error) {
|
|
throw new DatabaseError(`Database error when updating an action for environment ${environmentId}`);
|
|
}
|
|
};
|
|
|
|
export const getActionClassCached = async (name: string, environmentId: string) =>
|
|
unstable_cache(
|
|
async () => {
|
|
return await prisma.eventClass.findFirst({
|
|
where: {
|
|
name,
|
|
environmentId,
|
|
},
|
|
});
|
|
},
|
|
getActionClassCacheKey(name, environmentId),
|
|
{
|
|
tags: getActionClassCacheKey(name, environmentId),
|
|
revalidate: halfHourInSeconds,
|
|
}
|
|
)();
|