1+ using BillingQuotas . Mutations ;
2+ using BillingQuotas . Policies ;
3+ using BillingQuotas . State ;
4+ using ModularityKit . Mutator . Abstractions . Context ;
5+ using ModularityKit . Mutator . Abstractions . Engine ;
6+
7+ namespace BillingQuotas . Scenarios ;
8+
9+ /// <summary>
10+ /// EmergencyIncreaseScenario
11+ ///
12+ /// Demonstrates an emergency increase of user quotas, simulating high priority situation where system administrators
13+ /// need to temporarily raise quotas beyond normal limits.
14+ ///
15+ /// This scenario exercises following capabilities of Mutators framework:
16+ ///
17+ /// - Batch execution of multiple <see cref="IMutationEngine.ExecuteBatchAsync{TState}"/> instances via <see cref="IMutationEngine"/>
18+ /// - User level <see cref="MutationContext"/> usage for audit and traceability
19+ /// - Policy evaluation and enforcement for high-risk operations
20+ /// - Reporting of successes and policy-denied mutations
21+ ///
22+ /// Key Steps:
23+ /// 1. Initialize <see cref="IncreaseQuotaMutation"/> with example user quotas.
24+ /// 2. Create <see cref="IMutationEngine.ExecuteBatchAsync{TState}"/> representing a system administrator initiating emergency action.
25+ /// 3. Generate <see cref="IMutationEngine"/> instances for each affected user.
26+ /// 4. Execute all mutations in batch using <see cref="MaxQuotaPolicy"/>.
27+ /// 5. Iterate over the batch results, printing success messages or policy denied reasons.
28+ ///
29+ /// Example Use Case:
30+ /// - Emergency quota increase in SaaS billing or subscription systems.
31+ /// - Temporary overrides for high priority customers or system critical operations.
32+ ///
33+ /// Notes:
34+ /// - Mutations blocked by policies are reported individually but do not prevent execution of other mutations in batch.
35+ /// - This scenario assumes that policies such as <see cref="IMutation{TState}"/> or <see cref="MutationContext"/>
36+ /// may apply, and demonstrates how violations are surfaced to the operator.
37+ /// </summary>
38+ internal static class EmergencyIncreaseScenario
39+ {
40+ internal static async Task Run ( IMutationEngine engine )
41+ {
42+ Console . WriteLine ( "\n === Emergency Increase Scenario ===" ) ;
43+
44+ var state = new QuotaState
45+ {
46+ UserQuotas = new Dictionary < string , int >
47+ {
48+ [ "alice" ] = 50 ,
49+ [ "bob" ] = 95
50+ }
51+ } ;
52+
53+ var ctx = MutationContext . User (
54+ userId : "admin" ,
55+ userName : "System Admin" ,
56+ reason : "Emergency quota increase" ) ;
57+
58+ var mutations = new IMutation < QuotaState > [ ]
59+ {
60+ new IncreaseQuotaMutation ( "alice" , 15 , ctx ) ,
61+ new IncreaseQuotaMutation ( "bob" , 10 , ctx )
62+ } ;
63+
64+ var result = await engine . ExecuteBatchAsync ( mutations , state ) ;
65+
66+ foreach ( var res in result . Results )
67+ {
68+ if ( res . IsSuccess )
69+ Console . WriteLine ( $ "✓ { res . NewState ! . UserQuotas . Keys . First ( ) } quota updated") ;
70+ else
71+ {
72+ Console . WriteLine ( "✗ Mutation blocked:" ) ;
73+ foreach ( var decision in res . PolicyDecisions )
74+ Console . WriteLine ( $ " Policy: { decision . PolicyName } – { decision . Reason } ") ;
75+ }
76+ }
77+ }
78+ }
0 commit comments