Page 1 of 1

Rest API subscription and Subscriber issue

PostPosted: Tue Feb 22, 2022 3:46 pm
by SBarnes
I think there is a bug in the REST API in that the below code from the start up does not take into consideration if the subscriber is enabled or not?


Code: Select all
 _webhookSubscribers = AppHost.GetDbConnection().Select<SY_WebhookSubscriber>();
            // Read all the web hook subscriptions
            _webhookSubscriptions = AppHost.GetDbConnection().Select<SY_WebhookSubscription>();

Re: Rest API subscription and Subscriber issue  Topic is solved

PostPosted: Tue Feb 22, 2022 5:05 pm
by Mike.Sheen
SBarnes wrote:I think there is a bug in the REST API in that the below code from the start up does not take into consideration if the subscriber is enabled or not?


Code: Select all
 _webhookSubscribers = AppHost.GetDbConnection().Select<SY_WebhookSubscriber>();
            // Read all the web hook subscriptions
            _webhookSubscriptions = AppHost.GetDbConnection().Select<SY_WebhookSubscription>();


Fixed in DEV-8178

Re: Rest API subscription and Subscriber issue

PostPosted: Tue Feb 22, 2022 5:09 pm
by SBarnes
Can I get the code I need to retrofit it into 7.2.0 SR6?

Re: Rest API subscription and Subscriber issue

PostPosted: Tue Feb 22, 2022 6:08 pm
by Mike.Sheen
SBarnes wrote:Can I get the code I need to retrofit it into 7.2.0 SR6?


Code: Select all
public void Post(WebhooksEventPOSTRequest request)
{
   // This is the route invoked by clients to indicate an event has occurred.

   // Check key               
   Helper.Service.ValidateInternalRequest(this.Request);

   // Add items to the message queue with status 0 (not sent) whose subscriber is enabled.
   foreach (var activeSubscription in RESTAPIPlugin.WebhookSubscriptions.Join(RESTAPIPlugin.WebhookSubscribers,
           x => x.SY_WebhookSubscriber_RecID,
           y => y.RecID,
            (subscription, subscriber) => new {IsEnabled = subscriber.IsEnabled, EventName = subscription.EventName, URL = subscription.URL, RecID = subscription.RecID, SY_WebhookSubscriber_RecID = subscriber.RecID })
            .Where(z => z.IsEnabled == true && z.EventName == request.EventName))
   {
      SY_WebhookMessage newMessage = new SY_WebhookMessage { RecID = System.Guid.NewGuid(), SY_WebhookSubscription_RecID = activeSubscription.RecID, Body = request.Body, Status = 0, Retries = 0 };
      Db.ExecuteSql(@"INSERT INTO SY_WebhookMessage (RecID, SY_WebhookSubscription_RecID, Body, ItemNo, Status, LastSavedDateTime, AddedDateTime, Retries)
                        SELECT @RecID, @SY_WebhookSubscription_RecID, @Body, (SELECT COALESCE(MAX(ItemNo), 0) + 1 FROM SY_WebhookMessage), @Status, SYSUTCDATETIME(), SYSUTCDATETIME(), @Retries WHERE @SY_WebhookSubscription_RecID IN (SELECT RecID FROM SY_WebhookSubscription)",
                 new { RecID = newMessage.RecID, SY_WebhookSubscription_RecID = newMessage.SY_WebhookSubscription_RecID, Body = newMessage.Body, Status = newMessage.Status, Retries = newMessage.Retries });

      // read back and keep that in our internal list as our item no and lastsaved date time is set at the database level.
      newMessage = Db.SingleById<SY_WebhookMessage>(newMessage.RecID);

      if(newMessage != null)
      {
         RESTAPIPlugin.WebhookMessages.Add(newMessage);
         // send to url         
         WebHookController.CallWebhook(newMessage);               
      }
   }
}