mirror of
https://github.com/formbricks/formbricks.git
synced 2026-01-05 21:32:02 -06:00
* Move cached functions to services, add new caching to set-attribute * update set-user-id route * move remaining cache functions to services, simplify revalidation and keys * simplify set-attribute further * fix build errors * comment out prisma client logging
121 lines
3.6 KiB
TypeScript
121 lines
3.6 KiB
TypeScript
"use server";
|
|
import "server-only";
|
|
import { prisma } from "@formbricks/database";
|
|
import {
|
|
TAttributeClass,
|
|
TAttributeClassType,
|
|
TAttributeClassUpdateInput,
|
|
ZAttributeClassUpdateInput,
|
|
} from "@formbricks/types/v1/attributeClasses";
|
|
import { ZId } from "@formbricks/types/v1/environment";
|
|
import { validateInputs } from "../utils/validate";
|
|
import { DatabaseError } from "@formbricks/types/v1/errors";
|
|
import { cache } from "react";
|
|
import { revalidateTag, unstable_cache } from "next/cache";
|
|
|
|
const attributeClassesCacheTag = (environmentId: string): string => `env-${environmentId}-attributeClasses`;
|
|
|
|
const getAttributeClassesCacheKey = (environmentId: string): string[] => [
|
|
attributeClassesCacheTag(environmentId),
|
|
];
|
|
|
|
export const transformPrismaAttributeClass = (attributeClass: any): TAttributeClass | null => {
|
|
if (attributeClass === null) {
|
|
return null;
|
|
}
|
|
|
|
const transformedAttributeClass: TAttributeClass = {
|
|
...attributeClass,
|
|
};
|
|
|
|
return transformedAttributeClass;
|
|
};
|
|
|
|
export const getAttributeClasses = cache(async (environmentId: string): Promise<TAttributeClass[]> => {
|
|
validateInputs([environmentId, ZId]);
|
|
try {
|
|
let attributeClasses = await prisma.attributeClass.findMany({
|
|
where: {
|
|
environmentId: environmentId,
|
|
},
|
|
orderBy: {
|
|
createdAt: "asc",
|
|
},
|
|
});
|
|
const transformedAttributeClasses: TAttributeClass[] = attributeClasses
|
|
.map(transformPrismaAttributeClass)
|
|
.filter((attributeClass): attributeClass is TAttributeClass => attributeClass !== null);
|
|
|
|
return transformedAttributeClasses;
|
|
} catch (error) {
|
|
throw new DatabaseError(`Database error when fetching attributeClasses for environment ${environmentId}`);
|
|
}
|
|
});
|
|
|
|
export const updatetAttributeClass = async (
|
|
attributeClassId: string,
|
|
data: Partial<TAttributeClassUpdateInput>
|
|
): Promise<TAttributeClass | null> => {
|
|
validateInputs([attributeClassId, ZId], [data, ZAttributeClassUpdateInput.partial()]);
|
|
try {
|
|
let attributeClass = await prisma.attributeClass.update({
|
|
where: {
|
|
id: attributeClassId,
|
|
},
|
|
data: {
|
|
description: data.description,
|
|
archived: data.archived,
|
|
},
|
|
});
|
|
const transformedAttributeClass: TAttributeClass | null = transformPrismaAttributeClass(attributeClass);
|
|
|
|
revalidateTag(attributeClassesCacheTag(attributeClass.environmentId));
|
|
return transformedAttributeClass;
|
|
} catch (error) {
|
|
throw new DatabaseError(`Database error when updating attribute class with id ${attributeClassId}`);
|
|
}
|
|
};
|
|
|
|
export const getAttributeClassByNameCached = async (environmentId: string, name: string) =>
|
|
await unstable_cache(
|
|
async () => {
|
|
return await getAttributeClassByName(environmentId, name);
|
|
},
|
|
getAttributeClassesCacheKey(environmentId),
|
|
{
|
|
tags: getAttributeClassesCacheKey(environmentId),
|
|
revalidate: 30 * 60, // 30 minutes
|
|
}
|
|
)();
|
|
|
|
export const getAttributeClassByName = cache(
|
|
async (environmentId: string, name: string): Promise<TAttributeClass | null> => {
|
|
const attributeClass = await prisma.attributeClass.findFirst({
|
|
where: {
|
|
environmentId,
|
|
name,
|
|
},
|
|
});
|
|
return transformPrismaAttributeClass(attributeClass);
|
|
}
|
|
);
|
|
|
|
export const createAttributeClass = async (
|
|
environmentId: string,
|
|
name: string,
|
|
type: TAttributeClassType
|
|
): Promise<TAttributeClass | null> => {
|
|
const attributeClass = await prisma.attributeClass.create({
|
|
data: {
|
|
name,
|
|
type,
|
|
environment: {
|
|
connect: {
|
|
id: environmentId,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
return transformPrismaAttributeClass(attributeClass);
|
|
};
|