feat: implement provider connection reordering on create, update, and delete
This commit is contained in:
parent
ed796d2724
commit
f2abcc6585
2 changed files with 66 additions and 10 deletions
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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 ============
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue