-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathCapabilities.cs
More file actions
923 lines (778 loc) · 33.7 KB
/
Copy pathCapabilities.cs
File metadata and controls
923 lines (778 loc) · 33.7 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
using FlowX;
namespace Workflow;
/// <summary>Errors this application can produce.</summary>
/// <remarks>
/// Declared in one place so the codes are greppable and so two capabilities cannot invent
/// two spellings of the same condition. Each one reaches the manifest, the generated OpenAPI
/// responses and the RFC 7807 <c>type</c> URI.
/// </remarks>
public static class OnboardingErrors
{
/// <summary>The offer names nobody.</summary>
public static Error MissingCandidate() =>
new("onboarding.missing_candidate", "The offer names no candidate.", ErrorCategory.Validation);
/// <summary>
/// The engagement type has no arm. The value the flow's <c>Default</c> block fails with.
/// </summary>
/// <remarks>
/// A <c>static readonly</c> field rather than a factory, because <c>.Fail(error)</c> takes
/// a value and the generator lifts the expression into a <c>static readonly Error</c> on
/// the flow's partial class. Rejecting a request therefore allocates nothing at the moment
/// the flow is already about to unwind.
/// </remarks>
public static readonly Error UnsupportedEmployment = new(
"onboarding.unsupported_employment",
"This flow onboards permanent employees and contractors only.",
ErrorCategory.Validation);
/// <summary>The directory would not create the account.</summary>
public static Error DirectoryUnavailable() =>
new("identity.directory_unavailable", "The directory service did not answer.", ErrorCategory.Unavailable);
/// <summary>There is no desk left at the site.</summary>
public static Error NoDeskAvailable(string site) =>
new Error("workspace.no_desk", $"No desk is free at '{site}'.", ErrorCategory.Conflict)
.With("site", site);
/// <summary>Security would not print a pass.</summary>
public static Error PassRefused(string reason) =>
new Error("workspace.pass_refused", $"The building pass was refused: {reason}.", ErrorCategory.Conflict)
.With("reason", reason);
}
// ---------------------------------------------------------------------------------------
// Validation
// ---------------------------------------------------------------------------------------
/// <summary>Checks the offer is well formed.</summary>
/// <remarks>
/// A read. No side effects, safe to retry, and — because the class references no transport
/// type and calls no other capability — testable by constructing it and calling the method.
/// </remarks>
[Capability("offer.validate", Version = "1.0.0",
Authorization = Authorization.Authenticated,
Idempotent = true)]
public sealed class ValidateOffer : ICapability<OnboardEmployee, ValidatedOffer>
{
/// <inheritdoc />
public ValueTask<Result<ValidatedOffer>> ExecuteAsync(
OnboardEmployee input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
if (string.IsNullOrWhiteSpace(input.CandidateId))
{
return ValueTask.FromResult(Result.Fail<ValidatedOffer>(OnboardingErrors.MissingCandidate()));
}
return ValueTask.FromResult(Result.Ok(new ValidatedOffer(
input.CandidateId,
input.Employment,
input.Site,
input.Equipment,
input.RequiresBackgroundCheck)));
}
}
// ---------------------------------------------------------------------------------------
// The switch's two arms, each with its own inverse
// ---------------------------------------------------------------------------------------
/// <summary>Opens a payroll record. The <c>Permanent</c> arm.</summary>
[Capability("payroll.open", Version = "1.0.0",
Authorization = Authorization.Permission, Permission = "payroll.write",
Idempotent = true,
SideEffects = ["payroll-ledger"])]
public sealed class OpenPayrollRecord : ICapability<ValidatedOffer, PayrollRecord>
{
private readonly IPeopleDirectory _people;
/// <summary>Creates the capability.</summary>
public OpenPayrollRecord(IPeopleDirectory people)
{
ArgumentNullException.ThrowIfNull(people);
_people = people;
}
/// <inheritdoc />
public async ValueTask<Result<PayrollRecord>> ExecuteAsync(
ValidatedOffer input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
var id = await _people.OpenPayrollAsync(input.CandidateId, ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new PayrollRecord(input.CandidateId, id);
}
}
/// <summary>Closes a payroll record. The business inverse of <see cref="OpenPayrollRecord"/>.</summary>
[Capability("payroll.close", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["payroll-ledger"])]
public sealed class ClosePayrollRecord : ICapability<ValidatedOffer, PayrollRecord>
{
private readonly IPeopleDirectory _people;
/// <summary>Creates the capability.</summary>
public ClosePayrollRecord(IPeopleDirectory people)
{
ArgumentNullException.ThrowIfNull(people);
_people = people;
}
/// <inheritdoc />
public async ValueTask<Result<PayrollRecord>> ExecuteAsync(
ValidatedOffer input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
await _people.ClosePayrollAsync(ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new PayrollRecord(input.CandidateId, ctx.IdempotencyKey);
}
}
/// <summary>Signs a supplier agreement. The <c>Contractor</c> arm.</summary>
[Capability("supplier.sign", Version = "1.0.0",
Authorization = Authorization.Permission, Permission = "supplier.write",
Idempotent = true,
SideEffects = ["supplier-register"])]
public sealed class SignSupplierAgreement : ICapability<ValidatedOffer, SupplierAgreement>
{
private readonly IPeopleDirectory _people;
/// <summary>Creates the capability.</summary>
public SignSupplierAgreement(IPeopleDirectory people)
{
ArgumentNullException.ThrowIfNull(people);
_people = people;
}
/// <inheritdoc />
public async ValueTask<Result<SupplierAgreement>> ExecuteAsync(
ValidatedOffer input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
var id = await _people.SignAgreementAsync(input.CandidateId, ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new SupplierAgreement(input.CandidateId, id);
}
}
/// <summary>Voids a supplier agreement. The inverse of <see cref="SignSupplierAgreement"/>.</summary>
[Capability("supplier.void", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["supplier-register"])]
public sealed class VoidSupplierAgreement : ICapability<ValidatedOffer, SupplierAgreement>
{
private readonly IPeopleDirectory _people;
/// <summary>Creates the capability.</summary>
public VoidSupplierAgreement(IPeopleDirectory people)
{
ArgumentNullException.ThrowIfNull(people);
_people = people;
}
/// <inheritdoc />
public async ValueTask<Result<SupplierAgreement>> ExecuteAsync(
ValidatedOffer input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
await _people.VoidAgreementAsync(ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new SupplierAgreement(input.CandidateId, ctx.IdempotencyKey);
}
}
// ---------------------------------------------------------------------------------------
// Identity — the step that carries a policy set
// ---------------------------------------------------------------------------------------
/// <summary>Creates the directory account.</summary>
/// <remarks>
/// Declares <c>Idempotent = true</c>, which is what makes the retry in
/// <see cref="Policies.DirectoryService"/> legal. Declaring it <c>false</c> and attaching the
/// same set is <c>FLOWX1014</c> at build time.
/// </remarks>
[Capability("identity.create", Version = "1.0.0",
Authorization = Authorization.Permission, Permission = "identity.write",
Idempotent = true,
SideEffects = ["directory"])]
public sealed class CreateIdentity : ICapability<ValidatedOffer, Identity>
{
private readonly IPeopleDirectory _people;
/// <summary>Creates the capability.</summary>
public CreateIdentity(IPeopleDirectory people)
{
ArgumentNullException.ThrowIfNull(people);
_people = people;
}
/// <inheritdoc />
public async ValueTask<Result<Identity>> ExecuteAsync(
ValidatedOffer input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
var upn = await _people
.CreateAccountAsync(input.CandidateId, ctx.IdempotencyKey, ct)
.ConfigureAwait(false);
return upn is null
? OnboardingErrors.DirectoryUnavailable()
: new Identity(input.CandidateId, upn);
}
}
/// <summary>Disables the directory account. The inverse of <see cref="CreateIdentity"/>.</summary>
[Capability("identity.disable", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["directory"])]
public sealed class DisableIdentity : ICapability<ValidatedOffer, Identity>
{
private readonly IPeopleDirectory _people;
/// <summary>Creates the capability.</summary>
public DisableIdentity(IPeopleDirectory people)
{
ArgumentNullException.ThrowIfNull(people);
_people = people;
}
/// <inheritdoc />
public async ValueTask<Result<Identity>> ExecuteAsync(
ValidatedOffer input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
await _people.DisableAccountAsync(ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new Identity(input.CandidateId, ctx.IdempotencyKey);
}
}
// ---------------------------------------------------------------------------------------
// The fork's three branches. Two carry their own inverse.
// ---------------------------------------------------------------------------------------
/// <summary>Orders a laptop.</summary>
[Capability("hardware.order", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["asset-register"])]
public sealed class OrderLaptop : ICapability<Identity, LaptopOrder>
{
private readonly IAssetRegistry _assets;
/// <summary>Creates the capability.</summary>
public OrderLaptop(IAssetRegistry assets)
{
ArgumentNullException.ThrowIfNull(assets);
_assets = assets;
}
/// <inheritdoc />
public async ValueTask<Result<LaptopOrder>> ExecuteAsync(
Identity input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
var id = await _assets
.OrderAsync("laptop", input.EmployeeId, ctx.IdempotencyKey, ct)
.ConfigureAwait(false);
return new LaptopOrder(id);
}
}
/// <summary>Cancels the laptop order. This branch's own inverse.</summary>
[Capability("hardware.cancel", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["asset-register"])]
public sealed class CancelLaptopOrder : ICapability<Identity, LaptopOrder>
{
private readonly IAssetRegistry _assets;
/// <summary>Creates the capability.</summary>
public CancelLaptopOrder(IAssetRegistry assets)
{
ArgumentNullException.ThrowIfNull(assets);
_assets = assets;
}
/// <inheritdoc />
public async ValueTask<Result<LaptopOrder>> ExecuteAsync(
Identity input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
await _assets.CancelAsync("laptop", ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new LaptopOrder(ctx.IdempotencyKey);
}
}
/// <summary>Grants access to the systems the role needs.</summary>
[Capability("access.grant", Version = "1.0.0",
Authorization = Authorization.Permission, Permission = "access.write",
Idempotent = true,
SideEffects = ["access-control"])]
public sealed class GrantSystemAccess : ICapability<Identity, AccessGrant>
{
private readonly IAccessControl _access;
/// <summary>Creates the capability.</summary>
public GrantSystemAccess(IAccessControl access)
{
ArgumentNullException.ThrowIfNull(access);
_access = access;
}
/// <inheritdoc />
public async ValueTask<Result<AccessGrant>> ExecuteAsync(
Identity input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
var id = await _access.GrantAsync(input.Upn, ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new AccessGrant(id);
}
}
/// <summary>Revokes system access. This branch's own inverse.</summary>
[Capability("access.revoke", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["access-control"])]
public sealed class RevokeSystemAccess : ICapability<Identity, AccessGrant>
{
private readonly IAccessControl _access;
/// <summary>Creates the capability.</summary>
public RevokeSystemAccess(IAccessControl access)
{
ArgumentNullException.ThrowIfNull(access);
_access = access;
}
/// <inheritdoc />
public async ValueTask<Result<AccessGrant>> ExecuteAsync(
Identity input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
await _access.RevokeAsync(ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new AccessGrant(ctx.IdempotencyKey);
}
}
/// <summary>Books a seat at the next induction. The branch with no inverse — a booking is not an effect worth undoing.</summary>
[Capability("induction.book", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["calendar"])]
public sealed class ScheduleInduction : ICapability<Identity, InductionBooking>
{
private readonly IAccessControl _access;
/// <summary>Creates the capability.</summary>
public ScheduleInduction(IAccessControl access)
{
ArgumentNullException.ThrowIfNull(access);
_access = access;
}
/// <inheritdoc />
public async ValueTask<Result<InductionBooking>> ExecuteAsync(
Identity input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
var id = await _access.BookInductionAsync(input.EmployeeId, ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new InductionBooking(id);
}
}
// ---------------------------------------------------------------------------------------
// The loop body: a conditional whose two arms meet, then a compensable step
// ---------------------------------------------------------------------------------------
/// <summary>Records a human's approval for an item that needs one. The loop's <c>then</c> arm.</summary>
/// <remarks>
/// <strong>This is not a wait.</strong> It records a decision that has already been made and
/// arrived with the request. A step that suspended until a human answered would need
/// <c>AwaitSignal</c>, which is why the README says what this sample does not do.
/// </remarks>
[Capability("equipment.approve", Version = "1.0.0",
Authorization = Authorization.Permission, Permission = "equipment.approve",
Idempotent = true,
SideEffects = ["approval-log"])]
public sealed class RecordEquipmentApproval : ICapability<EquipmentRequest, ApprovedEquipment>
{
private readonly IAssetRegistry _assets;
/// <summary>Creates the capability.</summary>
public RecordEquipmentApproval(IAssetRegistry assets)
{
ArgumentNullException.ThrowIfNull(assets);
_assets = assets;
}
/// <inheritdoc />
public async ValueTask<Result<ApprovedEquipment>> ExecuteAsync(
EquipmentRequest input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
await _assets.RecordApprovalAsync(input.Item, ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new ApprovedEquipment(input.Item, "line-manager");
}
}
/// <summary>Clears an item that needs no approval. The loop's <c>Otherwise</c> arm.</summary>
/// <remarks>
/// Produces the same contract as <see cref="RecordEquipmentApproval"/>, which is what lets
/// the step after the conditional bind by type without knowing which arm ran.
/// </remarks>
[Capability("equipment.auto_clear", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true)]
public sealed class AutoClearEquipment : ICapability<EquipmentRequest, ApprovedEquipment>
{
/// <inheritdoc />
public ValueTask<Result<ApprovedEquipment>> ExecuteAsync(
EquipmentRequest input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
return ValueTask.FromResult(Result.Ok(new ApprovedEquipment(input.Item, "policy")));
}
}
/// <summary>Hands the item over. Compensable, per element.</summary>
/// <remarks>
/// <para>
/// <strong>It binds <see cref="EquipmentRequest"/> — the loop's element — rather than the
/// <see cref="ApprovedEquipment"/> the arm above it produced, and that is forced.</strong>
/// A compensation is invoked with <em>the input of the step it undoes</em>: the generated
/// <c>CompensateAsync</c> emits the same input expression the forward case does, so
/// <see cref="ReturnEquipment"/> cannot bind a different contract from this one. Binding
/// <c>ApprovedEquipment</c> here would therefore make the undo bind it too, and the undo runs
/// after the loop has finished, when the shared bag holds only the last element's copy.
/// </para>
/// <para>
/// So the rule this sample had to learn is: <strong>a step inside a <c>ForEach</c> that
/// declares a compensation must bind the element type.</strong> Only the element is scoped to
/// the iteration; what a body step returns goes into the flow's shared, type-keyed bag.
/// </para>
/// </remarks>
[Capability("equipment.assign", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["asset-register"])]
public sealed class AssignEquipment : ICapability<EquipmentRequest, EquipmentAssignment>
{
private readonly IAssetRegistry _assets;
/// <summary>Creates the capability.</summary>
public AssignEquipment(IAssetRegistry assets)
{
ArgumentNullException.ThrowIfNull(assets);
_assets = assets;
}
/// <inheritdoc />
public async ValueTask<Result<EquipmentAssignment>> ExecuteAsync(
EquipmentRequest input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
var tag = await _assets.AssignAsync(input.Item, ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new EquipmentAssignment(input.Item, tag);
}
}
/// <summary>Takes the item back. The per-element inverse of <see cref="AssignEquipment"/>.</summary>
/// <remarks>
/// <para>
/// <strong>Its input is <see cref="EquipmentRequest"/> because
/// <see cref="AssignEquipment"/>'s is, and this sample was written the other way round
/// first.</strong> Each completed step is recorded with the scope it completed in, so this
/// does run under its own iteration's view of the context — but only the <em>element</em> is
/// scoped to that iteration. What a body step returns goes into the flow's shared, type-keyed
/// bag, so every element's <c>ApprovedEquipment</c> lands in one slot and the last write wins.
/// </para>
/// <para>
/// With the loop's effectful step bound to <c>ApprovedEquipment</c>, both undos read the item
/// the loop ended on and returned it twice, leaving the first item outstanding — with a green
/// trace, because both undos ran and both reported success.
/// <c>CompensationTests.EachElementsUndoReversesItsOwnElement</c> is the test that found it,
/// and <c>CompensationTests.AnUndoSeesItsOwnElementAndTheLoopsLastSharedValue</c> pins the
/// difference so nobody re-introduces it.
/// </para>
/// </remarks>
[Capability("equipment.return", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["asset-register"])]
public sealed class ReturnEquipment : ICapability<EquipmentRequest, EquipmentAssignment>
{
private readonly IAssetRegistry _assets;
/// <summary>Creates the capability.</summary>
public ReturnEquipment(IAssetRegistry assets)
{
ArgumentNullException.ThrowIfNull(assets);
_assets = assets;
}
/// <inheritdoc />
public async ValueTask<Result<EquipmentAssignment>> ExecuteAsync(
EquipmentRequest input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
await _assets.ReturnAsync(input.Item, ct).ConfigureAwait(false);
return new EquipmentAssignment(input.Item, "returned");
}
}
// ---------------------------------------------------------------------------------------
// The trailing conditional, and the close
// ---------------------------------------------------------------------------------------
/// <summary>Starts a background check. The trailing conditional's <c>then</c> arm.</summary>
[Capability("screening.start", Version = "1.0.0",
Authorization = Authorization.Permission, Permission = "screening.write",
Idempotent = true,
SideEffects = ["screening-vendor"])]
public sealed class StartBackgroundCheck : ICapability<Identity, BackgroundCheck>
{
private readonly IAccessControl _access;
/// <summary>Creates the capability.</summary>
public StartBackgroundCheck(IAccessControl access)
{
ArgumentNullException.ThrowIfNull(access);
_access = access;
}
/// <inheritdoc />
public async ValueTask<Result<BackgroundCheck>> ExecuteAsync(
Identity input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
var id = await _access.StartScreeningAsync(input.EmployeeId, ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new BackgroundCheck(id);
}
}
/// <summary>Records that no check was required. The trailing conditional's <c>Otherwise</c> arm.</summary>
/// <remarks>
/// A named capability rather than an empty <c>Otherwise</c>, because a branch that does
/// nothing is invisible in a trace and a reader cannot tell it from a branch that was never
/// taken. This one leaves a row.
/// </remarks>
[Capability("screening.waive", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["screening-log"])]
public sealed class WaiveBackgroundCheck : ICapability<Identity, CheckWaiver>
{
private readonly IAccessControl _access;
/// <summary>Creates the capability.</summary>
public WaiveBackgroundCheck(IAccessControl access)
{
ArgumentNullException.ThrowIfNull(access);
_access = access;
}
/// <inheritdoc />
public async ValueTask<Result<CheckWaiver>> ExecuteAsync(
Identity input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
await _access.WaiveScreeningAsync(input.EmployeeId, ct).ConfigureAwait(false);
return new CheckWaiver("the role does not require screening");
}
}
/// <summary>Posts the welcome pack. The last capability before the event.</summary>
[Capability("welcome.send", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["post"])]
public sealed class SendWelcomePack : ICapability<Identity, WelcomePack>
{
private readonly IAccessControl _access;
/// <summary>Creates the capability.</summary>
public SendWelcomePack(IAccessControl access)
{
ArgumentNullException.ThrowIfNull(access);
_access = access;
}
/// <inheritdoc />
public async ValueTask<Result<WelcomePack>> ExecuteAsync(
Identity input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
var id = await _access.PostWelcomePackAsync(input.EmployeeId, ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new WelcomePack(id);
}
}
// ---------------------------------------------------------------------------------------
// The composed child's capabilities
// ---------------------------------------------------------------------------------------
/// <summary>Holds a desk at the site. Compensable, inside the child.</summary>
[Capability("workspace.allocate_desk", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["facilities"])]
public sealed class AllocateDesk : ICapability<ProvisionWorkspace, DeskAllocation>
{
private readonly IFacilities _facilities;
/// <summary>Creates the capability.</summary>
public AllocateDesk(IFacilities facilities)
{
ArgumentNullException.ThrowIfNull(facilities);
_facilities = facilities;
}
/// <inheritdoc />
public async ValueTask<Result<DeskAllocation>> ExecuteAsync(
ProvisionWorkspace input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
var desk = await _facilities
.AllocateDeskAsync(input.Site, ctx.IdempotencyKey, ct)
.ConfigureAwait(false);
return desk is null
? OnboardingErrors.NoDeskAvailable(input.Site)
: new DeskAllocation(desk);
}
}
/// <summary>Releases the desk. The inverse of <see cref="AllocateDesk"/>, and the one the parent's failure has to reach.</summary>
[Capability("workspace.release_desk", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["facilities"])]
public sealed class ReleaseDesk : ICapability<ProvisionWorkspace, DeskAllocation>
{
private readonly IFacilities _facilities;
/// <summary>Creates the capability.</summary>
public ReleaseDesk(IFacilities facilities)
{
ArgumentNullException.ThrowIfNull(facilities);
_facilities = facilities;
}
/// <inheritdoc />
public async ValueTask<Result<DeskAllocation>> ExecuteAsync(
ProvisionWorkspace input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
await _facilities.ReleaseDeskAsync(ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new DeskAllocation(ctx.IdempotencyKey);
}
}
/// <summary>Issues a building pass.</summary>
[Capability("workspace.issue_pass", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["security"])]
public sealed class IssueBuildingPass : ICapability<DeskAllocation, BuildingPass>
{
private readonly IFacilities _facilities;
/// <summary>Creates the capability.</summary>
public IssueBuildingPass(IFacilities facilities)
{
ArgumentNullException.ThrowIfNull(facilities);
_facilities = facilities;
}
/// <inheritdoc />
public async ValueTask<Result<BuildingPass>> ExecuteAsync(
DeskAllocation input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
var pass = await _facilities
.IssuePassAsync(input.DeskId, ctx.IdempotencyKey, ct)
.ConfigureAwait(false);
return pass is null
? OnboardingErrors.PassRefused("security hold")
: new BuildingPass(pass);
}
}
/// <summary>Cancels a building pass. The inverse of <see cref="IssueBuildingPass"/>.</summary>
[Capability("workspace.cancel_pass", Version = "1.0.0",
Authorization = Authorization.Internal,
Idempotent = true,
SideEffects = ["security"])]
public sealed class CancelBuildingPass : ICapability<DeskAllocation, BuildingPass>
{
private readonly IFacilities _facilities;
/// <summary>Creates the capability.</summary>
public CancelBuildingPass(IFacilities facilities)
{
ArgumentNullException.ThrowIfNull(facilities);
_facilities = facilities;
}
/// <inheritdoc />
public async ValueTask<Result<BuildingPass>> ExecuteAsync(
DeskAllocation input,
CapabilityContext ctx,
CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(ctx);
await _facilities.CancelPassAsync(ctx.IdempotencyKey, ct).ConfigureAwait(false);
return new BuildingPass(ctx.IdempotencyKey);
}
}
// ---------------------------------------------------------------------------------------
// The ports. Infrastructure implements these; no capability above names a driver.
// ---------------------------------------------------------------------------------------
/// <summary>People records the application reads and writes.</summary>
public interface IPeopleDirectory
{
/// <summary>Opens a payroll record, returning its id.</summary>
ValueTask<string> OpenPayrollAsync(string candidateId, string idempotencyKey, CancellationToken ct);
/// <summary>Closes a payroll record opened under an idempotency key.</summary>
ValueTask ClosePayrollAsync(string idempotencyKey, CancellationToken ct);
/// <summary>Signs a supplier agreement, returning its id.</summary>
ValueTask<string> SignAgreementAsync(string candidateId, string idempotencyKey, CancellationToken ct);
/// <summary>Voids an agreement signed under an idempotency key.</summary>
ValueTask VoidAgreementAsync(string idempotencyKey, CancellationToken ct);
/// <summary>Creates a directory account, returning its UPN or <c>null</c> when the directory is down.</summary>
ValueTask<string?> CreateAccountAsync(string candidateId, string idempotencyKey, CancellationToken ct);
/// <summary>Disables an account created under an idempotency key.</summary>
ValueTask DisableAccountAsync(string idempotencyKey, CancellationToken ct);
}
/// <summary>Assets the application orders, approves, assigns and takes back.</summary>
public interface IAssetRegistry
{
/// <summary>Orders an item, returning the order id.</summary>
ValueTask<string> OrderAsync(string item, string employeeId, string idempotencyKey, CancellationToken ct);
/// <summary>Cancels an order placed under an idempotency key.</summary>
ValueTask CancelAsync(string item, string idempotencyKey, CancellationToken ct);
/// <summary>Records a human approval for an item.</summary>
ValueTask RecordApprovalAsync(string item, string idempotencyKey, CancellationToken ct);
/// <summary>Assigns an item, returning its asset tag.</summary>
ValueTask<string> AssignAsync(string item, string idempotencyKey, CancellationToken ct);
/// <summary>Takes an item back.</summary>
ValueTask ReturnAsync(string item, CancellationToken ct);
}
/// <summary>Access, calendar and screening.</summary>
public interface IAccessControl
{
/// <summary>Grants access, returning the grant id.</summary>
ValueTask<string> GrantAsync(string upn, string idempotencyKey, CancellationToken ct);
/// <summary>Revokes a grant made under an idempotency key.</summary>
ValueTask RevokeAsync(string idempotencyKey, CancellationToken ct);
/// <summary>Books an induction seat, returning the booking id.</summary>
ValueTask<string> BookInductionAsync(string employeeId, string idempotencyKey, CancellationToken ct);
/// <summary>Starts a screening, returning its id.</summary>
ValueTask<string> StartScreeningAsync(string employeeId, string idempotencyKey, CancellationToken ct);
/// <summary>Records that screening was not required.</summary>
ValueTask WaiveScreeningAsync(string employeeId, CancellationToken ct);
/// <summary>Posts the welcome pack, returning a tracking id.</summary>
ValueTask<string> PostWelcomePackAsync(string employeeId, string idempotencyKey, CancellationToken ct);
}
/// <summary>Desks and building passes.</summary>
public interface IFacilities
{
/// <summary>Holds a desk, or returns <c>null</c> when the site is full.</summary>
ValueTask<string?> AllocateDeskAsync(string site, string idempotencyKey, CancellationToken ct);
/// <summary>Releases a desk held under an idempotency key.</summary>
ValueTask ReleaseDeskAsync(string idempotencyKey, CancellationToken ct);
/// <summary>Issues a pass, or returns <c>null</c> when security refuses.</summary>
ValueTask<string?> IssuePassAsync(string deskId, string idempotencyKey, CancellationToken ct);
/// <summary>Cancels a pass issued under an idempotency key.</summary>
ValueTask CancelPassAsync(string idempotencyKey, CancellationToken ct);
}