11using Bit . Billing . Constants ;
2+ using Bit . Billing . Jobs ;
23using Bit . Core . AdminConsole . OrganizationFeatures . Organizations . Interfaces ;
4+ using Bit . Core . AdminConsole . Repositories ;
5+ using Bit . Core . AdminConsole . Services ;
36using Bit . Core . Billing . Extensions ;
47using Bit . Core . Services ;
8+ using Quartz ;
59using Event = Stripe . Event ;
610namespace Bit . Billing . Services . Implementations ;
711
@@ -11,17 +15,26 @@ public class SubscriptionDeletedHandler : ISubscriptionDeletedHandler
1115 private readonly IUserService _userService ;
1216 private readonly IStripeEventUtilityService _stripeEventUtilityService ;
1317 private readonly IOrganizationDisableCommand _organizationDisableCommand ;
18+ private readonly IProviderRepository _providerRepository ;
19+ private readonly IProviderService _providerService ;
20+ private readonly ISchedulerFactory _schedulerFactory ;
1421
1522 public SubscriptionDeletedHandler (
1623 IStripeEventService stripeEventService ,
1724 IUserService userService ,
1825 IStripeEventUtilityService stripeEventUtilityService ,
19- IOrganizationDisableCommand organizationDisableCommand )
26+ IOrganizationDisableCommand organizationDisableCommand ,
27+ IProviderRepository providerRepository ,
28+ IProviderService providerService ,
29+ ISchedulerFactory schedulerFactory )
2030 {
2131 _stripeEventService = stripeEventService ;
2232 _userService = userService ;
2333 _stripeEventUtilityService = stripeEventUtilityService ;
2434 _organizationDisableCommand = organizationDisableCommand ;
35+ _providerRepository = providerRepository ;
36+ _providerService = providerService ;
37+ _schedulerFactory = schedulerFactory ;
2538 }
2639
2740 /// <summary>
@@ -53,9 +66,38 @@ public async Task HandleAsync(Event parsedEvent)
5366
5467 await _organizationDisableCommand . DisableAsync ( organizationId . Value , subscription . GetCurrentPeriodEnd ( ) ) ;
5568 }
69+ else if ( providerId . HasValue )
70+ {
71+ var provider = await _providerRepository . GetByIdAsync ( providerId . Value ) ;
72+ if ( provider != null )
73+ {
74+ provider . Enabled = false ;
75+ await _providerService . UpdateAsync ( provider ) ;
76+
77+ await QueueProviderOrganizationDisableJobAsync ( providerId . Value , subscription . GetCurrentPeriodEnd ( ) ) ;
78+ }
79+ }
5680 else if ( userId . HasValue )
5781 {
5882 await _userService . DisablePremiumAsync ( userId . Value , subscription . GetCurrentPeriodEnd ( ) ) ;
5983 }
6084 }
85+
86+ private async Task QueueProviderOrganizationDisableJobAsync ( Guid providerId , DateTime ? expirationDate )
87+ {
88+ var scheduler = await _schedulerFactory . GetScheduler ( ) ;
89+
90+ var job = JobBuilder . Create < ProviderOrganizationDisableJob > ( )
91+ . WithIdentity ( $ "disable-provider-orgs-{ providerId } ", "provider-management" )
92+ . UsingJobData ( "providerId" , providerId . ToString ( ) )
93+ . UsingJobData ( "expirationDate" , expirationDate ? . ToString ( "O" ) )
94+ . Build ( ) ;
95+
96+ var trigger = TriggerBuilder . Create ( )
97+ . WithIdentity ( $ "disable-trigger-{ providerId } ", "provider-management" )
98+ . StartNow ( )
99+ . Build ( ) ;
100+
101+ await scheduler . ScheduleJob ( job , trigger ) ;
102+ }
61103}
0 commit comments