public ResultMessage CreatePayoffAccount(HttpRequestMessage request)
        {
            try
            {
                using (var db = new SES_ServicesEntities())
                {
                    // TODO: Perform entity permission check here

                    // TODO: Check to see if the entity passing the payoff account to this can pre-approve payoff accounts

                    var doc = new XmlDocument();
                    doc.Load(request.Content.ReadAsStreamAsync().Result);

                    var validationResult = ValidatePayoffAccountXml(doc);
                    if (validationResult.Result != ResultEnum.Success)
                    {
                        return(validationResult);
                    }

                    var payoffAccount = ConvertXmlToPayoffAccount(doc.DocumentElement);

                    var existingPayoffAccount = db.PayoffBankAccounts.FirstOrDefault(p => p.AccountNumber == payoffAccount.AccountNumber && p.RoutingNumber == payoffAccount.RoutingNumber);
                    if (existingPayoffAccount != null)
                    {
                        return(new ResultMessage(ResultEnum.FailureDuplicateData, "Unable to create payoff account.  An account with the same account and routing numbers already exists."));
                    }

                    payoffAccount.Status      = PayoffAccountStatusEnum.PendingApproval; // TODO: Determine beginning status based on entity
                    payoffAccount.CreatedDate = DateTime.Now;

                    payoffAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                    {
                        EntityId      = payoffAccount.CreatedByEntity_Id,
                        UserProfileId = payoffAccount.CreatedBy_Id,
                        TimeStamp     = DateTime.Now,
                        Username      = payoffAccount.CreatedByUsername,
                        Message       = "Payoff account created."
                    });

                    db.PayoffBankAccounts.Add(payoffAccount);
                    db.SaveChanges();

                    return(new ResultMessage(ResultEnum.Success, "Success."));
                }
            }
            catch (Exception ex)
            {
                // TODO: log the actual exception
                return(new ResultMessage(ResultEnum.Error, "There was a problem creating a new payoff account."));
            }
        }
        public ResultMessage ValidatePayoffAccount(HttpRequestMessage request)
        {
            try
            {
                using (var db = new SES_ServicesEntities())
                {
                    // TODO: Perform entity permission check here

                    var doc = new XmlDocument();
                    doc.Load(request.Content.ReadAsStreamAsync().Result);

                    if (doc.DocumentElement == null)
                    {
                        return(new ResultMessage(ResultEnum.FailureDocumentReadError, "Unable to open xml document."));
                    }

                    var accountNumber = doc.DocumentElement.SelectSingleNode("accountNumber")?.InnerText;
                    if (string.IsNullOrEmpty(accountNumber))
                    {
                        return(new ResultMessage(ResultEnum.FailureMissingData, "Unable to validate payoff account.  You must provide an account number."));
                    }

                    var routingNumber = doc.DocumentElement.SelectSingleNode("routingNumber")?.InnerText;
                    if (string.IsNullOrEmpty(routingNumber))
                    {
                        return(new ResultMessage(ResultEnum.FailureMissingData, "Unable to validate payoff account.  You must provide a routing number."));
                    }

                    // TODO: Check for additional elements like file id, entity, etc.

                    var activePayoffAccounts = db.PayoffBankAccounts.Where(p => p.Status == PayoffAccountStatusEnum.Active && p.RoutingNumber == routingNumber);

                    if (activePayoffAccounts.Any() == false)
                    {
                        return(new ResultMessage(ResultEnum.FailureExistingDataNotFound, "Unable to locate any active payoff accounts with the given routing number."));
                    }

                    // If we find an exact account and routing number match, return a success
                    var foundAccount = activePayoffAccounts.FirstOrDefault(p => p.AccountNumber == accountNumber);
                    if (foundAccount != null)
                    {
                        return(new ResultMessage(ResultEnum.Success, $"Payoff account with the account number {accountNumber} and routing number {routingNumber} found to be active and valid."));
                    }

                    // If we don't find an exact match, check the wildcard accounts
                    foreach (var wildcardAccount in activePayoffAccounts.Where(p => p.AccountNumber.Contains("X")))
                    {
                        // If the wildcard account # and account # to validate do not match length, check the next one
                        if (accountNumber.Length != wildcardAccount.AccountNumber.Length)
                        {
                            continue;
                        }

                        for (var i = 0; i < accountNumber.Length; i++)
                        {
                            var accountChar  = accountNumber[i];
                            var wildcardChar = wildcardAccount.AccountNumber[i];

                            // if we hit an X at this point, we have a match
                            if (wildcardChar == 'X')
                            {
                                return(new ResultMessage(ResultEnum.Success, $"Payoff account with the account number {accountNumber} and routing number {routingNumber} found to be active and valid."));
                            }

                            // If we haven't hit an 'X' yet but the characters still match, move to the next character
                            if (accountChar == wildcardChar)
                            {
                                continue;
                            }

                            // If we haven't hit an 'X' yet but the characters do not match, it isn't a matching number
                            break;
                        }
                    }

                    return(new ResultMessage(ResultEnum.FailureExistingDataNotFound, "Unable to locate any active payoff accounts with the given account and routing numbers."));
                }
            }
            catch (Exception ex)
            {
                // TODO: log the actual exception
                return(new ResultMessage(ResultEnum.Error, "There was a problem validating the payoff account."));
            }
        }
        public ResultMessage UpdatePayoffAccount(HttpRequestMessage request)
        {
            try
            {
                using (var db = new SES_ServicesEntities())
                {
                    // TODO: Perform entity permission check here

                    var doc = new XmlDocument();
                    doc.Load(request.Content.ReadAsStreamAsync().Result);

                    var validationResult = ValidatePayoffAccountXml(doc);
                    if (validationResult.Result != ResultEnum.Success)
                    {
                        return(validationResult);
                    }

                    var updatedAccount = ConvertXmlToPayoffAccount(doc.DocumentElement);

                    var duplicateAccount = db.PayoffBankAccounts.FirstOrDefault(p => p.Id != updatedAccount.Id && p.AccountNumber == updatedAccount.AccountNumber && p.RoutingNumber == updatedAccount.RoutingNumber);
                    if (duplicateAccount != null)
                    {
                        return(new ResultMessage(ResultEnum.FailureDuplicateData, "Unable to save payoff account information.  The account and routing number pair is already in use by another payoff account."));
                    }

                    var existingAccount = db.PayoffBankAccounts.FirstOrDefault(p => p.Id == updatedAccount.Id);
                    if (existingAccount == null)
                    {
                        return(new ResultMessage(ResultEnum.FailureExistingDataNotFound, "Unable to locate the existing payoff account with the given Id."));
                    }

                    if (existingAccount.Status != PayoffAccountStatusEnum.PendingApproval)
                    {
                        return(new ResultMessage(ResultEnum.FailureStatusError, "You may only make changes to existing payoff accounts if the account is still pending approval."));
                    }

                    if (existingAccount.AccountNumber != updatedAccount.AccountNumber)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Account number changed from {existingAccount.AccountNumber} to {updatedAccount.AccountNumber}."
                        });
                        existingAccount.AccountNumber = updatedAccount.AccountNumber;
                    }

                    if (existingAccount.RoutingNumber != updatedAccount.RoutingNumber)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Routing number changed from {existingAccount.RoutingNumber} to {updatedAccount.RoutingNumber}."
                        });
                        existingAccount.RoutingNumber = updatedAccount.RoutingNumber;
                    }

                    if (existingAccount.Name != updatedAccount.Name)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Name changed from {existingAccount.Name} to {updatedAccount.Name}."
                        });
                        existingAccount.Name = updatedAccount.Name;
                    }

                    if (existingAccount.Description != updatedAccount.Description)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Description changed from {(string.IsNullOrEmpty(existingAccount.Description) ? "no description" : existingAccount.Description)} to {(string.IsNullOrEmpty(updatedAccount.Description) ? "no description" : updatedAccount.Description)}."
                        });
                        existingAccount.Description = updatedAccount.Description;
                    }

                    if (existingAccount.BankName != updatedAccount.BankName)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Bank Name changed from {existingAccount.BankName} to {updatedAccount.BankName}."
                        });
                        existingAccount.BankName = updatedAccount.BankName;
                    }

                    if (existingAccount.Address1 != updatedAccount.Address1)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Address 1 changed from {existingAccount.Address1} to {updatedAccount.Address1}."
                        });
                        existingAccount.Address1 = updatedAccount.Address1;
                    }

                    if (existingAccount.Address2 != updatedAccount.Address2)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Address 2 changed from {(string.IsNullOrEmpty(existingAccount.Address2) ? "no address 2" : existingAccount.Address2)} to {(string.IsNullOrEmpty(updatedAccount.Address2) ? "no address 2" : updatedAccount.Address2)}."
                        });
                        existingAccount.Address2 = updatedAccount.Address2;
                    }

                    if (existingAccount.City != updatedAccount.City)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"City changed from {existingAccount.City} to {updatedAccount.City}."
                        });
                        existingAccount.City = updatedAccount.City;
                    }

                    if (existingAccount.State != updatedAccount.State)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"State changed from {existingAccount.State} to {updatedAccount.State}."
                        });
                        existingAccount.State = updatedAccount.State;
                    }

                    if (existingAccount.Zip != updatedAccount.Zip)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Zip code changed from {existingAccount.Zip} to {updatedAccount.Zip}."
                        });
                        existingAccount.Zip = updatedAccount.Zip;
                    }

                    db.SaveChanges();

                    return(new ResultMessage(ResultEnum.Success, "Payoff account updated."));
                }
            }
            catch (Exception ex)
            {
                // TODO: Log the actual exception
                return(new ResultMessage(ResultEnum.Error, "There was a problem updating the payoff account."));
            }
        }
Пример #4
0
 protected ControllerBase()
 {
     _entities = new SES_ServicesEntities();
 }