mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-21 03:03:25 -05:00
53ef8771f3
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com> Co-authored-by: Johannes <johannes@formbricks.com>
204 lines
5.6 KiB
TypeScript
204 lines
5.6 KiB
TypeScript
"use server";
|
|
import "server-only";
|
|
|
|
import { prisma } from "@formbricks/database";
|
|
import { SERVICES_REVALIDATION_INTERVAL, ITEMS_PER_PAGE } from "../constants";
|
|
import { TActionClass, TActionClassInput, ZActionClassInput } from "@formbricks/types/actionClasses";
|
|
import { ZId } from "@formbricks/types/environment";
|
|
import { ZOptionalNumber, ZString } from "@formbricks/types/common";
|
|
import { DatabaseError, ResourceNotFoundError } from "@formbricks/types/errors";
|
|
import { unstable_cache } from "next/cache";
|
|
import { validateInputs } from "../utils/validate";
|
|
import { actionClassCache } from "./cache";
|
|
|
|
const select = {
|
|
id: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
name: true,
|
|
description: true,
|
|
type: true,
|
|
noCodeConfig: true,
|
|
environmentId: true,
|
|
};
|
|
|
|
export const getActionClasses = (environmentId: string, page?: number): Promise<TActionClass[]> =>
|
|
unstable_cache(
|
|
async () => {
|
|
validateInputs([environmentId, ZId], [page, ZOptionalNumber]);
|
|
|
|
try {
|
|
const actionClasses = await prisma.actionClass.findMany({
|
|
where: {
|
|
environmentId: environmentId,
|
|
},
|
|
select,
|
|
take: page ? ITEMS_PER_PAGE : undefined,
|
|
skip: page ? ITEMS_PER_PAGE * (page - 1) : undefined,
|
|
orderBy: {
|
|
createdAt: "asc",
|
|
},
|
|
});
|
|
|
|
return actionClasses;
|
|
} catch (error) {
|
|
throw new DatabaseError(`Database error when fetching actions for environment ${environmentId}`);
|
|
}
|
|
},
|
|
[`getActionClasses-${environmentId}-${page}`],
|
|
{
|
|
tags: [actionClassCache.tag.byEnvironmentId(environmentId)],
|
|
revalidate: SERVICES_REVALIDATION_INTERVAL,
|
|
}
|
|
)();
|
|
|
|
export const getActionClassByEnvironmentIdAndName = async (
|
|
environmentId: string,
|
|
name: string
|
|
): Promise<TActionClass | null> =>
|
|
unstable_cache(
|
|
async () => {
|
|
validateInputs([environmentId, ZId], [name, ZString]);
|
|
|
|
try {
|
|
const actionClass = await prisma.actionClass.findFirst({
|
|
where: {
|
|
name,
|
|
environmentId,
|
|
},
|
|
select,
|
|
});
|
|
|
|
return actionClass;
|
|
} catch (error) {
|
|
throw new DatabaseError(`Database error when fetching action`);
|
|
}
|
|
},
|
|
[`getActionClass-${environmentId}-${name}`],
|
|
{
|
|
tags: [actionClassCache.tag.byNameAndEnvironmentId(environmentId, name)],
|
|
revalidate: SERVICES_REVALIDATION_INTERVAL,
|
|
}
|
|
)();
|
|
|
|
export const getActionClass = async (actionClassId: string): Promise<TActionClass | null> =>
|
|
unstable_cache(
|
|
async () => {
|
|
validateInputs([actionClassId, ZId]);
|
|
|
|
try {
|
|
const actionClass = await prisma.actionClass.findUnique({
|
|
where: {
|
|
id: actionClassId,
|
|
},
|
|
select,
|
|
});
|
|
|
|
return actionClass;
|
|
} catch (error) {
|
|
throw new DatabaseError(`Database error when fetching action`);
|
|
}
|
|
},
|
|
[`getActionClass-${actionClassId}`],
|
|
{
|
|
tags: [actionClassCache.tag.byId(actionClassId)],
|
|
revalidate: SERVICES_REVALIDATION_INTERVAL,
|
|
}
|
|
)();
|
|
|
|
export const deleteActionClass = async (
|
|
environmentId: string,
|
|
actionClassId: string
|
|
): Promise<TActionClass> => {
|
|
validateInputs([environmentId, ZId], [actionClassId, ZId]);
|
|
|
|
try {
|
|
const result = await prisma.actionClass.delete({
|
|
where: {
|
|
id: actionClassId,
|
|
},
|
|
select,
|
|
});
|
|
if (result === null) throw new ResourceNotFoundError("Action", actionClassId);
|
|
|
|
actionClassCache.revalidate({
|
|
environmentId,
|
|
id: actionClassId,
|
|
});
|
|
|
|
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 actionClassPrisma = await prisma.actionClass.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,
|
|
});
|
|
|
|
actionClassCache.revalidate({
|
|
name: actionClassPrisma.name,
|
|
environmentId: actionClassPrisma.environmentId,
|
|
id: actionClassPrisma.id,
|
|
});
|
|
|
|
return actionClassPrisma;
|
|
} 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.actionClass.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
|
|
actionClassCache.revalidate({
|
|
environmentId: result.environmentId,
|
|
name: result.name,
|
|
id: result.id,
|
|
});
|
|
|
|
return result;
|
|
} catch (error) {
|
|
throw new DatabaseError(`Database error when updating an action for environment ${environmentId}`);
|
|
}
|
|
};
|