Repository: Mintplex-Labs/anything-llm
File: server/models/systemSettings.js (JavaScript)

Code before:
      );
      if (!originalExists) {
        return; // Skip - original connection doesn't exist, update may have already been applied
      }

      // Check if new database_id conflicts with existing ones (excluding the original)
      const otherDbIds = updatedConnections
        .filter((conn) => conn.database_id !== originalId)
        .map((conn) => conn.database_id);
      if (otherDbIds.includes(newId)) {
        return; // Skip - new name conflicts with existing connection, reject rather than auto-rename
      }

      // Remove the old connection
      updatedConnections = updatedConnections.filter(
        (conn) => conn.database_id !== originalId
      );

      // Add the updated connection
      updatedConnections.push({
        engine: update.engine,
        database_id: newId,
        connectionString: update.connectionString,
      });
    });

  // Next add all 'action:add' candidates into the updatedConnections; We DO NOT validate the connection strings.
  // but we do validate their database_id is unique.
  // Get the list of database_ids that already exist in the backend (before any modifications in this batch)
  const existingDbIds = existingConnections.map((conn) => conn.database_id);

  updates
    .filter((conn) => conn.action === "add")
    .forEach((update) => {
      if (!update.connectionString) return; // invalid connection string

      const slugifiedId = slugify(update.database_id);

      // If this connection already exists in the backend, skip it entirely.
      // This handles the case where the frontend re-sends previously saved connections
      // that still have action: "add" in their local state.
      if (existingDbIds.includes(slugifiedId)) {
        return; // Skip - already exists in backend
      }

      // Check against current updatedConnections for duplicates within this batch
      const currentDbIds = updatedConnections.map((conn) => conn.database_id);
      if (currentDbIds.includes(slugifiedId)) {
        return; // Skip - duplicate within same batch, reject rather than auto-rename
      }


Review comment (refactor):
This logic here is pretty hard to follow. Is there any way we can simplify this? It seems overly complex for what the code is trying to accomplish. The code should be self explanatory without being too verbose with comments here.

Code after:

  simpleSSO: {
    /**
     * Gets the no login redirect URL. If the conditions below are not met, this will return null.
     * - If simple SSO is not enabled.
     * - If simple SSO login page is not disabled.
     * - If the no login redirect is not a valid URL or is not set.
     * @returns {string | null}
     */
    noLoginRedirect: () => {
      if (!("SIMPLE_SSO_ENABLED" in process.env)) return null; // if simple SSO is not enabled, return null
      if (!("SIMPLE_SSO_NO_LOGIN" in process.env)) return null; // if the no login config is not set, return null
      if (!("SIMPLE_SSO_NO_LOGIN_REDIRECT" in process.env)) return null; // if the no login redirect is not set, return null

      try {
        let url = new URL(process.env.SIMPLE_SSO_NO_LOGIN_REDIRECT);
        return url.toString();
      } catch {}

      // if the no login redirect is not a valid URL or is not set, return null
      return null;
    },
  },
};

/**
 * Merges SQL connection updates from the frontend with existing backend connections.
 * Processes three types of actions: "remove", "update", and "add".
 *
 * @param {Array<Object>} existingConnections - Current connections stored in the database
 * @param {Array<Object>} updates - Connection updates from frontend, each with an action property
 * @returns {Array<Object>} - The merged connections array
 */
function mergeConnections(existingConnections = [], updates = []) {
  const connectionsMap = new Map(
    existingConnections.map((conn) => [conn.database_id, conn])
  );

  for (const update of updates) {
    const {
      action,
      database_id,
      originalDatabaseId,
      connectionString,
      engine,
    } = update;

    switch (action) {
      case "remove": {
        connectionsMap.delete(database_id);
        break;