diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 516280a3d..67a1f9019 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -34,55 +34,32 @@ bool UpdateRole(Role role, bool updateRoleAgents = false) #endregion #region User - User? GetUserByEmail(string email) - => throw new NotImplementedException(); - User? GetUserByPhone(string phone, string type = UserType.Client, string regionCode = "CN") - => throw new NotImplementedException(); - User? GetAffiliateUserByPhone(string phone) - => throw new NotImplementedException(); - User? GetUserById(string id) - => throw new NotImplementedException(); - List GetUserByIds(List ids) - => throw new NotImplementedException(); - List GetUsersByAffiliateId(string affiliateId) - => throw new NotImplementedException(); - User? GetUserByUserName(string userName) - => throw new NotImplementedException(); - void UpdateUserName(string userId, string userName) - => throw new NotImplementedException(); - Dashboard? GetDashboard(string userId = null) - => throw new NotImplementedException(); - void CreateUser(User user) - => throw new NotImplementedException(); - void UpdateExistUser(string userId, User user) - => throw new NotImplementedException(); - void UpdateUserVerified(string userId) - => throw new NotImplementedException(); - void AddDashboardConversation(string userId, string conversationId) - => throw new NotImplementedException(); - void RemoveDashboardConversation(string userId, string conversationId) - => throw new NotImplementedException(); - void UpdateDashboardConversation(string userId, DashboardConversation dashConv) - => throw new NotImplementedException(); - void UpdateUserVerificationCode(string userId, string verficationCode) - => throw new NotImplementedException(); - void UpdateUserPassword(string userId, string password) - => throw new NotImplementedException(); - void UpdateUserEmail(string userId, string email) - => throw new NotImplementedException(); - void UpdateUserPhone(string userId, string Iphone, string regionCode) - => throw new NotImplementedException(); - void UpdateUserIsDisable(string userId, bool isDisable) - => throw new NotImplementedException(); - void UpdateUsersIsDisable(List userIds, bool isDisable) - => throw new NotImplementedException(); - PagedItems GetUsers(UserFilter filter) - => throw new NotImplementedException(); - User? GetUserDetails(string userId, bool includeAgent = false) - => throw new NotImplementedException(); - bool UpdateUser(User user, bool updateUserAgents = false) - => throw new NotImplementedException(); - + User? GetUserByEmail(string email) => throw new NotImplementedException(); + User? GetUserByPhone(string phone, string type = UserType.Client, string regionCode = "CN") => throw new NotImplementedException(); + User? GetUserByPhoneV2(string phone, string source = UserType.Internal, string regionCode = "CN") => throw new NotImplementedException(); + User? GetAffiliateUserByPhone(string phone) => throw new NotImplementedException(); + User? GetUserById(string id) => throw new NotImplementedException(); + List GetUserByIds(List ids) => throw new NotImplementedException(); + List GetUsersByAffiliateId(string affiliateId) => throw new NotImplementedException(); + User? GetUserByUserName(string userName) => throw new NotImplementedException(); + void UpdateUserName(string userId, string userName) => throw new NotImplementedException(); + Dashboard? GetDashboard(string id = null) => throw new NotImplementedException(); + void CreateUser(User user) => throw new NotImplementedException(); + void UpdateExistUser(string userId, User user) => throw new NotImplementedException(); + void UpdateUserVerified(string userId) => throw new NotImplementedException(); + void AddDashboardConversation(string userId, string conversationId) => throw new NotImplementedException(); + void RemoveDashboardConversation(string userId, string conversationId) => throw new NotImplementedException(); + void UpdateDashboardConversation(string userId, DashboardConversation dashConv) => throw new NotImplementedException(); + void UpdateUserVerificationCode(string userId, string verficationCode) => throw new NotImplementedException(); + void UpdateUserPassword(string userId, string password) => throw new NotImplementedException(); + void UpdateUserEmail(string userId, string email) => throw new NotImplementedException(); + void UpdateUserPhone(string userId, string Iphone, string regionCode) => throw new NotImplementedException(); + void UpdateUserIsDisable(string userId, bool isDisable) => throw new NotImplementedException(); + void UpdateUsersIsDisable(List userIds, bool isDisable) => throw new NotImplementedException(); + PagedItems GetUsers(UserFilter filter) => throw new NotImplementedException(); + List SearchLoginUsers(User filter, string source = UserSource.Internal) =>throw new NotImplementedException(); + User? GetUserDetails(string userId, bool includeAgent = false) => throw new NotImplementedException(); + bool UpdateUser(User user, bool updateUserAgents = false) => throw new NotImplementedException(); #endregion #region Agent diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs b/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs index 8563b215f..15b04f28c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs @@ -8,6 +8,7 @@ public interface IUserService { Task GetUser(string id); Task> GetUsers(UserFilter filter); + Task> SearchLoginUsers(User filter); Task GetUserDetails(string userId, bool includeAgent = false); Task IsAdminUser(string userId); Task GetUserAuthorizations(IEnumerable? agentIds = null); diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs index 5c9846840..6b543c8bb 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs @@ -28,6 +28,23 @@ public partial class FileRepository return query.FirstOrDefault(); } + public User? GetUserByPhoneV2(string phone, string? source = UserType.Internal, string regionCode = "CN") + { + var query = Users.Where(x => x.Phone == phone); + + if (!string.IsNullOrEmpty(source)) + { + query = query.Where(x => x.Type == source); + } + + if (!string.IsNullOrEmpty(regionCode)) + { + query = query.Where(x => x.RegionCode == regionCode); + } + + return query.FirstOrDefault(); + } + public User? GetAffiliateUserByPhone(string phone) { return Users.FirstOrDefault(x => x.Phone == phone && x.Type == UserType.Affiliate); @@ -128,6 +145,65 @@ public PagedItems GetUsers(UserFilter filter) }; } + public List SearchLoginUsers(User filter, string source = UserSource.Internal) + { + List searchResult = new List(); + + // search by filters + if (!string.IsNullOrWhiteSpace(filter.Id)) + { + var curUser = Users.FirstOrDefault(x => x.Source == source && x.Id == filter.Id.ToLower()); + User user = curUser != null ? curUser : null; + if (user != null) + { + searchResult.Add(user); + } + } + else if (!string.IsNullOrWhiteSpace(filter.Phone) && !string.IsNullOrWhiteSpace(filter.RegionCode)) + { + string[] regionCodeData = filter.RegionCode.Split('|'); + if (regionCodeData.Length == 2) + { + string phoneNoCallingCode = filter.Phone; + string phoneWithCallingCode = filter.Phone; + if (!filter.Phone.StartsWith('+')) + { + phoneNoCallingCode = filter.Phone; + phoneWithCallingCode = $"{regionCodeData[1]}{filter.Phone}"; + } + else + { + phoneNoCallingCode = filter.Phone.Replace(regionCodeData[1], ""); + } + searchResult = Users.AsQueryable() + .Where(x => x.Source == source && (x.Phone == phoneNoCallingCode || x.Phone == phoneWithCallingCode) && x.RegionCode == regionCodeData[0]) + .ToList(); + } + } + else if (!string.IsNullOrWhiteSpace(filter.Email)) + { + var curUser = Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.Email == filter.Email.ToString()); + User user = curUser != null ? curUser : null; + if (user != null) + { + searchResult.Add(user); + } + } + + + if (searchResult.Count == 0 && !string.IsNullOrWhiteSpace(filter.UserName)) + { + var curUser = Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.UserName == filter.UserName); + User user = curUser != null ? curUser : null; + if (user != null) + { + searchResult.Add(user); + } + } + + return searchResult; + } + public User? GetUserDetails(string userId, bool includeAgent = false) { if (string.IsNullOrWhiteSpace(userId)) return null; diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index 18bf96ebe..60192e5f4 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -55,6 +55,11 @@ record = db.GetUserByUserName(user.UserName); if (record == null && !string.IsNullOrWhiteSpace(user.Phone)) { + //if (user.Type != "internal") + //{ + // record = db.GetUserByPhoneV2(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode)); + //} + record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode)); } @@ -477,11 +482,17 @@ public async Task ActiveUser(UserActivationModel model) var id = model.UserName; var db = _services.GetRequiredService(); var record = id.Contains("@") ? db.GetUserByEmail(id) : db.GetUserByUserName(id); + if (record == null) { record = db.GetUserByPhone(id, regionCode: (string.IsNullOrWhiteSpace(model.RegionCode) ? "CN" : model.RegionCode)); } + //if (record == null) + //{ + // record = db.GetUserByPhoneV2(id, regionCode: (string.IsNullOrWhiteSpace(model.RegionCode) ? "CN" : model.RegionCode)); + //} + if (record == null) { return default; @@ -570,6 +581,18 @@ public async Task VerifyEmailExisting(string email) return false; } + public async Task> SearchLoginUsers(User filter) + { + if (filter == null) + { + return new List(); + } + + var db = _services.GetRequiredService(); + + return db.SearchLoginUsers(filter); + } + public async Task VerifyPhoneExisting(string phone, string regionCode) { if (string.IsNullOrWhiteSpace(phone)) @@ -609,21 +632,12 @@ public async Task SendVerificationCodeNoLogin(User user) public async Task ResetVerificationCode(User user) { var db = _services.GetRequiredService(); - User record = null; - if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone)) + if (!string.IsNullOrWhiteSpace(user.Email) && !string.IsNullOrWhiteSpace(user.Phone)) { return null; } - if (!string.IsNullOrEmpty(user.Phone)) - { - record = db.GetUserByPhone(user.Phone, regionCode: user.RegionCode); - } - - if (!string.IsNullOrEmpty(user.Email)) - { - record = db.GetUserByEmail(user.Email); - } + User? record = GetLoginUserByUniqueFilter(user, db); if (record == null) { @@ -638,6 +652,36 @@ record = db.GetUserByEmail(user.Email); return record; } + private static User? GetLoginUserByUniqueFilter(User user, IBotSharpRepository db) + { + User? record = null; + if (!string.IsNullOrWhiteSpace(user.Id)) + { + record = db.GetUserById(user.Id); + } + + if (record == null && !string.IsNullOrWhiteSpace(user.Phone)) + { + record = db.GetUserByPhone(user.Phone, regionCode: string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode); + //if (record == null) + //{ + // record = db.GetUserByPhoneV2(user.Phone, regionCode: string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode); + //} + } + + if (record == null && !string.IsNullOrWhiteSpace(user.Email)) + { + record = db.GetUserByEmail(user.Email); + } + + if (record == null && !string.IsNullOrWhiteSpace(user.UserName)) + { + record = db.GetUserByUserName(user.UserName); + } + + return record; + } + public async Task SendVerificationCodeLogin() { var db = _services.GetRequiredService(); @@ -677,17 +721,7 @@ public async Task ResetUserPassword(User user) } var db = _services.GetRequiredService(); - User? record = null; - - if (!string.IsNullOrEmpty(user.Email)) - { - record = db.GetUserByEmail(user.Email); - } - - if (!string.IsNullOrEmpty(user.Phone)) - { - record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode)); - } + User? record = GetLoginUserByUniqueFilter(user, db); if (record == null) { @@ -712,20 +746,7 @@ public async Task SetUserPassword(User user) } var db = _services.GetRequiredService(); - User? record = null; - - if (!string.IsNullOrEmpty(user.Id)) - { - record = db.GetUserById(user.Id); - } - else if (!string.IsNullOrEmpty(user.Phone)) - { - record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode)); - } - else if (!string.IsNullOrEmpty(user.Email)) - { - record = db.GetUserByEmail(user.Email); - } + User? record = GetLoginUserByUniqueFilter(user, db); if (record == null) { diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs index 57ea76b96..3b6dd05f9 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs @@ -37,6 +37,30 @@ public partial class MongoRepository return user != null ? user.ToUser() : null; } + public User? GetUserByPhoneV2(string phone, string source = UserType.Internal, string regionCode = "CN") + { + string phoneSecond = string.Empty; + // if phone number length is less than 4, return null + if (string.IsNullOrWhiteSpace(phone) || phone?.Length < 4) + { + return null; + } + + if (regionCode == "CN") + { + phoneSecond = (phone ?? "").StartsWith("+86") ? (phone ?? "").Replace("+86", "") : ($"+86{phone ?? ""}"); + } + else + { + phoneSecond = (phone ?? "").Substring(regionCode == "US" ? 2 : 3); + } + + var user = _dc.Users.AsQueryable().FirstOrDefault(x => (x.Phone == phone || x.Phone == phoneSecond) + && (x.RegionCode == regionCode || string.IsNullOrWhiteSpace(x.RegionCode)) + && (x.Source == source)); + return user != null ? user.ToUser() : null; + } + public User? GetAffiliateUserByPhone(string phone) { var user = _dc.Users.AsQueryable().FirstOrDefault(x => x.Phone == phone && x.Type == UserType.Affiliate); @@ -233,6 +257,77 @@ public PagedItems GetUsers(UserFilter filter) }; } + public List SearchLoginUsers(User filter, string source = UserSource.Internal) + { + List searchResult = new List(); + + // search by filters + if (!string.IsNullOrWhiteSpace(filter.Id)) + { + var curUser = _dc.Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.Id == filter.Id.ToLower()); + User user = curUser != null ? curUser.ToUser() : null; + if (user != null) + { + searchResult.Add(user); + } + } + else if (!string.IsNullOrWhiteSpace(filter.Phone) && !string.IsNullOrWhiteSpace(filter.RegionCode)) + { + string[] regionCodeData = filter.RegionCode.Split('|'); + if (regionCodeData.Length == 2) + { + string phoneNoCallingCode = filter.Phone; + string phoneWithCallingCode = filter.Phone; + if (!filter.Phone.StartsWith('+')) + { + phoneNoCallingCode = filter.Phone; + phoneWithCallingCode = $"{regionCodeData[1]}{filter.Phone}"; + } + else + { + phoneNoCallingCode = filter.Phone.Replace(regionCodeData[1], ""); + } + var phoneUsers = _dc.Users.AsQueryable() + .Where(x => x.Source == source && (x.Phone == phoneNoCallingCode || x.Phone == phoneWithCallingCode) && x.RegionCode == regionCodeData[0]) + .ToList(); + + if (phoneUsers != null && phoneUsers.Count > 0) + { + foreach (var user in phoneUsers) + { + if (user != null) + { + searchResult.Add(user.ToUser()); + } + } + } + + } + } + else if (!string.IsNullOrWhiteSpace(filter.Email)) + { + var curUser = _dc.Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.Email == filter.Email.ToLower()); + User user = curUser != null ? curUser.ToUser() : null; + if (user != null) + { + searchResult.Add(user); + } + } + + + if (searchResult.Count == 0 && !string.IsNullOrWhiteSpace(filter.UserName)) + { + var curUser = _dc.Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.UserName == filter.UserName); + User user = curUser != null ? curUser.ToUser() : null; + if (user != null) + { + searchResult.Add(user); + } + } + + return searchResult; + } + public User? GetUserDetails(string userId, bool includeAgent = false) { if (string.IsNullOrWhiteSpace(userId)) return null; diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.cs index 4be411408..4d13e26ff 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.cs @@ -75,9 +75,6 @@ public async Task PressKey(MessageInfo message, string key) var page = _instance.GetPage(message.ContextId); if (page != null) { - // Click on the body to give focus to the page - await page.FocusAsync("input"); - await page.Keyboard.PressAsync(key); } }