private async Task <List <TransactionDetail> > GetVenmoDeposits(Database.Models.VenmoUser venmoUser, Configuration configuration) { TransactionsApi transactionsApi = new TransactionsApi(configuration); TransactionsResponse transactions = await transactionsApi.GetTransactionsByAccountAsync(Default, venmoUser.YNAB !.DefaultAccount); var venmoDeposits = transactions.Transactions .Where(t => t.PayeeName != null && t.PayeeName.ToLower().Contains("from venmo")) .OrderByDescending(t => t.Date) .ToList(); return(venmoDeposits); }
// This will look for payments between the two dates but also transfers up to 5 days before the toDate and // 5 days before the fromDate private async Task <List <VenmoTransaction> > GetVenmoTransactionsBetweenDates(DateOnly fromDate, DateOnly toDate, VenmoApi venmoApi, Database.Models.VenmoUser venmoUser) { logger.LogDebug($"Finding Venmo transactions between {fromDate} and {toDate}"); List <VenmoTransaction> transactions = new List <VenmoTransaction>(); string? beforeId = null; bool foundAllTransactions = false; VenmoTransaction?toTransfer = null; VenmoTransaction?fromTransfer = null; VenmoTransaction?previousTransaction = null; do { var transactionsResponse = await venmoApi.GetTransactions(beforeId); if (transactionsResponse.Data != null) { foreach (var transaction in transactionsResponse.Data) { if (transaction.DateCreated != null && transaction.Type != null) { if (transaction.Type == "transfer") { DateTime transactionDate = DateTime.Parse(transaction.DateCreated); logger.LogDebug($"Found transfer on {transactionDate}. ID: {transaction.Id}"); // if the transfer initiated from Venmo was before the toDate and after 5 days before // the toDate set the transaction as the toTransfer if (toTransfer == null && transactionDate < toDate.ToDateTime(TimeOnly.MinValue) && transactionDate > toDate.AddDays(-5).ToDateTime(TimeOnly.MinValue)) { logger.LogDebug($"Set transfer {transaction.Id} as toTransfer"); toTransfer = transaction; transactions.Add(transaction); } // need to handle transfers done one after another (due to Venmo transfer limit) if (toTransfer != null && previousTransaction != null && previousTransaction.Type == "transfer") { logger.LogDebug($"Found transfers one after another\n" + $"Transfer 1: {previousTransaction.Id} at {previousTransaction.DateCreated}\n" + $"Transfer 2: {transaction.Id} at {transaction.DateCreated}"); transactions.Add(transaction); } if (fromTransfer == null && transactionDate < fromDate.ToDateTime(TimeOnly.MinValue) && transactionDate > fromDate.AddDays(-5).ToDateTime(TimeOnly.MinValue)) { logger.LogDebug($"Set transfer {transaction.Id} as fromTransfer"); fromTransfer = transaction; transactions.Add(transaction); foundAllTransactions = true; break; } } else if (transaction.Type == "payment") { // if the user received money, either someone paid them or they charged someone if ((transaction.Payment !.Action == "pay" && transaction.Payment.Target !.User.Id == venmoUser.Venmo !.UserId) || (transaction.Payment.Action == "charge" && transaction.Payment.Actor !.Id == venmoUser.Venmo !.UserId)) { DateTime transactionDate = DateTime.Parse(transaction.DateCreated); if (toTransfer != null) { if (fromTransfer == null) { if (transactionDate < DateTime.Parse(toTransfer.DateCreated !)) { transactions.Add(transaction); } } } } } previousTransaction = transaction; } } if (transactionsResponse.Data.Count == 0) { break; } beforeId = previousTransaction !.Id; } }while (!foundAllTransactions); return(transactions); }