public async Task <ActionResult> RegisterAgency() { var model = new RegisterAgencyModel(); model.Type = CompanyType.Government; model = await InitRegisterCompany(model); return(View(model)); }
private async Task <RegisterAgencyModel> InitRegisterCompany(RegisterAgencyModel model) { model.Sectors = Enumerable.Empty <SelectListItem>(); model.Countries = Enumerable.Empty <SelectListItem>(); model.States = Enumerable.Empty <SelectListItem>(); model.Ministries = Enumerable.Empty <SelectListItem>(); var sectorResponse = await WepApiMethod.SendApiAsync <List <SectorModel> >(HttpVerbs.Get, $"Administration/Sector"); if (sectorResponse.isSuccess) { model.Sectors = new SelectList(sectorResponse.Data.OrderBy(o => o.Name), "Id", "Name", 0); } var countryResponse = await WepApiMethod.SendApiAsync <List <CountryModel> >(HttpVerbs.Get, $"Administration/Country"); if (countryResponse.isSuccess) { model.Countries = new SelectList(countryResponse.Data.Where(c => c.Name != "Malaysia").OrderBy(o => o.Name), "Id", "Name", 0); model.MalaysiaCountryId = countryResponse.Data.Where(c => c.Name == "Malaysia").Select(s => s.Id).FirstOrDefault(); model.CountryCode = countryResponse.Data.Where(c => c.Name == "Malaysia").Select(s => s.CountryCode).FirstOrDefault(); } var stateResponse = await WepApiMethod.SendApiAsync <List <StateModel> >(HttpVerbs.Get, $"Administration/State"); if (stateResponse.isSuccess) { var states = stateResponse.Data; model.States = new SelectList(states.OrderBy(o => o.Name), "Id", "Name", 0); } var stateMinistry = await WepApiMethod.SendApiAsync <List <MinistryModel> >(HttpVerbs.Get, $"Administration/Ministry"); if (stateMinistry.isSuccess) { var ministries = stateMinistry.Data; model.Ministries = new SelectList(ministries.OrderBy(o => o.Name), "Id", "Name", 0); } return(model); }
public async Task <ActionResult> RegisterAgency(RegisterAgencyModel model) { if (model.Type == CompanyType.Government) { ModelState.Remove("PassportNo"); ModelState.Remove("PostCodeNonMalaysian"); ModelState.Remove("State"); ModelState.Remove("CountryId"); ModelState.Remove("CompanyName"); ModelState.Remove("CompanyRegNo"); ModelState.Remove("SectorId"); model.CountryId = model.MalaysiaCountryId; } else if (model.Type == CompanyType.MalaysianCompany) { ModelState.Remove("PassportNo"); ModelState.Remove("PostCodeNonMalaysian"); ModelState.Remove("State"); ModelState.Remove("CountryId"); ModelState.Remove("AgencyName"); ModelState.Remove("MinistryId"); model.CountryId = model.MalaysiaCountryId; } else { ModelState.Remove("ICNo"); ModelState.Remove("PostCodeMalaysian"); ModelState.Remove("StateId"); ModelState.Remove("AgencyName"); ModelState.Remove("MinistryId"); ModelState.Remove("CompanyRegNo"); } var emailResponse = await WepApiMethod.SendApiAsync <bool>(HttpVerbs.Get, $"Administration/User/IsEmailExist?id={null}&email={model.Email}"); if (emailResponse.Data) { ModelState.AddModelError("Email", Language.Auth.ValidIsExistEmail); } var icno = model.ICNo; if (model.Type == CompanyType.NonMalaysianCompany) { icno = model.PassportNo; } var icnoResponse = await WepApiMethod.SendApiAsync <bool>(HttpVerbs.Get, $"Administration/User/IsICNoExist?id={null}&icno={icno}"); if (icnoResponse.Data) { if (model.Type == CompanyType.NonMalaysianCompany) { ModelState.AddModelError("PassportNo", Language.Auth.ValidIsExistPassportNo); } else { ModelState.AddModelError("ICNo", Language.Auth.ValidIsExistICNo); } } var passwordResponse = await WepApiMethod.SendApiAsync <bool>(HttpVerbs.Get, $"Auth/ValidatePassword?password={model.Password}"); if (!passwordResponse.isSuccess) { var error = JsonConvert.DeserializeObject <Dictionary <string, string> >(passwordResponse.ErrorMessage); if (error.ContainsKey("Message")) { ModelState.AddModelError("Password", error["Message"]); } } if (ModelState.IsValid) { var response = await WepApiMethod.SendApiAsync <dynamic>(HttpVerbs.Post, $"Auth/RegisterAgency", model); if (response.isSuccess) { ParameterListToSend notificationParameter = new ParameterListToSend(); notificationParameter.UserFullName = model.Name; notificationParameter.Link = $"<a href = '" + BaseURL + "/Auth/ActivateAccount/" + response.Data.UID + "' > here </a>"; notificationParameter.LoginDetail = $"Email: { model.Email }\nPassword: { model.Password }"; CreateAutoReminder notification = new CreateAutoReminder { NotificationType = NotificationType.ActivateAccount, NotificationCategory = NotificationCategory.Learning, ParameterListToSend = notificationParameter, StartNotificationDate = DateTime.Now, ReceiverId = new List <int> { (int)response.Data.UserId } }; var responseNotification = await WepApiMethod.SendApiAsync <ReminderResponse>(HttpVerbs.Post, $"Reminder/SLA/GenerateAutoNotificationReminder/", notification); TempData["SuccessMessage"] = Language.Auth.AlertRegisterSuccess; return(RedirectToAction("Login", "Auth", new { area = "" })); } } model = await InitRegisterCompany(model); return(View(model)); }
public IHttpActionResult RegisterAgency([FromBody] RegisterAgencyModel model) { if (model.Type == CompanyType.Government) { ModelState.Remove("model.PassportNo"); ModelState.Remove("model.PostCodeNonMalaysian"); ModelState.Remove("model.State"); ModelState.Remove("model.CountryId"); ModelState.Remove("model.CompanyName"); ModelState.Remove("model.CompanyRegNo"); ModelState.Remove("model.SectorId"); } else if (model.Type == CompanyType.MalaysianCompany) { ModelState.Remove("model.PassportNo"); ModelState.Remove("model.PostCodeNonMalaysian"); ModelState.Remove("model.State"); ModelState.Remove("model.CountryId"); ModelState.Remove("model.AgencyName"); ModelState.Remove("model.MinistryId"); } else { ModelState.Remove("model.ICNo"); ModelState.Remove("model.PostCodeMalaysian"); ModelState.Remove("model.StateId"); ModelState.Remove("model.AgencyName"); ModelState.Remove("model.MinistryId"); ModelState.Remove("model.CompanyRegNo"); } if (ModelState.IsValid) { var countryCode = db.Country.Where(c => c.Id == model.CountryId && c.Display).FirstOrDefault(); if (countryCode == null) { return(InternalServerError()); } Authentication.GeneratePassword(model.Password); //get default role var roles = db.RoleDefault.Where(r => r.DefaultRoleType == DefaultRoleType.DefaultCompany).ToList(); List <UserRole> userroles = new List <UserRole>(); foreach (var role in roles) { userroles.Add(new UserRole { Role = role.Role }); } var account = new UserAccount { LoginId = model.Email, IsEnable = false, HashPassword = Authentication.HashPassword, Salt = Authentication.Salt, LoginAttempt = 0, UserRoles = userroles }; var company = new CompanyProfile { Type = model.Type, CompanyName = model.Type == CompanyType.Government ? model.AgencyName : model.CompanyName, MinistryId = model.MinistryId, CompanyRegNo = model.CompanyRegNo, SectorId = model.SectorId, Address1 = model.Address1, Address2 = model.Address2, City = model.City, PostCode = model.Type == CompanyType.NonMalaysianCompany ? model.PostCodeNonMalaysian : model.PostCodeMalaysian, StateId = model.StateId, StateName = model.State, CountryId = model.CountryId, CompanyPhoneNo = model.CompanyPhoneNo, CountryCode = countryCode.CountryCode1 }; var user = new User { UserType = UserType.Company, Name = model.Name, Email = model.Email, ICNo = model.Type == CompanyType.NonMalaysianCompany ? model.PassportNo : model.ICNo, MobileNo = model.MobileNo, CountryCode = countryCode.CountryCode1, Display = true, CreatedBy = null, CreatedDate = DateTime.Now, UserAccount = account, CompanyProfile = company }; db.User.Add(user); ActivateAccount activateAccount = new ActivateAccount { UID = Authentication.RandomString(50, true),//random alphanumeric UserId = user.Id, CreatedDate = DateTime.Now, IsActivate = false }; db.ActivateAccount.Add(activateAccount); db.SaveChanges(); return(Ok(new { UserId = user.Id, UID = activateAccount.UID })); } return(BadRequest(ModelState)); }