mirror of
https://github.com/formbricks/formbricks.git
synced 2025-12-29 09:50:10 -06:00
Make prisma call in survey service specific to avoid model conflicts (#423)
* make prisma call in survey service specific to avoid model conflicts * make zod survey schema more readable by using enums
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { prisma } from "@formbricks/database";
|
||||
import { z } from "zod";
|
||||
import { ValidationError } from "@formbricks/errors";
|
||||
import { DatabaseError, ResourceNotFoundError } from "@formbricks/errors";
|
||||
import { TSurvey, ZSurvey } from "@formbricks/types/v1/surveys";
|
||||
@@ -11,7 +12,21 @@ export const getSurvey = async (surveyId: string): Promise<TSurvey | null> => {
|
||||
where: {
|
||||
id: surveyId,
|
||||
},
|
||||
include: {
|
||||
select: {
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
name: true,
|
||||
type: true,
|
||||
environmentId: true,
|
||||
status: true,
|
||||
questions: true,
|
||||
thankYouCard: true,
|
||||
displayOption: true,
|
||||
recontactDays: true,
|
||||
autoClose: true,
|
||||
delay: true,
|
||||
autoComplete: true,
|
||||
triggers: {
|
||||
select: {
|
||||
eventClass: {
|
||||
@@ -76,6 +91,9 @@ export const getSurvey = async (surveyId: string): Promise<TSurvey | null> => {
|
||||
const survey = ZSurvey.parse(transformedSurvey);
|
||||
return survey;
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
console.error(JSON.stringify(error.errors, null, 2)); // log the detailed error information
|
||||
}
|
||||
throw new ValidationError("Data validation of survey failed");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -15,17 +15,17 @@ export const ZSurveyChoice = z.object({
|
||||
label: z.string(),
|
||||
});
|
||||
|
||||
export const ZSurveyLogicCondition = z.union([
|
||||
z.literal("submitted"),
|
||||
z.literal("skipped"),
|
||||
z.literal("equals"),
|
||||
z.literal("notEquals"),
|
||||
z.literal("lessThan"),
|
||||
z.literal("lessEqual"),
|
||||
z.literal("greaterThan"),
|
||||
z.literal("greaterEqual"),
|
||||
z.literal("includesAll"),
|
||||
z.literal("includesOne"),
|
||||
export const ZSurveyLogicCondition = z.enum([
|
||||
"submitted",
|
||||
"skipped",
|
||||
"equals",
|
||||
"notEquals",
|
||||
"lessThan",
|
||||
"lessEqual",
|
||||
"greaterThan",
|
||||
"greaterEqual",
|
||||
"includesAll",
|
||||
"includesOne",
|
||||
]);
|
||||
|
||||
export const ZSurveyLogicBase = z.object({
|
||||
@@ -35,40 +35,36 @@ export const ZSurveyLogicBase = z.object({
|
||||
});
|
||||
|
||||
export const ZSurveyOpenTextLogic = ZSurveyLogicBase.extend({
|
||||
condition: z.union([z.literal("submitted"), z.literal("skipped")]).optional(),
|
||||
condition: z.enum(["submitted", "skipped"]).optional(),
|
||||
value: z.undefined(),
|
||||
});
|
||||
|
||||
export const ZSurveyConsentLogic = ZSurveyLogicBase.extend({
|
||||
condition: z.union([z.literal("submitted"), z.literal("skipped"), z.literal("accepted")]).optional(),
|
||||
condition: z.enum(["submitted", "skipped", "accepted"]).optional(),
|
||||
value: z.undefined(),
|
||||
});
|
||||
|
||||
export const ZSurveyMultipleChoiceSingleLogic = ZSurveyLogicBase.extend({
|
||||
condition: z
|
||||
.union([z.literal("submitted"), z.literal("skipped"), z.literal("equals"), z.literal("notEquals")])
|
||||
.optional(),
|
||||
condition: z.enum(["submitted", "skipped", "equals", "notEquals"]).optional(),
|
||||
value: z.string(),
|
||||
});
|
||||
|
||||
export const ZSurveyMultipleChoiceMultiLogic = ZSurveyLogicBase.extend({
|
||||
condition: z
|
||||
.union([z.literal("submitted"), z.literal("skipped"), z.literal("includesAll"), z.literal("includesOne")])
|
||||
.optional(),
|
||||
condition: z.enum(["submitted", "skipped", "includesAll", "includesOne"]).optional(),
|
||||
value: z.array(z.string()),
|
||||
});
|
||||
|
||||
export const ZSurveyNPSLogic = ZSurveyLogicBase.extend({
|
||||
condition: z
|
||||
.union([
|
||||
z.literal("submitted"),
|
||||
z.literal("skipped"),
|
||||
z.literal("lessThan"),
|
||||
z.literal("lessEqual"),
|
||||
z.literal("greaterThan"),
|
||||
z.literal("greaterEqual"),
|
||||
z.literal("equals"),
|
||||
z.literal("notEquals"),
|
||||
.enum([
|
||||
"submitted",
|
||||
"skipped",
|
||||
"lessThan",
|
||||
"lessEqual",
|
||||
"greaterThan",
|
||||
"greaterEqual",
|
||||
"equals",
|
||||
"notEquals",
|
||||
])
|
||||
.optional(),
|
||||
value: z.number(),
|
||||
@@ -81,15 +77,15 @@ const ZSurveyCTALogic = ZSurveyLogicBase.extend({
|
||||
|
||||
const ZSurveyRatingLogic = ZSurveyLogicBase.extend({
|
||||
condition: z
|
||||
.union([
|
||||
z.literal("submitted"),
|
||||
z.literal("skipped"),
|
||||
z.literal("lessThan"),
|
||||
z.literal("lessEqual"),
|
||||
z.literal("greaterThan"),
|
||||
z.literal("greaterEqual"),
|
||||
z.literal("equals"),
|
||||
z.literal("notEquals"),
|
||||
.enum([
|
||||
"submitted",
|
||||
"skipped",
|
||||
"lessThan",
|
||||
"lessEqual",
|
||||
"greaterThan",
|
||||
"greaterEqual",
|
||||
"equals",
|
||||
"notEquals",
|
||||
])
|
||||
.optional(),
|
||||
value: z.number(),
|
||||
@@ -183,26 +179,18 @@ export const ZSurvey = z.object({
|
||||
createdAt: z.date(),
|
||||
updatedAt: z.date(),
|
||||
name: z.string(),
|
||||
type: z.union([z.literal("web"), z.literal("email"), z.literal("link"), z.literal("mobile")]),
|
||||
type: z.enum(["web", "email", "link", "mobile"]),
|
||||
environmentId: z.string(),
|
||||
status: z.union([
|
||||
z.literal("draft"),
|
||||
z.literal("inProgress"),
|
||||
z.literal("archived"),
|
||||
z.literal("paused"),
|
||||
z.literal("completed"),
|
||||
]),
|
||||
status: z.enum(["draft", "inProgress", "archived", "paused", "completed"]),
|
||||
attributeFilters: z.array(ZSurveyAttributeFilter),
|
||||
displayOption: z.union([
|
||||
z.literal("displayOnce"),
|
||||
z.literal("displayMultiple"),
|
||||
z.literal("respondMultiple"),
|
||||
]),
|
||||
displayOption: z.enum(["displayOnce", "displayMultiple", "respondMultiple"]),
|
||||
autoClose: z.union([z.number(), z.null()]),
|
||||
triggers: z.array(ZEventClass),
|
||||
recontactDays: z.union([z.number(), z.null()]),
|
||||
questions: ZSurveyQuestions,
|
||||
thankYouCard: ZSurveyThankYouCard,
|
||||
delay: z.number(),
|
||||
autoComplete: z.union([z.boolean(), z.null()]),
|
||||
analytics: z.object({
|
||||
numDisplays: z.number(),
|
||||
responseRate: z.number(),
|
||||
|
||||
Reference in New Issue
Block a user