Files
wekan/models/lib/swimlaneSort.js
T
Lauri Ojansivu e3f17be14d Fix #3624: new_swimlane REST API appends the swimlane last (and accepts a sort)
POST /api/boards/:boardId/swimlanes set the new swimlane's sort to
board.swimlanes().length (a count). When the existing swimlanes' sort values
were not a contiguous 0..n-1 run (after deletions/reordering, or swimlanes
created top-first with decreasing sorts), the count collided with or preceded
existing sorts and the new swimlane appeared FIRST instead of last.

The endpoint now computes the sort with nextSwimlaneSort(): max(existing sort)+1
for a reliable append (0 on an empty board), and honors an optional explicit
`sort` in the request body (the enhancement the reporter asked for). The pure
logic lives in models/lib/swimlaneSort.js, unit tested (positive + negative) in
tests/swimlaneSort.test.cjs.

Thanks to tamasberesoebb and xet7.
2026-07-09 09:45:53 +03:00

30 lines
1.2 KiB
JavaScript

'use strict';
// #3624 "new_swimlane API sorting problem". POST /api/boards/:boardId/swimlanes
// set the new swimlane's sort to board.swimlanes().length (a COUNT). When the
// existing swimlanes' sort values are not a contiguous 0..n-1 run (after
// deletions, reordering, or swimlanes created top-first with decreasing sorts),
// the count collides with or precedes existing sorts, so the new swimlane could
// appear FIRST instead of last. The issue also asked to allow an explicit index.
//
// nextSwimlaneSort() returns the sort to use: the caller's requested sort when a
// finite number is supplied, otherwise max(existing sorts)+1 for a reliable
// append (0 when the board has no swimlanes yet). Meteor-free and unit tested.
function nextSwimlaneSort(existingSorts, requestedSort) {
if (
requestedSort !== undefined &&
requestedSort !== null &&
requestedSort !== '' &&
Number.isFinite(Number(requestedSort))
) {
return Number(requestedSort);
}
const nums = (Array.isArray(existingSorts) ? existingSorts : [])
.map(Number)
.filter(Number.isFinite);
if (nums.length === 0) return 0;
return Math.max(...nums) + 1;
}
module.exports = { nextSwimlaneSort };