From d238064af964da5efa09371086f5b06947ca150c Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 19 Sep 2023 13:18:29 -0500 Subject: [PATCH] refine agent and update --- .../Repository/FileRepository.cs | 8 +++++ .../Controllers/AgentController.cs | 34 ++++++++++++++++++- .../ViewModels/Agents/AgentCreationModel.cs | 6 ++-- .../ViewModels/Agents/AgentUpdateModel.cs | 8 +++-- .../ViewModels/Agents/AgentViewModel.cs | 10 ++++++ .../Agents/RoutingRuleUpdateModel.cs | 25 ++++++++++++++ .../Collections/RoutingItemCollection.cs | 11 ------ .../Collections/RoutingProfileCollection.cs | 7 ---- .../MongoDbContext.cs | 6 ---- 9 files changed, 85 insertions(+), 30 deletions(-) create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/RoutingRuleUpdateModel.cs delete mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingItemCollection.cs delete mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingProfileCollection.cs diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 1544a985..ecda379f 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -335,6 +335,8 @@ public class FileRepository : IBotSharpRepository private void UpdateAgentProfiles(string agentId, List profiles) { + if (profiles.IsNullOrEmpty()) return; + var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) return; @@ -346,6 +348,8 @@ public class FileRepository : IBotSharpRepository private void UpdateAgentRoutingRules(string agentId, List rules) { + if (rules.IsNullOrEmpty()) return; + var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) return; @@ -453,6 +457,10 @@ public class FileRepository : IBotSharpRepository agent.Name = inputAgent.Name; agent.Description = inputAgent.Description; agent.IsPublic = inputAgent.IsPublic; + agent.Disabled = inputAgent.Disabled; + agent.AllowRouting = inputAgent.AllowRouting; + agent.Profiles = inputAgent.Profiles; + agent.RoutingRules = inputAgent.RoutingRules; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); File.WriteAllText(agentFile, json); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index b5cf73fb..7baaa16a 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -66,6 +66,38 @@ public class AgentController : ControllerBase, IApiAdapter await _agentService.UpdateAgent(model, AgentField.IsPublic); } + [HttpPut("/agent/{agentId}/disabled")] + public async Task UpdateAgentDisabled([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) + { + var model = agent.ToAgent(); + model.Id = agentId; + await _agentService.UpdateAgent(model, AgentField.Disabled); + } + + [HttpPut("/agent/{agentId}/allow-routing")] + public async Task UpdateAgentAllowRouting([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) + { + var model = agent.ToAgent(); + model.Id = agentId; + await _agentService.UpdateAgent(model, AgentField.AllowRouting); + } + + [HttpPut("/agent/{agentId}/profiles")] + public async Task UpdateAgentProfiles([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) + { + var model = agent.ToAgent(); + model.Id = agentId; + await _agentService.UpdateAgent(model, AgentField.Profiles); + } + + [HttpPut("/agent/{agentId}/routing-rules")] + public async Task UpdateAgentRoutingRules([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) + { + var model = agent.ToAgent(); + model.Id = agentId; + await _agentService.UpdateAgent(model, AgentField.RoutingRules); + } + [HttpPut("/agent/{agentId}/instruction")] public async Task UpdateAgentInstruction([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) { @@ -83,7 +115,7 @@ public class AgentController : ControllerBase, IApiAdapter } [HttpPut("/agent/{agentId}/templates")] - public async Task UpdateAgenttemplates([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) + public async Task UpdateAgentTemplates([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) { var model = agent.ToAgent(); model.Id = agentId; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs index 277f2d8a..3857d82c 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs @@ -15,7 +15,7 @@ public class AgentCreationModel public bool AllowRouting { get; set; } public bool Disabled { get; set; } public List Profiles { get; set; } - public List RoutingRules { get; set; } + public List RoutingRules { get; set; } public Agent ToAgent() { @@ -31,7 +31,9 @@ public class AgentCreationModel AllowRouting = AllowRouting, Disabled = Disabled, Profiles = Profiles, - RoutingRules = RoutingRules + RoutingRules = RoutingRules? + .Select(x => RoutingRuleUpdateModel.ToDomainElement(x))? + .ToList() ?? new List() }; } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs index bad6e861..38452740 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs @@ -42,9 +42,9 @@ public class AgentUpdateModel /// /// Profile by channel /// - public List Profiles { get; set; } + public List? Profiles { get; set; } - public List RoutingRules { get; set; } + public List? RoutingRules { get; set; } public Agent ToAgent() { @@ -56,7 +56,9 @@ public class AgentUpdateModel Disabled = Disabled, AllowRouting = AllowRouting, Profiles = Profiles ?? new List(), - RoutingRules = RoutingRules ?? new List(), + RoutingRules = RoutingRules? + .Select(x => RoutingRuleUpdateModel.ToDomainElement(x))? + .ToList() ?? new List(), Instruction = Instruction ?? string.Empty, Templates = Templates ?? new List(), Functions = Functions ?? new List(), diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs index ec9c29ad..bf25ec64 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Agents.Models; +using BotSharp.Abstraction.Routing.Models; namespace BotSharp.OpenAPI.ViewModels.Agents; @@ -12,6 +13,11 @@ public class AgentViewModel public List Functions { get; set; } public List Responses { get; set; } public bool IsPublic { get; set; } + public bool AllowRouting { get; set; } + public bool Disabled { get; set; } + public List Profiles { get; set; } + public List RoutingRules { get; set; } + public DateTime CreatedDateTime { get; set; } public DateTime UpdatedDateTime { get; set; } @@ -27,6 +33,10 @@ public class AgentViewModel Functions = agent.Functions, Responses = agent.Responses, IsPublic= agent.IsPublic, + Disabled = agent.Disabled, + AllowRouting = agent.AllowRouting, + Profiles = agent.Profiles, + RoutingRules = agent.RoutingRules, CreatedDateTime = agent.CreatedDateTime, UpdatedDateTime = agent.UpdatedDateTime }; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/RoutingRuleUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/RoutingRuleUpdateModel.cs new file mode 100644 index 00000000..9736db19 --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/RoutingRuleUpdateModel.cs @@ -0,0 +1,25 @@ +using BotSharp.Abstraction.Routing.Models; + +namespace BotSharp.OpenAPI.ViewModels.Agents; + +public class RoutingRuleUpdateModel +{ + public string Field { get; set; } + public bool Required { get; set; } + public string? RedirectTo { get; set; } + + public RoutingRuleUpdateModel() + { + + } + + public static RoutingRule ToDomainElement(RoutingRuleUpdateModel model) + { + return new RoutingRule + { + Field = model.Field, + Required = model.Required, + RedirectTo = model.RedirectTo + }; + } +} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingItemCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingItemCollection.cs deleted file mode 100644 index 63208306..00000000 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingItemCollection.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace BotSharp.Plugin.MongoStorage.Collections; - -public class RoutingItemCollection : MongoBase -{ - public Guid AgentId { get; set; } - public string Name { get; set; } - public string Description { get; set; } - public List RequiredFields { get; set; } - public Guid? RedirectTo { get; set; } - public bool Disabled { get; set; } -} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingProfileCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingProfileCollection.cs deleted file mode 100644 index 3b41ab7a..00000000 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingProfileCollection.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace BotSharp.Plugin.MongoStorage.Collections; - -public class RoutingProfileCollection : MongoBase -{ - public string Name { get; set; } - public List AgentIds { get; set; } -} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index 4782a5b3..4c8479dd 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -42,10 +42,4 @@ public class MongoDbContext public IMongoCollection UserAgents => Database.GetCollection($"{_collectionPrefix}_UserAgents"); - - public IMongoCollection RoutingItems - => Database.GetCollection($"{_collectionPrefix}_RoutingItems"); - - public IMongoCollection RoutingProfiles - => Database.GetCollection($"{_collectionPrefix}_RoutingProfiles"); }