Esempio n. 1
0
        /// <summary>
        /// The get time zone for batch import.
        /// </summary>
        /// <param name="emailCell">
        /// The email cell.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        private int GetTimeZoneIdForBatchImport(IXLCell emailCell)
        {
            int result;

            if (
                !int.TryParse(
                    emailCell.With(x => x.CellRight()).With(x => x.Value.ToString()),
                    NumberStyles.Any,
                    CultureInfo.InvariantCulture,
                    out result))
            {
                return(int.Parse(this.settings.BatchImportDefaultTimeZoneId));
            }

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// The upload batch of users.
        /// </summary>
        /// <param name="company">
        /// The company.
        /// </param>
        /// <param name="fileStream">
        /// The file stream.
        /// </param>
        /// <param name="type">
        /// The type.
        /// </param>
        /// <param name="failed">
        /// The failed.
        /// </param>
        /// <param name="error">
        /// The error.
        /// </param>
        /// <param name="sendActivation">
        /// The send Activation.
        /// </param>
        /// <param name="notifyViaRTMP">
        /// The notify via RTMP.
        /// </param>
        /// <returns>
        /// The <see cref="IEnumerable{User}"/>.
        /// </returns>
        public IEnumerable <User> UploadBatchOfUsers(
            Company company,
            Stream fileStream,
            string type,
            out List <string> failed,
            out string error,
            Action <User> sendActivation = null,
            // ReSharper disable once InconsistentNaming
            bool notifyViaRTMP = true)
        {
            var result = new List <User>();

            failed = new List <string>();
            error  = null;
            try
            {
                var          bookToRead = new XLWorkbook(fileStream);
                IXLWorksheet sheet      = bookToRead.Worksheets.FirstOrDefault();
                if (sheet != null)
                {
                    var     license           = company.Licenses.FirstOrDefault();
                    var     currentUsersCount = this.GetCountForCompany(company.Id).Value;
                    IXLRows rows = sheet.RowsUsed();
                    foreach (IXLRow row in rows)
                    {
                        IXLCell nameCell          = row.FirstCellUsed();
                        string  nameValue         = nameCell.With(x => x.Value.ToString());
                        IXLCell familyNameCell    = nameCell.With(x => x.CellRight());
                        string  familyNameValue   = familyNameCell.With(x => x.Value.ToString());
                        IXLCell emailCell         = familyNameCell.With(x => x.CellRight());
                        string  emailValue        = emailCell.With(x => x.Value.ToString());
                        IXLCell passwordCell      = emailCell.With(x => x.CellRight());
                        string  passwordCellValue = passwordCell.With(x => x.Value.ToString());
                        if (!string.IsNullOrWhiteSpace(emailValue) && emailValidator.Match(emailValue).Success)
                        {
                            if (this.GetOneByEmail(emailValue).Value != null)
                            {
                                failed.Add(string.Format("{0}{1}{2}{3}", nameValue, familyNameValue, emailValue, ": Email exist"));
                                continue;
                            }

                            if (license != null && license.TotalLicensesCount < currentUsersCount + result.Count + 1)
                            {
                                error = "Max users amount is reached";
                                return(result);
                            }

                            try
                            {
                                var instance = new User
                                {
                                    Email        = emailValue,
                                    FirstName    = nameValue,
                                    LastName     = familyNameValue,
                                    Company      = company,
                                    DateCreated  = DateTime.Now,
                                    DateModified = DateTime.Now,
                                    Status       = UserStatus.Inactive,
                                    Language     =
                                        this.languageModel.GetOneById(
                                            this.GetLanguageIdBatchImport(emailCell)).Value,
                                    TimeZone =
                                        this.timeZoneModel.GetOneById(
                                            this.GetTimeZoneIdForBatchImport(emailCell)).Value,
                                    UserRole =
                                        this.userRoleModel.GetOneById((int)UserRoleEnum.User)
                                        .Value,
                                    CreatedBy  = company.PrimaryContact,
                                    ModifiedBy = null
                                };
                                if (string.IsNullOrWhiteSpace(passwordCellValue))
                                {
                                    instance.SetPassword(Password.CreateAlphaNumericRandomPassword(8));
                                    if (sendActivation != null)
                                    {
                                        sendActivation(instance);
                                    }
                                }
                                else
                                {
                                    instance.Status = UserStatus.Active;
                                    instance.SetPassword(passwordCellValue);
                                }

                                this.RegisterSave(instance, true);
                                //if (notifyViaRTMP)
                                //{
                                //    IoC.Resolve<RealTimeNotificationModel>()
                                //        .NotifyClientsAboutChangesInTable<User>(
                                //            NotificationType.Update,
                                //            instance.Company.Id,
                                //            instance.Id);
                                //}

                                result.Add(instance);
                            }
                            catch (Exception ex)
                            {
                                failed.Add(
                                    string.Format(
                                        "{0}{1}{2}{3}",
                                        nameValue,
                                        familyNameValue,
                                        emailValue,
                                        ": Creation error + " + ex.With(x => x.Message)));
                            }
                        }
                        else
                        {
                            failed.Add(
                                string.Format("{0}{1}{2}{3}", nameValue, familyNameValue, emailValue, ": Invalid email"));
                        }
                    }
                }
                else
                {
                    error = "Wrong excel file";
                }
            }
            catch (Exception ex)
            {
                error = ex.ToString();
            }

            return(result);
        }