getWalletsByUserId method

Future<M4eWalletList> getWalletsByUserId ({@required M4eUniqueId currentUserId @required M4eUniqueId otherUserId })

Returns other m4e user Wallets which the current authenticated user has SEE, PAY permissions on from m4e

Example:

final service = M4eWalletService(); // instantiate M4eWalletService


final otherUserWallets = service.getWalletsByUserId(currentUserId: 97fdebd0-7424-474e-a17c-67be85cc5638, otherUserId: 97fdebd0-7424-474e-a17c-67be85cc5638);  // returns other-m4e-user with [M4ePhoneNumber] wallets which the current authenticated user has a `SEE` and 'PAY' permissions on

Throws AuthException with code NO_AUTH_USER when no user is authenticated

Throws M4eServerException if request fails.

Throws [ServerException] with NO_INTERNET_CONNECTION error code when device is not connected to the internet

Implementation

Future<M4eWalletList> getWalletsByUserId({
  @required M4eUniqueId currentUserId,
  @required M4eUniqueId otherUserId,
}) async {
  assert(currentUserId != null,
      'getWalletsWithUserId [currentUserId] should not be null');
  assert(otherUserId != null,
      'getWalletsWithUserId [otherUserId] should not be null');

  SearchFormTypeCollection _searchFormTypeCollection;

  if ((await _connectionChecker.hasConnection) ?? false) {
    try {
      final _ownerWalletAccessPermissionsSet = <M4eAccessPermissionSet>[
        EntityWalletAccess.view,
        EntityWalletAccess.grant,
        EntityWalletAccess.alter,
      ];

      final _otherUsersWalletAccessPermissionsSet = <M4eAccessPermissionSet>[
        EntityWalletAccess.see,
        EntityWalletAccess.pay,
      ];

      /// accounts for cases where the user wants to fetch his wallets as recipient
      /// (in cases where transaction is maded between owner wallets)
      ///
      /// this will use simple clause query set for same user-id as `currentUserId` and `otherUserId`
      /// and compound clause query for different case
      if (currentUserId == otherUserId) {
        _searchFormTypeCollection = SingleClauseSearchForm(
          clause: SearchClause(
            subjectId: currentUserId,
            type: M4eAccessPermissionType.ENTITY_WALLET,
            encoding: _ownerWalletAccessPermissionsSet,
          ),
        );
      } else {
        /// accessing wallets owned by other user
        final _ownerWalletsAccessSearchClause = SearchClause(
          subjectId: otherUserId,
          type: M4eAccessPermissionType.ENTITY_WALLET,
          encoding: _ownerWalletAccessPermissionsSet,
        );

        /// accessing wallets current user has [SEE, PAY] ENTITY_WALLET permissions on
        final _currentUsersWalletAccessSearchClause = SearchClause(
          subjectId: currentUserId,
          type: M4eAccessPermissionType.ENTITY_WALLET,
          encoding: _otherUsersWalletAccessPermissionsSet,
        );

        _searchFormTypeCollection = MultipleClauseSearchForm(
          conjunction: M4eSearchConjunctionType.AND,
          clauses: <SearchClause>[
            _ownerWalletsAccessSearchClause,
            _currentUsersWalletAccessSearchClause,
          ],
        );
      }

      final _runQueryUniqueId =
          await _walletApi.queryUserWallets(_searchFormTypeCollection);

      final _walletList =
          await _runSearchQueryForM4eWallets(_runQueryUniqueId);

      if (_walletList.wallets.isEmpty) {
        throw M4eExceptionMessages.kNotFoundException;
      }

      return _walletList;
    } catch (e) {
      rethrow;
    }
  }

  throw M4eExceptionMessages.kNoInternetConnectionException;
}