Пример #1
0
        public async Task <IActionResult> PostAsync([FromForm] ExpenseAuthorizationRequest request, [FromForm] IFormFileCollection files)
        {
            // By annotating the controller with ApiControllerAttribute,
            // the ModelStateInvalidFilter will automatically check ModelState.IsValid
            // see https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1#automatic-http-400-responses

            try
            {
                var command  = new Features.ExpenseAuthorization.CreateCommand(request, files);
                var response = await _mediator.Send(command);

                if (response.Exception != null)
                {
                    return(GetProblemResult(response.Exception));
                }

                return(Ok(new ExpenseAuthorizationResponse {
                    Id = response.Id
                }));
            }
            catch (Exception e)
            {
                return(GetProblemResult(e));
            }
        }
Пример #2
0
        private string FormatContactInfo(ExpenseAuthorizationRequest source)
        {
            StringBuilder buffer = new StringBuilder();

            if (!string.IsNullOrEmpty(source.AuthName))
            {
                buffer.Append("Name: ");
                buffer.Append(source.AuthName);
            }

            if (!string.IsNullOrEmpty(source.AuthTelephone))
            {
                if (buffer.Length != 0)
                {
                    buffer.Append('\n');
                }
                buffer.Append("Tel: ");
                buffer.Append(source.AuthTelephone);
            }

            if (!string.IsNullOrEmpty(source.AuthEmail))
            {
                if (buffer.Length != 0)
                {
                    buffer.Append('\n');
                }
                buffer.Append("Email: ");
                buffer.Append(source.AuthEmail);
            }

            return(buffer.ToString());
        }
Пример #3
0
            public CreateCommand(ExpenseAuthorizationRequest request, IEnumerable <IFormFile> files)
            {
                Request = request ?? throw new ArgumentNullException(nameof(request));

                if (files != null)
                {
                    Files = new List <IFormFile>(files);
                }
            }
Пример #4
0
        public void ExpenditureNotToExceed_rounds_up()
        {
            ExpenseAuthorizationRequest sut = new ExpenseAuthorizationRequest();

            sut.ExpenditureNotToExceed = _fixture.Create <int>() + 0.01m;
            var expected = Math.Ceiling(sut.ExpenditureNotToExceed); // expect to be round up

            Assert.Equal(expected, sut.ExpenditureNotToExceed);
        }
Пример #5
0
        private IList <string> GetDefaultToEmailRecipients(ExpenseAuthorizationRequest request)
        {
            // if the config file does not have configured recipient, fall back to the configuration
            var defaultService = new DefaultEmailRecipientService(_emailOptions);
            var defaultList    = defaultService.GetToRecipients(request);

            if (defaultList.Count == 0)
            {
                _logger.LogError("No default email recipients not found, sending email is probably going to fail");
            }

            return(defaultList);
        }
Пример #6
0
        public IDictionary <string, string> Map(
            ExpenseAuthorizationRequest source,
            string priority,
            string resourceCategory,
            string currentStatus
            )
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (priority == null)
            {
                throw new ArgumentNullException(nameof(priority));
            }

            if (resourceCategory == null)
            {
                throw new ArgumentNullException(nameof(resourceCategory));
            }

            if (currentStatus == null)
            {
                throw new ArgumentNullException(nameof(currentStatus));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var items = new Dictionary <string, string>();

            items.Add("reportType", "resource_request");
            items.Add("quantity", "1");
            items.Add("priority", priority);
            items.Add("currentStatus", currentStatus);
            items.Add("resourceCategory", resourceCategory);            // Expenditure Authorization
            items.Add("approvedTime", source.DateTime.ToString("o"));
            items.Add("requestorContactInfo", FormatContactInfo(source));
            items.Add("resourceType", source.ResourceType);
            items.Add("requestNumber", source.EAFNo);
            items.Add("reqTrackNoState", source.EMBCTaskNo);
            items.Add("requestionOrg", source.RequestingOrg);
            items.Add("mission", FormatMission(source));
            items.Add("estimatedResourceCost", FormatEstimatedResourceCost(source));

            return(items);
        }
Пример #7
0
        public async Task <CreateReportResponse> CreateReportAsync(ExpenseAuthorizationRequest expenseAuthorizationRequest)
        {
            if (expenseAuthorizationRequest == null)
            {
                throw new ArgumentNullException(nameof(expenseAuthorizationRequest));
            }

            // login and get session cookie
            await LoginAsync(_options.Value);

            CreateReportResponse ret;
            // get the defaults, we could cache this in the future
            var resourceCategories = await GetLookupAsync(LookupType.ResourceCategory);

            var statuses = await GetLookupAsync(LookupType.StatusResource);

            var priorities = await GetLookupAsync(LookupType.PriorityResource);

            string resourceCategory = resourceCategories.FirstOrDefault(_ => _.Value == DefaultResourceCategory)?.Value;
            string currentStatus    = statuses.FirstOrDefault(_ => _.Value == DefaultCurrentStatus)?.Id;
            string priority         = priorities.FirstOrDefault(_ => _.Value == DefaultPriority)?.Id;

            var items = _mapper.Map(expenseAuthorizationRequest, priority, resourceCategory, currentStatus);

            var    request     = new CreaterReportRequest(items);
            string soapRequest = request.CreateSoapRequest();

            try
            {
                CreateReportResponse response1 = new CreateReportResponse();

                var soapResponse = await _client.CreateReportAsync(soapRequest);

                response1.LoadFromXml(soapResponse);

                ret = response1;
            }
            catch (ApiException exception)
            {
                throw new SoapFaultException(exception);
            }

            CreateReportResponse response = ret;

            return(response);
        }
Пример #8
0
        public IList <string> GetToRecipients(ExpenseAuthorizationRequest request)
        {
            _logger.LogDebug("Getting the email address for {RequestingOrg}", request.RequestingOrg);
            // do not use the default to list
            var recipients = GetRecipients(request, _ => _.To, _ => Enumerable.Empty <string>());

            if (recipients.Count != 0)
            {
                _logger.LogDebug("Found {@EmailRecipients} ", recipients);
                return(recipients);
            }

            _logger.LogInformation("Email recipient not found based on request, using the default email address from configuration");

            // if the config file does not have configured recipient, fall back to the configuration
            return(GetDefaultToEmailRecipients(request));
        }
Пример #9
0
        private CommunityEmailRecipient GetCommunityEmailRecipient(ExpenseAuthorizationRequest request)
        {
            if (request?.RequestingOrg == null)
            {
                _logger.LogInformation("Request or RequestingOrg is null");
                return(null);
            }

            IDictionary <string, CommunityEmailRecipient> recipients = GetRecipients();

            var requestingOrg = request.RequestingOrg.Trim();

            if (recipients.TryGetValue(requestingOrg, out var recipient))
            {
                return(recipient);
            }

            return(null);
        }
Пример #10
0
        private IList <string> GetRecipients(ExpenseAuthorizationRequest request, Func <CommunityEmailRecipient, string> fieldSelector, Func <EmailSettings, IEnumerable <string> > defaultRecipients)
        {
            List <string> recipients = new List <string>();

            var recipient = GetCommunityEmailRecipient(request);

            if (recipient != null)
            {
                var field = fieldSelector(recipient);

                if (!string.IsNullOrEmpty(field))
                {
                    recipients.AddRange(field.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
                }
            }

            var settings = _emailOptions.Value;

            recipients.AddRange(defaultRecipients(settings));

            return(recipients);
        }
Пример #11
0
        public async Task SendEmailAsync(
            ExpenseAuthorizationRequest request,
            CreateReportResponse response,
            IList <IFormFile> attachments)
        {
            ETeamSettings eteamSettings = _eteamOptions.Value;

            // create and apply data to the email template
            string content = new EmailTemplate()
                             .Apply(request)
                             .Apply(response, eteamSettings.Url)
                             .Content;

            _logger.LogDebug("Getting the email to recipient list base on request");
            var to = _recipientService.GetToRecipients(request);

            _logger.LogDebug("Email will be sent to {@EmailTo}", to);

            // Request from R. Wainwright, subject line of the email should
            // be [Region abbreviation + PREOC] – [A new EAF has been submitted to ETEAMS]
            // however, it is difficult to know the region abbreviation

            string subject = "A new EAF has been submitted to ETEAMS";

            if (!string.IsNullOrEmpty(eteamSettings.Environment))
            {
                subject = $"[{eteamSettings.Environment}] {subject}";
            }

            var message = new Message(to, subject, content, attachments);

            message.AddCc(_recipientService.GetCcRecipients(request));
            message.AddBcc(_recipientService.GetBccRecipients(request));

            await _sender.SendEmailAsync(message);
        }
Пример #12
0
        private string FormatMission(ExpenseAuthorizationRequest source)
        {
            int amountRequested = (int)Math.Ceiling(source.AmountRequested);

            return(source.Description + "\nAmount Requested: " + amountRequested);
        }
Пример #13
0
 /// <summary>Gets to recipients.</summary>
 /// <param name="request">The request.</param>
 /// <returns>
 ///   <br />
 /// </returns>
 public IList <string> GetToRecipients(ExpenseAuthorizationRequest request) => Split(_ => _.DefaultSendTo);
Пример #14
0
        public IList <string> GetBccRecipients(ExpenseAuthorizationRequest request)
        {
            var recipients = GetRecipients(request, _ => _.Bcc, _ => _.GetBcc());

            return(recipients);
        }
Пример #15
0
 /// <summary>Gets the BCC recipients.</summary>
 /// <param name="request">The request.</param>
 /// <returns>
 ///   <br />
 /// </returns>
 public IList <string> GetBccRecipients(ExpenseAuthorizationRequest request) => Split(_ => _.Bcc);
Пример #16
0
 public EmailTemplate Apply(ExpenseAuthorizationRequest data)
 {
     Apply(data, "Request.");
     return(this);
 }
Пример #17
0
        private string FormatEstimatedResourceCost(ExpenseAuthorizationRequest source)
        {
            int expenditureNotToExceed = (int)Math.Ceiling(source.ExpenditureNotToExceed);

            return(expenditureNotToExceed.ToString());
        }