Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/bruno/form-response/folder.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
meta {
name: form-response
seq: 5
}

auth {
mode: inherit
}
20 changes: 20 additions & 0 deletions backend/bruno/form-response/getFormResponsesForFormOwner.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
meta {
name: getFormResponsesForFormOwner
type: http
seq: 2
}

get {
url: http://localhost:8000/responses/:formId
body: none
auth: inherit
}

params:path {
formId: ef50e45d-d095-418d-ab49-7196900fa5c2
}

settings {
encodeUrl: true
timeout: 0
}
20 changes: 20 additions & 0 deletions backend/bruno/form-response/getSubmittedResponse.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
meta {
name: getSubmittedResponse
type: http
seq: 3
}

get {
url: http://localhost:8000/responses/:formId
body: none
auth: inherit
}

params:path {
formId: ef50e45d-d095-418d-ab49-7196900fa5c2
}

settings {
encodeUrl: true
timeout: 0
}
30 changes: 30 additions & 0 deletions backend/bruno/form-response/submitResponse.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
meta {
name: submitResponse
type: http
seq: 1
}

post {
url: http://localhost:8000/responses/:formId
body: json
auth: inherit
}

params:path {
formId: ef50e45d-d095-418d-ab49-7196900fa5c2
}

body:json {
{
"answers":{
"575f5192-d46c-4974-bb0c-f909d9fb80ce": "Jack",
"894b4cf0-1320-4403-b116-8504f23ef6e3": "jack@gmail.com",
"15026215-127b-44bc-8a06-a12e33f79802":"3"
}
}
}

settings {
encodeUrl: true
timeout: 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:

- You are about to drop the column `nextField` on the `form_fields` table. All the data in the column will be lost.

*/
-- AlterTable
ALTER TABLE "form_fields" DROP COLUMN "nextField";
5 changes: 0 additions & 5 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ model FormFields {
// This field stores the ID of the field passing control to this one
prevFieldId String?

// The "Next" Field (Child)
// This field is the virtual counterpart to the relation above.
// We don't need a physical 'childFieldId' column because Prisma handles the other side of the relation automatically.
nextField String?

@@index([formId])
@@index([formId, prevFieldId])
@@map("form_fields")
Expand Down
146 changes: 78 additions & 68 deletions backend/src/api/form-fields/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ export async function getAllFields({ params, set }: GetAllFieldsContext) {
}

const fields = await prisma.formFields.findMany({
select: {
id: true,
fieldName: true,
label: true,
fieldValueType: true,
fieldType: true,
validation: true,
prevFieldId: true,
nextField: true,
},
where: { formId: params.formId },
});

Expand All @@ -44,14 +34,22 @@ export async function getAllFields({ params, set }: GetAllFieldsContext) {
data: [],
};
}
logger.info(
`Fetched all fields for formId: ${params.formId}, fieldCount: ${fields.length}`,

const ordered: typeof fields = [];

let current = fields.find(
(f): f is (typeof fields)[number] => f.prevFieldId === null,
);
return {
success: true,
message: "All form fields fetched successfully",
data: fields,
};

while (current) {
ordered.push(current);

current = fields.find(
(f): f is (typeof fields)[number] => f.prevFieldId === current!.id,
);
}

return { success: true, data: ordered };
}

export async function createField({
Expand All @@ -69,79 +67,93 @@ export async function createField({

if (!form) {
set.status = 404;
return {
success: false,
message: "Form not found",
};
return { success: false, message: "Form not found" };
}

const field = await prisma.$transaction(async (tx: any) => {
// 1. If we are inserting after a specific field (prevFieldId provided)
if (body.prevFieldId) {
// Fetch the previous field to see if it has a next field
const prevField = await tx.formFields.findUnique({
where: { id: body.prevFieldId },
const createdField = await prisma.$transaction(async (tx) => {
/**
* INSERT AT HEAD
*/
if (!body.prevFieldId) {
const currentHead = await tx.formFields.findFirst({
where: {
formId: params.formId,
prevFieldId: null,
},
});

if (!prevField) {
throw new Error("Previous field not found");
}

// 2. Create the new field
// It points back to prevFieldId
// It points forward to whatever prevField was pointing to
const newField = await tx.formFields.create({
const created = await tx.formFields.create({
data: {
fieldName: body.fieldName,
label: body.label,
fieldValueType: body.fieldValueType,
fieldType: body.fieldType,
validation: body.validation ?? undefined,
prevFieldId: body.prevFieldId,
nextField: prevField.nextField, // Inherit the link
formId: params.formId,
prevFieldId: null,
},
});

// 3. Update the previous field to point to the new field
await tx.formFields.update({
where: { id: body.prevFieldId },
data: { nextField: newField.id },
});

// 4. If there was a next field, update it to point back to the new field
if (prevField.nextField) {
// We can't query by `id` directly if `nextField` is just a string without relation,
// but typically we can update the row where id matches the string.
if (currentHead) {
await tx.formFields.update({
where: { id: prevField.nextField },
data: { prevFieldId: newField.id },
where: { id: currentHead.id },
data: { prevFieldId: created.id },
});
}

return newField;
return created;
}

// Fallback: If no prevFieldId is provided, we assume it's the first field
// or simply creating a field without links yet.
return await tx.formFields.create({
/**
* INSERT AFTER A FIELD
*/
const prevField = await tx.formFields.findFirst({
where: {
id: body.prevFieldId,
formId: params.formId,
},
});

if (!prevField) {
// ❗ This will automatically rollback the transaction
throw new Error("Previous field not found in the specified form");
}

const nextField = await tx.formFields.findFirst({
where: {
formId: params.formId,
prevFieldId: prevField.id,
},
});

const created = await tx.formFields.create({
data: {
fieldName: body.fieldName,
label: body.label,
fieldValueType: body.fieldValueType,
fieldType: body.fieldType,
validation: body.validation ?? undefined,
formId: params.formId,
prevFieldId: prevField.id,
},
});

if (nextField) {
await tx.formFields.update({
where: { id: nextField.id },
data: { prevFieldId: created.id },
});
}

return created;
});

logger.info(`Created field ${field.id} for form ${params.formId}`);
logger.info(`Created field ${createdField.id} in form ${params.formId}`);

return {
success: true,
message: "Field created successfully",
data: field,
data: createdField,
};
}

Expand Down Expand Up @@ -202,26 +214,24 @@ export async function deleteField({ params, set, user }: DeleteFieldContext) {
return { success: false, message: "Unauthorized" };
}

await prisma.$transaction(async (tx: any) => {
// 1. Link Previous to Next
if (field.prevFieldId) {
await tx.formFields.update({
where: { id: field.prevFieldId },
data: { nextField: field.nextField },
});
}
await prisma.$transaction(async (tx) => {
const nextField = await tx.formFields.findFirst({
where: {
formId: field.formId,
prevFieldId: field.id,
},
});

// 2. Link Next to Previous
if (field.nextField) {
// relink previous → next
if (nextField) {
await tx.formFields.update({
where: { id: field.nextField },
where: { id: nextField.id },
data: { prevFieldId: field.prevFieldId },
});
}

// 3. Delete the field
await tx.formFields.delete({
where: { id: params.id },
where: { id: field.id },
});
});

Expand Down
Loading