createConsultation method

Future<Consultation> createConsultation({
  1. required String question,
  2. required Medium medium,
  3. required int userID,
  4. List<String>? mediaIDs,
  5. String? followUpId,
  6. String? forceWhiteLabelingPartnerName,
})

Creates a new consultation with the provided question, medium, userID, and optional mediaIDs. Returns the created consultation object if the API call is successful.

Implementation

Future<Consultation> createConsultation(
    {required String question,
    required Medium medium,
    required int userID,
    List<String>? mediaIDs,
    String? followUpId,
    String? forceWhiteLabelingPartnerName}) async {
  if (!Medium.values.contains(medium)) {
    throw Exception('Invalid medium value');
  }
  final Map<String, dynamic> body = {
    "question": question,
    "medium": medium.toString().split('.').last,
    "user_id": userID,
    "media_ids": mediaIDs,
    "expand":
        "pusherAppKey,parentConsultation,consultations,user,media,pusherChannel,"
            "chatConfig,chatHistory,voipConfig,videoConfig,recommendation"
  };
  if (followUpId != null) {
    body['parent_consultation_id'] = followUpId;
  }
  if (forceWhiteLabelingPartnerName != null && forceWhiteLabelingPartnerName.length > 3) {
    body['question'] = "${body['question']} ~$forceWhiteLabelingPartnerName~";
  }
  final response =
      await callApi(endpoint: 'consultations', method: 'post', body: body);

  if (response.statusCode == 201) {
    final responseData = json.decode(response.body);
    final createdConsultation = Consultation.fromJson(responseData);
    return createdConsultation;
  } else {
    throw Exception(response);
  }
}