backup Mapping Actions and remove them and try to use Enricher Service for AiConversation ...
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using xAiModels.Interfaces;
|
||||
using xAiModels.Interfaces.Dtos;
|
||||
using xAiModels.Interfaces.Enrichers;
|
||||
using xAiModels.Models.Dtos;
|
||||
using xAiModels.Models.Entities;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityService.Interfaces;
|
||||
using xIdentityService.Extensions;
|
||||
using xModels.Dtos;
|
||||
using System.Linq;
|
||||
|
||||
namespace xAiModels.Providers.Enrichers
|
||||
{
|
||||
public class XAiConversationEnricher : IXAiConversationEnricher
|
||||
{
|
||||
private readonly IXIdentityProvider identityProvider;
|
||||
private readonly IXAiProjectRepositoryService projectRepositoryService;
|
||||
private readonly IXAiConversationTitleResourceProvider conversationTitleResourceProvider;
|
||||
|
||||
public XAiConversationEnricher(
|
||||
IXIdentityProvider identityProvider,
|
||||
IXAiProjectRepositoryService projectRepositoryService,
|
||||
IXAiConversationTitleResourceProvider conversationTitleResourceProvider
|
||||
)
|
||||
{
|
||||
this.identityProvider = identityProvider;
|
||||
this.projectRepositoryService = projectRepositoryService;
|
||||
this.conversationTitleResourceProvider = conversationTitleResourceProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Signle Entity to Dto ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="destination"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task EnrichAsync(
|
||||
XAiConversationDto source,
|
||||
XAiConversationDto destination,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Run independent async operations concurrently
|
||||
var ownerTask = ResolveOwnerAsync(source.OwnerId, cancellationToken);
|
||||
var projectTask = ResolveProjectAsync(source.ProjectId, cancellationToken);
|
||||
var titleLocalesTask = ResolveTitleLocalesAsync(source.Title, cancellationToken);
|
||||
|
||||
//
|
||||
// wait until All Tasks Resolved ...
|
||||
await Task.WhenAll(ownerTask, titleLocalesTask, projectTask);
|
||||
|
||||
//
|
||||
// Attached Resolved Info to Destination ...
|
||||
destination.Owner = ownerTask.Result;
|
||||
destination.TitleLocales = titleLocalesTask.Result;
|
||||
var project = projectTask.Result;
|
||||
if (!project.IsNullOrDefault())
|
||||
{
|
||||
destination.ProjectTitle = project.Title;
|
||||
destination.ProjectDescription = project.Description;
|
||||
destination.ProjectTitleLocales = project.TitleLocales;
|
||||
destination.ProjectDescriptionLocales = project.DescriptionLocales;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Collection of Entities to Dtos ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="destinations"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XAiConversationDto>> EnrichRangeAsync(
|
||||
IEnumerable<XAiConversationDto> sources,
|
||||
IEnumerable<XAiConversationDto> destinations,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Make Tasks Ready ...
|
||||
var tasks = sources.Zip(destinations, (src, dst) =>
|
||||
EnrichAsync(src, dst, cancellationToken)
|
||||
);
|
||||
|
||||
//
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
//
|
||||
return destinations;
|
||||
}
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
/// <summary>
|
||||
/// Reading Owner User Info ...
|
||||
/// </summary>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<XPersonDto> ResolveOwnerAsync(
|
||||
string ownerId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
XPersonDto result = null;
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
if (ownerId.IsNullOrEmpty())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Try to Retrieve User Info ...
|
||||
var device = identityProvider.GetDevice();
|
||||
var userInfo = await identityProvider.GetUserInfo(
|
||||
device: device,
|
||||
userSelectByParam: ownerId
|
||||
);
|
||||
result = userInfo.ToXPersonDto();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reading Title Locales ...
|
||||
/// </summary>
|
||||
/// <param name="title"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<XLocaleResourceDto[]> ResolveTitleLocalesAsync(
|
||||
string title,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
XLocaleResourceDto[] result = [];
|
||||
if (title.IsNullOrEmpty())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
var locales = await conversationTitleResourceProvider.GetLocales(
|
||||
identifier: title,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
result = [.. locales];
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Project ...
|
||||
/// </summary>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<XAiProjectDto> ResolveProjectAsync(
|
||||
Guid projectId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
XAiProjectDto result = null;
|
||||
if (projectId.IsNull() || projectId.IsDefaultGuid())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = await projectRepositoryService.GetAsync(
|
||||
id: projectId,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user