Add tenant services

This commit is contained in:
2025-08-03 15:19:16 -06:00
parent 7c92a7e791
commit 0eadd6e217
18 changed files with 1134 additions and 514 deletions

View File

@@ -3,6 +3,7 @@
// AgileWebs
// </copyright>
// ***********************************************************************
using Core.Blueprint.Mongo;
using Core.Blueprint.Redis;
using Core.Blueprint.Redis.Helpers;
@@ -10,8 +11,8 @@ using Core.Thalos.BuildingBlocks;
using Core.Thalos.Domain.Contexts.Onboarding.Request;
using Core.Thalos.Provider.Contracts;
using Mapster;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using StatusEnum = Core.Blueprint.Mongo.StatusEnum;
namespace Core.Thalos.Provider.Providers.Onboarding
{
@@ -21,59 +22,63 @@ namespace Core.Thalos.Provider.Providers.Onboarding
public class ModuleProvider : IModuleProvider
{
private readonly CollectionRepository<ModuleAdapter> repository;
private readonly CacheSettings cacheSettings;
private readonly ICacheSettings cacheSettings;
private readonly IRedisCacheProvider cacheProvider;
public ModuleProvider(CollectionRepository<ModuleAdapter> repository,
public ModuleProvider(
CollectionRepository<ModuleAdapter> repository,
IRedisCacheProvider cacheProvider,
IOptions<CacheSettings> cacheSettings)
ICacheSettings cacheSettings)
{
this.repository = repository;
this.repository.CollectionInitialization();
this.cacheSettings = cacheSettings.Value;
this.cacheProvider = cacheProvider;
this.cacheSettings = cacheSettings;
}
/// <summary>
/// Creates a new Module.
/// </summary>
/// <param name="entity">The Module to be created.</param>
/// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
/// <param name="newModule">The Module to be created.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>
/// A <see cref="ValueTask{ModuleAdapter}"/> representing the asynchronous execution of the service.
/// </returns>
public async ValueTask<ModuleAdapter> CreateModule(ModuleRequest newModule, CancellationToken cancellationToken)
{
var moduleCollection = newModule.Adapt<ModuleAdapter>();
await repository.InsertOneAsync(moduleCollection);
return moduleCollection;
}
/// <summary>
/// Gets an Module by identifier.
/// Gets a Module by identifier.
/// </summary>
/// <param name="id">The Module identifier.</param>
/// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>0
/// <param name="_id">The Module Mongo identifier.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>
/// A <see cref="ValueTask{ModuleAdapter}"/> representing the asynchronous execution of the service.
/// </returns>
public async ValueTask<ModuleAdapter> GetModuleById(string _id, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModuleById", _id);
var cachedData = await cacheProvider.GetAsync<ModuleAdapter>(cacheKey);
if (cachedData is not null) { return cachedData; }
//if (cachedData is not null) return cachedData;
var module = await repository.FindByIdAsync(_id);
await cacheProvider.SetAsync(cacheKey, module);
await cacheProvider.SetAsync(cacheKey, module, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
return module;
}
/// <summary>
/// Gets all the modules.
/// Gets all the Modules.
/// </summary>
/// <returns>A <see cref="{Task{IEnumerbale{ModuleAdapter}}}"/> representing
/// the asynchronous execution of the service.</returns>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>
/// A <see cref="ValueTask{IEnumerable{ModuleAdapter}}"/> representing the asynchronous execution of the service.
/// </returns>
public async ValueTask<IEnumerable<ModuleAdapter>> GetAllModules(CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModules");
@@ -82,22 +87,22 @@ namespace Core.Thalos.Provider.Providers.Onboarding
if (cachedData.Any()) return cachedData;
var modules = await repository.AsQueryable();
await cacheProvider.SetAsync(cacheKey, modules);
return modules;
}
/// <summary>
/// Gets all the modules by modules identifier list.
/// Gets all the Modules by a list of identifiers.
/// </summary>
/// <param name="modules">The list of modules identifiers.</param>
/// <returns>A <see cref="Task{IEnumerable{ModuleAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
/// <param name="modules">The list of Module identifiers.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>
/// A <see cref="ValueTask{IEnumerable{ModuleAdapter}}"/> representing the asynchronous execution of the service.
/// </returns>
public async ValueTask<IEnumerable<ModuleAdapter>> GetAllModulesByList(string[] modules, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllModulesByList", modules);
var cachedData = await cacheProvider.GetAsync<IEnumerable<ModuleAdapter>>(cacheKey) ?? [];
if (cachedData.Any()) return cachedData;
@@ -105,49 +110,61 @@ namespace Core.Thalos.Provider.Providers.Onboarding
var builder = Builders<ModuleAdapter>.Filter;
var filters = new List<FilterDefinition<ModuleAdapter>>();
if (modules != null || !modules.Any())
if (modules is { Length: > 0 })
{
filters.Add(builder.In(x => x._Id, modules));
}
var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty;
var modulesList = await repository.FilterByMongoFilterAsync(finalFilter);
await cacheProvider.SetAsync(cacheKey, modulesList);
await cacheProvider.SetAsync(cacheKey, modulesList, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
return modulesList;
}
/// <summary>
/// Changes the status of the module.
/// Changes the status of the Module.
/// </summary>
/// <param name="id">The module identifier.</param>
/// <param name="newStatus">The new status of the module.</param>
/// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<ModuleAdapter> ChangeModuleStatus(string id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken)
/// <param name="_id">The Module Mongo identifier.</param>
/// <param name="newStatus">The new status of the Module.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>
/// A <see cref="ValueTask{ModuleAdapter}"/> representing the asynchronous execution of the service.
/// </returns>
public async ValueTask<ModuleAdapter> ChangeModuleStatus(string _id, StatusEnum newStatus, CancellationToken cancellationToken)
{
var entity = await repository.FindByIdAsync(id);
var entity = await repository.FindByIdAsync(_id);
entity.Status = newStatus;
await repository.ReplaceOneAsync(entity);
return entity;
}
/// <summary>
/// Updates a Module by id.
/// Updates a Module.
/// </summary>
/// <param name="entity">The Module to be updated.</param>
/// <param name="id">The Module identifier.</param>
/// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>
/// A <see cref="ValueTask{ModuleAdapter}"/> representing the asynchronous execution of the service.
/// </returns>
public async ValueTask<ModuleAdapter> UpdateModule(ModuleAdapter entity, CancellationToken cancellationToken)
{
await repository.ReplaceOneAsync(entity);
return entity;
}
/// <summary>
/// Deletes a Module by identifier.
/// </summary>
/// <param name="_id">The Module Mongo identifier.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>
/// A <see cref="ValueTask{ModuleAdapter}"/> representing the asynchronous deletion result.
/// The deleted Module entity if found; otherwise, null.
/// </returns>
public async ValueTask<ModuleAdapter?> DeleteModule(string _id, CancellationToken cancellationToken)
{
var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id);
return entity;
}
}