Example #1
0
        public async Task <IActionResult> GetDispatches([FromHeader(Name = "Authorization")] string authToken)
        {
            ACMGenericResult <DispatchesAndQueueDetails> response;

            if (!(await AuthTokenValidation.ValidateBearerToken(authToken)))
            {
                return(Unauthorized(SharedSettings.AuthorizationDenied));
            }
            else
            {
                response = await ConfigService.GetDispatches(authToken);
            }
            if (response.StatusCode == 400)
            {
                return(StatusCode(400, "Settings/Dispatches received from WXM were Null/Empty"));
            }
            return(StatusCode(response.StatusCode, response.Value));
        }
        public async Task <IActionResult> DispatchRequest([FromHeader(Name = "Authorization")] string authToken,
                                                          List <DispatchRequest> request)
        {
            try
            {
                if (request == null)
                {
                    return(BadRequest("Bad Request"));
                }

                // Fetch account configuration to be used through the whole request.
                AccountConfiguration accConfiguration = GetAccountConfiguration().Result;
                if (accConfiguration == null)
                {
                    EventLogList.AddEventByLevel(2, SharedSettings.NoConfigInSPA, null);
                    await EventLogList.AddEventLogs(ViaMongoDB);

                    return(StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError, SharedSettings.NoConfigInSPA));
                }

                // Validate Auth token(Basic or Bearer) and reject if fail.
                if (!AuthTokenValidation.ValidateBearerToken(authToken, accConfiguration))
                {
                    EventLogList.AddEventByLevel(2, SharedSettings.AuthorizationDenied, null, null);
                    await EventLogList.AddEventLogs(ViaMongoDB);

                    return(Unauthorized(SharedSettings.AuthDeniedResponse));
                }

                // Check for Payload size and number of Dispatches
                if (!PayloadValidation.ValidateRequestPayloadSize(request, EventLogList))
                {
                    await EventLogList.AddEventLogs(ViaMongoDB);

                    return(StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status413PayloadTooLarge, SharedSettings.PayLoadTooLarge));
                }

                //Generate batch ID for the request
                string batchId = Guid.NewGuid().ToString();

                // Check for sampling
                if (accConfiguration.ExtendedProperties.TryGetValue("Sampler", out string samplername))
                {
                    if (SharedSettings.AvailableSamplers.TryGetValue(samplername, out ISampler sampler))
                    {
                        await sampler.IsSampledAsync(request);
                    }
                    else
                    {
                        EventLogList.AddEventByLevel(4, SharedSettings.NoSamplingConfigured, batchId);
                    }
                }

                BatchResponse batchResponse = new BatchResponse()
                {
                    BatchId          = batchId,
                    StatusByDispatch = new List <StatusByDispatch>()
                };

                try
                {
                    ProcessInvitations processInvitations = new ProcessInvitations(authToken, ViaMongoDB, batchId,
                                                                                   EventLogList, accConfiguration);

                    bool res = processInvitations.GetAllInfoForDispatch();
                    if (!res)
                    {
                        EventLogList.AddEventByLevel(2, SharedSettings.APIResponseFail, batchId, null);
                        await EventLogList.AddEventLogs(ViaMongoDB);

                        return(StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError,
                                          SharedSettings.APIResponseFail));
                    }

                    await processInvitations.CheckDispatchData(request, batchId, batchResponse);
                }
                catch (Exception ex)
                {
                    EventLogList.AddExceptionEvent(ex, batchId, null, null, null, SharedSettings.DispatchControllerEx2);
                    await EventLogList.AddEventLogs(ViaMongoDB);

                    return(ex.Message switch
                    {
                        SharedSettings.AuthorizationDenied => Unauthorized(ex.Message),
                        _ => StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError, ex.Message)
                    });
                }