Esempio n. 1
0
        public async Task <ActionResult> EditAgent(EditAgentViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _agencyService.UpdateAgentAsync(model, User.Identity.GetUserId());

                if (!result)
                {
                    this.AddToastMessage(Resource.Error, Resource.General_Operation_Failed, ToastType.Error);
                }
                else
                {
                    this.AddToastMessage(Resource.Success, Resource.General_Operation_Successfull, ToastType.Success);
                }
                return(RedirectToAction(nameof(EditAgent), new { id = model.AgentId }));
            }
            model.Languages = CultureHelper.GetAvailableAgentCultures().Select(c => new AddLanguageModel {
                Item = new SelectListItem {
                    Value = c.Name, Text = c.NativeName
                }
            }).ToList();
            model.Branches = _agencyService.GetBranches(User.Identity.GetUserId()).Select(c => new SelectListItem {
                Value = c.Id.ToString(), Text = c.Name
            }).ToList();
            return(View(model));
        }
        public ActionResult Edit(EditAgentViewModel editAgentViewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var isUser = _iUserMaster.CheckUserIdExists(editAgentViewModel.UserId);
                    if (isUser)
                    {
                        var createUserViewModel = _iUserMaster.EditUserbyUserId(editAgentViewModel.UserId);

                        if (createUserViewModel.EmailId != editAgentViewModel.EmailId)
                        {
                            if (_iUserMaster.CheckEmailIdExists(editAgentViewModel.EmailId))
                            {
                                ModelState.AddModelError("", "EmailId already exists");
                                return(View(editAgentViewModel));
                            }
                        }

                        if (createUserViewModel.MobileNo != editAgentViewModel.MobileNo)
                        {
                            if (_iUserMaster.CheckMobileNoExists(editAgentViewModel.MobileNo))
                            {
                                ModelState.AddModelError("", "MobileNo already exists");
                                return(View(editAgentViewModel));
                            }
                        }

                        var result = _iUserMaster.UpdateAgentDetails(editAgentViewModel);
                        TempData["MessageUpdateAgent"]    = "Agent Details Updated Successfully";
                        editAgentViewModel.ListofCategory = _category.GetAllActiveSelectListItemCategory();
                        return(View(editAgentViewModel));
                    }
                    else
                    {
                        editAgentViewModel.ListofCategory    = _category.GetAllActiveSelectListItemCategory();
                        TempData["MessageCreateUsersErrors"] = "Agent Details doesn't exist";
                        return(View(editAgentViewModel));
                    }
                }
                else
                {
                    editAgentViewModel.ListofCategory = _category.GetAllActiveSelectListItemCategory();
                    return(View("Edit", editAgentViewModel));
                }
            }
            catch
            {
                throw;
            }
        }
Esempio n. 3
0
        public async Task <EditAgentViewModel> GetAgent(int?id, string getUserId)
        {
            var agency = await _dbContext.Agencies.FirstOrDefaultAsync(c => c.ManagerId.Equals(getUserId));

            var model = new EditAgentViewModel();

            if (agency != null)
            {
                var agent = agency.Agents.FirstOrDefault(c => c.Id == id);
                if (agent != null)
                {
                    model.SelectedBranchId = agent.BranchId;
                    model.FirstName        = agent.User.FirstName;
                    model.LastName         = agent.User.LastName;
                    model.Education        = agent.Education;
                    model.Email            = agent.User.Email;
                    model.Languages        = CultureHelper.GetAvailableAgentCultures().Select(c => new AddLanguageModel
                    {
                        Item = new SelectListItem
                        {
                            Value    = c.Name, Text = c.NativeName,
                            Selected = agent.Languages.Any(t => t.LanguageCulture == c.Name)
                        },
                        LanguageLevel = agent.Languages.FirstOrDefault(t => t.LanguageCulture == c.Name)?.Level
                    }).ToList();


                    model.Qualifications = Enum.GetValues(typeof(QualificationType))
                                           .Cast <QualificationType>()
                                           .Select(c => new AddQualificationModel {
                        QualificationType = c, Selected = (agent.Qualifications.Any(m => m.QualificationType == c))
                    })
                                           .ToList();

                    model.Branches = GetBranches(getUserId).Select(c => new SelectListItem {
                        Value = c.Id.ToString(), Text = c.Name
                    }).ToList();
                    model.FieldOfResponsibility = agent.FieldOfResponsibility;
                    model.PositionInCompany     = agent.PositionInCompany;
                    model.SelectedBranchId      = agent.BranchId;
                    model.AgentId = agent.Id;
                }
            }
            return(model);
        }
Esempio n. 4
0
        public async Task <bool> UpdateAgentAsync(EditAgentViewModel model, string userId)
        {
            var agency = await _dbContext.Agencies.FirstOrDefaultAsync(c => c.ManagerId.Equals(userId));

            if (agency != null)
            {
                var agent = agency.Agents.FirstOrDefault(c => c.Id == model.AgentId);
                if (agent != null)
                {
                    agent.User.FirstName        = model.FirstName;
                    agent.User.LastName         = model.LastName;
                    agent.Education             = model.Education;
                    agent.FieldOfResponsibility = model.FieldOfResponsibility;
                    agent.PositionInCompany     = model.PositionInCompany;
                    agent.BranchId = model.SelectedBranchId;
                    foreach (var languageModel in model.Languages)
                    {
                        if (languageModel.Item.Selected && agent.Languages.All(c => c.LanguageCulture != languageModel.Item.Value))
                        {
                            agent.Languages.Add(new Language {
                                LanguageCulture = languageModel.Item.Value, Level = languageModel.LanguageLevel.GetValueOrDefault()
                            });
                        }
                        if (languageModel.Item.Selected && agent.Languages.Any(c => c.LanguageCulture == languageModel.Item.Value))
                        {
                            var lang = agent.Languages.FirstOrDefault(c => c.LanguageCulture == languageModel.Item.Value);
                            if (lang != null)
                            {
                                lang.Level = languageModel.LanguageLevel.GetValueOrDefault(LanguageLevel.Basic);
                            }
                        }
                        else if (!languageModel.Item.Selected && agent.Languages.Any(c => c.LanguageCulture == languageModel.Item.Value))
                        {
                            var item = agent.Languages.FirstOrDefault(c => c.LanguageCulture == languageModel.Item.Value);
                            if (item != null)
                            {
                                _dbContext.Languages.Remove(item);
                            }
                        }
                    }
                    foreach (var qual in model.Qualifications)
                    {
                        if (qual.Selected && qual.QualificationType.HasValue && agent.Qualifications.All(c => c.QualificationType != qual.QualificationType.Value))
                        {
                            agent.Qualifications.Add(new Qualification {
                                QualificationType = qual.QualificationType.Value
                            });
                        }
                        else if (!qual.Selected && qual.QualificationType.HasValue && agent.Qualifications.Any(c => c.QualificationType == qual.QualificationType.Value))
                        {
                            var item = agent.Qualifications.FirstOrDefault(c => c.QualificationType == qual.QualificationType.Value);
                            if (item != null)
                            {
                                _dbContext.Qualifications.Remove(item);
                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(model.Password) && model.Password.Equals(model.ConfirmPassword))
                    {
                        var result = await _authManager.ChangePasswordAsync(agent.UserId, model.Password);

                        return(result.Succeeded);
                    }
                }
                await _dbContext.SaveChangesAsync();

                return(true);
            }
            return(false);
        }