Esempio n. 1
0
        /// <summary>
        /// Retrieve the list of accounts from a financial institution using OFX and return all accounts that are not already present in the database
        /// </summary>
        /// <param name="financialInstitution">Financial institution to query</param>
        /// <param name="fiCredentials">Credentials for financial institution account</param>
        /// <returns>List of accounts</returns>
        public static async Task <IEnumerable <Account> > EnumerateNewAccounts(
            OFX.Types.FinancialInstitution financialInstitution, OFX.Types.Credentials fiCredentials)
        {
            using (BackgroundTaskTracker.BeginTask("Retrieving Account Information"))
            {
                var ofxService     = new OFX2Service(financialInstitution, fiCredentials);
                var accountList    = new List <Account>();
                var ofxAccountList = await ofxService.ListAccounts().ConfigureAwait(false);

                // TODO: If ofxAccountList is null, raise a more detailed exception

                using (var dataService = new DataService())
                {
                    foreach (var ofxAccount in ofxAccountList)
                    {
                        // Convert from OFX account type to db account type and encode account id
                        AccountType accountType = AccountType.Checking;
                        string      accountId   = "";
                        if (ofxAccount.GetType() == typeof(OFX.Types.CheckingAccount))
                        {
                            accountType = AccountType.Checking;
                            accountId   = ((OFX.Types.CheckingAccount)ofxAccount).RoutingId + ":" + ofxAccount.AccountId;
                        }
                        else if (ofxAccount.GetType() == typeof(OFX.Types.SavingsAccount))
                        {
                            accountType = AccountType.Savings;
                            accountId   = ((OFX.Types.SavingsAccount)ofxAccount).RoutingId + ":" + ofxAccount.AccountId;
                        }
                        else if (ofxAccount.GetType() == typeof(OFX.Types.CreditCardAccount))
                        {
                            accountType = AccountType.Creditcard;
                            accountId   = ofxAccount.AccountId;
                        }

                        // Look for a matching account in the database
                        if (!dataService.GetAccountByFinancialId(accountId).Any())
                        {
                            // This account is not already in the DB, add to new account list
                            accountList.Add(new Account
                            {
                                AccountName =
                                    accountType + ":" +
                                    ofxAccount.AccountId.Substring(ofxAccount.AccountId.Length - 4),
                                AccountType = accountType.ToString(),
                                Currency    = "USD",
                                FiAccountId = accountId
                            });
                        }
                    }
                }

                // Return the finalized list of new accounts
                return(accountList);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieve the list of accounts from a financial institution using OFX and return all accounts that are not already present in the database
        /// </summary>
        /// <param name="financialInstitution">Financial institution to query</param>
        /// <param name="fiCredentials">Credentials for financial institution account</param>
        /// <returns>List of accounts</returns>
        public static async Task<IEnumerable<Account>> EnumerateNewAccounts(
            OFX.Types.FinancialInstitution financialInstitution, OFX.Types.Credentials fiCredentials)
        {

            using (BackgroundTaskTracker.BeginTask("Retrieving Account Information"))
            {

                var ofxService = new OFX2Service(financialInstitution, fiCredentials);
                var accountList = new List<Account>();
                var ofxAccountList = await ofxService.ListAccounts().ConfigureAwait(false);

                // TODO: If ofxAccountList is null, raise a more detailed exception

                using (var dataService = new DataService())
                {
                    foreach (var ofxAccount in ofxAccountList)
                    {
                        // Convert from OFX account type to db account type and encode account id 
                        AccountType accountType = AccountType.Checking;
                        string accountId = "";
                        if (ofxAccount.GetType() == typeof (OFX.Types.CheckingAccount))
                        {
                            accountType = AccountType.Checking;
                            accountId = ((OFX.Types.CheckingAccount) ofxAccount).RoutingId + ":" + ofxAccount.AccountId;
                        }
                        else if (ofxAccount.GetType() == typeof (OFX.Types.SavingsAccount))
                        {
                            accountType = AccountType.Savings;
                            accountId = ((OFX.Types.SavingsAccount) ofxAccount).RoutingId + ":" + ofxAccount.AccountId;
                        }
                        else if (ofxAccount.GetType() == typeof (OFX.Types.CreditCardAccount))
                        {
                            accountType = AccountType.Creditcard;
                            accountId = ofxAccount.AccountId;
                        }

                        // Look for a matching account in the database
                        if (!dataService.GetAccountByFinancialId(accountId).Any())
                        {
                            // This account is not already in the DB, add to new account list
                            accountList.Add(new Account
                            {
                                AccountName =
                                    accountType + ":" +
                                    ofxAccount.AccountId.Substring(ofxAccount.AccountId.Length - 4),
                                AccountType = accountType.ToString(),
                                Currency = "USD",
                                FiAccountId = accountId
                            });
                        }
                    }
                }

                // Return the finalized list of new accounts
                return accountList;
            }
        }