How to Delete Chat session and Messages associated with session in a different schema

Database & APIs

I am building a Chat with Pdf app. I have three schema.

  1. Users schema:

  1. chat_sessions: This collection is to create chat sessions that user can come back to after they have successfully upserted PDFs or website into the knowledge base. This saves users from having to upsert again if they want to ask more questions about a PDF that they have previously upserted and chatted with. The most important fields are: linked_user (to control access), query_namespace(as a filter when querying for information), upserted_date (to sort previous chat sessions).

  1. flowise_messages: This is to remember what questions are asked and responses are generated during a chat session. I named it “flowise_message” because “message” is already in user for another use case in my Firebase, you can simply name it “message” if you want to. The most important fields are: linked_user (to control access), linked_session (control what messages to appear for each chat sessions), is_user (to identify if this is question sent by user or a response from Flowise).

    I want to delete the a chat_session and all the flowise_messages related to it when I click the delete icon.

    Note: I am Query collection (chat_sessions) on the list of saved chats. I am also referencing flowise_messages collection but FlutterFlow is not allowing me to set the variable i.e. linked_session Docment Reference (chat_session) in flowise_messages with the sessionRef.

What have you tried so far?

I have tried Firestore document delete first with sessionReference and in the next I am choosing flowise_message but unable to set its variable. I have tried custom action.

// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

Future<String> deleteChatSessionAndMessages(
    DocumentReference sessionRef) async {
  // Add your function code here!
  try {
    // Check if the session reference is valid
    if (sessionRef == null) {
      print('Error: Session reference is null');
      return 'Error: Invalid session reference';
    }

    print('Attempting to delete chat session: ${sessionRef.path}');

    // Delete the chat session
    await sessionRef.delete();
    print('Chat session deleted successfully');

    // Query for associated messages
    print('Querying for associated messages');
    QuerySnapshot messagesSnapshot;
    try {
      messagesSnapshot = await FirebaseFirestore.instance
          .collection('flowise_messages')
          .where('linked_session', isEqualTo: sessionRef)
          .get();
      print('Found ${messagesSnapshot.docs.length} associated messages');
    } catch (queryError) {
      print('Error querying associated messages: $queryError');
      return 'Error querying associated messages';
    }

    // Delete associated messages
    if (messagesSnapshot.docs.isNotEmpty) {
      print('Deleting associated messages');
      for (var doc in messagesSnapshot.docs) {
        try {
          await doc.reference.delete();
        } catch (deleteError) {
          print('Error deleting message ${doc.id}: $deleteError');
        }
      }
      print('All associated messages deleted');
    }

    return 'Chat session and associated messages deleted successfully.';
  } catch (error) {
    print('Unexpected error in deleteChatSessionAndMessages: $error');
    return 'Unexpected error occurred: $error';
  }
}
Did you check FlutterFlow's Documentation for this topic?
Yes
2 replies