Skip to content

Determining if Member is New in the MemberSavedNotification? #20071

@mistyn8

Description

@mistyn8

Umbraco 13.9.2

So although the documentation doesn't seem to contain this little gem for members.. (rather than the IRememberBeingDirty)

Testing it out I don't get it detecting isNew.. I think it's because we still have two events with one suppressed so even though on the face of it var isNew = member.CreateDate == member.UpdateDate; to the millisecond looks the same when looking at the ticks they are different??

Image

perhaps var isNew = Math.Abs((entity.UpdateDate - entity.CreateDate).TotalSeconds) < 1; or some other arbitrary level of nearness?

We've resolved this via #18207 to be released in 13.8 and 15.3.

We decided against introducing PR with the MemberCreatedNotification - mainly for consistency reasons (we don't have them on all other entities, so it's a bit odd to have them just for member). But as others have said, thanks very much for your efforts on providing that @skttl and leading us to consider how best to tackle this.

Instead we've gone with adding an overload to supress specific notifications when saving members via the IMemberService. We then use that when creating members to ensure just one Saved notification is published.

The following notification handler shows how you can use this to respond to a member being saved, determining if they were created or updated in the save operation.

using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;

namespace Umbraco.Cms.Web.UI.Custom;

public class MemberSavedNotificationHandler : INotificationHandler<MemberSavedNotification>
{
    private readonly ILogger<MemberSavedNotificationHandler> _logger;

    public MemberSavedNotificationHandler(ILogger<MemberSavedNotificationHandler> logger) => _logger = logger;

    public void Handle(MemberSavedNotification notification)
    {
        foreach (IMember member in notification.SavedEntities)
        {
            var isNew = member.CreateDate == member.UpdateDate;
            _logger.LogInformation("Member {MemberId} {MemberName} was {Action}.", member.Id, member.Name, isNew ? "created" : "updated");
        }
    }
}

Originally posted by @AndyButland in #12673


This item has been added to our backlog AB#56362

Metadata

Metadata

Assignees

No one assigned

    Labels

    state/sprint-candidateWe're trying to get this in a sprint at HQ in the next few weeks

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions