Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,98 @@
|
||||
"use strict";
|
||||
|
||||
// adpanel:planner-reorder — pure helpers for the planner board's two
|
||||
// concurrency fixes:
|
||||
//
|
||||
// 1. computeRelativeReorder — the decision core of the drag-reorder route.
|
||||
// The old protocol shipped the client's ABSOLUTE ordered id list and the
|
||||
// server appended every id it had not been told about to the END of the
|
||||
// queue, so an item created by a teammate after the dragger's last board
|
||||
// render was silently demoted to last priority — and since the
|
||||
// "planner_next_to_agent" scheduler task consumes the top active item by
|
||||
// sort_order, the agent executed a stale priority. The wire contract is
|
||||
// now a RELATIVE MOVE: the client names the dragged item plus the
|
||||
// sibling it landed under (aboveId) and/or the sibling it landed above
|
||||
// (belowId), and the server splices the item between those anchors in
|
||||
// ITS OWN current order. The server stays the source of truth; an item a
|
||||
// client never saw keeps its position (a case the absolute protocol made
|
||||
// impossible to express).
|
||||
//
|
||||
// 2. plannerUpdatedAtMatches — strict epoch-ms equality for the Compose
|
||||
// form's optimistic-concurrency precondition: the client echoes the
|
||||
// updated_at it loaded and the PUT route rejects the write with 409 when
|
||||
// the row has changed since (a teammate's concurrent edit), instead of
|
||||
// silently applying last-writer-wins.
|
||||
|
||||
// Result codes (mapped to HTTP by the route):
|
||||
// missing_item -> 404 'Planner item not found'
|
||||
// invalid_neighbors -> 400 'That move is not valid for the current board.
|
||||
// The board will refresh.'
|
||||
// anchor_missing -> 409 'planner_order_changed'
|
||||
//
|
||||
// Semantics (currentIds is the server's ordered ACTIVE ids):
|
||||
// - itemId must be present, else missing_item.
|
||||
// - itemId must differ from both anchors, else invalid_neighbors.
|
||||
// - Both anchors null: a valid "move" only for a single-item board (the
|
||||
// order is unchanged); with more than one active item the payload cannot
|
||||
// describe a position, so invalid_neighbors.
|
||||
// - Otherwise: if aboveId is a finite number it must still exist in the
|
||||
// current order (else anchor_missing — the board changed under the user,
|
||||
// e.g. the anchor was completed or deleted by a teammate) and itemId is
|
||||
// inserted immediately AFTER it; else if belowId is a finite number the
|
||||
// same check applies and itemId is inserted immediately BEFORE it. When
|
||||
// both anchors are provided aboveId wins and belowId is informational.
|
||||
//
|
||||
// Never mutates its inputs; returns the new order as a fresh array.
|
||||
function computeRelativeReorder(currentIds, itemId, aboveId, belowId) {
|
||||
const list = Array.isArray(currentIds) ? currentIds : [];
|
||||
if (!Number.isFinite(itemId) || !list.includes(itemId)) {
|
||||
return { ok: false, code: "missing_item" };
|
||||
}
|
||||
|
||||
const hasAbove = Number.isFinite(aboveId);
|
||||
const hasBelow = Number.isFinite(belowId);
|
||||
if (aboveId === itemId || belowId === itemId) {
|
||||
return { ok: false, code: "invalid_neighbors" };
|
||||
}
|
||||
if (!hasAbove && !hasBelow) {
|
||||
if (list.length === 1) {
|
||||
return { ok: true, order: list.slice() };
|
||||
}
|
||||
return { ok: false, code: "invalid_neighbors" };
|
||||
}
|
||||
|
||||
const withoutItem = list.filter((id) => id !== itemId);
|
||||
if (hasAbove) {
|
||||
if (!list.includes(aboveId)) {
|
||||
return { ok: false, code: "anchor_missing" };
|
||||
}
|
||||
withoutItem.splice(withoutItem.indexOf(aboveId) + 1, 0, itemId);
|
||||
} else {
|
||||
if (!list.includes(belowId)) {
|
||||
return { ok: false, code: "anchor_missing" };
|
||||
}
|
||||
withoutItem.splice(withoutItem.indexOf(belowId), 0, itemId);
|
||||
}
|
||||
return { ok: true, order: withoutItem };
|
||||
}
|
||||
|
||||
// Strict epoch-ms equality between a row value (mysql2 hands TIMESTAMP back
|
||||
// as a Date; a string is equally legal) and the client-echoed value (the ISO
|
||||
// string the board JSON carried). Anything unparseable, NaN, null or
|
||||
// undefined on either side is a MISMATCH, never a pass: the precondition must
|
||||
// fail closed, because a client that cannot name the version it saw cannot
|
||||
// prove it saw the current one. Epoch comparison makes the check immune to
|
||||
// timezone/serialization differences between Date and ISO string.
|
||||
function plannerUpdatedAtMatches(rowValue, expectedValue) {
|
||||
if (rowValue === null || rowValue === undefined) return false;
|
||||
if (expectedValue === null || expectedValue === undefined) return false;
|
||||
const rowMs = new Date(rowValue).getTime();
|
||||
const expectedMs = new Date(expectedValue).getTime();
|
||||
if (!Number.isFinite(rowMs) || !Number.isFinite(expectedMs)) return false;
|
||||
return rowMs === expectedMs;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
computeRelativeReorder,
|
||||
plannerUpdatedAtMatches,
|
||||
};
|
||||
Reference in new issue
Block a user