From 5fceb2cd4f254fdcc4705b683d9b0b4fff20230a Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Wed, 18 Sep 2024 19:49:32 -0500 Subject: [PATCH] Add UserType. --- .../BotSharp.Abstraction/Users/Enums/UserRole.cs | 4 ++-- .../BotSharp.Abstraction/Users/Enums/UserType.cs | 8 ++++++++ .../BotSharp.Abstraction/Users/Models/User.cs | 6 +++++- .../Conversations/Services/ConversationStateService.cs | 2 +- .../BotSharp.Core/Users/Services/UserService.cs | 3 +++ .../ViewModels/Users/UserCreationModel.cs | 6 ++++-- .../BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs | 5 ++++- .../Collections/UserDocument.cs | 3 +++ .../Repository/MongoRepository.User.cs | 1 + 9 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Users/Enums/UserType.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/Enums/UserRole.cs b/src/Infrastructure/BotSharp.Abstraction/Users/Enums/UserRole.cs index 620a696e..22a8eb95 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/Enums/UserRole.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/Enums/UserRole.cs @@ -13,9 +13,9 @@ public class UserRole public const string CSR = "csr"; /// - /// Client + /// Authorized user /// - public const string Client = "client"; + public const string User = "user"; /// /// Back office operations diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/Enums/UserType.cs b/src/Infrastructure/BotSharp.Abstraction/Users/Enums/UserType.cs new file mode 100644 index 00000000..cb3a39a6 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Users/Enums/UserType.cs @@ -0,0 +1,8 @@ +namespace BotSharp.Abstraction.Users.Enums; + +public class UserType +{ + public const string Internal = "internal"; + public const string Client = "client"; + public const string Affiliate = "affiliate"; +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs b/src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs index 689cba9f..8e584dba 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs @@ -14,7 +14,11 @@ public class User public string Password { get; set; } = string.Empty; public string Source { get; set; } = "internal"; public string? ExternalId { get; set; } - public string Role { get; set; } = UserRole.Client; + /// + /// internal, client, affiliate + /// + public string Type { get; set; } = UserType.Client; + public string Role { get; set; } = UserRole.User; public string? VerificationCode { get; set; } public bool Verified { get; set; } public DateTime UpdatedTime { get; set; } = DateTime.UtcNow; diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index aa518ee7..d74f71ac 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -126,7 +126,7 @@ public class ConversationStateService : IConversationStateService, IDisposable _historyStates = _db.GetConversationStates(conversationId); var dialogs = _db.GetConversationDialogs(conversationId); - var userDialogs = dialogs.Where(x => x.MetaData?.Role == AgentRole.User || x.MetaData?.Role == UserRole.Client) + var userDialogs = dialogs.Where(x => x.MetaData?.Role == AgentRole.User || x.MetaData?.Role == UserRole.User) .GroupBy(x => x.MetaData?.MessageId) .Select(g => g.First()) .OrderBy(x => x.MetaData?.CreateTime) diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index 42b708d6..bc5c9535 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Users.Enums; using BotSharp.Abstraction.Users.Models; using BotSharp.Abstraction.Users.Settings; using BotSharp.OpenAPI.ViewModels.Users; @@ -137,6 +138,7 @@ public class UserService : IUserService Source = user.Source, ExternalId = user.ExternalId, Password = user.Password, + Type = user.Type, }; await CreateUser(record); } @@ -190,6 +192,7 @@ public class UserService : IUserService new Claim(JwtRegisteredClaimNames.FamilyName, user?.LastName ?? string.Empty), new Claim("source", user.Source), new Claim("external_id", user.ExternalId ?? string.Empty), + new Claim("type", user.Type ?? UserType.Client), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim("phone", user.Phone ?? string.Empty) }; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs index c31b93d2..b157ef59 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs @@ -10,7 +10,8 @@ public class UserCreationModel public string? Email { get; set; } public string? Phone { get; set; } public string Password { get; set; } = string.Empty; - public string Role { get; set; } = UserRole.Client; + public string Type { get; set; } = UserType.Client; + public string Role { get; set; } = UserRole.User; public User ToUser() { @@ -22,7 +23,8 @@ public class UserCreationModel Email = Email, Phone = Phone, Password = Password, - Role = Role + Role = Role, + Type = Type }; } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs index cbd7e723..544cc9ff 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs @@ -14,7 +14,8 @@ public class UserViewModel public string? LastName { get; set; } public string? Email { get; set; } public string? Phone { get; set; } - public string Role { get; set; } = UserRole.Client; + public string Type { get; set; } = UserType.Client; + public string Role { get; set; } = UserRole.User; [JsonPropertyName("full_name")] public string FullName => $"{FirstName} {LastName}".Trim(); public string Source { get; set; } @@ -34,6 +35,7 @@ public class UserViewModel { FirstName = "Unknown", LastName = "Anonymous", + Type = UserType.Client, Role = AgentRole.User }; } @@ -46,6 +48,7 @@ public class UserViewModel LastName = user.LastName, Email = user.Email, Phone = user.Phone, + Type = user.Type, Role = user.Role, Source = user.Source, ExternalId = user.ExternalId, diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserDocument.cs index 16dfb959..6dcc87c2 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserDocument.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserDocument.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Users.Enums; using BotSharp.Abstraction.Users.Models; namespace BotSharp.Plugin.MongoStorage.Collections; @@ -13,6 +14,7 @@ public class UserDocument : MongoBase public string Password { get; set; } = null!; public string Source { get; set; } = "internal"; public string? ExternalId { get; set; } + public string Type { get; set; } = UserType.Client; public string Role { get; set; } = null!; public string? VerificationCode { get; set; } public bool Verified { get; set; } @@ -33,6 +35,7 @@ public class UserDocument : MongoBase Salt = Salt, Source = Source, ExternalId = ExternalId, + Type = Type, Role = Role, VerificationCode = VerificationCode, Verified = Verified, diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs index 4f8da156..145689f9 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs @@ -46,6 +46,7 @@ public partial class MongoRepository Source = user.Source, ExternalId = user.ExternalId, Role = user.Role, + Type = user.Type, VerificationCode = user.VerificationCode, Verified = user.Verified, CreatedTime = DateTime.UtcNow,