searchAndWriteToDB method

FutureOr<void> searchAndWriteToDB({
  1. required String apiEndPoint,
  2. Object? query,
  3. required String projectId,
  4. DateTime? lastSelectedDate,
  5. required Isar isar,
})

Implementation

FutureOr<void> searchAndWriteToDB({
  required String apiEndPoint, // API endpoint URL
  Object? query, // Query parameters for the API request
  required String projectId, // Project ID for filtering data
  DateTime? lastSelectedDate, // Last selected date for filtering data
  required Isar isar, // Isar database instance
}) async {
  try {
    final response = await _client.post(
      apiEndPoint,
      data: query,
    ); // Make a POST request to the API endpoint with the query parameters

    // Map the response data to the DashboardResponseModel
    final dashboardResponse = DashboardResponseModelMapper.fromMap(
      json.decode(response.data)[DSSEnums.responseData.toValue()],
    );

    if (dashboardResponse.data != null) {
      // Write transaction to delete existing data and insert new data
      isar.writeTxnSync(() {
        isar.dashboardResponses
            .where()
            .filter()
            .projectIdEqualTo(projectId)
            .visualizationCodeEqualTo(dashboardResponse.visualizationCode)
            .chartTypeEqualTo(dashboardResponse.chartType)
            .deleteAllSync(); // Delete existing data matching the project ID, visualization code, and chart type
      });

      final data =
          dashboardResponse; // Assign the dashboard response data to a local variable
      final chart =
          DashboardResponse(); // Create a new DashboardResponse instance
      chart.chartType = data.chartType; // Set chart type
      chart.hideHeaderDenomination =
          data.hideHeaderDenomination; // Set hide header denomination flag
      chart.lastSelectedDate =
          lastSelectedDate ?? DateTime.now(); // Set last selected date
      chart.visualizationCode =
          data.visualizationCode; // Set visualization code
      chart.projectId = projectId; // Set project ID
      chart.drillDownChartId =
          data.drillDownChartId; // Set drill down chart ID
      chart.hideInsights = data.hideInsights; // Set hide insights flag
      chart.showLabel = data.showLabel; // Set show label flag

      // Map the data plots to the DashboardChartData and DashboardPlot instances
      final dataPlots = data.data?.map((c) {
        final chartData = DashboardChartData();
        chartData.headerValue = c.headerValue;
        chartData.headerName = c.headerName;
        chartData.headerSymbol = c.headerSymbol;
        chartData.plots = c.plots?.map((p) {
          final plot = DashboardPlot();
          plot.label = p.label;
          plot.strValue = p.strValue;
          plot.name = p.name;
          plot.value = p.value;
          plot.symbol = p.symbol;
          return plot;
        }).toList();

        // Map the insight data to the Insight instance
        final insight = Insight();
        insight.value = c.insight?.value;
        insight.name = c.insight?.name;
        insight.colorCode = c.insight?.colorCode;
        insight.indicator = c.insight?.indicator;
        chartData.insight = insight;
        return chartData;
      }).toList();
      chart.data = dataPlots; // Set the data plots

      // Write transaction to insert the new chart data
      return isar.writeTxnSync(() {
        isar.dashboardResponses.putSync(chart);
      });
    }
  } on DioException catch (e) {
    // Handle Dio exceptions and log the error
    debugPrint(e.toString());
    AppLogger.instance.error(
      title: 'Dashboard Repository',
      message: '$e',
      stackTrace: e.stackTrace,
    );
    rethrow; // Rethrow the exception
  }
}