getUserReports method

  1. @override
Future<List<Report>> getUserReports(
  1. String businessId
)
override

Implementation

@override
Future<List<Report>> getUserReports(String businessId) async {
  if (businessId.isEmpty) {
    throw Exception(
      'Business Id is required, please provide a valid businessId',
    );
  }

  if (user == null) {
    throw Exception('User is not authenticated, please sign in to continue');
  }

  if (!isReady) {
    throw Exception(
        'Report client is not ready for usage, call initialise before attempting to make this call');
  } else {
    logger.info(
      this,
      'getting user reports for businessId: $businessId, and user ${user?.uid}',
    );

    final String url =
        '$baseUrl/Get${settings.provider}Dashboards/businessId=$businessId';

    logger.debug(this, 'getting user reports from $url');

    final LittleFishHttpClient http = LittleFishHttpClient();

    final response =
        await http.get(url: url, user: authService.user, additionalHeaders: {
      'x-api-key': apiKey,
    });

    logger.debug(
      this,
      'response status code: ${response?.statusCode}, from getting reports, for business: $businessId, and user:${user?.uid}, from url: $url',
    );

    if ((response?.isOk ?? false)) {
      logger.debug(this, 'got user reports from $url successfully');
      final List<Report> reports = [];

      final items = response?.data!["data"] ?? [];

      for (final item in items) {
        reports.add(Report.fromJson(item));
      }

      logger.info(
        this,
        'got ${reports.length} reports for $businessId, user: ${user?.uid}',
      );
      return reports;
    } else {
      logger.error(
        this,
        'failed to get user reports from $url, for business: $businessId, and user:${user?.uid}, with error: ${response?.statusCode}, and message: ${response?.statusMessage}',
      );

      throw Exception(
        'failed to get user reports from $url, for business: $businessId, and user:${user?.uid}, with error: ${response?.statusCode}, and message: ${response?.statusMessage}',
      );
    }
  }
}