public async Task <IActionResult> Post([FromBody] ScreeningRequest screeningRequest)
        {
            try
            {
                var user = new User(HttpContext.User);

                var ministries = await ScreeningRequest.GetMinistryScreeningTypesAsync(_dynamicsClient);

                var siteMinderMinistry    = ministries.FirstOrDefault(m => m.Name == user.Ministry);
                var siteMinderProgramArea = siteMinderMinistry?.ProgramAreas.FirstOrDefault(a => a.Name == user.ProgramArea);
                var screeningType         = siteMinderProgramArea?.ScreeningTypes.Find(t => t.Value == screeningRequest.ScreeningType);

                // ensure ministry and program area from siteminder match a ministry and program area in our system
                if (siteMinderProgramArea == null)
                {
                    _logger.LogWarning(string.Join(Environment.NewLine, "Cannot find ministry and program area in database matching SiteMinder headers", "{@User}", "{@RetrievedMinistries}"), user, ministries);
                    return(Unauthorized());
                }

                // validate screening request
                try
                {
                    bool validationResult = await screeningRequest.Validate(_dynamicsClient, siteMinderMinistry, siteMinderProgramArea);

                    if (validationResult == false)
                    {
                        _logger.LogWarning("Validation failed for screening request {@ScreeningRequest} {@Ministry} {@ProgramArea}", screeningRequest, siteMinderMinistry, siteMinderProgramArea);
                        return(BadRequest());
                    }
                }
                catch (DynamicsEntityNotFoundException ex)
                {
                    _logger.LogError(ex, "Validation failed for screening request {@ScreeningRequest} {@Ministry} {@ProgramArea}", screeningRequest, siteMinderMinistry, siteMinderProgramArea);
                    return(BadRequest());
                }

                // validate user
                if (string.IsNullOrWhiteSpace(user.Email))
                {
                    _logger.LogWarning("Validation failed for user {@user} submitting screening request {@ScreeningRequest}", user, screeningRequest);
                    return(BadRequest());
                }

                // submit request to dynamics and return its new screeningId
                string screeningId = await screeningRequest.Submit(_dynamicsClient, _logger, user, screeningType, siteMinderProgramArea.Value);

                return(new JsonResult(new { screeningId }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to submit screening request {@ScreeningRequest}", screeningRequest);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Post([FromBody] ScreeningRequest screeningRequest)
        {
            try
            {
                var user = new User(HttpContext.User);

                var(ministry, programArea, screeningType) =
                    await ScreeningRequest.GetMinistryScreeningTypeAsync(
                        _dynamicsClient,
                        user.OrgCode,
                        screeningRequest.ScreeningType
                        );

                // validate screening request
                try
                {
                    bool validationResult = await screeningRequest.Validate(_dynamicsClient, ministry, programArea, screeningType);

                    if (validationResult == false)
                    {
                        _logger.LogWarning(
                            string.Join(
                                Environment.NewLine,
                                "Validation failed for screening request {@ScreeningRequest}",
                                "{@ministry}",
                                "{@programArea}",
                                "{@screeningType}"
                                ),
                            screeningRequest,
                            ministry,
                            programArea,
                            screeningType
                            );

                        return(BadRequest());
                    }
                }
                catch (DynamicsEntityNotFoundException ex)
                {
                    _logger.LogError(
                        ex,
                        string.Join(
                            Environment.NewLine,
                            "Validation failed for screening request {@ScreeningRequest}",
                            "{@ministry}",
                            "{@programArea}",
                            "{@screeningType}"
                            ),
                        screeningRequest,
                        ministry,
                        programArea,
                        screeningType
                        );

                    return(BadRequest());
                }

                // validate user
                if (string.IsNullOrWhiteSpace(user.Email))
                {
                    _logger.LogWarning("Validation failed for user {@user} submitting screening request {@ScreeningRequest}", user, screeningRequest);
                    return(BadRequest());
                }

                // submit request to dynamics and return its new screeningId
                string screeningId = await screeningRequest.Submit(_dynamicsClient, _logger, user, screeningType, programArea.Value);

                return(new JsonResult(new { screeningId }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to submit screening request {@ScreeningRequest}", screeningRequest);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }