From fef807318dccdbce55fb642163e847c31b8d9435 Mon Sep 17 00:00:00 2001 From: Husain Baghwala Date: Sat, 12 Jul 2025 18:18:09 +0530 Subject: [PATCH] feat: add document ID cleanup from configurations during deletion --- src/controllers/RagController.js | 3 ++ src/db_services/ConfigurationServices.js | 64 +++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/controllers/RagController.js b/src/controllers/RagController.js index e1053535..448be35f 100644 --- a/src/controllers/RagController.js +++ b/src/controllers/RagController.js @@ -3,6 +3,7 @@ import embeddings from '../services/langchainOpenai.js'; import { queryPinecone } from '../db_services/pineconeDbservice.js'; import rag_parent_data from '../db_services/rag_parent_data.js'; import queue from '../services/queue.js'; +import configurationServices from '../db_services/ConfigurationServices.js'; import { genrateToken } from '../utils/ragUtils.js'; import { sendRagUpdates } from '../services/alertingService.js'; const QUEUE_NAME = process.env.RAG_QUEUE || 'rag-queue'; @@ -107,6 +108,8 @@ export const delete_doc = async (req, res, next) => { const orgId = embed ? embed.org_id : req.profile.org.id; // const userId = req.profile.user.id; const { id } = req.params; + // Remove doc id from configuration collections before deleting the document + await configurationServices.removeDocIdFromConfigs(id, orgId); const result = await rag_parent_data.deleteDocumentById(id); const nestedDocs = await rag_parent_data.getDocumentsByQuery({ 'source.nesting.parentDocId': id }); await rag_parent_data.deleteDocumentsByQuery({ 'source.nesting.parentDocId': id }); diff --git a/src/db_services/ConfigurationServices.js b/src/db_services/ConfigurationServices.js index 7c5b7ccb..b030002a 100644 --- a/src/db_services/ConfigurationServices.js +++ b/src/db_services/ConfigurationServices.js @@ -288,6 +288,66 @@ const getBridgeByUrlSlugname = async (url_slugName) => { } }; +// Remove document id from doc_ids arrays in configuration and version collections +const removeDocIdFromConfigs = async (docId, orgId) => { + try { + console.log(`Attempting to remove docId: ${docId} from configurations with orgId: ${orgId}`); + + // First, find documents that contain this ID to verify they exist + const configsWithId = await configurationModel.find({ doc_ids: docId }).lean(); + const versionsWithId = await versionModel.find({ doc_ids: docId }).lean(); + + console.log(`Found ${configsWithId.length} configs and ${versionsWithId.length} versions with docId`); + if (configsWithId.length > 0) { + console.log('Sample config:', JSON.stringify({ + _id: configsWithId[0]._id, + org_id: configsWithId[0].org_id, + doc_ids: configsWithId[0].doc_ids + })); + } + + if (versionsWithId.length > 0) { + console.log('Sample version:', JSON.stringify({ + _id: versionsWithId[0]._id, + org_id: versionsWithId[0].org_id, + doc_ids: versionsWithId[0].doc_ids + })); + } + + // Try a more direct approach with explicit string conversion + const stringId = String(docId); + console.log(`Using string ID: ${stringId}`); + + // Get the MongoDB collections directly + const configCollection = configurationModel.collection; + const versionCollection = versionModel.collection; + + // Use direct MongoDB driver calls + const resConfig = await configCollection.updateMany( + { doc_ids: stringId }, + { $pull: { doc_ids: stringId } } + ); + + const resVersion = await versionCollection.updateMany( + { doc_ids: stringId }, + { $pull: { doc_ids: stringId } } + ); + + console.log('Update results:', { + configResult: JSON.stringify(resConfig), + versionResult: JSON.stringify(resVersion) + }); + + const removedFromConfigs = resConfig?.modifiedCount ?? resConfig?.nModified ?? 0; + const removedFromVersions = resVersion?.modifiedCount ?? resVersion?.nModified ?? 0; + const totalRemoved = removedFromConfigs + removedFromVersions; + console.log(`Removed doc id ${docId} from ${removedFromConfigs} configuration docs and ${removedFromVersions} version docs (total ${totalRemoved}).`); + return { success: true, removedFromConfigs, removedFromVersions, totalRemoved }; + } catch (error) { + console.error("Error removing doc id from configs =>", error); + return { success: false, error: error?.message || 'something went wrong!!' }; + } +}; export default { deleteBridge, @@ -304,5 +364,7 @@ export default { removeActionInBridge, getBridges, getBridgeNameById, - getBridgeByUrlSlugname + getBridgeByUrlSlugname, + removeDocIdFromConfigs, + };