Skip to content

Commit 1c71836

Browse files
authored
Merge pull request #69 from Qtoss-AI/hdongDev
hdong: add search user logic.
2 parents e30519c + d48dd07 commit 1c71836

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
4848
void UpdateUserIsDisable(string userId, bool isDisable) => throw new NotImplementedException();
4949
void UpdateUsersIsDisable(List<string> userIds, bool isDisable) => throw new NotImplementedException();
5050
PagedItems<User> GetUsers(UserFilter filter) => throw new NotImplementedException();
51+
List<User> SearchLoginUsers(User filter, string source = UserSource.Internal) =>throw new NotImplementedException();
5152
User? GetUserDetails(string userId, bool includeAgent = false) => throw new NotImplementedException();
5253
bool UpdateUser(User user, bool updateUserAgents = false) => throw new NotImplementedException();
5354

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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,63 @@ public PagedItems<User> GetUsers(UserFilter filter)
128128
};
129129
}
130130

131+
public List<User> SearchLoginUsers(User filter, string source = UserSource.Internal)
132+
{
133+
List<User> searchResult = new List<User>();
134+
135+
// search by filters
136+
if (!string.IsNullOrWhiteSpace(filter.Id))
137+
{
138+
var curUser = Users.FirstOrDefault(x => x.Source == source && x.Id == filter.Id.ToLower());
139+
User user = curUser != null ? curUser : null;
140+
if (user != null)
141+
{
142+
searchResult.Add(user);
143+
}
144+
}
145+
else if (!string.IsNullOrWhiteSpace(filter.Phone) && !string.IsNullOrWhiteSpace(filter.RegionCode))
146+
{
147+
string[] regionCodeData = filter.RegionCode.Split('|');
148+
if (regionCodeData.Length == 2)
149+
{
150+
string phoneNoCallingCode = filter.Phone;
151+
string phoneWithCallingCode = filter.Phone;
152+
if (!filter.Phone.StartsWith('+'))
153+
{
154+
phoneNoCallingCode = filter.Phone;
155+
phoneWithCallingCode = $"{regionCodeData[1]}{filter.Phone}";
156+
}
157+
else
158+
{
159+
phoneNoCallingCode = filter.Phone.Replace(regionCodeData[1], "");
160+
}
161+
searchResult = Users.AsQueryable()
162+
.Where(x => x.Source == source && (x.Phone == phoneNoCallingCode || x.Phone == phoneWithCallingCode) && x.RegionCode == regionCodeData[0])
163+
.ToList();
164+
}
165+
}
166+
else if (!string.IsNullOrWhiteSpace(filter.Email))
167+
{
168+
var curUser = Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.Email == filter.Email.ToString());
169+
User user = curUser != null ? curUser : null;
170+
if (user != null)
171+
{
172+
searchResult.Add(user);
173+
}
174+
}
175+
else if (!string.IsNullOrWhiteSpace(filter.UserName))
176+
{
177+
var curUser = Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.UserName == filter.UserName);
178+
User user = curUser != null ? curUser : null;
179+
if (user != null)
180+
{
181+
searchResult.Add(user);
182+
}
183+
}
184+
185+
return searchResult;
186+
}
187+
131188
public User? GetUserDetails(string userId, bool includeAgent = false)
132189
{
133190
if (string.IsNullOrWhiteSpace(userId)) return null;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,18 @@ public async Task<bool> VerifyEmailExisting(string email)
570570
return false;
571571
}
572572

573+
public async Task<List<User>> SearchLoginUsers(User filter)
574+
{
575+
if (filter == null)
576+
{
577+
return new List<User>();
578+
}
579+
580+
var db = _services.GetRequiredService<IBotSharpRepository>();
581+
582+
return db.SearchLoginUsers(filter);
583+
}
584+
573585
public async Task<bool> VerifyPhoneExisting(string phone, string regionCode)
574586
{
575587
if (string.IsNullOrWhiteSpace(phone))

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,75 @@ public PagedItems<User> GetUsers(UserFilter filter)
233233
};
234234
}
235235

236+
public List<User> SearchLoginUsers(User filter, string source = UserSource.Internal)
237+
{
238+
List<User> searchResult = new List<User>();
239+
240+
// search by filters
241+
if (!string.IsNullOrWhiteSpace(filter.Id))
242+
{
243+
var curUser = _dc.Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.Id == filter.Id.ToLower());
244+
User user = curUser != null ? curUser.ToUser() : null;
245+
if (user != null)
246+
{
247+
searchResult.Add(user);
248+
}
249+
}
250+
else if (!string.IsNullOrWhiteSpace(filter.Phone) && !string.IsNullOrWhiteSpace(filter.RegionCode))
251+
{
252+
string[] regionCodeData = filter.RegionCode.Split('|');
253+
if (regionCodeData.Length == 2)
254+
{
255+
string phoneNoCallingCode = filter.Phone;
256+
string phoneWithCallingCode = filter.Phone;
257+
if (!filter.Phone.StartsWith('+'))
258+
{
259+
phoneNoCallingCode = filter.Phone;
260+
phoneWithCallingCode = $"{regionCodeData[1]}{filter.Phone}";
261+
}
262+
else
263+
{
264+
phoneNoCallingCode = filter.Phone.Replace(regionCodeData[1], "");
265+
}
266+
var phoneUsers = _dc.Users.AsQueryable()
267+
.Where(x => x.Source == source && (x.Phone == phoneNoCallingCode || x.Phone == phoneWithCallingCode) && x.RegionCode == regionCodeData[0])
268+
.ToList();
269+
270+
if (phoneUsers != null && phoneUsers.Count > 0)
271+
{
272+
foreach (var user in phoneUsers)
273+
{
274+
if (user != null)
275+
{
276+
searchResult.Add(user.ToUser());
277+
}
278+
}
279+
}
280+
281+
}
282+
}
283+
else if (!string.IsNullOrWhiteSpace(filter.Email))
284+
{
285+
var curUser = _dc.Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.Email == filter.Email.ToString());
286+
User user = curUser != null ? curUser.ToUser() : null;
287+
if (user != null)
288+
{
289+
searchResult.Add(user);
290+
}
291+
}
292+
else if (!string.IsNullOrWhiteSpace(filter.UserName))
293+
{
294+
var curUser = _dc.Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.UserName == filter.UserName);
295+
User user = curUser != null ? curUser.ToUser() : null;
296+
if (user != null)
297+
{
298+
searchResult.Add(user);
299+
}
300+
}
301+
302+
return searchResult;
303+
}
304+
236305
public User? GetUserDetails(string userId, bool includeAgent = false)
237306
{
238307
if (string.IsNullOrWhiteSpace(userId)) return null;

0 commit comments

Comments
 (0)