BotSharp/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs

75 lines
2.5 KiB
C#
Raw Normal View History

2023-11-14 04:56:06 +00:00
using BotSharp.Abstraction.Users.Enums;
2023-11-14 14:13:54 +00:00
using System.Text.Json.Serialization;
2023-08-19 00:15:02 +00:00
namespace BotSharp.OpenAPI.ViewModels.Users;
public class UserViewModel
{
2024-10-11 20:07:41 +00:00
public string Id { get; set; } = string.Empty;
2024-01-05 03:20:50 +00:00
[JsonPropertyName("user_name")]
2024-10-11 20:07:41 +00:00
public string UserName { get; set; } = string.Empty;
2023-11-14 14:13:54 +00:00
[JsonPropertyName("first_name")]
2024-10-11 20:07:41 +00:00
public string FirstName { get; set; } = string.Empty;
2023-11-14 14:13:54 +00:00
[JsonPropertyName("last_name")]
2024-05-31 03:24:34 +00:00
public string? LastName { get; set; }
public string? Email { get; set; }
public string? Phone { get; set; }
2024-09-19 00:49:32 +00:00
public string Type { get; set; } = UserType.Client;
public string Role { get; set; } = UserRole.User;
2024-10-31 19:36:26 +00:00
2023-11-14 14:13:54 +00:00
[JsonPropertyName("full_name")]
2024-02-14 23:45:28 +00:00
public string FullName => $"{FirstName} {LastName}".Trim();
2024-10-11 20:07:41 +00:00
public string? Source { get; set; }
2024-10-31 19:36:26 +00:00
2024-01-23 19:32:35 +00:00
[JsonPropertyName("external_id")]
public string? ExternalId { get; set; }
2024-05-22 04:33:31 +00:00
public string Avatar { get; set; } = "/user/avatar";
2024-10-31 19:36:26 +00:00
public IEnumerable<string> Permissions { get; set; } = [];
[JsonPropertyName("agent_actions")]
public IEnumerable<UserAgentActionViewModel> AgentActions { get; set; } = [];
2024-01-23 19:32:35 +00:00
[JsonPropertyName("create_date")]
public DateTime CreateDate { get; set; }
2024-10-31 19:36:26 +00:00
2024-01-23 19:32:35 +00:00
[JsonPropertyName("update_date")]
public DateTime UpdateDate { get; set; }
2023-11-14 01:25:25 +00:00
2024-10-29 10:30:53 +00:00
public string RegionCode { get; set; } = "CN";
2023-08-19 00:15:02 +00:00
public static UserViewModel FromUser(User user)
{
2023-11-15 13:33:46 +00:00
if (user == null)
{
return new UserViewModel
{
2024-01-31 19:57:54 +00:00
FirstName = "Unknown",
LastName = "Anonymous",
2024-09-19 00:49:32 +00:00
Type = UserType.Client,
2024-01-31 19:57:54 +00:00
Role = AgentRole.User
2023-11-15 13:33:46 +00:00
};
}
2023-08-19 00:15:02 +00:00
return new UserViewModel
{
Id = user.Id,
2024-01-05 03:20:50 +00:00
UserName = user.UserName,
2023-08-19 00:15:02 +00:00
FirstName = user.FirstName,
LastName = user.LastName,
2023-11-14 04:56:06 +00:00
Email = user.Email,
2024-11-05 03:08:18 +00:00
Phone = (!string.IsNullOrWhiteSpace(user.Phone) && user.Phone.Length > 3) ? (user.Phone.Substring(0, 3) == "+86" ? user.Phone.Substring(3) : user.Phone) : user.Phone,
2024-09-19 00:49:32 +00:00
Type = user.Type,
2024-01-23 19:32:35 +00:00
Role = user.Role,
2024-02-14 23:45:28 +00:00
Source = user.Source,
2024-01-23 19:32:35 +00:00
ExternalId = user.ExternalId,
2024-10-31 19:36:26 +00:00
Permissions = user.Permissions,
AgentActions = user.AgentActions?.Select(x => UserAgentActionViewModel.ToViewModel(x)) ?? [],
2024-01-23 19:32:35 +00:00
CreateDate = user.CreatedTime,
2024-05-22 04:33:31 +00:00
UpdateDate = user.UpdatedTime,
2024-10-29 10:30:53 +00:00
Avatar = "/user/avatar",
RegionCode = string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode
2023-08-19 00:15:02 +00:00
};
}
}