getOrgs method

  1. @override
Future<List<OrgUser>> getOrgs()
override

Retrieves all the orgs the current user is a part of.

Implementation

@override
Future<List<OrgUser>> getOrgs() async {
  final operation = _amplifyApi.query<String>(
    request: GraphQLRequest(
      document: '''
        query GetOrgs {
          getUser {
            orgs {
              orgUsers {
                org {
                  orgID
                  orgName
                  verified
                  config
                  quotas {
                    quotaName
                    description
                    period
                    softLimit
                    limit
                    used
                  }
                }
                isOwner
                isAdmin
                status
              }
            }
          }
        }
      ''',
    ),
  );

  try {
    final response = await operation.response;
    if (response.hasErrors) {
      final errorPayload = jsonEncode(response.errors);
      _logger.severe(
        'Failed to retrieve orgs for current user: $errorPayload',
      );

      throw UserOrgsLoadError(
        message: 'Failed to retrieve orgs current user is a member of',
      );
    } else {
      final data = jsonDecode(response.data!);

      final orgUsersData = data['getUser']?['orgs']?['orgUsers'];
      if (orgUsersData != null) {
        final orgUsers = (orgUsersData as List)
            .map((orgUserData) => OrgUser.fromJson(orgUserData))
            .toList();

        return orgUsers;
      }
    }
  } catch (e, stackTrace) {
    _logger.severe(
      'Exception when invoking service call to '
      'retrieve orgs for current user: $e',
    );

    throw OrgServiceError(
      message: 'Failed to retrieve orgs current user is a member of',
      innerException: e is Exception ? e : Exception(e),
      innerStackTrace: stackTrace,
    );
  }
  return [];
}