public async Task <ServiceLink> CreateLink(string userId, IQuestradeLink questradeLink) { QuestradeCredentials questradeCredentials = await GetLinkCredentials(questradeLink); QuestradeClient client = m_clientFactory.CreateClient(questradeCredentials); AccountsResponse response = await client.FetchAccounts(); ServiceType serviceType = ServiceType.Questrade; string serviceId = response.UserId.ToString(); if (LinkExists(userId, serviceType, serviceId)) { throw new ConflictException(); } ServiceLink link = new ServiceLink(userId, ServiceType.Questrade, serviceId, questradeLink.IsPractice); using (IDbContextTransaction transaction = m_context.Database.BeginTransaction()) { m_context.ServiceLinks.Add(link); m_context.SaveChanges(); Credentials credentials = new Credentials(link, questradeCredentials); m_context.Credentials.Add(credentials); m_context.SaveChanges(); m_accountsManager.SynchronizeAccounts(link, response.Accounts); transaction.Commit(); } return(link); }
public async Task <QuestradeCredentials> GetLinkCredentials(IQuestradeLink link) { try { return(await m_signInService.SignIn(link.RefreshToken, link.IsPractice)); } catch (Exception ex) { m_logger.LogWarning($"Failed to link Questrade account: {ex}"); throw new InvalidCredentialException("Failed to link Questrade account.", ex); } }