Migrate from MongoDB to Couchbase Lite for local storage; update related services and configurations
Some checks failed
Publish Container / publish (push) Failing after 3m43s

This commit is contained in:
Maddo 2026-03-17 13:53:33 +01:00
commit f976d70db8
24 changed files with 328 additions and 218 deletions

View file

@ -1,20 +1,12 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace WorkTracker.Services.Auth;
public sealed class MongoAuthUser
public sealed class AuthUser
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; init; } = string.Empty;
[BsonElement("email")]
public string Email { get; init; } = string.Empty;
[BsonElement("emailNormalized")]
public string EmailNormalized { get; init; } = string.Empty;
[BsonElement("passwordHash")]
public string PasswordHash { get; init; } = string.Empty;
}
}

View file

@ -0,0 +1,102 @@
using Couchbase.Lite;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using WorkTracker.Configuration;
using WorkTracker.Services.Storage;
namespace WorkTracker.Services.Auth;
public sealed class CouchbaseLiteAuthService : IAuthService
{
private readonly Collection users;
private readonly PasswordHasher<AuthUser> passwordHasher = new();
private readonly IOptions<SingleUserOptions> options;
private readonly ILogger<CouchbaseLiteAuthService> logger;
public CouchbaseLiteAuthService(
CouchbaseLiteDatabaseProvider databaseProvider,
IOptions<SingleUserOptions> options,
ILogger<CouchbaseLiteAuthService> logger)
{
users = databaseProvider.Users;
this.options = options;
this.logger = logger;
}
public Task EnsureSeedUserAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var email = options.Value.Email.Trim();
var normalizedEmail = NormalizeEmail(email);
if (users.GetDocument(normalizedEmail) is not null)
{
return Task.CompletedTask;
}
var user = new AuthUser
{
Id = normalizedEmail,
Email = email,
EmailNormalized = normalizedEmail,
PasswordHash = string.Empty
};
var passwordHash = passwordHasher.HashPassword(user, options.Value.Password);
var userToCreate = new AuthUser
{
Id = normalizedEmail,
Email = email,
EmailNormalized = normalizedEmail,
PasswordHash = passwordHash
};
SaveUser(userToCreate);
logger.LogInformation("Seeded single user account {Email} in Couchbase Lite", email);
return Task.CompletedTask;
}
public Task<AuthUser?> ValidateCredentialsAsync(string email, string password, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var user = GetUser(NormalizeEmail(email));
if (user is null)
{
return Task.FromResult<AuthUser?>(null);
}
var result = passwordHasher.VerifyHashedPassword(user, user.PasswordHash, password);
return Task.FromResult(result == PasswordVerificationResult.Failed ? null : user);
}
private AuthUser? GetUser(string normalizedEmail)
{
var document = users.GetDocument(normalizedEmail);
if (document is null)
{
return null;
}
return new AuthUser
{
Id = document.Id,
Email = document.GetString("email") ?? string.Empty,
EmailNormalized = document.GetString("emailNormalized") ?? string.Empty,
PasswordHash = document.GetString("passwordHash") ?? string.Empty
};
}
private void SaveUser(AuthUser user)
{
var document = new MutableDocument(user.Id);
document.SetString("email", user.Email);
document.SetString("emailNormalized", user.EmailNormalized);
document.SetString("passwordHash", user.PasswordHash);
users.Save(document);
}
private static string NormalizeEmail(string email) => email.Trim().ToUpperInvariant();
}

View file

@ -0,0 +1,8 @@
namespace WorkTracker.Services.Auth;
public interface IAuthService
{
Task EnsureSeedUserAsync(CancellationToken cancellationToken);
Task<AuthUser?> ValidateCredentialsAsync(string email, string password, CancellationToken cancellationToken);
}

View file

@ -1,10 +0,0 @@
using System.Threading;
namespace WorkTracker.Services.Auth;
public interface IMongoAuthService
{
Task EnsureSeedUserAsync(CancellationToken cancellationToken);
Task<MongoAuthUser?> ValidateCredentialsAsync(string email, string password, CancellationToken cancellationToken);
}

View file

@ -1,77 +0,0 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using WorkTracker.Configuration;
namespace WorkTracker.Services.Auth;
public sealed class MongoAuthService : IMongoAuthService
{
private const string UsersCollectionName = "users";
private readonly IMongoCollection<MongoAuthUser> users;
private readonly PasswordHasher<MongoAuthUser> passwordHasher = new();
private readonly IOptions<SingleUserOptions> options;
private readonly ILogger<MongoAuthService> logger;
public MongoAuthService(
IMongoDatabase database,
IOptions<SingleUserOptions> options,
ILogger<MongoAuthService> logger)
{
users = database.GetCollection<MongoAuthUser>(UsersCollectionName);
this.options = options;
this.logger = logger;
}
public async Task EnsureSeedUserAsync(CancellationToken cancellationToken)
{
var email = options.Value.Email.Trim();
var normalizedEmail = NormalizeEmail(email);
var existingUser = await users
.Find(x => x.EmailNormalized == normalizedEmail)
.FirstOrDefaultAsync(cancellationToken);
if (existingUser is not null)
{
return;
}
var user = new MongoAuthUser
{
Email = email,
EmailNormalized = normalizedEmail,
PasswordHash = string.Empty
};
var passwordHash = passwordHasher.HashPassword(user, options.Value.Password);
var userToCreate = new MongoAuthUser
{
Email = email,
EmailNormalized = normalizedEmail,
PasswordHash = passwordHash
};
await users.InsertOneAsync(userToCreate, cancellationToken: cancellationToken);
logger.LogInformation("Seeded single user account {Email} in MongoDB", email);
}
public async Task<MongoAuthUser?> ValidateCredentialsAsync(string email, string password, CancellationToken cancellationToken)
{
var normalizedEmail = NormalizeEmail(email);
var user = await users
.Find(x => x.EmailNormalized == normalizedEmail)
.FirstOrDefaultAsync(cancellationToken);
if (user is null)
{
return null;
}
var result = passwordHasher.VerifyHashedPassword(user, user.PasswordHash, password);
return result == PasswordVerificationResult.Failed ? null : user;
}
private static string NormalizeEmail(string email) => email.Trim().ToUpperInvariant();
}

View file

@ -5,12 +5,12 @@ namespace WorkTracker.Services.Auth;
public sealed class SingleUserSeedService : IHostedService
{
private readonly IMongoAuthService authService;
private readonly IAuthService authService;
private readonly IOptions<SingleUserOptions> options;
private readonly ILogger<SingleUserSeedService> logger;
public SingleUserSeedService(
IMongoAuthService authService,
IAuthService authService,
IOptions<SingleUserOptions> options,
ILogger<SingleUserSeedService> logger)
{
@ -32,7 +32,7 @@ public sealed class SingleUserSeedService : IHostedService
}
catch (Exception ex)
{
logger.LogError(ex, "Unable to seed MongoDB single user account {Email}", options.Value.Email);
logger.LogError(ex, "Unable to seed Couchbase Lite single user account {Email}", options.Value.Email);
}
}

View file

@ -0,0 +1,96 @@
using Couchbase.Lite;
using WorkTracker.Domain;
using WorkTracker.Services.Storage;
namespace WorkTracker.Services.Settings;
public sealed class CouchbaseLiteAppSettingsService : IAppSettingsService
{
private const string DefaultSettingsId = "global";
private readonly Collection appSettingsCollection;
public CouchbaseLiteAppSettingsService(CouchbaseLiteDatabaseProvider databaseProvider)
{
appSettingsCollection = databaseProvider.AppSettings;
}
public Task<AppSettingsDocument> GetAsync(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var document = appSettingsCollection.GetDocument(DefaultSettingsId);
if (document is not null)
{
return Task.FromResult(Map(document));
}
var defaults = new AppSettingsDocument();
SaveDocument(defaults);
return Task.FromResult(defaults);
}
public async Task<AppSettingsDocument> SaveAsync(AppSettingsDocument settings, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var existing = await GetAsync(cancellationToken);
settings.Id = DefaultSettingsId;
settings.CreatedAtUtc = existing.CreatedAtUtc;
settings.UpdatedAtUtc = DateTimeOffset.UtcNow;
SaveDocument(settings);
return settings;
}
private void SaveDocument(AppSettingsDocument settings)
{
var document = new MutableDocument(DefaultSettingsId);
document.SetDouble("standardWorkHoursPerDay", Decimal.ToDouble(settings.StandardWorkHoursPerDay));
document.SetDouble("lunchBreakHours", Decimal.ToDouble(settings.LunchBreakHours));
document.SetDouble("hourlyGrossRate", Decimal.ToDouble(settings.HourlyGrossRate));
document.SetDouble("profitabilityCoefficient", Decimal.ToDouble(settings.ProfitabilityCoefficient));
document.SetDouble("inpsRate", Decimal.ToDouble(settings.InpsRate));
document.SetDouble("substituteTaxRate", Decimal.ToDouble(settings.SubstituteTaxRate));
document.SetString("currency", settings.Currency);
document.SetString("locale", settings.Locale);
document.SetString("createdAtUtc", settings.CreatedAtUtc.ToString("O"));
document.SetString("updatedAtUtc", settings.UpdatedAtUtc.ToString("O"));
appSettingsCollection.Save(document);
}
private static AppSettingsDocument Map(Document document)
{
return new AppSettingsDocument
{
Id = document.Id,
StandardWorkHoursPerDay = ReadDecimal(document, "standardWorkHoursPerDay", 8m),
LunchBreakHours = ReadDecimal(document, "lunchBreakHours", 1m),
HourlyGrossRate = ReadDecimal(document, "hourlyGrossRate", 17.5m),
ProfitabilityCoefficient = ReadDecimal(document, "profitabilityCoefficient", 0.67m),
InpsRate = ReadDecimal(document, "inpsRate", 0.2607m),
SubstituteTaxRate = ReadDecimal(document, "substituteTaxRate", 0.15m),
Currency = document.GetString("currency") ?? "EUR",
Locale = document.GetString("locale") ?? "it-IT",
CreatedAtUtc = ReadDateTimeOffset(document, "createdAtUtc"),
UpdatedAtUtc = ReadDateTimeOffset(document, "updatedAtUtc")
};
}
private static decimal ReadDecimal(Document document, string key, decimal defaultValue)
{
return document.Contains(key)
? Convert.ToDecimal(document.GetDouble(key))
: defaultValue;
}
private static DateTimeOffset ReadDateTimeOffset(Document document, string key)
{
var value = document.GetString(key);
return DateTimeOffset.TryParse(value, out var parsed)
? parsed
: DateTimeOffset.UtcNow;
}
}

View file

@ -1,44 +0,0 @@
using MongoDB.Driver;
using WorkTracker.Domain;
namespace WorkTracker.Services.Settings;
public sealed class MongoAppSettingsService : IAppSettingsService
{
private const string DefaultSettingsId = "global";
private readonly IMongoCollection<AppSettingsDocument> appSettingsCollection;
public MongoAppSettingsService(IMongoDatabase database)
{
appSettingsCollection = database.GetCollection<AppSettingsDocument>("app_settings");
}
public async Task<AppSettingsDocument> GetAsync(CancellationToken cancellationToken = default)
{
var filter = Builders<AppSettingsDocument>.Filter.Eq(x => x.Id, DefaultSettingsId);
var settings = await appSettingsCollection.Find(filter).FirstOrDefaultAsync(cancellationToken);
if (settings is not null)
{
return settings;
}
var defaults = new AppSettingsDocument();
await appSettingsCollection.InsertOneAsync(defaults, cancellationToken: cancellationToken);
return defaults;
}
public async Task<AppSettingsDocument> SaveAsync(AppSettingsDocument settings, CancellationToken cancellationToken = default)
{
var existing = await GetAsync(cancellationToken);
settings.Id = DefaultSettingsId;
settings.CreatedAtUtc = existing.CreatedAtUtc;
settings.UpdatedAtUtc = DateTimeOffset.UtcNow;
var filter = Builders<AppSettingsDocument>.Filter.Eq(x => x.Id, DefaultSettingsId);
await appSettingsCollection.ReplaceOneAsync(filter, settings, new ReplaceOptions { IsUpsert = true }, cancellationToken);
return settings;
}
}

View file

@ -0,0 +1,52 @@
using Couchbase.Lite;
using Microsoft.Extensions.Options;
using WorkTracker.Configuration;
namespace WorkTracker.Services.Storage;
public sealed class CouchbaseLiteDatabaseProvider : IDisposable
{
private const string AppSettingsCollectionName = "app_settings";
private const string UsersCollectionName = "users";
private readonly Database database;
public CouchbaseLiteDatabaseProvider(IOptions<CouchbaseLiteOptions> options, IHostEnvironment environment)
{
var configuredOptions = options.Value;
var databaseDirectory = ResolveDirectory(configuredOptions.Directory, environment.ContentRootPath);
Directory.CreateDirectory(databaseDirectory);
database = new Database(
configuredOptions.DatabaseName,
new DatabaseConfiguration
{
Directory = databaseDirectory
});
AppSettings = database.GetCollection(AppSettingsCollectionName) ?? database.CreateCollection(AppSettingsCollectionName);
Users = database.GetCollection(UsersCollectionName) ?? database.CreateCollection(UsersCollectionName);
}
public Collection AppSettings { get; }
public Collection Users { get; }
public void Dispose()
{
database.Close();
database.Dispose();
}
private static string ResolveDirectory(string configuredDirectory, string contentRootPath)
{
if (string.IsNullOrWhiteSpace(configuredDirectory))
{
return Path.Combine(contentRootPath, "App_Data", "couchbase");
}
return Path.IsPathRooted(configuredDirectory)
? configuredDirectory
: Path.GetFullPath(Path.Combine(contentRootPath, configuredDirectory));
}
}