buildConversation method

Future buildConversation(
  1. dynamic conversationObject
)

Implementation

Future<dynamic> buildConversation(dynamic conversationObject) async {
  Map<String, dynamic> conversationData;
  Completer completer = Completer();
  if (conversationObject is String) {
    conversationData = jsonDecode(conversationObject);
  } else if (conversationObject is Map<String, dynamic>) {
    conversationData = conversationObject;
  } else {
    throw ArgumentError(
        'conversationObject must be a JSON string or a Map<String, dynamic>');
  }

  Map<String, dynamic> conversationDetail = {};

  if (conversationData["messageMetadata"] != null) {
    conversationDetail["conversationMetadata"] =
        conversationData["messageMetadata"];
  }
  if (conversationData["agentIds"] != null) {
    conversationDetail["agentIds"] = conversationData["agentIds"];
    if (conversationDetail["skipRouting"] == null) {
      conversationDetail["skipRouting"] = "true";
    }
  }
  if (conversationData["botIds"] != null) {
    conversationDetail["botIds"] = conversationData["botIds"];
    if (conversationDetail["skipRouting"] == null) {
      conversationDetail["skipRouting"] = "true";
    }
  }
  if (conversationData["conversationAssignee"] != null) {
    conversationDetail["assignee"] = conversationData["conversationAssignee"];
    if (conversationDetail["skipRouting"] == null) {
      conversationDetail["skipRouting"] = "true";
    }
  }
  if (conversationData["clientConversationId"] != null) {
    conversationDetail["clientGroupId"] =
        conversationData["clientConversationId"];
  }
  if (conversationData["conversationTitle"] != null) {
    conversationDetail["defaultGroupName"] =
        conversationData["conversationTitle"];
  }

  void createConversationCallBackFunction(js.JsObject response) {
    completer.complete(response);
  }

  js.context['createConversationCallBack'] = createConversationCallBackFunction;

  String jsCode = '''
          Kommunicate.startConversation(${jsonEncode(conversationDetail)}, function (response) {
          console.log("new conversation created");
          createConversationCallBack(response);
          });
      ''';

  await js.context.callMethod('eval', [jsCode]);
  return completer.future;
}