The home page becomes the widget dashboard of mockups 004-006. Each user switches widgets on and off, orders them (drag and drop or buttons that also work from the keyboard), sizes them on the 12-column grid and picks the start page; every change is saved at once. Whoever may change the company settings sets a default layout per role, used by people who never customized; Reset goes back to it. Todo, Done and Deadlines collect lines from sources each module registers, so later milestones fill them without touching the widgets; a failing source is logged and skipped. M3 provides licence, unread warnings and scheduled task runs. Notes, Shortcuts and System status work now; widgets of modules still to come say which milestone brings their data instead of showing fake numbers. Role defaults live in the new company_settings table, written through CompanySettingsService with company.settings.write (ADR 0022). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
75d60ee4c4
commit
acb7e598d1
34 files changed
+2812
-76
No files matched your search
@@ -0,0 +1,126 @@
|
||||
using Khaemhat.Application.Security;
|
||||
using Khaemhat.Core.Instance;
|
||||
using Khaemhat.Data;
|
||||
using Khaemhat.Data.Querying;
|
||||
|
||||
namespace Khaemhat.Application.Instance;
|
||||
|
||||
/// <summary>
|
||||
/// Reads and writes the preferences of a company as a whole (<c>company_settings</c>, SPEC §11.12): the default
|
||||
/// dashboard of each role today, the company options of the later modules tomorrow. Reading is open to the company
|
||||
/// code (the callers check what they show); writing needs <c>company.settings.write</c>.
|
||||
/// </summary>
|
||||
/// <param name="factory">Opens the databases.</param>
|
||||
/// <param name="permissions">Checks the permission to write.</param>
|
||||
public sealed class CompanySettingsService(IUnitOfWorkFactory factory, PermissionService permissions)
|
||||
{
|
||||
/// <summary>The permission to change the settings.</summary>
|
||||
public const string WritePermission = "company.settings.write";
|
||||
|
||||
/// <summary>The longest value accepted (layouts are JSON).</summary>
|
||||
public const int MaxValueLength = 64 * 1024;
|
||||
|
||||
/// <summary>Reads the settings of a company, all or those whose key starts with a prefix.</summary>
|
||||
/// <param name="companyCode">The company.</param>
|
||||
/// <param name="prefix">The start of the keys, or <see langword="null"/> for all.</param>
|
||||
/// <param name="cancellationToken">Cancellation.</param>
|
||||
/// <returns>The values by key.</returns>
|
||||
public async Task<IReadOnlyDictionary<string, string?>> GetAllAsync(string companyCode, string? prefix = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(companyCode);
|
||||
await using var unitOfWork = await factory.CreateAsync(DatabaseScope.Company(companyCode), cancellationToken).ConfigureAwait(false);
|
||||
var rows = await ListAsync(unitOfWork, cancellationToken).ConfigureAwait(false);
|
||||
return rows.Where(r => prefix is null || r.Key.StartsWith(prefix, StringComparison.Ordinal))
|
||||
.ToDictionary(r => r.Key, r => r.Value, StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>Reads one setting.</summary>
|
||||
/// <param name="companyCode">The company.</param>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="cancellationToken">Cancellation.</param>
|
||||
/// <returns>The value, or <see langword="null"/>.</returns>
|
||||
public async Task<string?> GetAsync(string companyCode, string key, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(key);
|
||||
var all = await GetAllAsync(companyCode, key, cancellationToken).ConfigureAwait(false);
|
||||
return all.TryGetValue(key, out var value) ? value : null;
|
||||
}
|
||||
|
||||
/// <summary>Writes one setting; <see langword="null"/> removes it.</summary>
|
||||
/// <param name="actor">Who writes: needs <see cref="WritePermission"/> in that company.</param>
|
||||
/// <param name="companyCode">The company.</param>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <param name="cancellationToken">Cancellation.</param>
|
||||
/// <returns>A task.</returns>
|
||||
public Task SetAsync(Actor actor, string companyCode, string key, string? value, CancellationToken cancellationToken = default) =>
|
||||
SetManyAsync(actor, companyCode, new Dictionary<string, string?>(StringComparer.Ordinal) { [key] = value }, cancellationToken);
|
||||
|
||||
/// <summary>Writes several settings in one transaction; <see langword="null"/> values remove the key.</summary>
|
||||
/// <param name="actor">Who writes: needs <see cref="WritePermission"/> in that company.</param>
|
||||
/// <param name="companyCode">The company.</param>
|
||||
/// <param name="values">The values by key.</param>
|
||||
/// <param name="cancellationToken">Cancellation.</param>
|
||||
/// <returns>A task.</returns>
|
||||
/// <exception cref="AccessDeniedException">The actor may not change this company's settings.</exception>
|
||||
public async Task SetManyAsync(Actor actor, string companyCode, IReadOnlyDictionary<string, string?> values, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(actor);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(companyCode);
|
||||
ArgumentNullException.ThrowIfNull(values);
|
||||
if (!actor.IsInstanceAccount && !string.Equals(actor.CompanyCode, companyCode, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new AccessDeniedException("Settings of another company.");
|
||||
}
|
||||
|
||||
await permissions.DemandAsync(actor, WritePermission, cancellationToken).ConfigureAwait(false);
|
||||
foreach (var (key, value) in values)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key) || key.Length > 200)
|
||||
{
|
||||
throw new ValidationFailedException("settings.key_invalid");
|
||||
}
|
||||
|
||||
if (value is { Length: > MaxValueLength })
|
||||
{
|
||||
throw new ValidationFailedException("settings.value_too_long");
|
||||
}
|
||||
}
|
||||
|
||||
if (values.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await using var unitOfWork = await factory.CreateAsync(DatabaseScope.Company(companyCode), cancellationToken).ConfigureAwait(false);
|
||||
await unitOfWork.InTransactionAsync(ct => WriteAsync(unitOfWork, values, ct), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static async Task<IReadOnlyList<CompanySetting>> ListAsync(IUnitOfWork unitOfWork, CancellationToken cancellationToken) =>
|
||||
(await unitOfWork.Repository<CompanySetting, long>().ListAsync(Query.For<CompanySetting>(), cancellationToken).ConfigureAwait(false)).Items;
|
||||
|
||||
private static async Task WriteAsync(IUnitOfWork unitOfWork, IReadOnlyDictionary<string, string?> values, CancellationToken cancellationToken)
|
||||
{
|
||||
var repository = unitOfWork.Repository<CompanySetting, long>();
|
||||
var existing = (await ListAsync(unitOfWork, cancellationToken).ConfigureAwait(false)).ToDictionary(r => r.Key, StringComparer.Ordinal);
|
||||
foreach (var (key, value) in values)
|
||||
{
|
||||
if (existing.TryGetValue(key, out var row))
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
await repository.DeleteAsync(row.Id, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else if (!string.Equals(row.Value, value, StringComparison.Ordinal))
|
||||
{
|
||||
row.Value = value;
|
||||
await repository.UpdateAsync(row, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else if (value is not null)
|
||||
{
|
||||
await repository.InsertAsync(new CompanySetting { Key = key, Value = value }, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user