mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
feat: implement provider connection reordering on create, update, and delete
This commit is contained in:
@@ -158,17 +158,32 @@ export default function ProviderDetailPage() {
|
|||||||
const handleSwapPriority = async (conn1, conn2) => {
|
const handleSwapPriority = async (conn1, conn2) => {
|
||||||
if (!conn1 || !conn2) return;
|
if (!conn1 || !conn2) return;
|
||||||
try {
|
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([
|
await Promise.all([
|
||||||
fetch(`/api/providers/${conn1.id}`, {
|
fetch(`/api/providers/${conn1.id}`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ priority: conn2.priority }),
|
body: JSON.stringify({ priority: p1 }),
|
||||||
}),
|
}),
|
||||||
fetch(`/api/providers/${conn2.id}`, {
|
fetch(`/api/providers/${conn2.id}`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ priority: conn1.priority }),
|
body: JSON.stringify({ priority: p2 }),
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
await fetchConnections();
|
await fetchConnections();
|
||||||
|
|||||||
+48
-7
@@ -194,7 +194,10 @@ export async function createProviderConnection(data) {
|
|||||||
|
|
||||||
db.data.providerConnections.push(connection);
|
db.data.providerConnections.push(connection);
|
||||||
await db.write();
|
await db.write();
|
||||||
|
|
||||||
|
// Reorder to ensure consistency
|
||||||
|
await reorderProviderConnections(data.provider);
|
||||||
|
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,16 +207,24 @@ export async function createProviderConnection(data) {
|
|||||||
export async function updateProviderConnection(id, data) {
|
export async function updateProviderConnection(id, data) {
|
||||||
const db = await getDb();
|
const db = await getDb();
|
||||||
const index = db.data.providerConnections.findIndex(c => c.id === id);
|
const index = db.data.providerConnections.findIndex(c => c.id === id);
|
||||||
|
|
||||||
if (index === -1) return null;
|
if (index === -1) return null;
|
||||||
|
|
||||||
|
const providerId = db.data.providerConnections[index].provider;
|
||||||
|
|
||||||
db.data.providerConnections[index] = {
|
db.data.providerConnections[index] = {
|
||||||
...db.data.providerConnections[index],
|
...db.data.providerConnections[index],
|
||||||
...data,
|
...data,
|
||||||
updatedAt: new Date().toISOString(),
|
updatedAt: new Date().toISOString(),
|
||||||
};
|
};
|
||||||
|
|
||||||
await db.write();
|
await db.write();
|
||||||
|
|
||||||
|
// Reorder if priority was changed
|
||||||
|
if (data.priority !== undefined) {
|
||||||
|
await reorderProviderConnections(providerId);
|
||||||
|
}
|
||||||
|
|
||||||
return db.data.providerConnections[index];
|
return db.data.providerConnections[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,15 +234,45 @@ export async function updateProviderConnection(id, data) {
|
|||||||
export async function deleteProviderConnection(id) {
|
export async function deleteProviderConnection(id) {
|
||||||
const db = await getDb();
|
const db = await getDb();
|
||||||
const index = db.data.providerConnections.findIndex(c => c.id === id);
|
const index = db.data.providerConnections.findIndex(c => c.id === id);
|
||||||
|
|
||||||
if (index === -1) return false;
|
if (index === -1) return false;
|
||||||
|
|
||||||
|
const providerId = db.data.providerConnections[index].provider;
|
||||||
|
|
||||||
db.data.providerConnections.splice(index, 1);
|
db.data.providerConnections.splice(index, 1);
|
||||||
await db.write();
|
await db.write();
|
||||||
|
|
||||||
|
// Reorder to fill gaps
|
||||||
|
await reorderProviderConnections(providerId);
|
||||||
|
|
||||||
return true;
|
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 ============
|
// ============ Model Aliases ============
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user