Avoiding duplicates on uSync Member Import
# help-with-umbraco
g
Hi, we are merging two websites together and we are also migrating the members. I already migrated all the members from the first website. Now I need to import cca 100k members from the other website. In the same way as I migrated the members from first website, I prepared a batch of members in
usync
archive. The problem is that potentially the members were using both websites and in some cases the usernames for one member are different, but the emails are the same. So with current approach I get duplicated email members which is obviously wrong. Is there any uSync extension where I could override the matching of the user not to be on username but rather on email? I was checking this functionality https://docs.jumoo.co.uk/usync/complete/guides/Exporter/#validating-sync-pack Is it possible to "edit" the import pack before importing? We are using Umbraco (13.7.0), uSync Complete (13.1.9) and it's Exporter functionality.
It looks like this event handler could be helpfull, but unfortunatelly I cannot inject IMemberService inside to check if the member with the email already exist.
I tried now with the
uSyncImportingItemNotification
and the users are not duplicated anymore:
Copy code
cs
public class MemberImportNotificationHandler(IMemberService memberService, ILogger<MemberImportNotificationHandler> logger) : INotificationHandler<uSyncImportingItemNotification>
{
    public void Handle(uSyncImportingItemNotification notification)
    {
        if (notification.Item is null)
        {
            return;
        }

        // import doc validation only for Member type
        if (notification.Item.Name == nameof(Member))
        {
            var memberImportDoc = notification.Item;
            var memberEmail = memberImportDoc.Element("Info")?.Element("Email")?.Value;

            if (!string.IsNullOrEmpty(memberEmail))
            {
                var existingUser = memberService.GetByEmail(memberEmail);

                if (existingUser is not null)
                {

                    // check if username differs
                    var userName = memberImportDoc.Element("Info")?.Element("Username")?.Value;

                    // replace existing username with the on in import file
                    if (existingUser.Username != userName)
                    {
                        // update key and username
                        memberImportDoc.SetAttributeValue(nameof(Member.Key), existingUser.Key);
                        memberImportDoc.Element("Info")!.Element("Username")!.SetValue(existingUser.Username);
                    }
                }
            }
        }
    }
}
Copy code
cs
builder.AddNotificationHandler<uSyncImportingItemNotification, MemberImportNotificationHandler>();
But now
Properties
properties from the XML file are not updated in the existing member in import. Properties in
Info
(Name, Created date) are updated. Would maybe @Kevin Jump know how to solve this issue?
I somehow solved it by the NotficationHandler. I change fields manually and cancel the import of current item:
Copy code
cs
public class MemberImportNotificationHandler(IMemberService memberService) : INotificationHandler<uSyncImportingItemNotification>
{
 public void Handle(uSyncImportingItemNotification notification)
 {
  // import doc validation only for Member type
  if (notification.Item?.Name == "Member")
  {
   var memberImportDoc = notification.Item;

   // get importing member email
   var memberEmail = memberImportDoc.Element("Info")?.Element("Email")?.Value;

   if (!string.IsNullOrEmpty(memberEmail))
   {
    // try find existing user
    var existingMember = memberService.GetByEmail(memberEmail);

    if (existingMember != null)
    {
     var importingMemberUserName = memberImportDoc.Element("Info")?.Element("Username")?.Value;

     // replace existing username with the on in import file
     if (existingMember.Username != importingMemberUserName)
     {
      // manually update member properties in Umbraco
      var memberImportProperties = memberImportDoc.Element("Properties");
      if (memberImportProperties != null)
      {
       bool memberUpdated = false;

       // zip code
       var zipCode = memberImportProperties.Element("zipCode")?.Value;
       if (zipCode != existingMember.GetValue("zipCode")?.ToString())
       {
        existingMember.SetValue("zipCode", zipCode);
        memberUpdated = true;
       }
       // country
       var country = memberImportProperties.Element("country")?.Value;
       if (country != existingMember.GetValue("country")?.ToString())
       {
        existingMember.SetValue("country", country);
        memberUpdated = true;
       }
       if (memberUpdated)
       {
        memberService.Save(existingMember);
        // cancel import, because it was updated manually
        notification.Cancel = true;
       }
      }
     }
    }
   }
 }
}
4 Views