190 lines
6.1 KiB
C#
190 lines
6.1 KiB
C#
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 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.TitleResource = titleLocalesTask.Result;
|
|
var project = projectTask.Result;
|
|
if (!project.IsNullOrDefault())
|
|
{
|
|
destination.ProjectTitle = project.Title;
|
|
destination.ProjectDescription = project.Description;
|
|
destination.ProjectTitleResource = project.TitleResource;
|
|
destination.ProjectDescriptionResource = project.DescriptionResource;
|
|
}
|
|
}
|
|
|
|
/// <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 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);
|
|
}
|
|
|
|
//
|
|
#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<XResourceDto> ResolveTitleLocalesAsync(
|
|
string title,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
XResourceDto result = null;
|
|
if (title.IsNullOrEmpty())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = await conversationTitleResourceProvider.GetResource(
|
|
owner: null,
|
|
identifier: title,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
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
|
|
}
|
|
} |