updateBug method

Future<Response> updateBug({
  1. required String title,
  2. required String reportId,
  3. required String description,
  4. required String type,
  5. required String priority,
  6. required String source,
  7. required String reporterId,
  8. required String appId,
  9. required String device,
  10. required String os,
  11. required String screenResolution,
  12. required String networkType,
  13. required String batteryLevel,
  14. required String appIdCall,
  15. required String reportingTokenCall,
  16. List<String?>? removedMediaId,
  17. List<File>? mediaFiles,
  18. File? crashLog,
})

Implementation

Future<Response> updateBug(
    {required String title,
    required String reportId,
      required String description,
      required String type,
      required String priority,
      required String source,
      required String reporterId,
      required String appId,
      required String device,
      required String os,
      required String screenResolution,
      required String networkType,
      required String batteryLevel,
      required String appIdCall,
      required String reportingTokenCall,
      List<String?>? removedMediaId,
      List<File>? mediaFiles,
      File? crashLog}) async {
  try {
    List<MultipartFile> mediaFileList = [];

    if (mediaFiles != null && mediaFiles.isNotEmpty) {
      for (File mediaFile in mediaFiles) {
        if (mediaFile.path.startsWith('http')) {
          continue; // Skip files that start with 'http'
        }
        String fileName = path.basename(mediaFile.path);
        String fileExtension = path.extension(mediaFile.path).substring(1);
        String contentType = 'application/octet-stream';

        if (fileExtension == 'jpg' || fileExtension == 'jpeg') {
          contentType = 'image/jpeg';
        } else if (fileExtension == 'png') {
          contentType = 'image/png';
        } else if (fileExtension == 'mp3') {
          contentType = 'audio/mpeg';
        }

        mediaFileList.add(await MultipartFile.fromFile(mediaFile.path,
            filename: fileName, contentType: MediaType.parse(contentType)));
      }
    }

    Map<String, dynamic> deviceMetadata = {
      'device': device,
      'os': os,
      'screenResolution': screenResolution,
      'networkType': networkType,
      'batteryLevel': batteryLevel,
    };

    FormData formData = FormData.fromMap({
      'title': title,
      'description': description,
      'type': type.toUpperCase(),
      'priority': priority.toUpperCase(),
      'source': source,
      'reporterId': reporterId,
      'appId': appIdCall,
      'deviceMetadata': jsonEncode(deviceMetadata),
      if (mediaFileList.isNotEmpty) 'newMediaFiles': mediaFileList,
      if(removedMediaId != null) 'mediaToRemoveIds': removedMediaId,
    });

    if (crashLog != null) {
      String crashLogFileName = path.basename(crashLog.path);
      String crashLogFileExtension =
      path.extension(crashLog.path).substring(1);
      String crashLogContentType = 'application/octet-stream';

      if (crashLogFileExtension == 'txt') {
        crashLogContentType = 'text/plain';
      }

      MultipartFile crashLogFile = await MultipartFile.fromFile(
        crashLog.path,
        filename: crashLogFileName,
        contentType: MediaType.parse(crashLogContentType),
      );

      formData.files.add(MapEntry('crashLog', crashLogFile));
    }

    Options options = Options(
      contentType: 'multipart/form-data',
      headers: {
        'Authorization': 'Bearer $reportingTokenCall',
      },
    );

    return _dio
        .patch<Map<String, dynamic>>(
      '${QuashConstants.baseUrl}${QuashConstants.updateBugEndPoint}$reportId',
      data: formData,
      options: options,
    )
        .then((value) async {
      return value;
    }).catchError((e){
      Fluttertoast.showToast(msg: 'An error occurred while updating bug  $e');
      return e;
    });
  } catch (e) {
    Fluttertoast.showToast(msg: 'An error occurred while updating bug  $e');
    rethrow;
  }
}