Files
formbricks/packages/lib/services/actions.ts
T
Shubham Palriwala 13afba7615 feat: move env vars from build-time to run-time to better support self-hosting environments (#789)
* feat: privacy, imprint, and terms URL env vars now do not need rebuilding

* feat: disable_singup env var now do not need rebuilding

* feat: password_reset_disabled env var now do not need rebuilding

* feat: email_verification_disabled env var now do not need rebuilding

* feat: github_oauth & google_oauth env var now do not need rebuilding

* feat: move logic of env vars to serverside and send boolean client-side

* feat: invite_disabled env var now do not need rebuilding

* feat: rename vars logically

* feat: migration guide

* feat: update docker-compose as per v1.1

* deprecate: unused NEXT_PUBLIC_VERCEL_URL & VERCEL_URL

* deprecate: unused RAILWAY_STATIC_URL

* deprecate: unused RENDER_EXTERNAL_URL

* deprecate: unused HEROKU_APP_NAME

* fix: define WEBAPP_URL & replace NEXT_WEBAPP_URL with it

* migrate: NEXT_PUBLIC_IS_FORMBRICKS_CLOUD to IS_FORMBRICKS_CLOUD

* chore: move all env parsing to a constants.ts from page files

* feat: migrate client side envs to server side

* redo: isFormbricksCloud to navbar serverside page

* fix: constants is now a server only file

* fix: removal of use swr underway

* fix: move 1 tag away from swr to service

* feat: move away from tags swr

* feat: move away from surveys  swr

* feat: move away from eventClass swr

* feat: move away from event swr

* fix: make constants server-only

* remove comments from .env.example, use constants in MetaInformation

* clean up services

* rename tag function

* fix build error

* fix smaller bugs, fix Response % not working in summary

---------

Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2023-09-30 21:40:59 +02:00

181 lines
4.5 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));
};
export const getActionCountInLastHour = cache(async (actionClassId: string) => {
try {
const numEventsLastHour = await prisma.event.count({
where: {
eventClassId: actionClassId,
createdAt: {
gte: new Date(Date.now() - 60 * 60 * 1000),
},
},
});
return numEventsLastHour;
} catch (error) {
throw error;
}
});
export const getActionCountInLast24Hours = cache(async (actionClassId: string) => {
try {
const numEventsLast24Hours = await prisma.event.count({
where: {
eventClassId: actionClassId,
createdAt: {
gte: new Date(Date.now() - 24 * 60 * 60 * 1000),
},
},
});
return numEventsLast24Hours;
} catch (error) {
throw error;
}
});
export const getActionCountInLast7Days = cache(async (actionClassId: string) => {
try {
const numEventsLast7Days = await prisma.event.count({
where: {
eventClassId: actionClassId,
createdAt: {
gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
},
},
});
return numEventsLast7Days;
} catch (error) {
throw error;
}
});