mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-18 19:41:17 -05:00
fix: code duplication
This commit is contained in:
@@ -20,6 +20,7 @@ import {
|
||||
checkForInvalidImagesInQuestions,
|
||||
stripIsDraftFromBlocks,
|
||||
transformPrismaSurvey,
|
||||
validateAndPrepareBlocks,
|
||||
} from "./utils";
|
||||
|
||||
interface TriggerUpdate {
|
||||
@@ -627,14 +628,9 @@ export const createSurvey = async (
|
||||
checkForInvalidImagesInQuestions(data.questions);
|
||||
}
|
||||
|
||||
// Add blocks validation and strip isDraft
|
||||
// Validate and prepare blocks for persistence
|
||||
if (data.blocks && data.blocks.length > 0) {
|
||||
const blocksValidation = checkForInvalidImagesInBlocks(data.blocks);
|
||||
if (!blocksValidation.ok) {
|
||||
throw blocksValidation.error;
|
||||
}
|
||||
// Strip isDraft from elements before persisting
|
||||
data.blocks = stripIsDraftFromBlocks(data.blocks);
|
||||
data.blocks = validateAndPrepareBlocks(data.blocks);
|
||||
}
|
||||
|
||||
const survey = await prisma.survey.create({
|
||||
|
||||
@@ -119,3 +119,26 @@ export const stripIsDraftFromBlocks = (blocks: TSurveyBlock[]): TSurveyBlock[] =
|
||||
}),
|
||||
}));
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates and prepares blocks for persistence
|
||||
* - Validates all image URLs in blocks
|
||||
* - Strips isDraft flags from elements
|
||||
* @param blocks - Array of survey blocks to validate and prepare
|
||||
* @returns Prepared blocks ready for database persistence
|
||||
* @throws Error if any image validation fails
|
||||
*/
|
||||
export const validateAndPrepareBlocks = (blocks: TSurveyBlock[]): TSurveyBlock[] => {
|
||||
if (!blocks || blocks.length === 0) {
|
||||
return blocks;
|
||||
}
|
||||
|
||||
// Validate images
|
||||
const validation = checkForInvalidImagesInBlocks(blocks);
|
||||
if (!validation.ok) {
|
||||
throw validation.error;
|
||||
}
|
||||
|
||||
// Strip isDraft
|
||||
return stripIsDraftFromBlocks(blocks);
|
||||
};
|
||||
|
||||
@@ -4,11 +4,7 @@ import { logger } from "@formbricks/logger";
|
||||
import { DatabaseError, InvalidInputError, ResourceNotFoundError } from "@formbricks/types/errors";
|
||||
import { TSegment, ZSegmentFilters } from "@formbricks/types/segment";
|
||||
import { TSurvey } from "@formbricks/types/surveys/types";
|
||||
import {
|
||||
checkForInvalidImagesInBlocks,
|
||||
checkForInvalidImagesInQuestions,
|
||||
stripIsDraftFromBlocks,
|
||||
} from "@/lib/survey/utils";
|
||||
import { checkForInvalidImagesInQuestions, validateAndPrepareBlocks } from "@/lib/survey/utils";
|
||||
import { TriggerUpdate } from "@/modules/survey/editor/types/survey-trigger";
|
||||
import { getActionClasses } from "@/modules/survey/lib/action-class";
|
||||
import { getOrganizationAIKeys, getOrganizationIdFromEnvironmentId } from "@/modules/survey/lib/organization";
|
||||
@@ -31,12 +27,9 @@ export const updateSurvey = async (updatedSurvey: TSurvey): Promise<TSurvey> =>
|
||||
|
||||
checkForInvalidImagesInQuestions(questions);
|
||||
|
||||
// Add blocks validation
|
||||
// Validate and prepare blocks for persistence
|
||||
if (updatedSurvey.blocks && updatedSurvey.blocks.length > 0) {
|
||||
const blocksValidation = checkForInvalidImagesInBlocks(updatedSurvey.blocks);
|
||||
if (!blocksValidation.ok) {
|
||||
throw blocksValidation.error;
|
||||
}
|
||||
data.blocks = validateAndPrepareBlocks(updatedSurvey.blocks);
|
||||
}
|
||||
|
||||
if (languages) {
|
||||
@@ -246,11 +239,6 @@ export const updateSurvey = async (updatedSurvey: TSurvey): Promise<TSurvey> =>
|
||||
return rest;
|
||||
});
|
||||
|
||||
// Strip isDraft from elements before saving
|
||||
if (updatedSurvey.blocks && updatedSurvey.blocks.length > 0) {
|
||||
data.blocks = stripIsDraftFromBlocks(updatedSurvey.blocks);
|
||||
}
|
||||
|
||||
const organizationId = await getOrganizationIdFromEnvironmentId(environmentId);
|
||||
const organization = await getOrganizationAIKeys(organizationId);
|
||||
if (!organization) {
|
||||
|
||||
Reference in New Issue
Block a user