Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(custom-fields): order locks and validate required drafts

Tindb73e697927a
diff --git a/apps/api/src/custom-field/controllers/reorder-custom-field.ts b/apps/api/src/custom-field/controllers/reorder-custom-field.ts
index 88c855642..2ea91f607 100644
--- a/apps/api/src/custom-field/controllers/reorder-custom-field.ts
+++ b/apps/api/src/custom-field/controllers/reorder-custom-field.ts
@@ -1,4 +1,4 @@
-import { and, eq } from "drizzle-orm";
+import { and, eq, inArray } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
import { customFieldDefinitionTable } from "../../database/schema";
@@ -8,6 +8,21 @@ async function reorderCustomFields(
customFields: Array<{ id: string; position: number }>,
) {
await db.transaction(async (tx) => {
+ // Match creation/duplication lock order before applying the requested display order.
+ await tx
+ .select({ id: customFieldDefinitionTable.id })
+ .from(customFieldDefinitionTable)
+ .where(
+ and(
+ eq(customFieldDefinitionTable.projectId, projectId),
+ inArray(
+ customFieldDefinitionTable.id,
+ customFields.map((field) => field.id),
+ ),
+ ),
+ )
+ .orderBy(customFieldDefinitionTable.id)
+ .for("update");
for (const field of customFields) {
const [updated] = await tx
.update(customFieldDefinitionTable)
diff --git a/apps/web/src/components/shared/modals/create-task-modal.test.tsx b/apps/web/src/components/shared/modals/create-task-modal.test.tsx
index 4c0f624c6..663b64131 100644
--- a/apps/web/src/components/shared/modals/create-task-modal.test.tsx
+++ b/apps/web/src/components/shared/modals/create-task-modal.test.tsx
@@ -238,6 +238,44 @@ describe("CreateTaskModal", () => {
});
});
+ it.each(["dropdown", "multiselect"])(
+ "blocks incomplete required %s values before updating an upload draft",
+ async (type) => {
+ useLocation.mockReturnValue({
+ pathname: "/dashboard/workspace/workspace-1/project/project-1/board",
+ });
+ customFields = [
+ {
+ id: "people",
+ name: "People",
+ type,
+ options: ["Former", "Current"],
+ hiddenOptions: [],
+ defaultValue: type === "dropdown" ? "Former" : '["Former"]',
+ required: true,
+ },
+ ];
+ const view = render(<CreateTaskModal open onClose={vi.fn()} />, {
+ wrapper: createWrapper(),
+ });
+ enterTitle();
+ await act(async () => {
+ await ensureTaskId?.();
+ });
+ customFields = customFields.map((field) => ({
+ ...field,
+ hiddenOptions: ["Former"],
+ defaultValue: "",
+ }));
+ view.rerender(<CreateTaskModal open onClose={vi.fn()} />);
+ await act(async () => {
+ submit();
+ });
+ expect(updateTask).not.toHaveBeenCalled();
+ expect(setCustomFieldValue).not.toHaveBeenCalled();
+ },
+ );
+
it("keeps unsaved input while discard confirmation is open", async () => {
useLocation.mockReturnValue({
pathname: "/dashboard/workspace/workspace-1/project/project-1/board",
diff --git a/apps/web/src/components/shared/modals/create-task-modal.tsx b/apps/web/src/components/shared/modals/create-task-modal.tsx
index b6f2c36b3..87b8f7f28 100644
--- a/apps/web/src/components/shared/modals/create-task-modal.tsx
+++ b/apps/web/src/components/shared/modals/create-task-modal.tsx
@@ -559,6 +559,19 @@ function CreateTaskModalContent({
customFieldValues,
]);
+ const missingRequiredField = customFields.find((field) => {
+ if (!field.required) return false;
+ const value = customFieldValues[field.id]?.trim();
+ if (!value) return true;
+ if (field.type !== "multiselect") return false;
+ try {
+ const selected: unknown = JSON.parse(value);
+ return !Array.isArray(selected) || selected.length === 0;
+ } catch {
+ return true;
+ }
+ });
+
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (
@@ -571,6 +584,13 @@ function CreateTaskModalContent({
)
return;
+ if (missingRequiredField) {
+ toast.error(
+ `${missingRequiredField.name}: ${t("settings:customFields.required")}`,
+ );
+ return;
+ }
+
submittingRef.current = true;
setIsSubmitting(true);
try {
@@ -1622,7 +1642,12 @@ function CreateTaskModalContent({
</Button>
<Button
type="submit"
- disabled={!title.trim() || !resolvedProjectId || isSubmitting}
+ disabled={
+ !title.trim() ||
+ !resolvedProjectId ||
+ isSubmitting ||
+ !!missingRequiredField
+ }
size="sm"
className="disabled:opacity-50"
>
diff --git a/tests/api-integration/custom-field-edit.test.ts b/tests/api-integration/custom-field-edit.test.ts
index b4b3db623..561d0483d 100644
--- a/tests/api-integration/custom-field-edit.test.ts
+++ b/tests/api-integration/custom-field-edit.test.ts
@@ -160,6 +160,52 @@ describe("custom field editing", () => {
},
);
+ it("reorders fields in reverse ID order while creating tasks", async () => {
+ const { project, field, app } = await fixture();
+ const [other] = await db
+ .insert(schema.customFieldDefinitionTable)
+ .values({
+ projectId: project.id,
+ name: "Other",
+ type: "text",
+ required: false,
+ })
+ .returning();
+ const reversed = [field, other]
+ .sort((a, b) => b.id.localeCompare(a.id))
+ .map((item, position) => ({ id: item.id, position }));
+ const results = await Promise.all([
+ app.request(`/api/custom-field/reorder/${project.id}`, {
+ method: "PUT",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ fields: reversed }),
+ }),
+ app.request(`/api/task/${project.id}`, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ projectId: project.id,
+ title: "Concurrent task",
+ priority: "no-priority",
+ description: "",
+ status: "to-do",
+ }),
+ }),
+ ]);
+ expect(
+ results.map((result) => result.status),
+ await results[1].clone().text(),
+ ).toEqual([200, 200]);
+ expect(
+ (await results[0].json()).map(
+ (item: { id: string; position: number }) => ({
+ id: item.id,
+ position: item.position,
+ }),
+ ),
+ ).toEqual(reversed);
+ });
+
it("handles simultaneous swaps without collapsing selections", async () => {
const { update, readValue } = await fixture();
expect(