-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnowledgeBaseSettingsContracts.cs
More file actions
281 lines (241 loc) · 11 KB
/
Copy pathKnowledgeBaseSettingsContracts.cs
File metadata and controls
281 lines (241 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using BuildingBlocks.Application.Actors;
using BuildingBlocks.Application.Auditing;
using BuildingBlocks.Application.Authorization;
using BuildingBlocks.Application.Dispatching;
using BuildingBlocks.Application.Modules;
using BuildingBlocks.Application.Results;
using KnowledgeBase.Application.Authorization;
using NodaTime;
using DomainClock = BuildingBlocks.Domain.Time.IClock;
namespace KnowledgeBase.Application.Settings;
public sealed record KnowledgeBaseModuleSettings(
string PublicExperienceTitle,
string PublicExperienceBlurb,
string SearchPlaceholder,
bool SearchEnabled,
int ManagementPreviewLimit,
int Version,
Instant UpdatedUtc,
string UpdatedByActorId);
public sealed record NormalizedKnowledgeBaseSettings(
string PublicExperienceTitle,
string PublicExperienceBlurb,
string SearchPlaceholder,
bool SearchEnabled,
int ManagementPreviewLimit);
public interface IKnowledgeBaseSettingsStore
{
ValueTask<KnowledgeBaseModuleSettings> GetAsync(CancellationToken cancellationToken);
ValueTask<Result<KnowledgeBaseModuleSettings>> UpdateAsync(
int expectedVersion,
NormalizedKnowledgeBaseSettings settings,
string actorId,
Instant now,
CancellationToken cancellationToken);
}
public static class KnowledgeBaseModuleSettingsDefaults
{
public const int DefaultManagementPreviewLimit = 12;
public const int MaxManagementPreviewLimit = 24;
public const int MinManagementPreviewLimit = 1;
public const string DefaultPublicExperienceBlurb = "A generic knowledge surface for FAQ-style publishing today and broader help content later.";
public const string DefaultPublicExperienceTitle = "Knowledge Base";
public const string DefaultSearchPlaceholder = "Search by title, answer, or category";
public const string SettingsTargetId = "default";
public static readonly Duration RecentAuthenticationWindow = Duration.FromMinutes(5);
}
public static class KnowledgeBaseSettingsErrors
{
public static Error ConcurrencyConflict()
{
return new Error(
"knowledge-base.settings_version_conflict",
"Knowledge base settings were changed by another operation.",
ErrorKind.Conflict);
}
public static Error PublicExperienceBlurbRequired()
{
return new Error(
"knowledge-base.settings_blurb_required",
"Knowledge base settings require a public experience blurb.",
ErrorKind.Validation);
}
public static Error PublicExperienceTitleRequired()
{
return new Error(
"knowledge-base.settings_title_required",
"Knowledge base settings require a public experience title.",
ErrorKind.Validation);
}
public static Error SearchPlaceholderRequired()
{
return new Error(
"knowledge-base.settings_search_placeholder_required",
"Knowledge base settings require a search placeholder when search is enabled.",
ErrorKind.Validation);
}
public static Error PreviewLimitOutOfRange(int limit)
{
return new Error(
"knowledge-base.settings_preview_limit_invalid",
$"Knowledge base settings preview limit '{limit}' must be between {KnowledgeBaseModuleSettingsDefaults.MinManagementPreviewLimit} and {KnowledgeBaseModuleSettingsDefaults.MaxManagementPreviewLimit}.",
ErrorKind.Validation);
}
public static Error VersionMustBePositive()
{
return new Error(
"knowledge-base.settings_version_required",
"A positive expected version is required when updating knowledge base settings.",
ErrorKind.Validation);
}
}
public sealed record GetKnowledgeBasePublicSettingsQuery : IQuery<KnowledgeBaseModuleSettings>, IModuleScoped
{
public string ModuleKey => KnowledgeBaseModuleInfo.ModuleKey;
}
internal sealed class GetKnowledgeBasePublicSettingsQueryHandler : IQueryHandler<GetKnowledgeBasePublicSettingsQuery, KnowledgeBaseModuleSettings>
{
private readonly IKnowledgeBaseSettingsStore _store;
public GetKnowledgeBasePublicSettingsQueryHandler(IKnowledgeBaseSettingsStore store)
{
_store = store ?? throw new ArgumentNullException(nameof(store));
}
public async Task<Result<KnowledgeBaseModuleSettings>> Handle(GetKnowledgeBasePublicSettingsQuery query, CancellationToken cancellationToken)
{
var settings = await _store.GetAsync(cancellationToken);
return Result<KnowledgeBaseModuleSettings>.Success(settings);
}
}
public sealed record GetKnowledgeBaseManagementSettingsQuery : IQuery<KnowledgeBaseModuleSettings>, IModuleScoped, IAuthorizeRequest
{
public string ModuleKey => KnowledgeBaseModuleInfo.ModuleKey;
public IReadOnlyCollection<RoleRequirement> AuthorizationRequirements { get; } =
[RoleRequirement.Admin];
}
internal sealed class GetKnowledgeBaseManagementSettingsQueryHandler : IQueryHandler<GetKnowledgeBaseManagementSettingsQuery, KnowledgeBaseModuleSettings>
{
private readonly IKnowledgeBaseSettingsStore _store;
public GetKnowledgeBaseManagementSettingsQueryHandler(IKnowledgeBaseSettingsStore store)
{
_store = store ?? throw new ArgumentNullException(nameof(store));
}
public async Task<Result<KnowledgeBaseModuleSettings>> Handle(GetKnowledgeBaseManagementSettingsQuery query, CancellationToken cancellationToken)
{
var settings = await _store.GetAsync(cancellationToken);
return Result<KnowledgeBaseModuleSettings>.Success(settings);
}
}
public sealed record UpdateKnowledgeBaseSettingsCommand(
int ExpectedVersion,
string PublicExperienceTitle,
string PublicExperienceBlurb,
string SearchPlaceholder,
bool SearchEnabled,
int ManagementPreviewLimit)
: ICommand<KnowledgeBaseModuleSettings>, IModuleScoped, IAuthorizeRequest, IRequireRecentAuthentication
{
public string ModuleKey => KnowledgeBaseModuleInfo.ModuleKey;
public IReadOnlyCollection<RoleRequirement> AuthorizationRequirements { get; } =
[RoleRequirement.Admin];
public Duration RecentAuthenticationWindow { get; } = KnowledgeBaseModuleSettingsDefaults.RecentAuthenticationWindow;
}
internal sealed class UpdateKnowledgeBaseSettingsCommandHandler : ICommandHandler<UpdateKnowledgeBaseSettingsCommand, KnowledgeBaseModuleSettings>
{
private readonly IAuditEventWriter _auditEventWriter;
private readonly DomainClock _clock;
private readonly ICurrentActorAccessor _currentActorAccessor;
private readonly IRequestContextAccessor _requestContextAccessor;
private readonly IKnowledgeBaseSettingsStore _store;
public UpdateKnowledgeBaseSettingsCommandHandler(
IAuditEventWriter auditEventWriter,
DomainClock clock,
ICurrentActorAccessor currentActorAccessor,
IRequestContextAccessor requestContextAccessor,
IKnowledgeBaseSettingsStore store)
{
_auditEventWriter = auditEventWriter ?? throw new ArgumentNullException(nameof(auditEventWriter));
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
_currentActorAccessor = currentActorAccessor ?? throw new ArgumentNullException(nameof(currentActorAccessor));
_requestContextAccessor = requestContextAccessor ?? throw new ArgumentNullException(nameof(requestContextAccessor));
_store = store ?? throw new ArgumentNullException(nameof(store));
}
public async Task<Result<KnowledgeBaseModuleSettings>> Handle(UpdateKnowledgeBaseSettingsCommand command, CancellationToken cancellationToken)
{
if (command.ExpectedVersion <= 0)
{
return Result<KnowledgeBaseModuleSettings>.Failure(KnowledgeBaseSettingsErrors.VersionMustBePositive());
}
var normalized = KnowledgeBaseSettingsValidation.Normalize(
command.PublicExperienceTitle,
command.PublicExperienceBlurb,
command.SearchPlaceholder,
command.SearchEnabled,
command.ManagementPreviewLimit);
if (normalized.IsFailure)
{
return Result<KnowledgeBaseModuleSettings>.Failure(normalized.Error);
}
var actor = await _currentActorAccessor.GetCurrentAsync(cancellationToken);
var actorId = string.IsNullOrWhiteSpace(actor.ActorId) ? "unknown" : actor.ActorId;
var now = _clock.GetCurrentInstant();
var result = await _store.UpdateAsync(command.ExpectedVersion, normalized.Value!, actorId, now, cancellationToken);
if (result.IsFailure)
{
return result;
}
await _auditEventWriter.WriteAsync(
new AuditEvent(
ModuleKey: KnowledgeBaseModuleInfo.ModuleKey,
Action: "knowledge-base.settings.update",
TargetType: "knowledge-base-settings",
TargetId: KnowledgeBaseModuleSettingsDefaults.SettingsTargetId,
Outcome: "updated",
OccurredUtc: now,
ActorId: actorId,
CorrelationId: _requestContextAccessor.Current?.CorrelationId),
cancellationToken);
return result;
}
}
internal static class KnowledgeBaseSettingsValidation
{
public static Result<NormalizedKnowledgeBaseSettings> Normalize(
string publicExperienceTitle,
string publicExperienceBlurb,
string searchPlaceholder,
bool searchEnabled,
int managementPreviewLimit)
{
var normalizedTitle = publicExperienceTitle.Trim();
if (string.IsNullOrWhiteSpace(normalizedTitle))
{
return Result<NormalizedKnowledgeBaseSettings>.Failure(KnowledgeBaseSettingsErrors.PublicExperienceTitleRequired());
}
var normalizedBlurb = publicExperienceBlurb.Trim();
if (string.IsNullOrWhiteSpace(normalizedBlurb))
{
return Result<NormalizedKnowledgeBaseSettings>.Failure(KnowledgeBaseSettingsErrors.PublicExperienceBlurbRequired());
}
if (managementPreviewLimit < KnowledgeBaseModuleSettingsDefaults.MinManagementPreviewLimit
|| managementPreviewLimit > KnowledgeBaseModuleSettingsDefaults.MaxManagementPreviewLimit)
{
return Result<NormalizedKnowledgeBaseSettings>.Failure(KnowledgeBaseSettingsErrors.PreviewLimitOutOfRange(managementPreviewLimit));
}
var normalizedSearchPlaceholder = searchPlaceholder.Trim();
if (searchEnabled && string.IsNullOrWhiteSpace(normalizedSearchPlaceholder))
{
return Result<NormalizedKnowledgeBaseSettings>.Failure(KnowledgeBaseSettingsErrors.SearchPlaceholderRequired());
}
if (string.IsNullOrWhiteSpace(normalizedSearchPlaceholder))
{
normalizedSearchPlaceholder = KnowledgeBaseModuleSettingsDefaults.DefaultSearchPlaceholder;
}
return Result<NormalizedKnowledgeBaseSettings>.Success(
new NormalizedKnowledgeBaseSettings(
normalizedTitle,
normalizedBlurb,
normalizedSearchPlaceholder,
searchEnabled,
managementPreviewLimit));
}
}