Ejemplo n.º 1
0
        private List <WebhookParameter> ParseWebhookParameters(Webhook webhook)
        {
            List <WebhookParameter> webhookParameters = new List <WebhookParameter>();
            Uri webhookUri = new Uri(webhook.WebhookUrl);

            _logger.LogInformation($"Parsing query string for uri {webhookUri}.");
            var parameters = HttpUtility.ParseQueryString(webhookUri.Query);

            foreach (var key in parameters.Keys)
            {
                string value = parameters[key.ToString()];
                if (value.StartsWith("{") && value.EndsWith("}"))
                {
                    //trim the brankets
                    string parameterName = value.Substring(1, value.Length - 2);
                    _logger.LogInformation($"Creating webhook parameter {parameterName} for webhook {webhook.WebhookName}.");
                    WebhookParameter webhookParam = new WebhookParameter
                    {
                        OfferId = webhook.OfferId,
                        Name    = parameterName,
                        // TODO: do we need to indicate an incomplete parameter?
                        Value = string.Empty
                    };

                    webhookParameters.Add(webhookParam);
                }
            }

            return(webhookParameters);
        }
Ejemplo n.º 2
0
        private async Task UpdateWebhookParameters(Offer offer, Webhook webhook, long webhookId)
        {
            List <WebhookParameter>        incompleteParams = ParseWebhookParameters(webhook);
            List <WebhookWebhookParameter> joinEntries      = await _webhookWebhookParameterService.GetAllJoinEntries(webhookId);

            Dictionary <string, WebhookParameter> paramsDb = new Dictionary <string, WebhookParameter>();
            HashSet <string> usedParamNames = new HashSet <string>();

            // Populate paramsDb so that it maps the WebhookParameter name to the WebhookParameter object
            foreach (WebhookWebhookParameter entry in joinEntries)
            {
                WebhookParameter webhookParameter = await _context.WebhookParameters.FindAsync(entry.WebhookParameterId);

                if (!paramsDb.ContainsKey(webhookParameter.Name))
                {
                    paramsDb.Add(webhookParameter.Name, webhookParameter);
                }
            }

            foreach (WebhookParameter incompleteParam in incompleteParams)
            {
                // Check if a param with the same name as the incompleteParam already exists
                if (!paramsDb.ContainsKey(incompleteParam.Name))
                {
                    // A param with the same name as the incompleteParam does not exist, so create it
                    await _webhookParameterService.CreateAsync(offer.OfferName, webhookId, incompleteParam);
                }

                // Keep track of all the new parameters we are using in usedParamNames
                if (!usedParamNames.Contains(incompleteParam.Name))
                {
                    usedParamNames.Add(incompleteParam.Name);
                }
            }

            foreach (KeyValuePair <string, WebhookParameter> paramDb in paramsDb)
            {
                // Check if there is a param in the db that we are no longer using
                if (!usedParamNames.Contains(paramDb.Key))
                {
                    WebhookWebhookParameter webhookWebhookParameter = await _context.WebhookWebhookParameters.FindAsync(webhookId, paramDb.Value.Id);

                    // Remove the join entry for any unused params
                    _context.WebhookWebhookParameters.Remove(webhookWebhookParameter);
                    await _context._SaveChangesAsync();

                    _logger.LogInformation(LoggingUtils.ComposeResourceDeletedMessage(
                                               nameof(WebhookWebhookParameter),
                                               $"(webhookId={webhookId}, webhookParameterId={webhookWebhookParameter.WebhookParameter})",
                                               offerName: offer.OfferName));
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a webhookParameter object within an offer.
        /// </summary>
        /// <param name="offerName">The name of the offer.</param>
        /// <param name="webhookId">The id of the webhook that the parameter is associated with.</param>
        /// <param name="webhookParameter">The webhookParameter to create.</param>
        /// <returns>The created webhookParameter.</returns>
        public async Task <WebhookParameter> CreateAsync(string offerName, long webhookId, WebhookParameter webhookParameter)
        {
            if (webhookParameter is null)
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(WebhookParameter).Name),
                                                      UserErrorCode.PayloadNotProvided);
            }

            if (ExpressionEvaluationUtils.ReservedParameterNames.Contains(webhookParameter.Name))
            {
                _logger.LogInformation($"Webhook {webhookId} is referencing system parameter {webhookParameter.Name}");
                return(webhookParameter);
                //throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(WebhookParameter).Name,
                //    webhookParameter.Name, offerName: offerName));
            }
            _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(WebhookParameter).Name, webhookParameter.Name, offerName: offerName, payload: JsonSerializer.Serialize(webhookParameter)));

            // Get the offer associated with the offerName provided
            var offer = await _offerService.GetAsync(offerName);

            // Check if the WebhookParameter already exists
            if (await ExistsAsync(offerName, webhookParameter.Name))
            {
                // Just create a new join entry to keep track of the fact that this Webhook is using this parameter
                WebhookParameter webhookParameterDb = await GetAsync(offerName, webhookParameter.Name);

                await _webhookWebhookParameterService.CreateJoinEntryAsync(webhookId, webhookParameterDb.Id);

                return(webhookParameterDb);
            }

            // Set the FK to offer
            webhookParameter.OfferId = offer.Id;

            // Add webhookParameter to db
            _context.WebhookParameters.Add(webhookParameter);
            await _context._SaveChangesAsync();

            await _webhookWebhookParameterService.CreateJoinEntryAsync(webhookId, webhookParameter.Id);

            _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(WebhookParameter).Name, webhookParameter.Name, offerName: offerName));

            return(webhookParameter);
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> UpdateAsync(string offerName, string name, [FromBody] WebhookParameter webhookParameter)
        {
            AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true);
            if (webhookParameter == null)
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(webhookParameter)), UserErrorCode.PayloadNotProvided);
            }

            if (!name.Equals(webhookParameter.Name))
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(WebhookParameter).Name),
                                                      UserErrorCode.NameMismatch);
            }

            _logger.LogInformation($"Update webhook parameter {name} in offer {offerName} with payload {JsonSerializer.Serialize(webhookParameter)}.");
            await _webhookParameterService.UpdateAsync(offerName, name, webhookParameter);

            return(Ok(webhookParameter));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Updates a webhookParameter within an offer.
        /// </summary>
        /// <param name="offerName">The name of the offer.</param>
        /// <param name="parameterName">The name of the webhookParameter to update.</param>
        /// <param name="webhookParameter">The updated webhookParameter.</param>
        /// <returns>The updated webhookParameter.</returns>
        public async Task <WebhookParameter> UpdateAsync(string offerName, string parameterName, WebhookParameter webhookParameter)
        {
            if (webhookParameter is null)
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(WebhookParameter).Name),
                                                      UserErrorCode.PayloadNotProvided);
            }

            _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(WebhookParameter).Name, webhookParameter.Name, offerName: offerName, payload: JsonSerializer.Serialize(webhookParameter)));

            // Get the webhookParameter that matches the name provided
            var webhookParameterDb = await GetAsync(offerName, parameterName);

            // Copy over the changes
            webhookParameterDb.Copy(webhookParameter);

            // Update webhookParameterDb values and save changes in db
            _context.WebhookParameters.Update(webhookParameterDb);
            await _context._SaveChangesAsync();

            _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage(typeof(WebhookParameter).Name, webhookParameter.Name, offerName: offerName));

            return(webhookParameterDb);
        }