mirror of
https://github.com/formbricks/formbricks.git
synced 2026-01-26 10:42:16 -06:00
* added input validation using zod * changed console.log to console.error * fix formatting issues --------- Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
"use server";
|
|
import "server-only";
|
|
import { prisma } from "@formbricks/database";
|
|
import {
|
|
TAttributeClass,
|
|
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";
|
|
|
|
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);
|
|
|
|
return transformedAttributeClass;
|
|
} catch (error) {
|
|
throw new DatabaseError(`Database error when updating attribute class with id ${attributeClassId}`);
|
|
}
|
|
};
|
|
|
|
export const getAttributeClassByName = cache(
|
|
async (environmentId: string, name: string): Promise<TAttributeClass | null> => {
|
|
const attributeClass = await prisma.attributeClass.findFirst({
|
|
where: {
|
|
environmentId,
|
|
name,
|
|
},
|
|
});
|
|
return transformPrismaAttributeClass(attributeClass);
|
|
}
|
|
);
|