Auto-delete unlinked connectors on creation of a new connector with the same name

This commit is contained in:
Weves 2023-10-13 13:34:51 -07:00 committed by Chris Weaver
parent 17e00b186e
commit f0337d2eba
2 changed files with 25 additions and 3 deletions

View File

@ -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<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,

View File

@ -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;