getWalletsByPhoneNumber method

Future<M4eWalletList> getWalletsByPhoneNumber ({@required M4eUniqueId currentUserId, M4ePhoneNumber otherUserPhoneNumber })

Returns current authenticated m4e user Wallets from m4e when no otherUserId is provided

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

Example:

final service = M4eWalletService(); // instantiate M4eWalletService

final currentUserWallets = service.getWalletsWithPhoneNumber(currentUserId: 97fdebd0-7424-474e-a17c-67be85cc5638); // returns 97fdebd0-7424-474e-a17c-67be85cc5638 wallets with [VIEW, GRANT, ALTER] permissions
final otherUserWallets = service.getWalletsByPhoneNumber(currentUserId: 97fdebd0-7424-474e-a17c-67be85cc5638, otherUserPhoneNumber: M4ePhoneNumber(country: 'GHANA', number: '0541234567'));  // 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> getWalletsByPhoneNumber({
  @required M4eUniqueId currentUserId,
  M4ePhoneNumber otherUserPhoneNumber,
}) async {
  assert(currentUserId != null,
      'getWallets [getWalletsWithPhoneNumber] should not be null');

  M4eUser _otherM4eUserDetails;
  SearchFormTypeCollection _searchFormTypeCollection;

  if ((await _connectionChecker.hasConnection) ?? false) {
    try {
      if (otherUserPhoneNumber != null) {
        /// gets m4eUser with phone number if phone number is passed in
        _otherM4eUserDetails =
            await _getM4eUserWithPhoneNumber(otherUserPhoneNumber);
      }

      final _isAuthenticatedUserWalletsRequest = otherUserPhoneNumber == null;

      final _ownerWalletAccessPermissionsSet = <M4eAccessPermissionSet>[
        EntityWalletAccess.view,
        EntityWalletAccess.grant,
        EntityWalletAccess.alter,
      ];

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

      if (_isAuthenticatedUserWalletsRequest) {
        // accessing current authenticated user wallets
        _searchFormTypeCollection = SingleClauseSearchForm(
          clause: SearchClause(
            subjectId: currentUserId,
            type: M4eAccessPermissionType.ENTITY_WALLET,
            encoding: _ownerWalletAccessPermissionsSet,
          ),
        );
      } else {
        /// 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 == _otherM4eUserDetails.id) {
          _searchFormTypeCollection = SingleClauseSearchForm(
            clause: SearchClause(
              subjectId: currentUserId,
              type: M4eAccessPermissionType.ENTITY_WALLET,
              encoding: _ownerWalletAccessPermissionsSet,
            ),
          );
        } else {
          /// accessing wallets owned by other user
          final _ownerWalletsAccessSearchClause = SearchClause(
            subjectId: _otherM4eUserDetails.id,
            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;
}