Skip to content

Commit

Permalink
Auto-delete unlinked connectors on creation of a new connector with t…
Browse files Browse the repository at this point in the history
…he same name
  • Loading branch information
Weves committed Oct 13, 2023
1 parent 17e00b1 commit f0337d2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
21 changes: 20 additions & 1 deletion web/src/components/admin/connectors/ConnectorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -113,6 +113,25 @@ export function ConnectorForm<T extends Yup.AnyObject>({
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<T>({
name: connectorName,
source,
Expand Down
7 changes: 5 additions & 2 deletions web/src/lib/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function runConnector(
return null;
}

export async function deleteConnectorIfExists({
export async function deleteConnectorIfExistsAndIsUnlinked({
source,
name,
}: {
Expand All @@ -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;
Expand Down

1 comment on commit f0337d2

@vercel
Copy link

@vercel vercel bot commented on f0337d2 Oct 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.