Skip to content

Commit caabc7d

Browse files
authored
Merge pull request #872 from Qtoss-AI/master
add search user logic.
2 parents 52f7a97 + 641e9dd commit caabc7d

File tree

6 files changed

+255
-88
lines changed

6 files changed

+255
-88
lines changed

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -34,55 +34,32 @@ bool UpdateRole(Role role, bool updateRoleAgents = false)
3434
#endregion
3535

3636
#region User
37-
User? GetUserByEmail(string email)
38-
=> throw new NotImplementedException();
39-
User? GetUserByPhone(string phone, string type = UserType.Client, string regionCode = "CN")
40-
=> throw new NotImplementedException();
41-
User? GetAffiliateUserByPhone(string phone)
42-
=> throw new NotImplementedException();
43-
User? GetUserById(string id)
44-
=> throw new NotImplementedException();
45-
List<User> GetUserByIds(List<string> ids)
46-
=> throw new NotImplementedException();
47-
List<User> GetUsersByAffiliateId(string affiliateId)
48-
=> throw new NotImplementedException();
49-
User? GetUserByUserName(string userName)
50-
=> throw new NotImplementedException();
51-
void UpdateUserName(string userId, string userName)
52-
=> throw new NotImplementedException();
53-
Dashboard? GetDashboard(string userId = null)
54-
=> throw new NotImplementedException();
55-
void CreateUser(User user)
56-
=> throw new NotImplementedException();
57-
void UpdateExistUser(string userId, User user)
58-
=> throw new NotImplementedException();
59-
void UpdateUserVerified(string userId)
60-
=> throw new NotImplementedException();
61-
void AddDashboardConversation(string userId, string conversationId)
62-
=> throw new NotImplementedException();
63-
void RemoveDashboardConversation(string userId, string conversationId)
64-
=> throw new NotImplementedException();
65-
void UpdateDashboardConversation(string userId, DashboardConversation dashConv)
66-
=> throw new NotImplementedException();
67-
void UpdateUserVerificationCode(string userId, string verficationCode)
68-
=> throw new NotImplementedException();
69-
void UpdateUserPassword(string userId, string password)
70-
=> throw new NotImplementedException();
71-
void UpdateUserEmail(string userId, string email)
72-
=> throw new NotImplementedException();
73-
void UpdateUserPhone(string userId, string Iphone, string regionCode)
74-
=> throw new NotImplementedException();
75-
void UpdateUserIsDisable(string userId, bool isDisable)
76-
=> throw new NotImplementedException();
77-
void UpdateUsersIsDisable(List<string> userIds, bool isDisable)
78-
=> throw new NotImplementedException();
79-
PagedItems<User> GetUsers(UserFilter filter)
80-
=> throw new NotImplementedException();
81-
User? GetUserDetails(string userId, bool includeAgent = false)
82-
=> throw new NotImplementedException();
83-
bool UpdateUser(User user, bool updateUserAgents = false)
84-
=> throw new NotImplementedException();
85-
37+
User? GetUserByEmail(string email) => throw new NotImplementedException();
38+
User? GetUserByPhone(string phone, string type = UserType.Client, string regionCode = "CN") => throw new NotImplementedException();
39+
User? GetUserByPhoneV2(string phone, string source = UserType.Internal, string regionCode = "CN") => throw new NotImplementedException();
40+
User? GetAffiliateUserByPhone(string phone) => throw new NotImplementedException();
41+
User? GetUserById(string id) => throw new NotImplementedException();
42+
List<User> GetUserByIds(List<string> ids) => throw new NotImplementedException();
43+
List<User> GetUsersByAffiliateId(string affiliateId) => throw new NotImplementedException();
44+
User? GetUserByUserName(string userName) => throw new NotImplementedException();
45+
void UpdateUserName(string userId, string userName) => throw new NotImplementedException();
46+
Dashboard? GetDashboard(string id = null) => throw new NotImplementedException();
47+
void CreateUser(User user) => throw new NotImplementedException();
48+
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();
49+
void UpdateUserVerified(string userId) => throw new NotImplementedException();
50+
void AddDashboardConversation(string userId, string conversationId) => throw new NotImplementedException();
51+
void RemoveDashboardConversation(string userId, string conversationId) => throw new NotImplementedException();
52+
void UpdateDashboardConversation(string userId, DashboardConversation dashConv) => throw new NotImplementedException();
53+
void UpdateUserVerificationCode(string userId, string verficationCode) => throw new NotImplementedException();
54+
void UpdateUserPassword(string userId, string password) => throw new NotImplementedException();
55+
void UpdateUserEmail(string userId, string email) => throw new NotImplementedException();
56+
void UpdateUserPhone(string userId, string Iphone, string regionCode) => throw new NotImplementedException();
57+
void UpdateUserIsDisable(string userId, bool isDisable) => throw new NotImplementedException();
58+
void UpdateUsersIsDisable(List<string> userIds, bool isDisable) => throw new NotImplementedException();
59+
PagedItems<User> GetUsers(UserFilter filter) => throw new NotImplementedException();
60+
List<User> SearchLoginUsers(User filter, string source = UserSource.Internal) =>throw new NotImplementedException();
61+
User? GetUserDetails(string userId, bool includeAgent = false) => throw new NotImplementedException();
62+
bool UpdateUser(User user, bool updateUserAgents = false) => throw new NotImplementedException();
8663
#endregion
8764

8865
#region Agent

src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public interface IUserService
88
{
99
Task<User> GetUser(string id);
1010
Task<PagedItems<User>> GetUsers(UserFilter filter);
11+
Task<List<User>> SearchLoginUsers(User filter);
1112
Task<User?> GetUserDetails(string userId, bool includeAgent = false);
1213
Task<bool> IsAdminUser(string userId);
1314
Task<UserAuthorization> GetUserAuthorizations(IEnumerable<string>? agentIds = null);

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ public partial class FileRepository
2828
return query.FirstOrDefault();
2929
}
3030

31+
public User? GetUserByPhoneV2(string phone, string? source = UserType.Internal, string regionCode = "CN")
32+
{
33+
var query = Users.Where(x => x.Phone == phone);
34+
35+
if (!string.IsNullOrEmpty(source))
36+
{
37+
query = query.Where(x => x.Type == source);
38+
}
39+
40+
if (!string.IsNullOrEmpty(regionCode))
41+
{
42+
query = query.Where(x => x.RegionCode == regionCode);
43+
}
44+
45+
return query.FirstOrDefault();
46+
}
47+
3148
public User? GetAffiliateUserByPhone(string phone)
3249
{
3350
return Users.FirstOrDefault(x => x.Phone == phone && x.Type == UserType.Affiliate);
@@ -128,6 +145,65 @@ public PagedItems<User> GetUsers(UserFilter filter)
128145
};
129146
}
130147

148+
public List<User> SearchLoginUsers(User filter, string source = UserSource.Internal)
149+
{
150+
List<User> searchResult = new List<User>();
151+
152+
// search by filters
153+
if (!string.IsNullOrWhiteSpace(filter.Id))
154+
{
155+
var curUser = Users.FirstOrDefault(x => x.Source == source && x.Id == filter.Id.ToLower());
156+
User user = curUser != null ? curUser : null;
157+
if (user != null)
158+
{
159+
searchResult.Add(user);
160+
}
161+
}
162+
else if (!string.IsNullOrWhiteSpace(filter.Phone) && !string.IsNullOrWhiteSpace(filter.RegionCode))
163+
{
164+
string[] regionCodeData = filter.RegionCode.Split('|');
165+
if (regionCodeData.Length == 2)
166+
{
167+
string phoneNoCallingCode = filter.Phone;
168+
string phoneWithCallingCode = filter.Phone;
169+
if (!filter.Phone.StartsWith('+'))
170+
{
171+
phoneNoCallingCode = filter.Phone;
172+
phoneWithCallingCode = $"{regionCodeData[1]}{filter.Phone}";
173+
}
174+
else
175+
{
176+
phoneNoCallingCode = filter.Phone.Replace(regionCodeData[1], "");
177+
}
178+
searchResult = Users.AsQueryable()
179+
.Where(x => x.Source == source && (x.Phone == phoneNoCallingCode || x.Phone == phoneWithCallingCode) && x.RegionCode == regionCodeData[0])
180+
.ToList();
181+
}
182+
}
183+
else if (!string.IsNullOrWhiteSpace(filter.Email))
184+
{
185+
var curUser = Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.Email == filter.Email.ToString());
186+
User user = curUser != null ? curUser : null;
187+
if (user != null)
188+
{
189+
searchResult.Add(user);
190+
}
191+
}
192+
193+
194+
if (searchResult.Count == 0 && !string.IsNullOrWhiteSpace(filter.UserName))
195+
{
196+
var curUser = Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.UserName == filter.UserName);
197+
User user = curUser != null ? curUser : null;
198+
if (user != null)
199+
{
200+
searchResult.Add(user);
201+
}
202+
}
203+
204+
return searchResult;
205+
}
206+
131207
public User? GetUserDetails(string userId, bool includeAgent = false)
132208
{
133209
if (string.IsNullOrWhiteSpace(userId)) return null;

src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ record = db.GetUserByUserName(user.UserName);
5555

5656
if (record == null && !string.IsNullOrWhiteSpace(user.Phone))
5757
{
58+
//if (user.Type != "internal")
59+
//{
60+
// record = db.GetUserByPhoneV2(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
61+
//}
62+
5863
record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
5964
}
6065

@@ -477,11 +482,17 @@ public async Task<Token> ActiveUser(UserActivationModel model)
477482
var id = model.UserName;
478483
var db = _services.GetRequiredService<IBotSharpRepository>();
479484
var record = id.Contains("@") ? db.GetUserByEmail(id) : db.GetUserByUserName(id);
485+
480486
if (record == null)
481487
{
482488
record = db.GetUserByPhone(id, regionCode: (string.IsNullOrWhiteSpace(model.RegionCode) ? "CN" : model.RegionCode));
483489
}
484490

491+
//if (record == null)
492+
//{
493+
// record = db.GetUserByPhoneV2(id, regionCode: (string.IsNullOrWhiteSpace(model.RegionCode) ? "CN" : model.RegionCode));
494+
//}
495+
485496
if (record == null)
486497
{
487498
return default;
@@ -570,6 +581,18 @@ public async Task<bool> VerifyEmailExisting(string email)
570581
return false;
571582
}
572583

584+
public async Task<List<User>> SearchLoginUsers(User filter)
585+
{
586+
if (filter == null)
587+
{
588+
return new List<User>();
589+
}
590+
591+
var db = _services.GetRequiredService<IBotSharpRepository>();
592+
593+
return db.SearchLoginUsers(filter);
594+
}
595+
573596
public async Task<bool> VerifyPhoneExisting(string phone, string regionCode)
574597
{
575598
if (string.IsNullOrWhiteSpace(phone))
@@ -609,21 +632,12 @@ public async Task<bool> SendVerificationCodeNoLogin(User user)
609632
public async Task<User> ResetVerificationCode(User user)
610633
{
611634
var db = _services.GetRequiredService<IBotSharpRepository>();
612-
User record = null;
613-
if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
635+
if (!string.IsNullOrWhiteSpace(user.Email) && !string.IsNullOrWhiteSpace(user.Phone))
614636
{
615637
return null;
616638
}
617639

618-
if (!string.IsNullOrEmpty(user.Phone))
619-
{
620-
record = db.GetUserByPhone(user.Phone, regionCode: user.RegionCode);
621-
}
622-
623-
if (!string.IsNullOrEmpty(user.Email))
624-
{
625-
record = db.GetUserByEmail(user.Email);
626-
}
640+
User? record = GetLoginUserByUniqueFilter(user, db);
627641

628642
if (record == null)
629643
{
@@ -638,6 +652,36 @@ record = db.GetUserByEmail(user.Email);
638652
return record;
639653
}
640654

655+
private static User? GetLoginUserByUniqueFilter(User user, IBotSharpRepository db)
656+
{
657+
User? record = null;
658+
if (!string.IsNullOrWhiteSpace(user.Id))
659+
{
660+
record = db.GetUserById(user.Id);
661+
}
662+
663+
if (record == null && !string.IsNullOrWhiteSpace(user.Phone))
664+
{
665+
record = db.GetUserByPhone(user.Phone, regionCode: string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode);
666+
//if (record == null)
667+
//{
668+
// record = db.GetUserByPhoneV2(user.Phone, regionCode: string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode);
669+
//}
670+
}
671+
672+
if (record == null && !string.IsNullOrWhiteSpace(user.Email))
673+
{
674+
record = db.GetUserByEmail(user.Email);
675+
}
676+
677+
if (record == null && !string.IsNullOrWhiteSpace(user.UserName))
678+
{
679+
record = db.GetUserByUserName(user.UserName);
680+
}
681+
682+
return record;
683+
}
684+
641685
public async Task<bool> SendVerificationCodeLogin()
642686
{
643687
var db = _services.GetRequiredService<IBotSharpRepository>();
@@ -677,17 +721,7 @@ public async Task<bool> ResetUserPassword(User user)
677721
}
678722
var db = _services.GetRequiredService<IBotSharpRepository>();
679723

680-
User? record = null;
681-
682-
if (!string.IsNullOrEmpty(user.Email))
683-
{
684-
record = db.GetUserByEmail(user.Email);
685-
}
686-
687-
if (!string.IsNullOrEmpty(user.Phone))
688-
{
689-
record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
690-
}
724+
User? record = GetLoginUserByUniqueFilter(user, db);
691725

692726
if (record == null)
693727
{
@@ -712,20 +746,7 @@ public async Task<bool> SetUserPassword(User user)
712746
}
713747
var db = _services.GetRequiredService<IBotSharpRepository>();
714748

715-
User? record = null;
716-
717-
if (!string.IsNullOrEmpty(user.Id))
718-
{
719-
record = db.GetUserById(user.Id);
720-
}
721-
else if (!string.IsNullOrEmpty(user.Phone))
722-
{
723-
record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
724-
}
725-
else if (!string.IsNullOrEmpty(user.Email))
726-
{
727-
record = db.GetUserByEmail(user.Email);
728-
}
749+
User? record = GetLoginUserByUniqueFilter(user, db);
729750

730751
if (record == null)
731752
{

0 commit comments

Comments
 (0)