From f0337d2eba89fdb8ff8eff0caef69ad546b75215 Mon Sep 17 00:00:00 2001 From: Weves Date: Fri, 13 Oct 2023 13:34:51 -0700 Subject: [PATCH] Auto-delete unlinked connectors on creation of a new connector with the same name --- .../admin/connectors/ConnectorForm.tsx | 21 ++++++++++++++++++- web/src/lib/connector.ts | 7 +++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/web/src/components/admin/connectors/ConnectorForm.tsx b/web/src/components/admin/connectors/ConnectorForm.tsx index 5e30b1fcf..f2706d45c 100644 --- a/web/src/components/admin/connectors/ConnectorForm.tsx +++ b/web/src/components/admin/connectors/ConnectorForm.tsx @@ -8,7 +8,7 @@ import { ValidInputTypes, ValidSources, } from "@/lib/types"; -import { deleteConnectorIfExists } from "@/lib/connector"; +import { deleteConnectorIfExistsAndIsUnlinked } from "@/lib/connector"; import { FormBodyBuilder, RequireAtLeastOne } from "./types"; import { TextFormField } from "./Field"; import { linkCredential } from "@/lib/credential"; @@ -113,6 +113,25 @@ export function ConnectorForm({ const connectorConfig = Object.fromEntries( Object.keys(initialValues).map((key) => [key, values[key]]) ) as T; + + // best effort check to see if existing connector exists + // delete it if: + // 1. it exists + // 2. AND it has no credentials linked to it + // If the ^ are true, that means things have gotten into a bad + // state, and we should delete the connector to recover + const errorMsg = await deleteConnectorIfExistsAndIsUnlinked({ + source, + name: connectorName, + }); + if (errorMsg) { + setPopup({ + message: `Unable to delete existing connector - ${errorMsg}`, + type: "error", + }); + return; + } + const { message, isSuccess, response } = await submitConnector({ name: connectorName, source, diff --git a/web/src/lib/connector.ts b/web/src/lib/connector.ts index 5a2bfee98..e64a80cde 100644 --- a/web/src/lib/connector.ts +++ b/web/src/lib/connector.ts @@ -66,7 +66,7 @@ export async function runConnector( return null; } -export async function deleteConnectorIfExists({ +export async function deleteConnectorIfExistsAndIsUnlinked({ source, name, }: { @@ -80,7 +80,10 @@ export async function deleteConnectorIfExists({ (connector) => connector.source === source && (!name || connector.name === name) ); - if (matchingConnectors.length > 0) { + if ( + matchingConnectors.length > 0 && + matchingConnectors[0].credential_ids.length === 0 + ) { const errorMsg = await deleteConnector(matchingConnectors[0].id); if (errorMsg) { return errorMsg;