getLinkedTerminalsByBusinessId method

  1. @override
Future<List<Terminal>> getLinkedTerminalsByBusinessId({
  1. required String businessId,
  2. int skip = 0,
  3. int take = 20,
})
override

Implementation

@override
Future<List<Terminal>> getLinkedTerminalsByBusinessId({
  required String businessId,
  int skip = 0,
  int take = 20,
}) async {
  final manager = this as TerminalManager;
  if (!isReady) {
    logger.warning(
      this,
      'Terminal Manager Service not initialised, call initialise first',
    );

    throw Exception(
      'Terminal Manager Service not initialised, call initialise first',
    );
  }

  if (!authService.isAuthenticated) {
    logger.warning(
      this,
      'user not authenticated, getLinkedTerminalsByBusinessId cancelled for business: $businessId',
    );

    throw Exception(
      'user not authenticated, getLinkedTerminalsByBusinessId cancelled for business: $businessId',
    );
  }

  final lfHttpClient = LittleFishHttpClient();

  final uri = Uri.parse(manager._endpoint).replace(
    queryParameters: {
      'BusinessId': businessId,
      'skip': skip.toString(),
      'take': take.toString(),
      'IncludeTotal': 'true',
    },
  );

  final result = await lfHttpClient.get(
    url: uri.toString(),
    user: authService.user,
  );

  if (result!.statusCode == HttpStatus.ok) {
    final List<Terminal> terminals = [];
    if (result.data is Map<String, dynamic>) {
      final resultMap = result.data;
      if (resultMap.containsKey('items')) {
        final mapList = resultMap['items'];
        if (mapList is List) {
          for (var item in mapList) {
            try {
              final terminal = terminalFromJson(item);
              if (terminal.id.isNotEmpty) {
                terminals.add(terminal);
              }
            } catch (e) {
              debugPrint('Error parsing terminal: $e');
            }
          }
        }
      }

      if (resultMap.containsKey('totalRecords')) {
        manager.totalDevices = resultMap['totalRecords'] ?? -1;
      }
    }

    return terminals;
  } else {
    final errorText =
        'Failed to get linked terminals for business: $businessId, ${result.data}';
    logger.error(
      this,
      errorText,
      error: errorText,
      stackTrace: StackTrace.current,
    );

    throw Exception(
      'Failed to get linked terminals for business: $businessId, ${result.data}',
    );
  }
}