-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathCapabilities.cs
More file actions
229 lines (201 loc) · 8.35 KB
/
Copy pathCapabilities.cs
File metadata and controls
229 lines (201 loc) · 8.35 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
using System.Text.Json;
using FlowX;
namespace EventDriven;
/// <summary>Refuses a request that could not become an invoice.</summary>
/// <remarks>
/// <para>
/// <strong>The stance is <see cref="Authorization.Internal"/> on all four business capabilities,
/// and that is the one authorisation decision transport portability actually constrains.</strong>
/// Two of the four transports here start a flow with no principal at all — a broker delivery and
/// a cron occurrence carry no caller — so <c>Authenticated</c> or <c>Permission</c> on a shared
/// step would refuse every message and every firing while passing over HTTP. A chain that has to
/// run under all four therefore states the truth: at this step there is no external caller to
/// refuse. What guards the chain is the trigger each flow carries.
/// </para>
/// </remarks>
[Capability("invoice.validate", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true)]
public sealed class ValidateInvoice : ICapability<IssueInvoice, ValidatedInvoice>
{
private static readonly string[] Billable = ["GBP", "EUR", "USD"];
/// <inheritdoc />
public ValueTask<Result<ValidatedInvoice>> ExecuteAsync(
IssueInvoice input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
if (input.Net <= 0m)
{
return ValueTask.FromResult(
Result.Fail<ValidatedInvoice>(InvoiceErrors.NotPositive(input.Net)));
}
return ValueTask.FromResult(
Array.IndexOf(Billable, input.Currency) < 0
? Result.Fail<ValidatedInvoice>(InvoiceErrors.UnsupportedCurrency(input.Currency))
: Result.Ok(new ValidatedInvoice(
input.Reference, input.Account, input.Net, input.Currency)));
}
}
/// <summary>Applies the tax rate. A pure function of its input, which is why replay is safe.</summary>
[Capability("invoice.tax", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true)]
public sealed class CalculateTax : ICapability<ValidatedInvoice, TaxedInvoice>
{
private const decimal Rate = 0.20m;
/// <inheritdoc />
public ValueTask<Result<TaxedInvoice>> ExecuteAsync(
ValidatedInvoice input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
return ValueTask.FromResult(Result.Ok(new TaxedInvoice(
input.Reference,
input.Account,
input.Net,
decimal.Round(input.Net * Rate, 2, MidpointRounding.ToEven),
input.Currency)));
}
}
/// <summary>Writes the invoice to the ledger.</summary>
/// <remarks>
/// <strong>Keyed on the billing reference and not on the instance</strong>, so the same reference
/// issued over four transports is one row rather than four. That is what an idempotent write
/// means here, and it is what lets the portability assertion compare outputs for equality — an
/// invoice id derived from the instance id would differ per transport by construction and would
/// prove nothing.
/// </remarks>
[Capability("invoice.persist", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["invoice-ledger"])]
public sealed class PersistInvoice : ICapability<TaxedInvoice, Invoice>
{
private readonly IInvoiceLedger _ledger;
/// <summary>Creates the capability.</summary>
/// <param name="ledger">Where invoices are written.</param>
public PersistInvoice(IInvoiceLedger ledger)
{
ArgumentNullException.ThrowIfNull(ledger);
_ledger = ledger;
}
/// <inheritdoc />
public async ValueTask<Result<Invoice>> ExecuteAsync(
TaxedInvoice input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
var invoice = new Invoice(
"INV-" + input.Reference,
input.Account,
input.Net,
input.Tax,
input.Net + input.Tax,
input.Currency);
await _ledger.WriteAsync(invoice, ct).ConfigureAwait(false);
return invoice;
}
}
/// <summary>Voids the invoice. The business inverse of <see cref="PersistInvoice"/>.</summary>
[Capability("invoice.void", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["invoice-ledger"])]
public sealed class VoidInvoice : ICapability<TaxedInvoice, Invoice>
{
private readonly IInvoiceLedger _ledger;
/// <summary>Creates the capability.</summary>
/// <param name="ledger">Where invoices are written.</param>
public VoidInvoice(IInvoiceLedger ledger)
{
ArgumentNullException.ThrowIfNull(ledger);
_ledger = ledger;
}
/// <inheritdoc />
public async ValueTask<Result<Invoice>> ExecuteAsync(
TaxedInvoice input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
await _ledger.VoidAsync("INV-" + input.Reference, ct).ConfigureAwait(false);
return new Invoice(
"INV-" + input.Reference, input.Account, input.Net, input.Tax, 0m, input.Currency);
}
}
/// <summary>
/// Turns a delivered or observed <c>invoice.requested</c> event into the chain's own input.
/// </summary>
/// <remarks>
/// <para>
/// <strong>The whole of what a bus transport costs this chain, and one capability serves two of
/// them.</strong> <see cref="IssueInvoiceOverBusFlow"/> is started by a broker and
/// <see cref="IssueInvoiceOverChangeFlow"/> by the outbox itself; both hand the flow a
/// <see cref="BusMessage"/> whose body the host deliberately did not deserialise, because doing
/// so needs a <c>JsonTypeInfo</c> only generated code can name. A capability is where a
/// serialiser context is in scope and where a malformed body is a <c>Result</c> rather than an
/// exception out of a background service.
/// </para>
/// </remarks>
[Capability("invoice.read_request", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true)]
public sealed class ReadInvoiceRequest : ICapability<BusMessage, IssueInvoice>
{
/// <inheritdoc />
public ValueTask<Result<IssueInvoice>> ExecuteAsync(
BusMessage input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
if (input.Payload is not { Length: > 0 } body)
{
return ValueTask.FromResult(
Result.Fail<IssueInvoice>(InvoiceErrors.EventHasNoBody(input.EventId)));
}
var requested = JsonSerializer.Deserialize(
body, EventDrivenJournalJsonContext.Default.InvoiceRequested);
return ValueTask.FromResult(requested is null
? Result.Fail<IssueInvoice>(InvoiceErrors.EventHasNoBody(input.EventId))
: Result.Ok(new IssueInvoice(
requested.Reference, requested.Account, requested.Net, requested.Currency)));
}
}
/// <summary>Turns a cron occurrence into the chain's own input.</summary>
/// <remarks>
/// <strong>The occurrence is read from the input and never from a clock.</strong> A run at 03:41
/// is still billing the run that fell due at 03:00, and <c>FLOWX1007</c> forbids a durable flow
/// reading an ambient clock at all — so the instant arrives as <see cref="ScheduledFire"/> and is
/// journalled with the instance.
/// </remarks>
[Capability("invoice.due", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true)]
public sealed class DueInvoice : ICapability<ScheduledFire, IssueInvoice>
{
private readonly IBillingCalendar _calendar;
/// <summary>Creates the capability.</summary>
/// <param name="calendar">What is billed at an occurrence.</param>
public DueInvoice(IBillingCalendar calendar)
{
ArgumentNullException.ThrowIfNull(calendar);
_calendar = calendar;
}
/// <inheritdoc />
public async ValueTask<Result<IssueInvoice>> ExecuteAsync(
ScheduledFire input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
var due = await _calendar.DueAtAsync(input.OccurrenceAt, ct).ConfigureAwait(false);
return due is null
? Result.Fail<IssueInvoice>(InvoiceErrors.NothingDue(input.OccurrenceAt))
: Result.Ok(due);
}
}