From 5531944a9f717f68eda9872d38262dfbb3b8e62b Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Sat, 2 Sep 2023 17:41:58 -0500 Subject: [PATCH] add routing item and routing profile --- .../Repositories/IBotSharpRepository.cs | 3 ++ .../Repositories/Records/RoutingItemRecord.cs | 14 ++++++ .../Repository/BotSharpDbContext.cs | 2 + .../Repository/FileRepository.cs | 48 ++++++++++++++++++ .../BotSharp.Core/Routing/Router.cs | 22 ++++++--- .../Collections/RoutingItemCollection.cs | 11 +++++ .../Collections/RoutingProfileCollection.cs | 7 +++ .../MongoDbContext.cs | 6 +++ .../Repository/MongoRepository.cs | 49 ++++++++++++++++++- 9 files changed, 153 insertions(+), 9 deletions(-) create mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingItemCollection.cs create mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingProfileCollection.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 3265d2f0..4197a52b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -9,6 +9,9 @@ public interface IBotSharpRepository IQueryable Agent { get; } IQueryable UserAgent { get; } IQueryable Conversation { get; } + IQueryable RoutingItem { get; } + IQueryable RoutingProfile { get; } + int Transaction(Action action); void Add(object entity); diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingItemRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingItemRecord.cs index b549723c..d443f90f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingItemRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingItemRecord.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Routing.Models; + namespace BotSharp.Abstraction.Repositories.Records; public class RoutingItemRecord : RecordBase @@ -8,4 +10,16 @@ public class RoutingItemRecord : RecordBase public List RequiredFields { get; set; } = new List(); public string RedirectTo { get; set; } public bool Disabled { get; set; } + + public RoutingItem ToRoutingItem() + { + return new RoutingItem + { + Name = Name, + Description = Description, + RequiredFields = RequiredFields, + RedirectTo = RedirectTo, + Disabled = Disabled + }; + } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index 7fd896dc..26810f84 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -10,6 +10,8 @@ public class BotSharpDbContext : Database, IBotSharpRepository public IQueryable Agent => Table(); public IQueryable UserAgent => Table(); public IQueryable Conversation => Table(); + public IQueryable RoutingItem => throw new NotImplementedException(); + public IQueryable RoutingProfile => throw new NotImplementedException(); public void UpdateAgent(AgentRecord agent) { diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 562a8b0a..c9c88ff9 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -1,6 +1,8 @@ using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Repositories.Records; +using BotSharp.Abstraction.Routing.Models; +using SharpCompress.Common; using System.IO; using System.Text.Json; namespace BotSharp.Core.Repository; @@ -118,6 +120,52 @@ public class FileRepository : IBotSharpRepository } } + private List _routingItems; + public IQueryable RoutingItem + { + get + { + if (_routingItems != null) + { + return _routingItems.AsQueryable(); + } + + _routingItems = new List(); + var agentSettings = _services.GetRequiredService(); + var dbSettings = _services.GetRequiredService(); + var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "route.json"); + if (File.Exists(filePath)) + { + _routingItems = JsonSerializer.Deserialize>(File.ReadAllText(filePath)); + } + + return _routingItems.AsQueryable(); + } + } + + private List _routingProfiles; + public IQueryable RoutingProfile + { + get + { + if (_routingProfiles != null) + { + return _routingProfiles.AsQueryable(); + } + + _routingProfiles = new List(); + var agentSettings = _services.GetRequiredService(); + var dbSettings = _services.GetRequiredService(); + var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "routing-profile.json"); + if (File.Exists(filePath)) + { + _routingProfiles = JsonSerializer.Deserialize>(File.ReadAllText(filePath)); + } + + return _routingProfiles.AsQueryable(); + } + } + public void Add(object entity) { _conversations = Conversation.ToList(); diff --git a/src/Infrastructure/BotSharp.Core/Routing/Router.cs b/src/Infrastructure/BotSharp.Core/Routing/Router.cs index 501d56d1..181f0619 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Router.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Router.cs @@ -33,20 +33,26 @@ public class Router : IAgentRouting { var agentSettings = _services.GetRequiredService(); var dbSettings = _services.GetRequiredService(); - var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "route.json"); - var records = JsonSerializer.Deserialize(File.ReadAllText(filePath)); + var db = _services.GetRequiredService(); + //var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "route.json"); + //var records = JsonSerializer.Deserialize(File.ReadAllText(filePath)); + + var records = db.RoutingItem.Select(x => x.ToRoutingItem()).ToArray(); // check if routing profile is specified - filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "routing-profile.json"); - if (File.Exists(filePath)) + //filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "routing-profile.json"); + + var profiles = db.RoutingProfile.ToList(); + + if (profiles != null && profiles.Any()) { var state = _services.GetRequiredService(); var name = state.GetState("channel"); - var profiles = JsonSerializer.Deserialize(File.ReadAllText(filePath)); - var spcificedProfile = profiles.FirstOrDefault(x => x.Name == name); - if (spcificedProfile != null) + //var profiles = JsonSerializer.Deserialize(File.ReadAllText(filePath)); + var specifiedProfile = profiles.FirstOrDefault(x => x.Name == name); + if (specifiedProfile != null) { - records = records.Where(x => spcificedProfile.AgentIds.Contains(x.AgentId)).ToArray(); + records = records.Where(x => specifiedProfile.AgentIds.Contains(x.AgentId)).ToArray(); } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingItemCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingItemCollection.cs new file mode 100644 index 00000000..deab7774 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingItemCollection.cs @@ -0,0 +1,11 @@ +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 new file mode 100644 index 00000000..3b41ab7a --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingProfileCollection.cs @@ -0,0 +1,7 @@ +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 5b07791f..27d7ec27 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -36,4 +36,10 @@ public class MongoDbContext public IMongoCollection UserAgents => Database.GetCollection("OneBrainUserAgents"); + + public IMongoCollection RoutingItems + => Database.GetCollection("OneBrainRoutingItems"); + + public IMongoCollection RoutingProfiles + => Database.GetCollection("OneBrainRoutingProfiles"); } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index 07a2059e..fa0d67c7 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -12,7 +12,6 @@ public class MongoRepository : IBotSharpRepository { _dc = dc; _services = services; - _options = new UpdateOptions { IsUpsert = true, @@ -125,6 +124,54 @@ public class MongoRepository : IBotSharpRepository } } + private List _routingItems; + public IQueryable RoutingItem + { + get + { + if (_routingItems != null) + { + return _routingItems.AsQueryable(); + } + + var routingItemDocs = _dc.RoutingItems?.AsQueryable()?.ToList() ?? new List(); + _routingItems = routingItemDocs.Select(x => new RoutingItemRecord + { + Id = x.Id.ToString(), + AgentId = x.AgentId.ToString(), + Description = x.Description, + RequiredFields = x.RequiredFields, + RedirectTo = x.RedirectTo.ToString(), + Disabled = x.Disabled + }).ToList(); + + return _routingItems.AsQueryable(); + } + } + + private List _routingProfiles; + public IQueryable RoutingProfile + { + get + { + if (_routingProfiles != null) + { + return _routingProfiles.AsQueryable(); + } + + var routingProfilDocs = _dc.RoutingProfiles?.AsQueryable()?.ToList() ?? new List(); + _routingProfiles = routingProfilDocs.Select(x => new RoutingProfileRecord + { + Id = x.Id.ToString(), + Name = x.Name, + AgentIds = x.AgentIds?.Select(x => x.ToString())?.ToList() ?? new List() + }).ToList(); + + return _routingProfiles.AsQueryable(); + } + } + + List _changedTableNames = new List(); public void Add(object entity) {