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(projects): keep omitted projects at their existing rank on reorder

Rojenson Lugoc6b876873272
diff --git a/apps/api/src/project/controllers/reorder-projects.ts b/apps/api/src/project/controllers/reorder-projects.ts
index 46591ffca..f1ab5e8d1 100644
--- a/apps/api/src/project/controllers/reorder-projects.ts
+++ b/apps/api/src/project/controllers/reorder-projects.ts
@@ -25,8 +25,9 @@ async function reorderProjects(
);
// The whole workspace, not just the payload: the client only ever sees
- // non-archived projects, so archived ones have to keep their place in the
- // ordering without being sent.
+ // non-archived projects, so archived ones have to hold their place in the
+ // ordering without being sent. Ordering here defines each project's current
+ // rank, which is what the renumbering below pins them to.
const existing = await tx
.select({ id: projectTable.id, position: projectTable.position })
.from(projectTable)
@@ -56,10 +57,19 @@ async function reorderProjects(
.sort((a, b) => a.position - b.position)
.map((project) => project.id);
const requestedIds = new Set(requestedOrder);
- const remaining = existing
- .filter((project) => !requestedIds.has(project.id))
- .map((project) => project.id);
- const finalOrder = [...requestedOrder, ...remaining];
+
+ // Projects the payload omits keep the rank they already hold, and the
+ // payload fills the slots around them in the order it asked for. Appending
+ // them instead would sink an archived project to the bottom of the
+ // workspace the first time anyone dragged a visible one.
+ const requested = requestedOrder[Symbol.iterator]();
+ const finalOrder = existing.map((project) =>
+ requestedIds.has(project.id)
+ ? // Every requested id was matched against `existing` above, so the
+ // slots and the payload are the same length and this never falls back.
+ (requested.next().value ?? project.id)
+ : project.id,
+ );
const currentPositions = new Map(
existing.map((project) => [project.id, project.position]),
diff --git a/tests/api-integration/project-reorder.test.ts b/tests/api-integration/project-reorder.test.ts
index b7e4471d7..af7fd81ee 100644
--- a/tests/api-integration/project-reorder.test.ts
+++ b/tests/api-integration/project-reorder.test.ts
@@ -190,7 +190,7 @@ describe("API integration: project reorder", () => {
expect(projects.map((project) => project.position)).toEqual([0, 1, 2]);
});
- it("appends workspace projects missing from the payload", async () => {
+ it("keeps workspace projects missing from the payload in place", async () => {
const member = await createWorkspaceMember({ role: "admin" });
const { project: first } = await createProjectFixture({
workspaceId: member.workspace.id,
@@ -231,21 +231,23 @@ describe("API integration: project reorder", () => {
workspaceId: member.workspace.id,
name: "First",
});
- const { project: second } = await createProjectFixture({
- workspaceId: member.workspace.id,
- name: "Second",
- });
+ // Deliberately in the middle: an archived project seeded last would be
+ // indistinguishable from one the controller shunted to the end.
const { project: archived } = await createProjectFixture({
workspaceId: member.workspace.id,
name: "Archived",
});
+ const { project: second } = await createProjectFixture({
+ workspaceId: member.workspace.id,
+ name: "Second",
+ });
mockAuthenticatedSession(member.user);
expect((await archiveRequest(archived.id)).status).toBe(200);
// The client never sees the archived project, so it cannot send it. The
- // controller has to keep it in the workspace ordering anyway.
+ // controller has to leave it where it is and slot the payload around it.
const response = await reorderRequest(member.workspace.id, [
{ id: second.id, position: 0 },
{ id: first.id, position: 1 },
@@ -262,8 +264,8 @@ describe("API integration: project reorder", () => {
const allProjects = await listProjects(member.workspace.id, true);
expect(allProjects.map((project) => project.id)).toEqual([
second.id,
- first.id,
archived.id,
+ first.id,
]);
expect(allProjects.map((project) => project.position)).toEqual([0, 1, 2]);
});