fix: coderabbit feedback

This commit is contained in:
pandeymangg
2025-11-02 17:59:16 +05:30
parent 49fa5c587c
commit 4642cc60c9
3 changed files with 70 additions and 4 deletions

View File

@@ -627,12 +627,14 @@ export const createSurvey = async (
checkForInvalidImagesInQuestions(data.questions);
}
// Add blocks validation
// Add blocks validation and strip isDraft
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);
}
const survey = await prisma.survey.create({

View File

@@ -333,6 +333,26 @@ describe("Element Operations", () => {
}
});
test("should return error for duplicate element ID within same block", () => {
const survey = createMockSurvey();
const duplicateElement = {
id: "elem-1", // Already exists in block-1
type: TSurveyElementTypeEnum.Rating,
headline: { default: "Duplicate in same block" },
required: false,
range: 5,
scale: "star",
} as any;
// Try to add to the same block where elem-1 already exists
const result = addElementToBlock(survey, "block-1", duplicateElement);
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error.message).toContain("already exists");
}
});
test("should return error for non-existent block", () => {
const survey = createMockSurvey();
const element = {
@@ -365,6 +385,42 @@ describe("Element Operations", () => {
}
});
test("should allow updating element ID to a unique ID", () => {
const survey = createMockSurvey();
const result = updateElementInBlock(survey, "block-1", "elem-1", {
id: "elem-new-id",
});
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.data.blocks![0].elements[0].id).toBe("elem-new-id");
}
});
test("should return error when updating element ID to duplicate within same block", () => {
const survey = createMockSurvey();
const result = updateElementInBlock(survey, "block-1", "elem-1", {
id: "elem-2", // elem-2 already exists in block-1
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error.message).toContain("already exists");
}
});
test("should return error when updating element ID to duplicate in another block", () => {
const survey = createMockSurvey();
const result = updateElementInBlock(survey, "block-1", "elem-1", {
id: "elem-3", // elem-3 exists in block-2
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error.message).toContain("already exists");
}
});
test("should return error for non-existent element", () => {
const survey = createMockSurvey();
const result = updateElementInBlock(survey, "block-1", "non-existent", {

View File

@@ -151,6 +151,7 @@ export const duplicateBlock = (
const elementsWithNewIds = blockToDuplicate.elements.map((element) => ({
...element,
id: createId(),
isDraft: true,
}));
const duplicatedBlock: TSurveyBlock = {
@@ -239,9 +240,9 @@ export const addElementToBlock = (
return err(new Error(`Block with ID "${blockId}" not found`));
}
// Validate element ID is unique across all blocks
if (!isElementIdUnique(element.id, blocks, blockId)) {
return err(new Error(`Element ID "${element.id}" already exists in another block`));
// Validate element ID is unique across all blocks (including the target block)
if (!isElementIdUnique(element.id, blocks)) {
return err(new Error(`Element ID "${element.id}" already exists`));
}
const block = { ...blocks[blockIndex] };
@@ -296,6 +297,13 @@ export const updateElementInBlock = (
return err(new Error(`Element with ID "${elementId}" not found in block "${blockId}"`));
}
// If changing the element ID, validate the new ID is unique across all blocks
if (updatedAttributes.id && updatedAttributes.id !== elementId) {
if (!isElementIdUnique(updatedAttributes.id, blocks)) {
return err(new Error(`Element ID "${updatedAttributes.id}" already exists`));
}
}
elements[elementIndex] = {
...elements[elementIndex],
...updatedAttributes,