public async Task<IHttpActionResult> UpdateCompanyDetails(OrganisationModel orgModel) { string companyId = User.Identity.GetUserId(); var organisation = this.crmManagerService.GetOrganisation(companyId); ////organisation.CountryCode = string.IsNullOrEmpty(orgModel.CountryCode) ? organisation.CountryCode : orgModel.CountryCode; organisation.Website = string.IsNullOrWhiteSpace(orgModel.Website) ? organisation.Website : orgModel.Website; organisation.FacebookURL = string.IsNullOrWhiteSpace(orgModel.FacebookURL) ? organisation.FacebookURL : orgModel.FacebookURL; organisation.GCMId = string.IsNullOrEmpty(orgModel.GCMId) ? organisation.GCMId : orgModel.GCMId; organisation.UDId = string.IsNullOrEmpty(orgModel.UDId) ? organisation.UDId : orgModel.UDId; organisation.Phone = string.IsNullOrEmpty(orgModel.Phone) ? organisation.Phone : orgModel.Phone; ////Only for handling old Android apps if (orgModel.SubBusinessType != null && orgModel.SubBusinessType.Length > 0) { organisation.SubBusinessType = orgModel.SubBusinessType; string subBusinessType = orgModel.SubBusinessType.Aggregate((a, b) => Convert.ToString(a) + "," + Convert.ToString(b)); organisation.MainBusinessType = this.youfferInterestService.GetMainBusinessTypeFromSub(subBusinessType).Select(x => x.ParentBusinessTypeName).Distinct().ToArray(); } if (orgModel.MainBusinessType != null && orgModel.MainBusinessType.Length > 0) { organisation.MainBusinessType = orgModel.MainBusinessType; string mainBusinessType = organisation.MainBusinessType.Aggregate((a, b) => Convert.ToString(a) + "," + Convert.ToString(b)); string[] subInterest = this.youfferInterestService.GetSubBusinessTypeFromMain(mainBusinessType).Select(x => x.BusinessTypeName).Distinct().ToArray(); organisation.SubBusinessType = subInterest; } organisation.IsActive = orgModel.IsActive ?? organisation.IsActive; organisation.Latitude = orgModel.Latitude <= 0 ? organisation.Latitude : orgModel.Latitude; organisation.Longitude = orgModel.Longitude <= 0 ? organisation.Longitude : orgModel.Longitude; orgModel = this.crmManagerService.UpdateOrganisation(companyId, organisation); if (orgModel == null) { return this.BadRequest(); } OrgResultModel orgResModel = this.mapperFactory.GetMapper<OrganisationModel, OrgResultModel>().Map(orgModel); CountryModel countryDet = this.commonService.GetCompanyCountryDetails(companyId); orgResModel.CountryDetails = countryDet; return this.Ok(orgResModel); }
public async Task<IHttpActionResult> GetCompanyDetails(string companyId) { OrganisationModel orgModel = new OrganisationModel(); orgModel = this.crmManagerService.GetOrganisation(companyId); CountryModel countryDet = this.commonService.GetCompanyCountryDetails(companyId); OrgResultModel orgResultModel = new OrgResultModel() { Id = companyId, Name = orgModel.AccountName, Email = orgModel.Email1, SecondaryEmail = orgModel.Email2, FacebookURL = orgModel.FacebookURL, GooglePlusURL = orgModel.GooglePlusURL, Website = orgModel.Website, Description = orgModel.Description, Phone = orgModel.Phone, OtherPhone = orgModel.OtherPhone, Fax = orgModel.Fax, AnnualRevenue = orgModel.AnnualRevenue, Employees = orgModel.Employees, CountryDetails = countryDet, Address = orgModel.BillAddress, City = orgModel.BillCity, State = orgModel.BillState, POBox = orgModel.BillPOBox, Timezone = orgModel.Timezone, Latitude = orgModel.Latitude, Longitude = orgModel.Longitude, ImageURL = orgModel.ImageURL, GCMId = orgModel.GCMId, UDId = orgModel.UDId, OSType = orgModel.OSType, UserRole = Roles.Company, Industry = orgModel.Industry, MainBusinessType = orgModel.MainBusinessType, SubBusinessType = orgModel.SubBusinessType, IsActive = orgModel.IsActive, PaymentDetails = string.Empty }; return this.Ok(orgResultModel); }
public async Task<IHttpActionResult> GetMExternalLogin(RegisterExternalBindingModel model) { try { try { this.LoggerService.LogException("MExternalLogin Request -json- " + JsonConvert.SerializeObject(model)); } catch (Exception ex1) { } var verifiedAccessToken = await this.VerifyExternalAccessToken(model.Provider, model.ExternalAccessToken); if (verifiedAccessToken == null) { return this.BadRequest("Invalid Provider or External Access Token"); } var user = await this.authRepository.FindAsync(new UserLoginInfo(model.Provider, verifiedAccessToken.UserId)); model.IsFromMobile = true; model.UserRole = (Roles)model.Role; model.OSType = (OSType)model.OS; ////string ipAddress = string.IsNullOrEmpty(model.IPAddress) ? HttpContext.Current.Request.UserHostAddress : model.IPAddress; ////this.LoggerService.LogException("IP Address from Request - " + ipAddress); ////CityDto cityData = this.ip2LocationService.GetCityData(ipAddress); ////model.Country = cityData.CountryInfo.CountryName; ////model.State = cityData.MostSpecificSubDivisionName == null ? string.Empty : cityData.MostSpecificSubDivisionName; ////model.Latitude = cityData.Latitude; ////model.Longitude = cityData.Longitude; bool hasRegistered = user != null; if (!hasRegistered) { return await this.RegisterExternal(model); } AccessTokenModel accessTokenResponse = this.GenerateLocalAccessTokenResponse(user); CountryModel countryDet = this.commonService.GetUserCountryDetails(user.Id); if (model.UserRole == Roles.Customer) { string crmContactId = this.userService.GetContact(user.Id).CRMId; decimal rank = this.crmManagerService.GetUserRank(crmContactId); string crmLeadId = string.Empty; if (!string.IsNullOrEmpty(crmContactId)) { crmLeadId = this.youfferContactService.GetMappingEntryByContactId(user.Id).LeadId; } ContactModel contactModel = new ContactModel(); contactModel = this.crmManagerService.GetContact(user.Id); contactModel.IsOnline = true; if (!string.IsNullOrWhiteSpace(model.GCMId)) { contactModel.GCMId = model.GCMId; } if (!string.IsNullOrWhiteSpace(model.UDId)) { contactModel.UDId = model.UDId; } contactModel = this.crmManagerService.UpdateContact(user.Id, contactModel); LeadModel leadModel = new LeadModel(); UserResultModel userResultModel = new UserResultModel(); if (!string.IsNullOrEmpty(crmLeadId)) { leadModel = this.crmManagerService.GetLead(crmLeadId); userResultModel = this.mapperFactory.GetMapper<LeadModel, UserResultModel>().Map(leadModel) ?? new UserResultModel(); userResultModel.Id = user.Id; userResultModel.ExternalAccessToken = accessTokenResponse; userResultModel.Rank = rank; userResultModel.CountryDetails = countryDet; userResultModel.UserRole = Roles.Customer; userResultModel.PaymentDetails = new PaymentModelDto() { PayPalId = leadModel.PaypalId, Mode = (PaymentMode)leadModel.PaymentMode }; } else { userResultModel = this.mapperFactory.GetMapper<ContactModel, UserResultModel>().Map(contactModel) ?? new UserResultModel(); userResultModel.Id = user.Id; userResultModel.ExternalAccessToken = accessTokenResponse; userResultModel.CountryDetails = countryDet; userResultModel.UserRole = Roles.Customer; userResultModel.PaymentDetails = new PaymentModelDto() { PayPalId = contactModel.PaypalId, Mode = (PaymentMode)contactModel.PaymentMode }; } return this.Ok(userResultModel); } else { OrgResultModel orgResultModel = new OrgResultModel(); OrganisationModel orgModel = new OrganisationModel(); orgModel = this.crmManagerService.GetOrganisation(user.Id); orgResultModel = this.mapperFactory.GetMapper<OrganisationModel, OrgResultModel>().Map(orgModel) ?? new OrgResultModel(); orgResultModel.Id = user.Id; orgResultModel.ExternalAccessToken = accessTokenResponse.ToString(); orgResultModel.CountryDetails = countryDet; orgResultModel.UserRole = Roles.Company; orgResultModel.PaymentDetails = string.Empty; return this.Ok(orgResultModel); } } catch (Exception ex) { this.LoggerService.LogException("GetMExternalLogin : " + ex.Message); return this.BadRequest(ex.Message); } }
public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model) { ExternalLoginResultModel externalLoginResultModel = new ExternalLoginResultModel(); try { model.Country = string.IsNullOrWhiteSpace(model.Country) ? string.Empty : HttpUtility.UrlDecode(model.Country); ContactModel userModel = new ContactModel(); OrganisationModel orgModel = new OrganisationModel(); if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var verifiedAccessToken = await this.VerifyExternalAccessToken(model.Provider, model.ExternalAccessToken); if (verifiedAccessToken == null) { return this.BadRequest("Invalid Provider or External Access Token"); } var user = await this.authRepository.FindAsync(new UserLoginInfo(model.Provider, verifiedAccessToken.UserId)); bool hasRegistered = user != null; if (!hasRegistered) { userModel = this.GetExternalUserDetails(model.Provider, model.ExternalAccessToken); if (string.IsNullOrEmpty(userModel.Email)) { userModel.Email = model.Email; } userModel.GCMId = model.GCMId; userModel.UDId = model.UDId; userModel.OSType = model.OSType; userModel.MailingCountry = model.Country; userModel.MailingState = model.State; userModel.CountryCode = model.CountryCode; userModel.Timezone = model.Timezone; userModel.Latitude = model.Latitude; userModel.Longitude = model.Longitude; userModel.IsAvailable = true; userModel.Availability = Availability.AllDays; userModel.AvailableFrom = "08:00:00"; userModel.AvailableTo = "20:00:00"; userModel.IsOnline = true; userModel.IsActive = true; string userName = userModel.Email; if (string.IsNullOrWhiteSpace(userModel.Email)) { userName = Guid.NewGuid().ToString(); } user = new ApplicationUserDto { UserName = userName, Email = userModel.Email, Name = userModel.FirstName, AccountType = (short)model.UserRole }; if (model.Provider == "Facebook") { user.FacebookId = verifiedAccessToken.UserId; user.FBAccessToken = model.ExternalAccessToken; } else { user.GoogleId = verifiedAccessToken.UserId; user.GoogleAccessToken = model.ExternalAccessToken; } IdentityResult result = await this.authRepository.CreateAsync(user, model.UserRole.ToString()); if (!result.Succeeded) { string errorMessage = result.Errors.Any() ? result.Errors.First() : string.Empty; return this.BadRequest(errorMessage); } var info = new ExternalLoginInfo { DefaultUserName = model.UserName, Login = new UserLoginInfo(model.Provider, verifiedAccessToken.UserId) }; result = await this.authRepository.AddLoginAsync(user.Id, info.Login); if (!result.Succeeded) { return this.GetErrorResult(result); } userModel.ImageURL = ImageHelper.SaveImageFromUrl(userModel.ImageURL, user.Id); if (model.UserRole == Roles.Company) { orgModel.AccountName = userModel.FirstName; orgModel.Email1 = userModel.Email; orgModel.FacebookURL = userModel.FBUrl; orgModel.GooglePlusURL = userModel.GooglePlusURL; orgModel.ImageURL = userModel.ImageURL; orgModel = this.crmManagerService.AddOrganisation(orgModel, user); } else { userModel = this.crmManagerService.AddContact(userModel, user); } } if (model.IsFromMobile) { var accessTokenResponse = this.GenerateLocalAccessTokenResponse(user); UserResultModel userResultModel = new UserResultModel(); OrgResultModel orgResultModel = new OrgResultModel(); CountryModel countryDet = this.commonService.GetUserCountryDetailsFromName(userModel.MailingCountry); if (model.UserRole == Roles.Customer) { userResultModel = this.mapperFactory.GetMapper<ContactModel, UserResultModel>().Map(userModel); userResultModel.ExternalAccessToken = accessTokenResponse; userResultModel.Rank = 0.0M; userResultModel.CountryDetails = countryDet; userResultModel.UserRole = Roles.Customer; userResultModel.IsActive = userModel.IsActive; userResultModel.PaymentDetails = new PaymentModelDto() { PayPalId = userModel.PaypalId, Mode = (PaymentMode)userModel.PaymentMode }; MessagesDto message = new MessagesDto(); message.ModifiedBy = message.FromUser = message.CreatedBy = "YoufferAdmin"; message.IsDeleted = false; message.CompanyId = AppSettings.Get<string>(ConfigConstants.SuperUserId); message.UserId = message.ToUser = user.Id; message.Name = "Youffer Admin"; message.MediaId = 0; MessageTemplatesDto msgTemplate = this.commonService.GetMessageTemplate(MessageTemplateType.WelcomeMsg); message.Message = msgTemplate.TemplateText; this.youfferMessageService.CreateMessage(message); int unreadMsgCount = this.youfferMessageService.GetUnreadMsgCount(message.UserId, false); if (!string.IsNullOrEmpty(userResultModel.GCMId)) { this.pushMessageService.SendMessageNotificationToAndroid(userResultModel.GCMId, message.Id.ToString(), message.Message, "Youffer", Notification.companymsg.ToString()); } if (!string.IsNullOrEmpty(userResultModel.UDId)) { this.pushMessageService.SendMessageNotificationToiOS(userResultModel.UDId, message.Id.ToString(), message.Message, "Youffer", unreadMsgCount, Notification.usermsg.ToString()); } return this.Ok(userResultModel); } else { orgResultModel = this.mapperFactory.GetMapper<OrganisationModel, OrgResultModel>().Map(orgModel); orgResultModel.ExternalAccessToken = accessTokenResponse.ToString(); orgResultModel.CountryDetails = countryDet; orgResultModel.UserRole = Roles.Company; orgResultModel.PaymentDetails = string.Empty; return this.Ok(orgResultModel); } } var redirectUri = string.Empty; var redirectUriValidationResult = this.ValidateClientAndRedirectUri(ref redirectUri); if (!string.IsNullOrEmpty(redirectUri)) { redirectUri = string.Format( "{0}?external_access_token={1}&provider={2}&haslocalaccount={3}&external_user_name={4}", redirectUri, model.ExternalAccessToken, model.Provider, hasRegistered.ToString(), model.UserName); return this.Redirect(redirectUri); } externalLoginResultModel.ExternalAccessToken = model.ExternalAccessToken; externalLoginResultModel.Provider = model.Provider; externalLoginResultModel.HasLocalAccount = hasRegistered; externalLoginResultModel.ExternalUserName = model.UserName; } catch (Exception ex) { this.LoggerService.LogException("RegisterExternal : " + ex.Message); } return this.Ok(externalLoginResultModel); }
public async Task<IHttpActionResult> Register(UserModel userModel) { try { this.LoggerService.LogException("Register Request -json- " + JsonConvert.SerializeObject(userModel)); } catch (Exception ex1) { } if (string.IsNullOrEmpty(userModel.Country) && !string.IsNullOrEmpty(userModel.CountryCode)) { userModel.Country = this.commonService.GetCountryFromISO2Code(userModel.CountryCode); } if (string.IsNullOrEmpty(userModel.Country)) { userModel.Country = "United States"; } ////string ipAddress = string.IsNullOrEmpty(userModel.IPAddress) ? HttpContext.Current.Request.UserHostAddress : userModel.IPAddress; ////CityDto cityData = this.ip2LocationService.GetCityData(ipAddress); ////userModel.Country = cityData.CountryInfo.CountryName; ////userModel.State = cityData.MostSpecificSubDivisionName == null ? string.Empty : cityData.MostSpecificSubDivisionName; ////userModel.Latitude = cityData.Latitude; ////userModel.Longitude = cityData.Longitude; ////Only for handling old Android apps List<string> lstCallingCodes = this.commonService.GetCountryMetaData().Select(x => x.CallingCode).ToList(); try { IEnumerable<string> code = lstCallingCodes.Where(x => x == userModel.CountryCode); if (!string.IsNullOrEmpty(code.FirstOrDefault())) { userModel.CountryCode = this.commonService.GetISO2CodeFromCallingCode(code.FirstOrDefault()); } } catch { } StatusMessage status = new StatusMessage(); Roles userRole; bool isRoleParsed = Enum.TryParse(userModel.Role.ToString(), true, out userRole); if (!Roles.IsDefined(typeof(Roles), userModel.Role) || userRole == Roles.Admin) { return this.BadRequest(); } userModel.UserRole = userRole.ToString(); if (!this.ModelState.IsValid) { if (userModel.OSType != OSType.Web) { string errorMsg = this.ModelState.Values.First().Errors[0].ErrorMessage; return this.BadRequest(errorMsg); } return this.BadRequest(this.ModelState); } IdentityResult result; try { result = await this.authRepository.RegisterUser(userModel); if (!result.Succeeded) { string errorMessage = result.Errors.Any() ? result.Errors.First() : string.Empty; return this.BadRequest(errorMessage); } var user = await this.authRepository.FindUser(userModel.EmailId, userModel.Password); string defaultImageUrl = AppSettings.Get<string>(ConfigConstants.ApiBaseUrl) + AppSettings.Get<string>(ConfigConstants.DefaultProfileImage); string imageUrl = ImageHelper.SaveImageFromUrl(defaultImageUrl, user.Id); if (userModel.UserRole == Roles.Company.ToString()) { OrganisationModel orgModel = new OrganisationModel(); orgModel = this.mapperFactory.GetMapper<UserModel, OrganisationModel>().Map(userModel); orgModel.ImageURL = imageUrl; orgModel.IsImageUploaded = false; orgModel.CashBalance = orgModel.CreditBalance = 0; ////Only for handling old Android apps if (!string.IsNullOrEmpty(userModel.SubBusinessType) && userModel.SubBusinessType.Length > 0) { orgModel.SubBusinessType = userModel.SubBusinessType.Split(new string[] { "," }, StringSplitOptions.None).Where(x => !string.IsNullOrEmpty(x)).ToArray(); orgModel.MainBusinessType = this.youfferInterestService.GetMainBusinessTypeFromSub(userModel.SubBusinessType).Select(x => x.ParentBusinessTypeName).Distinct().ToArray(); } if (!string.IsNullOrEmpty(userModel.MainBusinessType) && userModel.MainBusinessType.Length > 0) { orgModel.MainBusinessType = userModel.MainBusinessType.Split(',').ToArray(); string[] subInterest = this.youfferInterestService.GetSubBusinessTypeFromMain(userModel.MainBusinessType).Select(x => x.BusinessTypeName).Distinct().ToArray(); orgModel.SubBusinessType = subInterest; } orgModel = this.crmManagerService.AddOrganisation(orgModel, user); ////Send username and password email await this.SendUserNamePasswordEmail(orgModel.Email1, orgModel.Password); } else { ContactModel contactModel = new ContactModel(); contactModel = this.mapperFactory.GetMapper<UserModel, ContactModel>().Map(userModel); contactModel.ImageURL = imageUrl; contactModel = this.crmManagerService.AddContact(contactModel, user); } } catch (Exception ex) { this.LoggerService.LogException("Register: " + ex.Message); this.ModelState.AddModelError("Exception", ex.Message); if (userModel.OSType != OSType.Web) { return this.BadRequest(ex.Message); } return this.BadRequest(this.ModelState); } IHttpActionResult errorResult = this.GetErrorResult(result); if (errorResult != null) { return errorResult; } status.IsSuccess = true; return this.Ok(status); }