Skip to main content

Create Consultation

Create Consultation Example

For creating a consultation you have to use the createConsultation method and provide the data needed in consultation creation using the Consultation class

class Consultation {
var userId: Int
var question: String
var medium: String
var mediaIds: [String]?
var parentConsultationId: Int?
var consultationCategoryId: Int?
// ...
}

Consider that the required fields are question, medium and userId

The accepted values for medium are: chat, gsm, voip, video

The parentConsultationId field is optional and used in case the consultation being created is related to a previous consultation, then you have to add the previous consultation id

The mediaIds field is optional and used in case of adding attachments to the consultation

consultationCategoryId is optional and used to specify the category of the consultation.

Before the consultation example, let us explain the use of mediaIds field

In some cases the consultation might include some media files (Images or PDFs) so there is a flow of uploading these files and add their IDs to the createConsultation method

We have the uploadMedia method that could be used to upload the files:

ApiService.uploadMedia(
jsonFile: Data,
type: String,
completion: @escaping (Media?, Data?, Error?) -> Void
)

In the example we are using the UIDocumentPickerViewController and UIImagePickerController along with the needed delegates UIDocumentPickerDelegate and UIImagePickerControllerDelegate

Check the code below:

import AltibbiTelehealth

// From documentPicker of UIDocumentPickerDelegate we can get the picked file this way
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let fileURL = urls.first else { return }
if fileURL.pathExtension.lowercased() == "pdf" {
do {
let pdfData = try Data(contentsOf: fileURL)
handleFileUpload(data: pdfData, type: "pdf")
} catch {
print("Error converting PDF to Data: \(error)")
}
}
}

// From the imagePickerController of UIImagePickerControllerDelegate we can get the picked image this way
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
picker.dismiss(animated: true, completion: nil)

if let pickedImage = info[.originalImage] as? UIImage {
if let imageData = pickedImage.jpegData(compressionQuality: 0.5) {
handleFileUpload(data: imageData, type: "jpg")
}
}
}

// Handle File Upload
func handleFileUpload(data: Data, type: String) -> Void {
ApiService.uploadMedia(jsonFile: data, type: type, completion: {media, failure, error in
if let error = error {
print("Data Error: \(String(describing: error))")
} else if let failure = failure {
// Handle failure
} else {
if let media = media {
// For creating consultation you could have an array of String and append the id from the media object
self.mediaIds.append(media.id!)
}
}
})
}

We have the struct Media to decode the response of the uploadMedia method

struct Media {
var id: String?
var type: String?
var name: String?
var path: String?
var ext: String?
var size: Int?
var createdAt: String?
var url: String?
}

In the code snippet above we just added the id of the uploaded file directly to the mediaIds but in case of showing the uploaded files consider using the Media struct and use the url to show the file

Creating the consultation

Check the example

import AltibbiTelehealth

var mediaIds: [String] = []

/**
/ ... /
* Consider the upload functionality is done here and appended some ids
/ ... /
**/

let consultation = Consultation(
userId: 999999999,
question: "I want to consult a doctor on ...",
medium: "chat",
mediaIds: mediaIds
)

ApiService.createConsultation(consultation: consultation, completion: {createdConsultation, failure, error in
if let error = error {
print("Data Error: \(String(describing: error))")
} else if let failure = failure {
// Handle failure
} else {
if let createdConsultation = createdConsultation {
print("Consultation ID: \(String(describing: createdConsultation.consultationId))")
// You can start the socket now
}
}
})

Responses

Status 201

Success

{
"id": 123,
"user_id": 999999999,
"question": "I want to consult a doctor on ...",
"medium": "chat",
"media_ids": [],
"status": "new"
}

Status 401

UnauthorizedHttpException represents an Unauthorized HTTP exception with status code 401

{
"name": "Unauthorized",
"message": "Your request was made with invalid credentials",
"code": "0",
"status": "401",
"type": "yii\\web\\UnauthorizedHttpException"
}

Status 403

Duplicate request

{
"name": "Forbidden",
"message": "duplicate request OR Current user cant access this page",
"code": "0",
"status": "403",
"type": "yii\\web\\ForbiddenHttpException"
}

Status 422

Data Validation Failed

[
{
"field": "string",
"message": "string"
}
]