mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-01 19:32:57 -05:00
d8b14f5f35
* fix: caching in actions endpoint * refactor --------- Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
133 lines
3.4 KiB
TypeScript
133 lines
3.4 KiB
TypeScript
import "server-only";
|
|
|
|
import z from "zod";
|
|
import { prisma } from "@formbricks/database";
|
|
import { DatabaseError } from "@formbricks/types/v1/errors";
|
|
import { TAction } from "@formbricks/types/v1/actions";
|
|
import { ZId } from "@formbricks/types/v1/environment";
|
|
import { Prisma } from "@prisma/client";
|
|
import { cache } from "react";
|
|
import { validateInputs } from "../utils/validate";
|
|
import { TJsActionInput } from "@formbricks/types/v1/js";
|
|
import { revalidateTag } from "next/cache";
|
|
import { EventType } from "@prisma/client";
|
|
import { getActionClassCacheTag, getActionClassCached } from "../services/actionClass";
|
|
import { getSessionCached } from "../services/session";
|
|
|
|
export const getActionsByEnvironmentId = cache(
|
|
async (environmentId: string, limit?: number): Promise<TAction[]> => {
|
|
validateInputs([environmentId, ZId], [limit, z.number().optional()]);
|
|
try {
|
|
const actionsPrisma = await prisma.event.findMany({
|
|
where: {
|
|
eventClass: {
|
|
environmentId: environmentId,
|
|
},
|
|
},
|
|
orderBy: {
|
|
createdAt: "desc",
|
|
},
|
|
take: limit ? limit : 20,
|
|
include: {
|
|
eventClass: true,
|
|
},
|
|
});
|
|
const actions: TAction[] = [];
|
|
// transforming response to type TAction[]
|
|
actionsPrisma.forEach((action) => {
|
|
actions.push({
|
|
id: action.id,
|
|
createdAt: action.createdAt,
|
|
sessionId: action.sessionId,
|
|
properties: action.properties,
|
|
actionClass: action.eventClass,
|
|
});
|
|
});
|
|
return actions;
|
|
} catch (error) {
|
|
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
|
throw new DatabaseError("Database operation failed");
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
}
|
|
);
|
|
|
|
export const createAction = async (data: TJsActionInput) => {
|
|
const { environmentId, name, properties, sessionId } = data;
|
|
|
|
let eventType: EventType = EventType.code;
|
|
if (name === "Exit Intent (Desktop)" || name === "50% Scroll") {
|
|
eventType = EventType.automatic;
|
|
}
|
|
|
|
const session = await getSessionCached(sessionId);
|
|
|
|
if (!session) {
|
|
throw new Error("Session not found");
|
|
}
|
|
|
|
const actionClass = await getActionClassCached(name, environmentId);
|
|
|
|
if (actionClass) {
|
|
await prisma.event.create({
|
|
data: {
|
|
properties,
|
|
sessionId: session.id,
|
|
eventClassId: actionClass.id,
|
|
},
|
|
});
|
|
|
|
return;
|
|
}
|
|
|
|
// if action class does not exist, create it and then create the action
|
|
await prisma.$transaction([
|
|
prisma.eventClass.create({
|
|
data: {
|
|
name,
|
|
type: eventType,
|
|
environmentId,
|
|
},
|
|
}),
|
|
|
|
prisma.event.create({
|
|
data: {
|
|
properties,
|
|
session: {
|
|
connect: {
|
|
id: sessionId,
|
|
},
|
|
},
|
|
eventClass: {
|
|
connectOrCreate: {
|
|
where: {
|
|
name_environmentId: {
|
|
name,
|
|
environmentId,
|
|
},
|
|
},
|
|
create: {
|
|
name,
|
|
type: eventType,
|
|
environment: {
|
|
connect: {
|
|
id: environmentId,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
select: {
|
|
id: true,
|
|
},
|
|
}),
|
|
]);
|
|
|
|
// revalidate cache
|
|
revalidateTag(sessionId);
|
|
revalidateTag(getActionClassCacheTag(name, environmentId));
|
|
};
|