HandleEvent(EntityInsertedEvent<NewsLetterSubscription> eventMessage) gets called twice

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Il y a 3 ans
Hello,
As per title, HandleEvent gets called twice when I'm trying to capture NewsLetterSubscription inserts.
I'm really not sure what could be causing this, i just go on the front page, enter a dummy email, and the event triggers twice. I removed all my code from inside the function to double-check and the behavior continues.

One thing i noticed though is that the call stack is shorter on the second call, it appears this particular element drops:


Also the emails aren't getting inserted twice in the database, so at least there is that.

Any clue what could be going on? It's not the end of the world as the system I'm trying to POST to behind can tell it already received the email, but still, i have not seen this behavior on other insert events.

This is on 4.40

Thanks!
Il y a 3 ans
You should handle the EmailSubscribedEvent to track new subscriptions instead.
Il y a 3 ans
Thanks for the reply, it would make sense but the HandleEvent is not getting triggered on this one, is it used the same way as EntityInsertedEvent<> ?
Il y a 3 ans
public partial class EventConsumer : IConsumer<EmailSubscribedEvent>
{
        public void HandleEvent(EmailSubscribedEvent subscribedEvent)
        {
        ...
        }
}
Il y a 3 ans
Hi Yidna,

so the code looks right as far as i can tell and It's not taking it for some obscure reason, see anything fishy in here? Thanks for your input.


namespace Nop.Plugin.Misc.Mautic
{
    public partial class MauticEventHandler : IConsumer<EmailSubscribedEvent>, IConsumer<EntityInsertedEvent<Customer>>, IConsumer<EntityInsertedEvent<NewsLetterSubscription>>
    {
        private readonly ISettingService _settingService;
        private readonly ICustomerService _customerService;
        private readonly IGenericAttributeService _genericAttributeService;
        
    public MauticEventHandler
        (
            ISettingService settingService,
            ICustomerService customerService,
            IGenericAttributeService genericAttributeService
        )
        {
            this._settingService = settingService;
            this._customerService = customerService;
            this._genericAttributeService = genericAttributeService;
        }

        public void HandleEvent(EntityInsertedEvent<NewsLetterSubscription> eventMessage)
        {
        }

        public void HandleEvent(EmailSubscribedEvent subscribedEvent)
        {
        }

        public void HandleEvent(EntityInsertedEvent<Customer> eventMessage)
        {
        }
  }
}
Il y a 3 ans
Debugging my way back I think it has to do with NewsLetterSubscription.Active defaulting to false in the NewsletterController:


                    subscription = new NewsLetterSubscription
                    {
                        NewsLetterSubscriptionGuid = Guid.NewGuid(),
                        Email = email,
                        Active = false,
                        StoreId = _storeContext.CurrentStore.Id,
                        CreatedOnUtc = DateTime.UtcNow
                    };


The value is checked downstream in the Service to publish the event or not.
Should it default to false when a user subscribes? that seems counterintuitive but maybe i'm missing something?


edit: can confirm setting the value to true triggers the event, I'm just not sure about the implications if any...
Il y a 3 ans
While i was at it, I debugged the double call for the insert, one comes from the repository and one from the service, I think the service one should be removed as the repository one is generic for all entities, but then again not sure what the implications are
Il y a 3 ans
Well, you are right. We should recheck this. Here is a work item.
Thanks for your investigation.
Il y a 3 ans
Done. Please see this commit for more details
Il y a 3 ans
Thanks for the quick review!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.