Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

organization status changed code changes #5113

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
private readonly ICurrentContext _currentContext;
private readonly IUserRepository _userRepository;
private readonly IStripeEventUtilityService _stripeEventUtilityService;
private readonly IPushNotificationService _pushNotificationService;

public PaymentSucceededHandler(
ILogger<PaymentSucceededHandler> logger,
Expand All @@ -37,7 +38,8 @@
IUserRepository userRepository,
IStripeEventUtilityService stripeEventUtilityService,
IUserService userService,
IOrganizationService organizationService)
IOrganizationService organizationService,
IPushNotificationService pushNotificationService)

Check warning on line 42 in src/Billing/Services/Implementations/PaymentSucceededHandler.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Services/Implementations/PaymentSucceededHandler.cs#L41-L42

Added lines #L41 - L42 were not covered by tests
{
_logger = logger;
_stripeEventService = stripeEventService;
Expand All @@ -50,6 +52,7 @@
_stripeEventUtilityService = stripeEventUtilityService;
_userService = userService;
_organizationService = organizationService;
_pushNotificationService = pushNotificationService;

Check warning on line 55 in src/Billing/Services/Implementations/PaymentSucceededHandler.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Services/Implementations/PaymentSucceededHandler.cs#L55

Added line #L55 was not covered by tests
}

/// <summary>
Expand Down Expand Up @@ -140,6 +143,7 @@

await _organizationService.EnableAsync(organizationId.Value, subscription.CurrentPeriodEnd);
var organization = await _organizationRepository.GetByIdAsync(organizationId.Value);
await _pushNotificationService.PushSyncOrganizationStatusAsync(organization);

Check warning on line 146 in src/Billing/Services/Implementations/PaymentSucceededHandler.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Services/Implementations/PaymentSucceededHandler.cs#L146

Added line #L146 was not covered by tests

await _referenceEventService.RaiseEventAsync(
new ReferenceEvent(ReferenceEventType.Rebilled, organization, _currentContext)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Bit.Billing.Constants;
using Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Utilities;
using Stripe;
Expand All @@ -15,21 +16,27 @@
private readonly IStripeFacade _stripeFacade;
private readonly IOrganizationSponsorshipRenewCommand _organizationSponsorshipRenewCommand;
private readonly IUserService _userService;
private readonly IPushNotificationService _pushNotificationService;
private readonly IOrganizationRepository _organizationRepository;

public SubscriptionUpdatedHandler(
IStripeEventService stripeEventService,
IStripeEventUtilityService stripeEventUtilityService,
IOrganizationService organizationService,
IStripeFacade stripeFacade,
IOrganizationSponsorshipRenewCommand organizationSponsorshipRenewCommand,
IUserService userService)
IUserService userService,
IPushNotificationService pushNotificationService,
IOrganizationRepository organizationRepository)

Check warning on line 30 in src/Billing/Services/Implementations/SubscriptionUpdatedHandler.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Services/Implementations/SubscriptionUpdatedHandler.cs#L28-L30

Added lines #L28 - L30 were not covered by tests
{
_stripeEventService = stripeEventService;
_stripeEventUtilityService = stripeEventUtilityService;
_organizationService = organizationService;
_stripeFacade = stripeFacade;
_organizationSponsorshipRenewCommand = organizationSponsorshipRenewCommand;
_userService = userService;
_pushNotificationService = pushNotificationService;
_organizationRepository = organizationRepository;

Check warning on line 39 in src/Billing/Services/Implementations/SubscriptionUpdatedHandler.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Services/Implementations/SubscriptionUpdatedHandler.cs#L38-L39

Added lines #L38 - L39 were not covered by tests
}

/// <summary>
Expand Down Expand Up @@ -70,6 +77,8 @@
case StripeSubscriptionStatus.Active when organizationId.HasValue:
{
await _organizationService.EnableAsync(organizationId.Value);
var organization = await _organizationRepository.GetByIdAsync(organizationId.Value);
await _pushNotificationService.PushSyncOrganizationStatusAsync(organization);

Check warning on line 81 in src/Billing/Services/Implementations/SubscriptionUpdatedHandler.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Services/Implementations/SubscriptionUpdatedHandler.cs#L80-L81

Added lines #L80 - L81 were not covered by tests
break;
}
case StripeSubscriptionStatus.Active:
Expand Down
1 change: 1 addition & 0 deletions src/Core/Enums/PushType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ public enum PushType : byte
AuthRequestResponse = 16,

SyncOrganizations = 17,
SyncOrganizationStatusChanged = 18,
}
6 changes: 6 additions & 0 deletions src/Core/Models/PushNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@
public Guid UserId { get; set; }
public Guid Id { get; set; }
}

public class OrganizationStatusPushNotification
{
public Guid OrganizationId { get; set; }
public bool Enabled { get; set; }

Check warning on line 57 in src/Core/Models/PushNotification.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Models/PushNotification.cs#L56-L57

Added lines #L56 - L57 were not covered by tests
}
12 changes: 12 additions & 0 deletions src/Core/NotificationHub/NotificationHubPushNotificationService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using System.Text.RegularExpressions;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Auth.Entities;
using Bit.Core.Context;
using Bit.Core.Enums;
Expand Down Expand Up @@ -226,6 +227,17 @@
}
}

public async Task PushSyncOrganizationStatusAsync(Organization organization)
{
var message = new OrganizationStatusPushNotification
{
OrganizationId = organization.Id,
Enabled = organization.Enabled
};

Check warning on line 236 in src/Core/NotificationHub/NotificationHubPushNotificationService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/NotificationHub/NotificationHubPushNotificationService.cs#L231-L236

Added lines #L231 - L236 were not covered by tests

await SendPayloadToOrganizationAsync(organization.Id, PushType.SyncOrganizationStatusChanged, message, false);
}

Check warning on line 239 in src/Core/NotificationHub/NotificationHubPushNotificationService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/NotificationHub/NotificationHubPushNotificationService.cs#L238-L239

Added lines #L238 - L239 were not covered by tests

private string GetContextIdentifier(bool excludeCurrentContext)
{
if (!excludeCurrentContext)
Expand Down
4 changes: 3 additions & 1 deletion src/Core/Services/IPushNotificationService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bit.Core.Auth.Entities;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Auth.Entities;
using Bit.Core.Enums;
using Bit.Core.Tools.Entities;
using Bit.Core.Vault.Entities;
Expand Down Expand Up @@ -27,4 +28,5 @@ public interface IPushNotificationService
Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier, string deviceId = null);
Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier,
string deviceId = null);
Task PushSyncOrganizationStatusAsync(Organization organization);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using Azure.Storage.Queues;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Auth.Entities;
using Bit.Core.Context;
using Bit.Core.Enums;
Expand Down Expand Up @@ -221,4 +222,15 @@
// Noop
return Task.FromResult(0);
}

public async Task PushSyncOrganizationStatusAsync(Organization organization)
{
var message = new OrganizationStatusPushNotification
{
OrganizationId = organization.Id,
Enabled = organization.Enabled
};
await SendMessageAsync(PushType.SyncOrganizationStatusChanged, message, false);
}

Check warning on line 234 in src/Core/Services/Implementations/AzureQueuePushNotificationService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/AzureQueuePushNotificationService.cs#L227-L234

Added lines #L227 - L234 were not covered by tests

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bit.Core.Auth.Entities;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Auth.Entities;
using Bit.Core.Enums;
using Bit.Core.Settings;
using Bit.Core.Tools.Entities;
Expand Down Expand Up @@ -144,6 +145,12 @@
return Task.FromResult(0);
}

public Task PushSyncOrganizationStatusAsync(Organization organization)
{
PushToServices((s) => s.PushSyncOrganizationStatusAsync(organization));
return Task.FromResult(0);
}

Check warning on line 152 in src/Core/Services/Implementations/MultiServicePushNotificationService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/MultiServicePushNotificationService.cs#L149-L152

Added lines #L149 - L152 were not covered by tests

private void PushToServices(Func<IPushNotificationService, Task> pushFunc)
{
if (_services != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bit.Core.Auth.Entities;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Auth.Entities;
using Bit.Core.Context;
using Bit.Core.Enums;
using Bit.Core.Models;
Expand Down Expand Up @@ -227,4 +228,15 @@
// Noop
return Task.FromResult(0);
}

public async Task PushSyncOrganizationStatusAsync(Organization organization)
{
var message = new OrganizationStatusPushNotification
{
OrganizationId = organization.Id,
Enabled = organization.Enabled
};

Check warning on line 238 in src/Core/Services/Implementations/NotificationsApiPushNotificationService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/NotificationsApiPushNotificationService.cs#L233-L238

Added lines #L233 - L238 were not covered by tests

await SendMessageAsync(PushType.SyncOrganizationStatusChanged, message, false);
}

Check warning on line 241 in src/Core/Services/Implementations/NotificationsApiPushNotificationService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/NotificationsApiPushNotificationService.cs#L240-L241

Added lines #L240 - L241 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bit.Core.Auth.Entities;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Auth.Entities;
using Bit.Core.Context;
using Bit.Core.Enums;
using Bit.Core.IdentityServer;
Expand Down Expand Up @@ -251,4 +252,15 @@
{
throw new NotImplementedException();
}

public async Task PushSyncOrganizationStatusAsync(Organization organization)
{
var message = new OrganizationStatusPushNotification
{
OrganizationId = organization.Id,
Enabled = organization.Enabled
};

Check warning on line 262 in src/Core/Services/Implementations/RelayPushNotificationService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/RelayPushNotificationService.cs#L257-L262

Added lines #L257 - L262 were not covered by tests

await SendPayloadToOrganizationAsync(organization.Id, PushType.SyncOrganizationStatusChanged, message, false);
}

Check warning on line 265 in src/Core/Services/Implementations/RelayPushNotificationService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/RelayPushNotificationService.cs#L264-L265

Added lines #L264 - L265 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bit.Core.Auth.Entities;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Auth.Entities;
using Bit.Core.Enums;
using Bit.Core.Tools.Entities;
using Bit.Core.Vault.Entities;
Expand Down Expand Up @@ -88,6 +89,11 @@
return Task.FromResult(0);
}

public Task PushSyncOrganizationStatusAsync(Organization organization)
{
return Task.FromResult(0);
}

Check warning on line 95 in src/Core/Services/NoopImplementations/NoopPushNotificationService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/NoopImplementations/NoopPushNotificationService.cs#L93-L95

Added lines #L93 - L95 were not covered by tests

public Task PushAuthRequestAsync(AuthRequest authRequest)
{
return Task.FromResult(0);
Expand Down
7 changes: 7 additions & 0 deletions src/Notifications/HubHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@
await hubContext.Clients.User(authRequestNotification.Payload.UserId.ToString())
.SendAsync("ReceiveMessage", authRequestNotification, cancellationToken);
break;
case PushType.SyncOrganizationStatusChanged:
var orgStatusNotification =
JsonSerializer.Deserialize<PushNotificationData<OrganizationStatusPushNotification>>(
notificationJson, _deserializerOptions);
await hubContext.Clients.Group($"Organization_{orgStatusNotification.Payload.OrganizationId}")
.SendAsync("ReceiveMessage", orgStatusNotification, cancellationToken);
break;

Check warning on line 94 in src/Notifications/HubHelpers.cs

View check run for this annotation

Codecov / codecov/patch

src/Notifications/HubHelpers.cs#L89-L94

Added lines #L89 - L94 were not covered by tests
default:
break;
}
Expand Down
Loading