getConversationDetails function

Future<Conversation> getConversationDetails(
  1. PapercupsProps p,
  2. Conversation conversation,
  3. PapercupsCustomer customer,
  4. Function sc, {
  5. Client? client,
})

This function will get the conversation details that we need in order to join the room. The most important detail is the ID, and this will return a new conversation.

Implementation

Future<Conversation> getConversationDetails(
  PapercupsProps p,
  Conversation conversation,
  PapercupsCustomer customer,
  Function sc, {
  Client? client,
}) async {
  client ??= Client();
  Conversation conv;

  try {
    var res = await client.post(
      Uri.parse("https://${p.baseUrl}/api/conversations"),
      headers: {
        "content-type": "application/json",
      },
      body: jsonEncode(
        {
          "conversation": {
            "account_id": p.accountId,
            "customer_id": customer.id,
          },
        },
      ),
    );
    var data = jsonDecode(res.body)["data"];
    conv = Conversation(
      id: data["id"],
      customerId: data["customer_id"],
      accountId: data["account_id"],
      asigneeId: data["asignee_id"],
      createdAt: data["created_at"],
      read: data["read"],
      messages: conversation.messages,
    );
    sc(conv);
  } catch (e) {
    rethrow;
  }
  client.close();
  return conv;
}