示例#1
0
        private ActivatedApplicationViewModel GetActivatedApplicationViewModel(Application application)
        {
            //Validate the Application Model State
            if (application.Person == null)
            {
                _logger.LogWarning($"The application model is invalid.Person Entity Is Null");
                throw new ArgumentNullException(nameof(application.Person));
            }
            if (application.IsLegalEntity && application.LegalEntity == null)
            {
                _logger.LogWarning($"The application model is invalid.Legal Entity Is Null");
                throw new ArgumentNullException(nameof(application.LegalEntity));
            }


            ActivatedApplicationViewModel vm = new ActivatedApplicationViewModel
            {
                ReferenceNumber      = application.ReferenceNumber,
                State                = application.State.ToDescription(),
                FullName             = string.Format("{0} {1}", application.Person.FirstName, application.Person.Surname),
                LegalEntity          = application.IsLegalEntity ? application.LegalEntity : null,
                PortfolioFunds       = application.Products.SelectMany(p => p.Funds),
                PortfolioTotalAmount = application.Products.SelectMany(p => p.Funds)
                                       .Select(f => (f.Amount - f.Fees) * _configuration.TaxRate)
                                       .Sum(),
                AppliedOn    = application.Date,
                SupportEmail = _configuration.SupportEmail,
                Signature    = _configuration.Signature
            };

            return(vm);
        }
示例#2
0
        public ActivatedApplicationViewModel MapActivatedApplication(Application application)
        {
            var newModel = new ActivatedApplicationViewModel
            {
                ReferenceNumber      = application.ReferenceNumber,
                State                = application.State.ToDescription(),
                FullName             = $"{application.Person.FirstName} {application.Person.Surname}",
                LegalEntity          = application.IsLegalEntity ? application.LegalEntity : null,
                PortfolioFunds       = application.Products.SelectMany(p => p.Funds),
                PortfolioTotalAmount = application.Products.SelectMany(p => p.Funds)
                                       .Select(f => (f.Amount - f.Fees) * _configuration.TaxRate)
                                       .Sum(),
                AppliedOn    = application.Date,
                SupportEmail = _configuration.SupportEmail,
                Signature    = _configuration.Signature
            };

            return(newModel);
        }
示例#3
0
        public static string AppActivated(Application application, IPathProvider _templatePathProvider, IConfiguration _configuration, IViewGenerator View_Generator, string baseUri)
        {
            string view;
            string path = _templatePathProvider.Get("ActivatedApplication");
            ActivatedApplicationViewModel vm = new ActivatedApplicationViewModel
            {
                ReferenceNumber      = application.ReferenceNumber,
                State                = application.State.ToDescription(),
                FullName             = $"{application.Person.FirstName} {application.Person.Surname}",
                LegalEntity          = application.IsLegalEntity ? application.LegalEntity : null,
                PortfolioFunds       = application.Products.SelectMany(p => p.Funds),
                PortfolioTotalAmount = application.Products.SelectMany(p => p.Funds)
                                       .Select(f => (f.Amount - f.Fees) * _configuration.TaxRate)
                                       .Sum(),
                AppliedOn    = application.Date,
                SupportEmail = _configuration.SupportEmail,
                Signature    = _configuration.Signature
            };

            return(view = View_Generator.GenerateFromPath(baseUri + path, vm));
        }
示例#4
0
        private static string GetActivatedApplicationView(Application application, string baseUri)
        {
            var path = _templatePathProvider.Get("ActivatedApplication");
            ActivatedApplicationViewModel ActivatedApplicationViewModel = new ActivatedApplicationViewModel
            {
                ReferenceNumber = application.ReferenceNumber,
                State           = application.State.ToDescription(),
                //changed for consistency
                FullName    = string.Format("{0} {1}", application.Person.FirstName, application.Person.Surname),
                LegalEntity = application.IsLegalEntity ? application.LegalEntity : null,
                //Assume that Legal entity can have a value evn when IsLegalEntity = 0
                PortfolioFunds       = application.Products.SelectMany(p => p.Funds),
                PortfolioTotalAmount = application.Products.SelectMany(p => p.Funds)
                                       //Below calculation results in calculating tax ONLY, not amount + tax(Assuming TaxRate is a percentage i.e 0.15)
                                       //.Select(f => (f.Amount - f.Fees) * _configuration.TaxRate)
                                       .Select(f => (f.Amount - f.Fees) + ((f.Amount - f.Fees) * _configuration.TaxRate))
                                       .Sum(),
                AppliedOn    = application.Date,
                SupportEmail = _configuration.SupportEmail,
                Signature    = _configuration.Signature
            };

            return(_viewGenerator.GenerateFromPath(string.Format("{0}{1}", baseUri, path), ActivatedApplicationViewModel));
        }
		//Implementing the interface method
		//Add exception handling
		public byte[] Generate(Guid applicationId, string baseUri)
		{
			var application = _dataContext.Applications.Single(app => app.Id == applicationId);

			if (application != null)
			{
                //The substring method here may not be the correct method to use
                //Normally a good base Uri ends with a trailing slash
                //Currently all this would do is return the "/" character as a substring

                /*********************************************************************************
                if (baseUri.EndsWith("/"))
					baseUri = baseUri.Substring(baseUri.Length - 1);
                **********************************************************************************/

                //Check for a bad baseUri
                if (!baseUri.EndsWith("/"))
                {
                    //append the trailing slash character
                    baseUri += "/";

                    //NB That this would work assuming that _templatePathProvider.Get() would return a file string path 
                    //which DOES NOT start with a "/" character
                }

                //Initialize string variable
                var view = "";

				switch (application.State)
                {
                    case ApplicationState.Pending:
                    {
                        var path = _templatePathProvider.Get("PendingApplication");

                        //Due to the convention used above for the base Uri, we cannot allow any paths to start with a "/" character
                        //Ideally this check should be done in the class which implements IPathProvider's Get() method but is added here as an extra check.
                        if (path.StartsWith("/") && path.Length > 1)
                        {
                            path = path.Substring(1);
                        }

                        var vm = new PendingApplicationViewModel
                        {
                            ReferenceNumber = application.ReferenceNumber,
                            State = application.State.ToDescription(),
                            FullName = application.Person.FirstName + " " + application.Person.Surname,
                            AppliedOn = application.Date,
                            SupportEmail = _configuration.SupportEmail,
                            Signature = _configuration.Signature
                        };

                        //Add some exception handling before generating the view as an extra measure
                        try
                        {
                            view = _viewGenerator.GenerateFromPath($"{baseUri}{path}", vm);
                        }
                        catch (Exception e)
                        {
                            _logger.LogWarning("Error generating view: " + e.Message);
                            throw;
                        }

                        break;
                    }
                    case ApplicationState.Activated:
                    {
                        var path = _templatePathProvider.Get("ActivatedApplication");

                        //Due to the convention used above for the base Uri, we cannot allow any paths to start with a "/" character
                        //Ideally this check should be done in the class which implements IPathProvider's Get() method but is added here as an extra check.
                        if (path.StartsWith("/") && path.Length > 1)
                        {
                            path = path.Substring(1);
                        }

                        var vm = new ActivatedApplicationViewModel
                        {
                            ReferenceNumber = application.ReferenceNumber,
                            State = application.State.ToDescription(),
                            FullName = $"{application.Person.FirstName} {application.Person.Surname}", 
                            LegalEntity = application.IsLegalEntity ? application.LegalEntity : null,

                            PortfolioFunds = application.Products.SelectMany(p => p.Funds),

                            PortfolioTotalAmount = application.Products.SelectMany(p => p.Funds)
                                .Select(f => (f.Amount - f.Fees) * _configuration.TaxRate)
                                .Sum(),

                            AppliedOn = application.Date,
                            SupportEmail = _configuration.SupportEmail,
                            Signature = _configuration.Signature
                        };

                        //Add some exception handling before generating the view as an extra measure
                        try
                        {
                            view = _viewGenerator.GenerateFromPath(baseUri + path, vm);
                        }
                        catch (Exception e)
                        {
                            _logger.LogWarning("Error generating view: " + e.Message);
                            throw;
                        }

                        break;
                    }
                    case ApplicationState.InReview:
                    {
                        var templatePath = _templatePathProvider.Get("InReviewApplication");

                        //Due to the convention used above for the base Uri, we cannot allow any paths to start with a "/" character
                        //Ideally this check should be done in the class which implements IPathProvider's Get() method but is added here as an extra check.
                        if (templatePath.StartsWith("/") && templatePath.Length > 1)
                        {
                            templatePath = templatePath.Substring(1);
                        }

                        var inReviewMessage = "Your application has been placed in review" +
                                              application.CurrentReview.Reason switch
                                              {
                                                  { } reason when reason.Contains("address") =>
                                                      " pending outstanding address verification for FICA purposes.",
                                                  { } reason when reason.Contains("bank") =>
                                                      " pending outstanding bank account verification.",
                                                  _ =>
                                                      " because of suspicious account behaviour. Please contact support ASAP."
					
                                              };

                        var inReviewApplicationViewModel = new InReviewApplicationViewModel
                        {
                            ReferenceNumber = application.ReferenceNumber,
                            State = application.State.ToDescription(),
                            FullName = $"{application.Person.FirstName} {application.Person.Surname}",
                            LegalEntity = application.IsLegalEntity ? application.LegalEntity : null,

                            PortfolioFunds = application.Products.SelectMany(p => p.Funds),

                            PortfolioTotalAmount = application.Products.SelectMany(p => p.Funds)
                                .Select(f => (f.Amount - f.Fees) * _configuration.TaxRate)
                                .Sum(),

                            InReviewMessage = inReviewMessage,
                            InReviewInformation = application.CurrentReview,
                            AppliedOn = application.Date,
                            SupportEmail = _configuration.SupportEmail,
                            Signature = _configuration.Signature
                        };

                        //Add some exception handling before generating the view as an extra measure
                        try
                        {
                            view = _viewGenerator.GenerateFromPath($"{baseUri}{templatePath}", inReviewApplicationViewModel);
                        }
                        catch (Exception e)
                        {
                            _logger.LogWarning("Error generating view: " + e.Message);
                            throw;
                        }

                        break;
                    }
                    default:
                        _logger.LogWarning(
                            $"The application is in state '{application.State}' and no valid document can be generated for it.");
                        return null;
                }

				var pdfOptions = new PdfOptions
				{
					PageNumbers = PageNumbers.Numeric,
					HeaderOptions = new HeaderOptions
					{
						HeaderRepeat = HeaderRepeat.FirstPageOnly,
						HeaderHtml = PdfConstants.Header
					}
				};

				//Add exception handling before generating the pdf
                try
                {
                    var pdf = _pdfGenerator.GenerateFromHtml(view, pdfOptions);
                    return pdf.ToBytes();
                }
                catch (Exception e)
                {
                    _logger.LogWarning("Error generating pdf: " + e.Message);
                    throw;
                }
				
			}

            _logger.LogWarning(
                $"No application found for id '{applicationId}' or an unexpected error was encountered");
            return null;
        }