mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-24 11:39:22 -05:00
fix: create survey API expects creator (#2007)
This commit is contained in:
@@ -15,5 +15,5 @@ export async function createSurveyAction(environmentId: string, surveyBody: TSur
|
||||
const isAuthorized = await hasUserEnvironmentAccess(session.user.id, environmentId);
|
||||
if (!isAuthorized) throw new AuthorizationError("Not authorized");
|
||||
|
||||
return await createSurvey(environmentId, surveyBody, session.user.id);
|
||||
return await createSurvey(environmentId, surveyBody);
|
||||
}
|
||||
|
||||
+4
-3
@@ -31,11 +31,12 @@ export default function SurveyStarter({
|
||||
setIsCreateSurveyLoading(true);
|
||||
const surveyType = environment?.widgetSetupCompleted ? "web" : "link";
|
||||
const autoComplete = surveyType === "web" ? 50 : null;
|
||||
const augmentedTemplate = {
|
||||
const augmentedTemplate: TSurveyInput = {
|
||||
...template.preset,
|
||||
type: surveyType,
|
||||
autoComplete,
|
||||
} as TSurveyInput;
|
||||
autoComplete: autoComplete || undefined,
|
||||
createdBy: user.id,
|
||||
};
|
||||
try {
|
||||
const survey = await createSurveyAction(environmentId, augmentedTemplate);
|
||||
router.push(`/environments/${environmentId}/surveys/${survey.id}/edit`);
|
||||
|
||||
@@ -70,11 +70,12 @@ export default function TemplateList({
|
||||
setLoading(true);
|
||||
const surveyType = environment?.widgetSetupCompleted ? "web" : "link";
|
||||
const autoComplete = surveyType === "web" ? 50 : null;
|
||||
const augmentedTemplate = {
|
||||
const augmentedTemplate: TSurveyInput = {
|
||||
...activeTemplate.preset,
|
||||
type: surveyType,
|
||||
autoComplete,
|
||||
} as TSurveyInput;
|
||||
createdBy: user.id,
|
||||
};
|
||||
const survey = await createSurveyAction(environmentId, augmentedTemplate);
|
||||
router.push(`/environments/${environmentId}/surveys/${survey.id}/edit`);
|
||||
};
|
||||
|
||||
@@ -15,5 +15,5 @@ export async function createSurveyAction(environmentId: string, surveyBody: TSur
|
||||
const isAuthorized = await hasUserEnvironmentAccess(session.user.id, environmentId);
|
||||
if (!isAuthorized) throw new AuthorizationError("Not authorized");
|
||||
|
||||
return await createSurvey(environmentId, surveyBody, session.user.id);
|
||||
return await createSurvey(environmentId, surveyBody);
|
||||
}
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
-- DropForeignKey
|
||||
ALTER TABLE "Survey" DROP CONSTRAINT "Survey_createdBy_fkey";
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Survey" ADD CONSTRAINT "Survey_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -257,7 +257,7 @@ model Survey {
|
||||
type SurveyType @default(web)
|
||||
environment Environment @relation(fields: [environmentId], references: [id], onDelete: Cascade)
|
||||
environmentId String
|
||||
creator User? @relation(fields: [createdBy], references: [id], onDelete: Cascade)
|
||||
creator User? @relation(fields: [createdBy], references: [id])
|
||||
createdBy String?
|
||||
status SurveyStatus @default(draft)
|
||||
/// @zod.custom(imports.ZSurveyWelcomeCard)
|
||||
|
||||
@@ -481,11 +481,7 @@ export async function deleteSurvey(surveyId: string) {
|
||||
return deletedSurvey;
|
||||
}
|
||||
|
||||
export const createSurvey = async (
|
||||
environmentId: string,
|
||||
surveyBody: TSurveyInput,
|
||||
userId?: string
|
||||
): Promise<TSurvey> => {
|
||||
export const createSurvey = async (environmentId: string, surveyBody: TSurveyInput): Promise<TSurvey> => {
|
||||
validateInputs([environmentId, ZId]);
|
||||
|
||||
if (surveyBody.attributeFilters) {
|
||||
@@ -496,25 +492,33 @@ export const createSurvey = async (
|
||||
const actionClasses = await getActionClasses(environmentId);
|
||||
revalidateSurveyByActionClassId(actionClasses, surveyBody.triggers);
|
||||
}
|
||||
// TODO: Create with triggers & attributeFilters
|
||||
delete surveyBody.triggers;
|
||||
delete surveyBody.attributeFilters;
|
||||
const data: Omit<TSurveyInput, "triggers" | "attributeFilters"> = {
|
||||
|
||||
const createdBy = surveyBody.createdBy;
|
||||
delete surveyBody.createdBy;
|
||||
|
||||
const data: Omit<Prisma.SurveyCreateInput, "environment"> = {
|
||||
...surveyBody,
|
||||
// TODO: Create with triggers & attributeFilters
|
||||
triggers: undefined,
|
||||
attributeFilters: undefined,
|
||||
};
|
||||
|
||||
if (surveyBody.type === "web" && data.thankYouCard) {
|
||||
data.thankYouCard.buttonLabel = "";
|
||||
data.thankYouCard.buttonLink = "";
|
||||
}
|
||||
|
||||
if (createdBy) {
|
||||
data.creator = {
|
||||
connect: {
|
||||
id: createdBy,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const survey = await prisma.survey.create({
|
||||
data: {
|
||||
...data,
|
||||
creator: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
environment: {
|
||||
connect: {
|
||||
id: environmentId,
|
||||
|
||||
@@ -444,6 +444,7 @@ export const ZSurvey = z.object({
|
||||
export const ZSurveyInput = z.object({
|
||||
name: z.string(),
|
||||
type: ZSurveyType.optional(),
|
||||
createdBy: z.string().cuid().optional(),
|
||||
status: ZSurveyStatus.optional(),
|
||||
displayOption: ZSurveyDisplayOption.optional(),
|
||||
autoClose: z.number().optional(),
|
||||
@@ -463,11 +464,13 @@ export const ZSurveyInput = z.object({
|
||||
});
|
||||
|
||||
export type TSurvey = z.infer<typeof ZSurvey>;
|
||||
|
||||
export type TSurveyDates = {
|
||||
createdAt: TSurvey["createdAt"];
|
||||
updatedAt: TSurvey["updatedAt"];
|
||||
closeOnDate: TSurvey["closeOnDate"];
|
||||
};
|
||||
|
||||
export type TSurveyInput = z.infer<typeof ZSurveyInput>;
|
||||
|
||||
export const ZSurveyTSurveyQuestionType = z.union([
|
||||
|
||||
Reference in New Issue
Block a user