Files
formbricks/packages/lib/services/attributeClass.ts
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

152 lines
4.6 KiB
TypeScript

"use server";
import "server-only";
import { prisma } from "@formbricks/database";
import {
TAttributeClass,
TAttributeClassUpdateInput,
ZAttributeClassUpdateInput,
TAttributeClassType,
} 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 =>
`environments-${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 getAttributeClass = cache(async (attributeClassId: string): Promise<TAttributeClass | null> => {
validateInputs([attributeClassId, ZId]);
try {
const attributeClass = await prisma.attributeClass.findFirst({
where: {
id: attributeClassId,
},
});
return transformPrismaAttributeClass(attributeClass);
} catch (error) {
throw new DatabaseError(`Database error when fetching attributeClass with id ${attributeClassId}`);
}
});
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,
},
},
},
});
revalidateTag(attributeClassesCacheTag(environmentId));
return transformPrismaAttributeClass(attributeClass);
};
export const deleteAttributeClass = async (attributeClassId: string): Promise<TAttributeClass> => {
validateInputs([attributeClassId, ZId]);
try {
const deletedAttributeClass = await prisma.attributeClass.delete({
where: {
id: attributeClassId,
},
});
return deletedAttributeClass;
} catch (error) {
throw new DatabaseError(`Database error when deleting webhook with ID ${attributeClassId}`);
}
};