Skip to content

Commit 1168b03

Browse files
authored
Merge pull request #25 from Nandgopal-R/revert-24-fix/form-fields
Revert "Fix/form fields"
2 parents 07ae2b1 + 63cd56e commit 1168b03

8 files changed

Lines changed: 42 additions & 155 deletions

File tree

‎bruno/forms/createForm.bru‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ settings {
2323
}
2424

2525
docs {
26-
# Create Form
26+
# 2. Create Form
2727

2828
Creates a new empty form container.
2929

‎bruno/forms/deleteForm.bru‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ settings {
2020
}
2121

2222
docs {
23-
# Delete Form
23+
# 5. Delete Form
2424

2525
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.
2626

‎bruno/forms/getAllForms.bru‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ settings {
1616
}
1717

1818
docs {
19-
# Get All Forms
19+
# 1. Get All Forms
2020

2121
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.
2222

‎bruno/forms/getFormById.bru‎

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,75 +20,45 @@ settings {
2020
}
2121

2222
docs {
23-
# Get Form By ID
23+
# 3. Get Form By ID
2424

25-
## Endpoint
26-
`GET /forms/:formId`
25+
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.
2726

28-
## Description
29-
Fetches a form owned by the authenticated user along with its fields in order.
27+
* **URL:** `/forms/:formId`
28+
* **Method:** `GET`
29+
* **Auth Required:** Yes
3030

31-
## Controller
32-
`getFormById`
31+
### Path Parameters
3332

34-
## Auth Required
35-
✅ Yes (User must be the form owner)
33+
| Parameter | Type | Description |
34+
| :--- | :--- | :--- |
35+
| `formId` | `string` (UUID) | The unique ID of the form to retrieve. |
3636

37-
## Path Parameters
37+
### Responses
3838

39-
| Name | Type | Required | Description |
40-
|--------|--------|----------|-------------|
41-
| formId | string | Yes | ID of the form |
39+
#### ✅ 200 OK: Success
4240

43-
## Request Body
44-
None
45-
46-
## Responses
47-
48-
### Success Response (Form with Fields)
49-
50-
#### Status: 200 OK
41+
Returns the full form object.
5142

5243
```json
5344
{
5445
"success": true,
5546
"message": "Form fetched successfully",
56-
"form": {
57-
"id": "form_456",
58-
"title": "Job Application",
59-
"description": "Apply for internship",
60-
"isPublished": false,
61-
"createdAt": "2026-02-07T10:30:00.000Z"
62-
},
63-
"fields": [
64-
{
65-
"id": "field_1",
66-
"formId": "form_456",
67-
"fieldName": "Full Name",
68-
"fieldType": "text",
69-
"prevFieldId": null
70-
},
71-
{
72-
"id": "field_2",
73-
"formId": "form_456",
74-
"fieldName": "Email",
75-
"fieldType": "email",
76-
"prevFieldId": "field_1"
77-
}
78-
]
47+
"data": {
48+
"id": "form_uuid_123",
49+
"title": "Customer Feedback Survey",
50+
"description": "A survey to collect customer opinions.",
51+
"isPublished": true,
52+
"ownerId": "user_uuid_555",
53+
"createdAt": "2023-10-27T10:00:00.000Z",
54+
"updatedAt": "2023-10-27T12:00:00.000Z"
55+
}
7956
}
8057

81-
Success Response (No Fields Found)
82-
Status: 200 OK
83-
84-
{
85-
"success": true,
86-
"message": "No forms fields found",
87-
"data": []
88-
}
58+
❌ 404 Not Found
8959

90-
Error Responses
91-
Status: 404 Not Found
60+
Occurs if the formId does not exist OR if the form belongs to a different user.
61+
JSON
9262

9363
{
9464
"success": false,

‎bruno/forms/publishForm.bru‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ settings {
2020
}
2121

2222
docs {
23-
# Publish Form
23+
# 6. Publish Form
2424

2525
Changes the status of a form to **Published**. Once published, the form becomes accessible to respondents for submission.
2626

‎bruno/forms/unPublishForm.bru‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ settings {
2020
}
2121

2222
docs {
23-
# Unpublish Form
23+
# 7. Unpublish Form
2424

2525
Reverts the status of a form to **Unpublished**. This hides the form from the public, preventing any new submissions until it is published again.
2626

‎src/api/forms/controller.ts‎

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@ export async function getFormById({ user, params, set }: GetFormByIdContext) {
6363
id: params.formId,
6464
ownerId: user.id,
6565
},
66-
select: {
67-
id: true,
68-
title: true,
69-
description: true,
70-
isPublished: true,
71-
createdAt: true,
72-
},
7366
});
7467

7568
if (!form) {
@@ -80,39 +73,11 @@ export async function getFormById({ user, params, set }: GetFormByIdContext) {
8073
};
8174
}
8275

83-
const fields = await prisma.formFields.findMany({
84-
where: { formId: params.formId },
85-
});
86-
87-
if (fields.length === 0) {
88-
logger.info(`No fields found for formId: ${params.formId}`);
89-
return {
90-
success: true,
91-
message: "No forms fields found",
92-
data: [],
93-
};
94-
}
95-
96-
const ordered: typeof fields = [];
97-
98-
let current = fields.find(
99-
(f): f is (typeof fields)[number] => f.prevFieldId === null,
100-
);
101-
102-
while (current) {
103-
ordered.push(current);
104-
105-
current = fields.find(
106-
(f): f is (typeof fields)[number] => f.prevFieldId === current!.id,
107-
);
108-
}
109-
11076
logger.info("Fetched form for user", { userId: user.id, formId: form.id });
11177
return {
11278
success: true,
11379
message: "Form fetched successfully",
114-
form: form,
115-
fields: ordered,
80+
data: form,
11681
};
11782
}
11883

‎src/test/forms.test.ts‎

Lines changed: 12 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { beforeEach, describe, expect, it, mock } from "bun:test";
22

33
// ---------- MOCK PRISMA ----------
4-
const formFindManyMock = mock();
5-
const formFieldsFindManyMock = mock();
4+
const findManyMock = mock();
65
const createMock = mock();
76
const findFirstMock = mock();
87
const updateMock = mock();
@@ -11,15 +10,12 @@ const deleteManyMock = mock();
1110
mock.module("../db/prisma", () => ({
1211
prisma: {
1312
form: {
14-
findMany: formFindManyMock,
13+
findMany: findManyMock,
1514
create: createMock,
1615
findFirst: findFirstMock,
1716
update: updateMock,
1817
deleteMany: deleteManyMock,
1918
},
20-
formFields: {
21-
findMany: formFieldsFindManyMock,
22-
},
2319
},
2420
}));
2521

@@ -41,8 +37,7 @@ const { getAllForms, createForm, getFormById, updateForm, deleteForm } =
4137

4238
describe("Forms Controller Tests", () => {
4339
beforeEach(() => {
44-
formFindManyMock.mockReset();
45-
formFieldsFindManyMock.mockReset();
40+
findManyMock.mockReset();
4641
createMock.mockReset();
4742
findFirstMock.mockReset();
4843
updateMock.mockReset();
@@ -56,7 +51,7 @@ describe("Forms Controller Tests", () => {
5651
// ===== getAllForms =====
5752

5853
it("getAllForms → success", async () => {
59-
formFindManyMock.mockResolvedValue([
54+
findManyMock.mockResolvedValue([
6055
{ id: "1", title: "A", isPublished: true, createdAt: new Date() },
6156
]);
6257

@@ -67,7 +62,7 @@ describe("Forms Controller Tests", () => {
6762
});
6863

6964
it("getAllForms → empty", async () => {
70-
formFindManyMock.mockResolvedValue([]);
65+
findManyMock.mockResolvedValue([]);
7166

7267
const res = await getAllForms({ user } as any);
7368

@@ -76,7 +71,7 @@ describe("Forms Controller Tests", () => {
7671
});
7772

7873
it("getAllForms → DB error", async () => {
79-
formFindManyMock.mockRejectedValue(new Error("DB fail"));
74+
findManyMock.mockRejectedValue(new Error("DB fail"));
8075

8176
expect(getAllForms({ user } as any)).rejects.toThrow();
8277
});
@@ -118,69 +113,26 @@ describe("Forms Controller Tests", () => {
118113

119114
// ===== getFormById =====
120115

121-
it("getFormById → found with ordered fields", async () => {
122-
findFirstMock.mockResolvedValue({
123-
id: "1",
124-
title: "Test Form",
125-
description: "Desc",
126-
isPublished: false,
127-
createdAt: new Date(),
128-
});
129-
130-
formFieldsFindManyMock.mockResolvedValue([
131-
{ id: "f1", formId: "1", prevFieldId: null },
132-
{ id: "f2", formId: "1", prevFieldId: "f1" },
133-
]);
134-
135-
const set: any = {};
136-
137-
const res = await getFormById({
138-
user,
139-
params: { formId: "1" },
140-
set,
141-
} as any);
142-
143-
const result: any = res;
144-
145-
expect(result.success).toBe(true);
146-
expect(result.form.id).toBe("1");
147-
expect(result.fields.length).toBe(2);
148-
expect(result.fields[0].id).toBe("f1");
149-
expect(result.fields[1].id).toBe("f2");
150-
});
151-
152-
it("getFormById → no fields", async () => {
153-
findFirstMock.mockResolvedValue({
154-
id: "1",
155-
title: "Test Form",
156-
description: "Desc",
157-
isPublished: false,
158-
createdAt: new Date(),
159-
});
160-
161-
formFieldsFindManyMock.mockResolvedValue([]);
116+
it("getFormById → found", async () => {
117+
findFirstMock.mockResolvedValue({ id: "1" });
162118

163119
const set: any = {};
164-
165120
const res = await getFormById({
166121
user,
167-
params: { formId: "1" },
122+
params: { id: "1" },
168123
set,
169124
} as any);
170125

171126
expect(res.success).toBe(true);
172-
expect(res.data).toEqual([]);
173-
expect(res.message).toBe("No forms fields found");
174127
});
175128

176129
it("getFormById → not found", async () => {
177130
findFirstMock.mockResolvedValue(null);
178131

179132
const set: any = {};
180-
181133
const res = await getFormById({
182134
user,
183-
params: { formId: "2" },
135+
params: { id: "2" },
184136
set,
185137
} as any);
186138

@@ -193,10 +145,10 @@ describe("Forms Controller Tests", () => {
193145

194146
const set: any = {};
195147

196-
await expect(
148+
expect(
197149
getFormById({
198150
user,
199-
params: { formId: "1" },
151+
params: { id: "1" },
200152
set,
201153
} as any),
202154
).rejects.toThrow();

0 commit comments

Comments
 (0)