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(api): keep imported tasks in file order

Andrej192b706d1b2b
diff --git a/apps/api/src/task/controllers/import-tasks.ts b/apps/api/src/task/controllers/import-tasks.ts
index 915f1c6f7..507b1a795 100644
--- a/apps/api/src/task/controllers/import-tasks.ts
+++ b/apps/api/src/task/controllers/import-tasks.ts
@@ -15,6 +15,7 @@ import {
getValidTaskStatuses,
} from "../validate-task-fields";
import { claimTaskNumber } from "./claim-task-numbers";
+import { nextTaskPosition } from "./next-task-position";
export type ImportTask = {
title: string;
@@ -93,6 +94,12 @@ async function importTasks(
const createdTask = await db.transaction(async (tx) => {
const taskNumber = await claimTaskNumber(projectId, tx);
+ const position = await nextTaskPosition(
+ tx,
+ projectId,
+ status,
+ column?.id ?? null,
+ );
const [task] = await tx
.insert(taskTable)
@@ -107,6 +114,7 @@ async function importTasks(
description: taskData.description || "",
priority,
number: taskNumber,
+ position,
})
.returning();
diff --git a/tests/api-integration/task-import-order.test.ts b/tests/api-integration/task-import-order.test.ts
new file mode 100644
index 000000000..431088ca6
--- /dev/null
+++ b/tests/api-integration/task-import-order.test.ts
@@ -0,0 +1,61 @@
+import { asc, eq } from "drizzle-orm";
+import { beforeEach, describe, expect, it } from "vite-plus/test";
+import db, { schema } from "../../apps/api/src/database";
+import { createApp } from "../../apps/api/src/index";
+import { mockAuthenticatedSession } from "./helpers/auth";
+import { resetTestDatabase } from "./helpers/database";
+import {
+ createProjectFixture,
+ createWorkspaceMember,
+} from "./helpers/fixtures";
+
+beforeEach(async () => {
+ await resetTestDatabase();
+});
+
+async function importTitles(
+ app: ReturnType<typeof createApp>["app"],
+ projectId: string,
+ titles: string[],
+) {
+ const response = await app.request(`/api/task/import/${projectId}`, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ tasks: titles.map((title) => ({ title, status: "to-do" })),
+ }),
+ });
+ expect(response.status).toBe(200);
+}
+
+describe("task import", () => {
+ it("keeps the file order and appends after existing tasks", async () => {
+ const { user, workspace } = await createWorkspaceMember({ role: "owner" });
+ const { project } = await createProjectFixture({
+ workspaceId: workspace.id,
+ });
+
+ mockAuthenticatedSession(user);
+ const { app } = createApp();
+
+ await importTitles(app, project.id, ["First", "Second", "Third"]);
+ await importTitles(app, project.id, ["Fourth"]);
+
+ const stored = await db
+ .select({
+ title: schema.taskTable.title,
+ position: schema.taskTable.position,
+ })
+ .from(schema.taskTable)
+ .where(eq(schema.taskTable.projectId, project.id))
+ .orderBy(asc(schema.taskTable.position));
+
+ expect(stored.map((task) => task.title)).toEqual([
+ "First",
+ "Second",
+ "Third",
+ "Fourth",
+ ]);
+ expect(new Set(stored.map((task) => task.position)).size).toBe(4);
+ });
+});