From 63cd56ec01a792bd8dd777a1f490ce8aa9c2cab9 Mon Sep 17 00:00:00 2001 From: Nandgopal R Nair Date: Sun, 8 Feb 2026 21:05:20 +0530 Subject: [PATCH] Revert "Fix/form fields" --- bruno/forms/createForm.bru | 2 +- bruno/forms/deleteForm.bru | 2 +- bruno/forms/getAllForms.bru | 2 +- bruno/forms/getFormById.bru | 78 +++++++++++------------------------ bruno/forms/publishForm.bru | 2 +- bruno/forms/unPublishForm.bru | 2 +- src/api/forms/controller.ts | 37 +---------------- src/test/forms.test.ts | 72 ++++++-------------------------- 8 files changed, 42 insertions(+), 155 deletions(-) diff --git a/bruno/forms/createForm.bru b/bruno/forms/createForm.bru index fb6fd4a..87fb5de 100644 --- a/bruno/forms/createForm.bru +++ b/bruno/forms/createForm.bru @@ -23,7 +23,7 @@ settings { } docs { - # Create Form + # 2. Create Form Creates a new empty form container. diff --git a/bruno/forms/deleteForm.bru b/bruno/forms/deleteForm.bru index eb04476..bcdd42b 100644 --- a/bruno/forms/deleteForm.bru +++ b/bruno/forms/deleteForm.bru @@ -20,7 +20,7 @@ settings { } docs { - # Delete Form + # 5. Delete Form Permanently removes a specific form. This endpoint ensures security by only allowing the **owner** of the form to delete it. If the form does not exist or belongs to another user, no action is taken, and a 404 error is returned. diff --git a/bruno/forms/getAllForms.bru b/bruno/forms/getAllForms.bru index 2958095..c94f10e 100644 --- a/bruno/forms/getAllForms.bru +++ b/bruno/forms/getAllForms.bru @@ -16,7 +16,7 @@ settings { } docs { - # Get All Forms + # 1. Get All Forms Retrieves a list of all forms created by the currently authenticated user. This endpoint provides a summary view, returning key metadata like the title and publication status, but not the detailed form fields. diff --git a/bruno/forms/getFormById.bru b/bruno/forms/getFormById.bru index b70e338..cdaf970 100644 --- a/bruno/forms/getFormById.bru +++ b/bruno/forms/getFormById.bru @@ -20,75 +20,45 @@ settings { } docs { - # Get Form By ID + # 3. Get Form By ID - ## Endpoint - `GET /forms/:formId` + Retrieves the details of a specific form. This endpoint is scoped to the **owner**, meaning a user can only fetch forms they created. If a form exists but belongs to another user, it will return a 404 error. - ## Description - Fetches a form owned by the authenticated user along with its fields in order. + * **URL:** `/forms/:formId` + * **Method:** `GET` + * **Auth Required:** Yes - ## Controller - `getFormById` + ### Path Parameters - ## Auth Required - ✅ Yes (User must be the form owner) + | Parameter | Type | Description | + | :--- | :--- | :--- | + | `formId` | `string` (UUID) | The unique ID of the form to retrieve. | - ## Path Parameters + ### Responses - | Name | Type | Required | Description | - |--------|--------|----------|-------------| - | formId | string | Yes | ID of the form | + #### ✅ 200 OK: Success - ## Request Body - None - - ## Responses - - ### Success Response (Form with Fields) - - #### Status: 200 OK + Returns the full form object. ```json { "success": true, "message": "Form fetched successfully", - "form": { - "id": "form_456", - "title": "Job Application", - "description": "Apply for internship", - "isPublished": false, - "createdAt": "2026-02-07T10:30:00.000Z" - }, - "fields": [ - { - "id": "field_1", - "formId": "form_456", - "fieldName": "Full Name", - "fieldType": "text", - "prevFieldId": null - }, - { - "id": "field_2", - "formId": "form_456", - "fieldName": "Email", - "fieldType": "email", - "prevFieldId": "field_1" - } - ] + "data": { + "id": "form_uuid_123", + "title": "Customer Feedback Survey", + "description": "A survey to collect customer opinions.", + "isPublished": true, + "ownerId": "user_uuid_555", + "createdAt": "2023-10-27T10:00:00.000Z", + "updatedAt": "2023-10-27T12:00:00.000Z" + } } - Success Response (No Fields Found) - Status: 200 OK - - { - "success": true, - "message": "No forms fields found", - "data": [] - } + ❌ 404 Not Found - Error Responses - Status: 404 Not Found + Occurs if the formId does not exist OR if the form belongs to a different user. + JSON { "success": false, diff --git a/bruno/forms/publishForm.bru b/bruno/forms/publishForm.bru index b510ffd..f17bb25 100644 --- a/bruno/forms/publishForm.bru +++ b/bruno/forms/publishForm.bru @@ -20,7 +20,7 @@ settings { } docs { - # Publish Form + # 6. Publish Form Changes the status of a form to **Published**. Once published, the form becomes accessible to respondents for submission. diff --git a/bruno/forms/unPublishForm.bru b/bruno/forms/unPublishForm.bru index 84895d6..62016f0 100644 --- a/bruno/forms/unPublishForm.bru +++ b/bruno/forms/unPublishForm.bru @@ -20,7 +20,7 @@ settings { } docs { - # Unpublish Form + # 7. Unpublish Form Reverts the status of a form to **Unpublished**. This hides the form from the public, preventing any new submissions until it is published again. diff --git a/src/api/forms/controller.ts b/src/api/forms/controller.ts index 00dfe0e..1e8334b 100644 --- a/src/api/forms/controller.ts +++ b/src/api/forms/controller.ts @@ -63,13 +63,6 @@ export async function getFormById({ user, params, set }: GetFormByIdContext) { id: params.formId, ownerId: user.id, }, - select: { - id: true, - title: true, - description: true, - isPublished: true, - createdAt: true, - }, }); if (!form) { @@ -80,39 +73,11 @@ export async function getFormById({ user, params, set }: GetFormByIdContext) { }; } - const fields = await prisma.formFields.findMany({ - where: { formId: params.formId }, - }); - - if (fields.length === 0) { - logger.info(`No fields found for formId: ${params.formId}`); - return { - success: true, - message: "No forms fields found", - data: [], - }; - } - - const ordered: typeof fields = []; - - let current = fields.find( - (f): f is (typeof fields)[number] => f.prevFieldId === null, - ); - - while (current) { - ordered.push(current); - - current = fields.find( - (f): f is (typeof fields)[number] => f.prevFieldId === current!.id, - ); - } - logger.info("Fetched form for user", { userId: user.id, formId: form.id }); return { success: true, message: "Form fetched successfully", - form: form, - fields: ordered, + data: form, }; } diff --git a/src/test/forms.test.ts b/src/test/forms.test.ts index 6379b8d..9b25b34 100644 --- a/src/test/forms.test.ts +++ b/src/test/forms.test.ts @@ -1,8 +1,7 @@ import { beforeEach, describe, expect, it, mock } from "bun:test"; // ---------- MOCK PRISMA ---------- -const formFindManyMock = mock(); -const formFieldsFindManyMock = mock(); +const findManyMock = mock(); const createMock = mock(); const findFirstMock = mock(); const updateMock = mock(); @@ -11,15 +10,12 @@ const deleteManyMock = mock(); mock.module("../db/prisma", () => ({ prisma: { form: { - findMany: formFindManyMock, + findMany: findManyMock, create: createMock, findFirst: findFirstMock, update: updateMock, deleteMany: deleteManyMock, }, - formFields: { - findMany: formFieldsFindManyMock, - }, }, })); @@ -41,8 +37,7 @@ const { getAllForms, createForm, getFormById, updateForm, deleteForm } = describe("Forms Controller Tests", () => { beforeEach(() => { - formFindManyMock.mockReset(); - formFieldsFindManyMock.mockReset(); + findManyMock.mockReset(); createMock.mockReset(); findFirstMock.mockReset(); updateMock.mockReset(); @@ -56,7 +51,7 @@ describe("Forms Controller Tests", () => { // ===== getAllForms ===== it("getAllForms → success", async () => { - formFindManyMock.mockResolvedValue([ + findManyMock.mockResolvedValue([ { id: "1", title: "A", isPublished: true, createdAt: new Date() }, ]); @@ -67,7 +62,7 @@ describe("Forms Controller Tests", () => { }); it("getAllForms → empty", async () => { - formFindManyMock.mockResolvedValue([]); + findManyMock.mockResolvedValue([]); const res = await getAllForms({ user } as any); @@ -76,7 +71,7 @@ describe("Forms Controller Tests", () => { }); it("getAllForms → DB error", async () => { - formFindManyMock.mockRejectedValue(new Error("DB fail")); + findManyMock.mockRejectedValue(new Error("DB fail")); expect(getAllForms({ user } as any)).rejects.toThrow(); }); @@ -118,69 +113,26 @@ describe("Forms Controller Tests", () => { // ===== getFormById ===== - it("getFormById → found with ordered fields", async () => { - findFirstMock.mockResolvedValue({ - id: "1", - title: "Test Form", - description: "Desc", - isPublished: false, - createdAt: new Date(), - }); - - formFieldsFindManyMock.mockResolvedValue([ - { id: "f1", formId: "1", prevFieldId: null }, - { id: "f2", formId: "1", prevFieldId: "f1" }, - ]); - - const set: any = {}; - - const res = await getFormById({ - user, - params: { formId: "1" }, - set, - } as any); - - const result: any = res; - - expect(result.success).toBe(true); - expect(result.form.id).toBe("1"); - expect(result.fields.length).toBe(2); - expect(result.fields[0].id).toBe("f1"); - expect(result.fields[1].id).toBe("f2"); - }); - - it("getFormById → no fields", async () => { - findFirstMock.mockResolvedValue({ - id: "1", - title: "Test Form", - description: "Desc", - isPublished: false, - createdAt: new Date(), - }); - - formFieldsFindManyMock.mockResolvedValue([]); + it("getFormById → found", async () => { + findFirstMock.mockResolvedValue({ id: "1" }); const set: any = {}; - const res = await getFormById({ user, - params: { formId: "1" }, + params: { id: "1" }, set, } as any); expect(res.success).toBe(true); - expect(res.data).toEqual([]); - expect(res.message).toBe("No forms fields found"); }); it("getFormById → not found", async () => { findFirstMock.mockResolvedValue(null); const set: any = {}; - const res = await getFormById({ user, - params: { formId: "2" }, + params: { id: "2" }, set, } as any); @@ -193,10 +145,10 @@ describe("Forms Controller Tests", () => { const set: any = {}; - await expect( + expect( getFormById({ user, - params: { formId: "1" }, + params: { id: "1" }, set, } as any), ).rejects.toThrow();