Skip to content

Commit d9b349f

Browse files
authored
Merge pull request #859 from iceljc/features/refine-agent-filter
Features/refine agent filter
2 parents 964de97 + d6ebf80 commit d9b349f

File tree

8 files changed

+59
-32
lines changed

8 files changed

+59
-32
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ List<User> GetUsersByAffiliateId(string affiliateId)
5050
=> throw new NotImplementedException();
5151
void UpdateUserName(string userId, string userName)
5252
=> throw new NotImplementedException();
53-
Dashboard? GetDashboard(string id = null)
53+
Dashboard? GetDashboard(string userId = null)
5454
=> throw new NotImplementedException();
5555
void CreateUser(User user)
5656
=> throw new NotImplementedException();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public interface IUserService
3333
Task<bool> UpdatePassword(string newPassword, string verificationCode);
3434
Task<DateTime> GetUserTokenExpires();
3535
Task<bool> UpdateUsersIsDisable(List<string> userIds, bool isDisable);
36-
Task<bool> AddDashboardConversation(string userId, string conversationId);
37-
Task<bool> RemoveDashboardConversation(string userId, string conversationId);
38-
Task UpdateDashboardConversation(string userId, DashboardConversation dashConv);
39-
Task<Dashboard?> GetDashboard(string userId);
36+
Task<bool> AddDashboardConversation(string conversationId);
37+
Task<bool> RemoveDashboardConversation(string conversationId);
38+
Task UpdateDashboardConversation(DashboardConversation dashConv);
39+
Task<Dashboard?> GetDashboard();
4040
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public List<User> GetUsersByAffiliateId(string affiliateId)
5353
return Users.FirstOrDefault(x => x.UserName == userName.ToLower());
5454
}
5555

56-
public Dashboard? GetDashboard(string id = null)
56+
public Dashboard? GetDashboard(string userId = null)
5757
{
5858
return Dashboards.FirstOrDefault();
5959
}

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -810,40 +810,47 @@ public async Task<bool> UpdateUsersIsDisable(List<string> userIds, bool isDisabl
810810
return true;
811811
}
812812

813-
public async Task<bool> AddDashboardConversation(string userId, string conversationId)
813+
public async Task<bool> AddDashboardConversation(string conversationId)
814814
{
815+
var user = await GetUser(_user.Id);
815816
var db = _services.GetRequiredService<IBotSharpRepository>();
816-
db.AddDashboardConversation(userId, conversationId);
817-
817+
db.AddDashboardConversation(user?.Id, conversationId);
818818
await Task.CompletedTask;
819819
return true;
820820
}
821821

822-
public async Task<bool> RemoveDashboardConversation(string userId, string conversationId)
822+
public async Task<bool> RemoveDashboardConversation(string conversationId)
823823
{
824+
var user = await GetUser(_user.Id);
824825
var db = _services.GetRequiredService<IBotSharpRepository>();
825-
db.RemoveDashboardConversation(userId, conversationId);
826-
826+
db.RemoveDashboardConversation(user?.Id, conversationId);
827827
await Task.CompletedTask;
828828
return true;
829829
}
830830

831-
public async Task UpdateDashboardConversation(string userId, DashboardConversation newDashConv)
831+
public async Task UpdateDashboardConversation(DashboardConversation newDashConv)
832832
{
833833
var db = _services.GetRequiredService<IBotSharpRepository>();
834-
var dashConv = db.GetDashboard(userId)?.ConversationList.FirstOrDefault(x => string.Equals(x.ConversationId, newDashConv.ConversationId));
834+
835+
var user = await GetUser(_user.Id);
836+
var dashConv = db.GetDashboard(user?.Id)?
837+
.ConversationList
838+
.FirstOrDefault(x => string.Equals(x.ConversationId, newDashConv.ConversationId));
835839
if (dashConv == null) return;
840+
836841
dashConv.Name = newDashConv.Name ?? dashConv.Name;
837842
dashConv.Instruction = newDashConv.Instruction ?? dashConv.Instruction;
838-
db.UpdateDashboardConversation(userId, dashConv);
843+
db.UpdateDashboardConversation(user?.Id, dashConv);
839844
await Task.CompletedTask;
840845
return;
841846
}
842847

843-
public async Task<Dashboard?> GetDashboard(string userId)
848+
public async Task<Dashboard?> GetDashboard()
844849
{
845850
var db = _services.GetRequiredService<IBotSharpRepository>();
846-
var dash = db.GetDashboard();
851+
852+
var user = await GetUser(_user.Id);
853+
var dash = db.GetDashboard(user?.Id);
847854
await Task.CompletedTask;
848855
return dash;
849856
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ public AgentSettings GetSettings()
6666
return targetAgent;
6767
}
6868

69-
[HttpGet("/agents")]
70-
public async Task<PagedItems<AgentViewModel>> GetAgents([FromQuery] AgentFilter filter, [FromQuery] bool checkAuth = false)
69+
[HttpPost("/agents")]
70+
public async Task<PagedItems<AgentViewModel>> GetAgents([FromBody] AgentQueryRequest request)
7171
{
7272
var agentSetting = _services.GetRequiredService<AgentSettings>();
7373
var userService = _services.GetRequiredService<IUserService>();
7474

7575
List<AgentViewModel> agents;
76-
var pagedAgents = await _agentService.GetAgents(filter);
76+
var pagedAgents = await _agentService.GetAgents(request.Filter);
7777

78-
if (!checkAuth)
78+
if (!request.CheckAuth)
7979
{
8080
agents = pagedAgents?.Items?.Select(x => AgentViewModel.FromAgent(x))?.ToList() ?? [];
8181
return new PagedItems<AgentViewModel>
@@ -160,4 +160,20 @@ public IEnumerable<AgentUtility> GetAgentUtilityOptions()
160160
}
161161
return utilities.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name).ToList();
162162
}
163+
164+
[HttpGet("/agent/labels")]
165+
public async Task<IEnumerable<string>> GetAgentLabels()
166+
{
167+
var agentService = _services.GetRequiredService<IAgentService>();
168+
var agents = await agentService.GetAgents(new AgentFilter
169+
{
170+
Pager = new Pagination { Size = 1000 }
171+
});
172+
173+
var labels = agents.Items?.SelectMany(x => x.Labels)
174+
.Distinct()
175+
.OrderBy(x => x)
176+
.ToList() ?? [];
177+
return labels;
178+
}
163179
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -535,24 +535,20 @@ public IActionResult DownloadMessageFile([FromRoute] string conversationId, [Fro
535535
}
536536
#endregion
537537

538-
#region miscellaneous
538+
#region Dashboard
539539
[HttpPut("/agent/{agentId}/conversation/{conversationId}/dashboard")]
540540
public async Task<bool> PinConversationToDashboard([FromRoute] string agentId, [FromRoute] string conversationId)
541541
{
542542
var userService = _services.GetRequiredService<IUserService>();
543-
544-
var user = await userService.GetUser(_user.Id);
545-
var pinned = await userService.AddDashboardConversation(user.Id, conversationId);
543+
var pinned = await userService.AddDashboardConversation(conversationId);
546544
return pinned;
547545
}
548546

549547
[HttpDelete("/agent/{agentId}/conversation/{conversationId}/dashboard")]
550548
public async Task<bool> UnpinConversationFromDashboard([FromRoute] string agentId, [FromRoute] string conversationId)
551549
{
552550
var userService = _services.GetRequiredService<IUserService>();
553-
554-
var user = await userService.GetUser(_user.Id);
555-
var unpinned = await userService.RemoveDashboardConversation(user.Id, conversationId);
551+
var unpinned = await userService.RemoveDashboardConversation(conversationId);
556552
return unpinned;
557553
}
558554
#endregion

src/Infrastructure/BotSharp.OpenAPI/Controllers/DashboardController.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ public DashboardController(IServiceProvider services,
1919
}
2020
#region User Components
2121
[HttpGet("/dashboard/components")]
22-
public async Task<UserDashboardModel> GetComponents(string userId)
22+
public async Task<UserDashboardModel> GetComponents()
2323
{
2424
var userService = _services.GetRequiredService<IUserService>();
25-
var dashboardProfile = await userService.GetDashboard(userId);
25+
var dashboardProfile = await userService.GetDashboard();
2626
if (dashboardProfile == null) return new UserDashboardModel();
27+
2728
var result = new UserDashboardModel
2829
{
2930
ConversationList = dashboardProfile.ConversationList.Select(
@@ -39,7 +40,7 @@ public async Task<UserDashboardModel> GetComponents(string userId)
3940
}
4041

4142
[HttpPost("/dashboard/component/conversation")]
42-
public async Task UpdateDashboardConversationInstruction(string userId, UserDashboardConversationModel dashConv)
43+
public async Task UpdateDashboardConversationInstruction(UserDashboardConversationModel dashConv)
4344
{
4445
if (string.IsNullOrEmpty(dashConv.Name) && string.IsNullOrEmpty(dashConv.Instruction))
4546
{
@@ -60,7 +61,7 @@ public async Task UpdateDashboardConversationInstruction(string userId, UserDash
6061
}
6162

6263
var userService = _services.GetRequiredService<IUserService>();
63-
await userService.UpdateDashboardConversation(userId, newDashConv);
64+
await userService.UpdateDashboardConversation(newDashConv);
6465
return;
6566
}
6667
#endregion
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace BotSharp.OpenAPI.ViewModels.Agents;
2+
3+
public class AgentQueryRequest
4+
{
5+
public AgentFilter Filter { get; set; } = AgentFilter.Empty();
6+
public bool CheckAuth { get; set; }
7+
}

0 commit comments

Comments
 (0)