From f2abcc65856006204f4695ae82ef171f272182e8 Mon Sep 17 00:00:00 2001 From: Catalin Stanciu Date: Tue, 6 Jan 2026 22:14:10 +0200 Subject: [PATCH] feat: implement provider connection reordering on create, update, and delete --- .../dashboard/providers/[id]/page.js | 21 ++++++- src/lib/localDb.js | 55 ++++++++++++++++--- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/app/(dashboard)/dashboard/providers/[id]/page.js b/src/app/(dashboard)/dashboard/providers/[id]/page.js index 8324869..0fbb56a 100644 --- a/src/app/(dashboard)/dashboard/providers/[id]/page.js +++ b/src/app/(dashboard)/dashboard/providers/[id]/page.js @@ -158,17 +158,32 @@ export default function ProviderDetailPage() { const handleSwapPriority = async (conn1, conn2) => { if (!conn1 || !conn2) return; try { - // Swap priorities + // If they have the same priority, we need to ensure the one moving up + // gets a lower value than the one moving down. + // We use a small offset which the backend re-indexing will fix. + let p1 = conn2.priority; + let p2 = conn1.priority; + + if (p1 === p2) { + // If moving conn1 "up" (index decreases) + const isConn1MovingUp = connections.indexOf(conn1) > connections.indexOf(conn2); + if (isConn1MovingUp) { + p1 = conn2.priority - 0.5; + } else { + p1 = conn2.priority + 0.5; + } + } + await Promise.all([ fetch(`/api/providers/${conn1.id}`, { method: "PUT", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ priority: conn2.priority }), + body: JSON.stringify({ priority: p1 }), }), fetch(`/api/providers/${conn2.id}`, { method: "PUT", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ priority: conn1.priority }), + body: JSON.stringify({ priority: p2 }), }), ]); await fetchConnections(); diff --git a/src/lib/localDb.js b/src/lib/localDb.js index bc65bb9..63120b3 100644 --- a/src/lib/localDb.js +++ b/src/lib/localDb.js @@ -194,7 +194,10 @@ export async function createProviderConnection(data) { db.data.providerConnections.push(connection); await db.write(); - + + // Reorder to ensure consistency + await reorderProviderConnections(data.provider); + return connection; } @@ -204,16 +207,24 @@ export async function createProviderConnection(data) { export async function updateProviderConnection(id, data) { const db = await getDb(); const index = db.data.providerConnections.findIndex(c => c.id === id); - + if (index === -1) return null; - + + const providerId = db.data.providerConnections[index].provider; + db.data.providerConnections[index] = { ...db.data.providerConnections[index], ...data, updatedAt: new Date().toISOString(), }; - + await db.write(); + + // Reorder if priority was changed + if (data.priority !== undefined) { + await reorderProviderConnections(providerId); + } + return db.data.providerConnections[index]; } @@ -223,15 +234,45 @@ export async function updateProviderConnection(id, data) { export async function deleteProviderConnection(id) { const db = await getDb(); const index = db.data.providerConnections.findIndex(c => c.id === id); - + if (index === -1) return false; - + + const providerId = db.data.providerConnections[index].provider; + db.data.providerConnections.splice(index, 1); await db.write(); - + + // Reorder to fill gaps + await reorderProviderConnections(providerId); + return true; } +/** + * Reorder provider connections to ensure unique, sequential priorities + */ +export async function reorderProviderConnections(providerId) { + const db = await getDb(); + if (!db.data.providerConnections) return; + + const providerConnections = db.data.providerConnections + .filter(c => c.provider === providerId) + .sort((a, b) => { + // Sort by priority first + const pDiff = (a.priority || 0) - (b.priority || 0); + if (pDiff !== 0) return pDiff; + // Use updatedAt as tie-breaker (newer first) + return new Date(b.updatedAt || 0) - new Date(a.updatedAt || 0); + }); + + // Re-assign sequential priorities + providerConnections.forEach((conn, index) => { + conn.priority = index + 1; + }); + + await db.write(); +} + // ============ Model Aliases ============ /**