Skip to content

Commit 26d0009

Browse files
authored
Merge pull request #47 from Nandgopal-R/feat/fields
fix: new field ordering
2 parents a92e365 + 8de91a5 commit 26d0009

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

src/api/form-fields/controller.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,17 @@ export async function createField({
115115

116116
const createdField = await prisma.$transaction(async (tx) => {
117117
/**
118-
* INSERT AT HEAD
118+
* INSERT AT TAIL
119119
*/
120120
if (!body.prevFieldId) {
121-
const currentHead = await tx.formFields.findFirst({
121+
const lastField = await tx.formFields.findFirst({
122122
where: {
123123
formId: params.formId,
124-
prevFieldId: null,
125124
},
125+
orderBy: {
126+
createdAt: "desc",
127+
},
128+
take: 1,
126129
});
127130

128131
const created = await tx.formFields.create({
@@ -134,17 +137,10 @@ export async function createField({
134137
validation: body.validation ?? undefined,
135138
options: body.options ?? undefined,
136139
formId: params.formId,
137-
prevFieldId: null,
140+
prevFieldId: lastField?.id ?? null,
138141
},
139142
});
140143

141-
if (currentHead) {
142-
await tx.formFields.update({
143-
where: { id: currentHead.id },
144-
data: { prevFieldId: created.id },
145-
});
146-
}
147-
148144
return created;
149145
}
150146

0 commit comments

Comments
 (0)