Example #1
0
        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="arg">The argument.</param>
        /// <param name="context">The context.</param>
        /// <returns>
        /// The <see cref="Customer" />.
        /// </returns>
        public override async Task <Customer> Run(DataRow arg, CommercePipelineExecutionContext context)
        {
            if (arg == null)
            {
                return(null);
            }

            var customer = new Customer();

            try
            {
                var profileProperties = context.GetPolicy <ProfilePropertiesPolicy>();

                // get profile definition
                var definitionResult = (await this._getProfileDefinitionPipeline.Run(string.Empty, context).ConfigureAwait(false)).ToList();
                if (!definitionResult.Any())
                {
                    return(customer);
                }

                context.CommerceContext.AddUniqueObjectByType(definitionResult);
                var profileDefinition = definitionResult.FirstOrDefault(d => d.Name.Equals(profileProperties?.UserObjectType, StringComparison.OrdinalIgnoreCase));

                // map base properties

                var    definitionProperty = profileDefinition.Properties.FirstOrDefault(p => p.Name.Equals(profileProperties?.UserIdProperty, StringComparison.OrdinalIgnoreCase));
                string friendlyId;
                if (Guid.TryParse(arg[definitionProperty.ColumnName] as string, out var id))
                {
                    friendlyId = id.ToString("D", CultureInfo.InvariantCulture);
                }
                else
                {
                    // Azure search naming restriction
                    byte[] toEncodeAsBytes = System.Text.Encoding.ASCII.GetBytes(arg[definitionProperty.ColumnName] as string);
                    friendlyId = System.Convert.ToBase64String(toEncodeAsBytes).TrimEnd('=');
                }

                customer.AccountNumber = friendlyId;
                customer.FriendlyId    = friendlyId;
                customer.Id            = $"{CommerceEntity.IdPrefix<Customer>()}{friendlyId}";

                // map custom properties to CustomerDetailsComponent
                customer = ComponentsHelper.CustomerDetailsGenerator(arg, customer, profileDefinition, context);

                customer.SetComponent(new ListMembershipsComponent
                {
                    Memberships = new List <string>
                    {
                        CommerceEntity.ListName <Customer>()
                    }
                });

                return(customer);
            }
            catch (Exception ex)
            {
                await context.CommerceContext.AddMessage(
                    context.GetPolicy <KnownResultCodes>().Warning,
                    "EntityNotFound",
                    new object[] { arg["u_user_id"] as string, arg["u_email_address"] as string, ex },
                    $"Profile { arg["u_email_address"] as string } was not found.").ConfigureAwait(false);

                return(customer);
            }
        }
Example #2
0
        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="arg">The argument.</param>
        /// <param name="context">The context.</param>
        /// <returns>
        /// The <see cref="Customer" />.
        /// </returns>
        public override async Task <Customer> Run(Customer arg, CommercePipelineExecutionContext context)
        {
            if (arg == null)
            {
                return(null);
            }

            var detailsComponent = arg.GetComponent <CustomerDetailsComponent>();
            var details          = detailsComponent?.View.ChildViews.FirstOrDefault(v => v.Name.Equals("Details", StringComparison.OrdinalIgnoreCase)) as EntityView;

            if (details == null || details.Properties.Count == 0)
            {
                return(arg);
            }

            // get profile definition
            var definitionResult = context.CommerceContext.GetObject <IEnumerable <ProfileDefinition> >();

            if (definitionResult == null || !definitionResult.Any())
            {
                return(arg);
            }

            var profileProperties   = context.GetPolicy <ProfilePropertiesPolicy>();
            var profileDefinition   = definitionResult.FirstOrDefault(d => d.Name.Equals(profileProperties?.UserObjectType, StringComparison.OrdinalIgnoreCase));
            var listProperty        = profileDefinition?.Properties.FirstOrDefault(p => p.TypeName == "PROFILE" && p.ProfileReference.Equals("Profile Definitions.Address", StringComparison.Ordinal));
            var addressListProperty = details.Properties.FirstOrDefault(p => p.Name.Equals($"{listProperty?.GroupName}-{listProperty?.Name}", StringComparison.OrdinalIgnoreCase));
            var addressList         = addressListProperty?.RawValue as string[];

            if (addressList == null)
            {
                return(arg);
            }

            var commerceContext = context?.CommerceContext;

            profileDefinition = definitionResult.FirstOrDefault(d => d.Name.Equals(profileProperties?.AddressType, StringComparison.OrdinalIgnoreCase));
            foreach (var addressId in addressList)
            {
                try
                {
                    var addressComponent = await ComponentsHelper.AddressComponentGenerator(addressId.ToString(), profileDefinition, context);

                    if (addressComponent != null)
                    {
                        arg.Components.Add(addressComponent);
                    }
                }
                catch (Exception ex)
                {
                    await context.CommerceContext.AddMessage(
                        context.GetPolicy <KnownResultCodes>().Warning,
                        "EntityNotFound",
                        new object[] { addressId, ex },
                        $"Profile address { addressId } was not found.");

                    return(arg);
                }
            }

            details.Properties.Remove(addressListProperty);
            return(arg);
        }
Example #3
0
        /// <summary>
        /// Gets the profile definition.
        /// </summary>
        /// <param name="definitionName">Name of the definition.</param>
        /// <returns>A <see cref="ProfileDefinition"/></returns>
        internal async Task <ProfileDefinition> GetProfileDefinition(string definitionName)
        {
            // Hook up the SQL server context
            using (var connection = new SqlConnection(this.ConnectionString))
            {
                await connection.OpenAsync();

                var command = connection.CreateCommand();
                command.CommandText = "sp_GetProfileProps";
                command.CommandType = CommandType.StoredProcedure;

                var categoryName = command.CreateParameter();
                categoryName.ParameterName = "@u_cat_name";
                categoryName.DbType        = DbType.String;
                categoryName.Value         = "Profile Definitions";
                command.Parameters.Add(categoryName);

                var profName = command.CreateParameter();
                profName.ParameterName = "@u_prof_name";
                profName.DbType        = DbType.String;
                profName.Value         = definitionName;
                command.Parameters.Add(profName);

                SqlDataAdapter adapter = new SqlDataAdapter(command);

                DataSet ds = new DataSet();
                adapter.Fill(ds);

                connection.Close();

                DataTable propertiesTable = ds.Tables[0];

                // definition name not found
                if (propertiesTable.Rows.Count == 0)
                {
                    return(null);
                }

                var definition = new ProfileDefinition {
                    Name = definitionName
                };

                foreach (DataRow row in propertiesTable.Rows)
                {
                    if (!(row["ClassDefName"] as string).StartsWith("ParentClass_", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    var profileProperty = new ProfileProperty
                    {
                        Name             = row["MemberDefName"] as string,
                        DisplayName      = row["DisplayName"] as string,
                        TypeName         = row["TypeName"] as string,
                        ProfileReference = row["ReferenceString"] as string,
                        IsMultiValued    = Convert.ToBoolean(row["IsMultiValued"]),
                        ColumnName       = string.IsNullOrEmpty(row["ColumnName"] as string) ? row["ValColumnName"] as string : row["ColumnName"] as string,
                        IsHidden         = false,
                        IsReadOnly       = false
                    };

                    profileProperty.OriginalType = ComponentsHelper.GetPropertyOriginalType(profileProperty.TypeName);
                    definition.Properties.Add(profileProperty);
                }

                propertiesTable = ds.Tables[1];
                foreach (DataRow row in propertiesTable.Rows)
                {
                    var id       = row["GroupMemDefName"] as string;
                    var property = definition.Properties.FirstOrDefault(p => p.Name.Equals(id, StringComparison.Ordinal));
                    if (property != null)
                    {
                        property.GroupName = row["GroupName"] as string;
                    }
                }

                propertiesTable = ds.Tables[2];
                foreach (DataRow row in propertiesTable.Rows)
                {
                    var id       = row["MemberName"] as string;
                    var property = definition.Properties.FirstOrDefault(p => p.Name.Equals(id, StringComparison.Ordinal));
                    if (property == null)
                    {
                        continue;
                    }

                    switch (row["AttribName"] as string)
                    {
                    case "MaxLength":
                        property.MaxLength = Convert.ToInt32(row["ValStr"]);
                        break;

                    case "MinLength":
                        property.MinLength = Convert.ToInt32(row["ValStr"]);
                        break;
                    }
                }

                return(definition);
            }
        }