Skip to content

Commit

Permalink
tasks: added RegisterDefaultNumeratorStartupTask, #1765
Browse files Browse the repository at this point in the history
  • Loading branch information
sfadeev committed Jan 8, 2023
1 parent 3b7157b commit 2d71230
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Montr.MasterData/Models/Numerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Numerator()

public static RegisterClassifierType GetDefaultMetadata()
{
return new()
return new RegisterClassifierType
{
Item = new ClassifierType
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ public async Task<ApiResult> Handle(UpdateSettings request, CancellationToken ca
settings.Set(property.Name, property.GetValue(request.Values));
}

var affected = await settings.Update(cancellationToken);
result = await settings.Update(cancellationToken);

scope.Commit();

result = new ApiResult { AffectedRows = affected };
if (result.Success) scope.Commit();
}
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/Montr.Settings/Services/ISettingsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface ISettingsRepository

IUpdatableSettings<TSettings> GetSettings<TSettings>(string entityTypeCode, Guid entityUid);

Task<int> Update(string entityTypeCode, Guid entityUid, ICollection<(string, object)> values, CancellationToken cancellationToken);
Task<ApiResult> Update(string entityTypeCode, Guid entityUid, ICollection<(string, object)> values, CancellationToken cancellationToken);
}

public static class SettingsRepositoryExtensions
Expand All @@ -28,7 +28,7 @@ public interface IUpdatableSettings
{
IUpdatableSettings Set(string key, object value);

Task<int> Update(CancellationToken cancellationToken);
Task<ApiResult> Update(CancellationToken cancellationToken);
}

public interface IUpdatableSettings<TSettings> : IUpdatableSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Transactions;
using LinqToDB;
using MediatR;
using Montr.Core.Models;
using Montr.Core.Services;
using Montr.Settings.Entities;
using Montr.Settings.Events;
Expand Down Expand Up @@ -38,7 +39,7 @@ public IUpdatableSettings<TSettings> GetSettings<TSettings>(string entityTypeCod
}

// todo: change first parameter to ICollection<(string, string)> and convert outside of method
public async Task<int> Update(string entityTypeCode, Guid entityUid,
public async Task<ApiResult> Update(string entityTypeCode, Guid entityUid,
ICollection<(string, object)> values, CancellationToken cancellationToken)
{
var affected = 0;
Expand Down Expand Up @@ -88,7 +89,7 @@ public IUpdatableSettings<TSettings> GetSettings<TSettings>(string entityTypeCod
}, cancellationToken);
};

return affected;
return new ApiResult { AffectedRows = affected };
}

private class UpdatableSettings : IUpdatableSettings
Expand Down Expand Up @@ -118,7 +119,7 @@ public IUpdatableSettings Set(string key, object value)
return this;
}

public async Task<int> Update(CancellationToken cancellationToken)
public async Task<ApiResult> Update(CancellationToken cancellationToken)
{
return await _repository.Update(_entityTypeCode, _entityUid, _values, cancellationToken);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Montr.Tasks/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Montr.Tasks
{
[Module(DependsOn = new[] { typeof(Core.Module), typeof(MasterData.Module), typeof(Idx.Module), typeof(Automate.Module) })]
// ReSharper disable once UnusedType.Global
public class Module : IModule
{
Expand All @@ -17,6 +18,7 @@ public void Configure(IAppBuilder appBuilder)
appBuilder.Services.BindOptions<TasksOptions>(appBuilder.Configuration);

appBuilder.Services.AddTransient<IStartupTask, RegisterClassifierTypeStartupTask>();
appBuilder.Services.AddTransient<IStartupTask, RegisterDefaultNumeratorStartupTask>();
appBuilder.Services.AddTransient<IStartupTask, ConfigurationStartupTask>();

appBuilder.Services.AddSingleton<IContentProvider, ContentProvider>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task Run(CancellationToken cancellationToken)
}
}

protected static IEnumerable<RegisterClassifierType> GetClassifierTypes()
private static IEnumerable<RegisterClassifierType> GetClassifierTypes()
{
yield return TaskType.GetDefaultMetadata();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Montr.Core.Services;
using Montr.MasterData.Models;
using Montr.MasterData.Services;
using Montr.Settings.Services;

namespace Montr.Tasks.Services.Implementations
{
public class RegisterDefaultNumeratorStartupTask : IStartupTask
{
private readonly ILogger<RegisterDefaultNumeratorStartupTask> _logger;
private readonly IUnitOfWorkFactory _unitOfWorkFactory;
private readonly INamedServiceFactory<IClassifierRepository> _classifierRepositoryFactory;
private readonly IOptionsMonitor<TasksOptions> _tasksOptionsMonitor;
private readonly ISettingsRepository _settingsRepository;

public RegisterDefaultNumeratorStartupTask(ILogger<RegisterDefaultNumeratorStartupTask> logger,
IUnitOfWorkFactory unitOfWorkFactory,
INamedServiceFactory<IClassifierRepository> classifierRepositoryFactory,
IOptionsMonitor<TasksOptions> tasksOptionsMonitor,
ISettingsRepository settingsRepository)
{
_logger = logger;
_unitOfWorkFactory = unitOfWorkFactory;
_classifierRepositoryFactory = classifierRepositoryFactory;
_tasksOptionsMonitor = tasksOptionsMonitor;
_settingsRepository = settingsRepository;
}

public async Task Run(CancellationToken cancellationToken)
{
await RegisterDefaultNumerator(cancellationToken);
}

private async Task RegisterDefaultNumerator(CancellationToken cancellationToken)
{
var tasksOptions = _tasksOptionsMonitor.CurrentValue;

if (tasksOptions.DefaultNumeratorId != null)
{
_logger.LogDebug("Default tasks numerator {numeratorId} already saved to settings.", tasksOptions.DefaultNumeratorId);

return;
}

var defaultNumerator = GetDefaultNumerator();

var numeratorRepository =
_classifierRepositoryFactory.GetRequiredService(MasterData.ClassifierTypeCode.Numerator);

var numerators = await numeratorRepository.Search(new NumeratorSearchRequest
{
TypeCode = MasterData.ClassifierTypeCode.Numerator,
Code = defaultNumerator.Code
}, cancellationToken);

if (numerators.Rows.Count > 0)
{
_logger.LogDebug("Default tasks numerator {numeratorId} already created.", numerators.Rows[0].Uid);

return;
}

using (var scope = _unitOfWorkFactory.Create())
{
var numerator = await numeratorRepository.Insert(defaultNumerator, cancellationToken);

numerator.AssertSuccess(() => "Failed to create default tasks numerator.");

var result = await _settingsRepository.GetApplicationSettings<TasksOptions>()
.Set(x => x.DefaultNumeratorId, numerator.Uid)
.Update(cancellationToken);

result.AssertSuccess(() => "Failed to save default tasks numerator to settings.");

scope.Commit();

_logger.LogDebug("Created default tasks numerator {numeratorId} and saved to settings.", numerator.Uid);
}
}

private static Numerator GetDefaultNumerator()
{
return new Numerator
{
Code = EntityTypeCode.Task,
Name = "Default tasks numerator",
EntityTypeCode = EntityTypeCode.Task,
IsActive = true,
IsSystem = true,
Pattern = "T-{Number}"
};
}
}
}
2 changes: 0 additions & 2 deletions src/Montr.Tasks/TasksOptions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using System.ComponentModel.DataAnnotations;
using Montr.MasterData.Services.Designers;
using Montr.Metadata;

namespace Montr.Tasks
{
public class TasksOptions
{
[Required]
[FieldDesigner(typeof(ClassifierFieldDesigner))]
[ClassifierField(MasterData.ClassifierTypeCode.Numerator)]
public Guid? DefaultNumeratorId { get; set; }
Expand Down

0 comments on commit 2d71230

Please sign in to comment.