Skip to content

Commit

Permalink
refactor: create factory methods for SendPushNotificationFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
tnotheis committed Nov 14, 2024
1 parent 7452e39 commit 52f0d0f
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ private async Task ExecuteDeletion(IdentityAddress identityAddress, Cancellation

private async Task NotifyIdentityAboutStartingDeletion(IdentityAddress identityAddress, CancellationToken cancellationToken)
{
await _pushNotificationSender.SendNotification(new DeletionStartsPushNotification(), new SendPushNotificationFilter { IncludedIdentities = [identityAddress] }, cancellationToken);
await _pushNotificationSender.SendNotification(
new DeletionStartsPushNotification(),
SendPushNotificationFilter.AllDevicesOf(identityAddress),
cancellationToken);
}

private async Task Delete(IdentityAddress identityAddress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ namespace Backbone.BuildingBlocks.Application.PushNotifications;

public record SendPushNotificationFilter
{
private SendPushNotificationFilter()
{
}

public List<DeviceId> ExcludedDevices { get; set; } = [];
public List<IdentityAddress> IncludedIdentities { get; set; } = [];

public static SendPushNotificationFilter AllDevicesOf(IdentityAddress address)
{
return new SendPushNotificationFilter
{
IncludedIdentities = [address]
};
}

public static SendPushNotificationFilter AllDevicesOfExcept(IdentityAddress address, params DeviceId[] deviceIds)
{
return new SendPushNotificationFilter
{
IncludedIdentities = [address],
ExcludedDevices = [.. deviceIds]
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ public DatawalletModifiedDomainEventHandler(IPushNotificationSender pushSenderSe
public async Task Handle(DatawalletModifiedDomainEvent domainEvent)
{
var notification = new DatawalletModificationsCreatedPushNotification(domainEvent.ModifiedByDevice);
await _pushSenderService.SendNotification(notification,
new SendPushNotificationFilter
{
ExcludedDevices = [DeviceId.Parse(domainEvent.ModifiedByDevice)],
IncludedIdentities = [domainEvent.Identity]
}, CancellationToken.None);
await _pushSenderService.SendNotification(
notification,
SendPushNotificationFilter.AllDevicesOfExcept(IdentityAddress.ParseUnsafe(domainEvent.Identity), DeviceId.Parse(domainEvent.ModifiedByDevice)),
CancellationToken.None);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task Handle(ExternalEventCreatedDomainEvent @event)
if (identity.Status != IdentityStatus.ToBeDeleted)
await _pushSenderService.SendNotification(
new ExternalEventCreatedPushNotification(),
new SendPushNotificationFilter { IncludedIdentities = [@event.Owner] },
SendPushNotificationFilter.AllDevicesOf(@event.Owner),
CancellationToken.None
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public IdentityDeletionProcessStartedDomainEventHandler(IPushNotificationSender

public async Task Handle(IdentityDeletionProcessStartedDomainEvent @event)
{
await _pushNotificationSender.SendNotification(new DeletionProcessStartedPushNotification(), new SendPushNotificationFilter { IncludedIdentities = [@event.Address] }, CancellationToken.None);
await _pushNotificationSender.SendNotification(new DeletionProcessStartedPushNotification(), SendPushNotificationFilter.AllDevicesOf(@event.Address), CancellationToken.None);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task<ApproveDeletionProcessResponse> Handle(ApproveDeletionProcessC

await _notificationSender.SendNotification(
new DeletionProcessApprovedPushNotification(daysUntilDeletion),
new SendPushNotificationFilter { IncludedIdentities = [identity.Address] },
SendPushNotificationFilter.AllDevicesOf(identity.Address),
cancellationToken
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task<CancelDeletionProcessAsOwnerResponse> Handle(CancelDeletionPro

await _notificationSender.SendNotification(
new DeletionProcessCancelledByOwnerPushNotification(),
new SendPushNotificationFilter { IncludedIdentities = [identity.Address] },
SendPushNotificationFilter.AllDevicesOf(identity.Address),
cancellationToken
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<CancelDeletionAsSupportResponse> Handle(CancelDeletionAsSuppor

await _notificationSender.SendNotification(
new DeletionProcessCancelledBySupportPushNotification(),
new SendPushNotificationFilter { IncludedIdentities = [identity.Address] },
SendPushNotificationFilter.AllDevicesOf(identity.Address),
cancellationToken
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private async Task SendReminder3(Identity identity, double daysUntilApprovalPeri
{
await _pushNotificationSender.SendNotification(
new DeletionProcessWaitingForApprovalReminderPushNotification((int)Math.Ceiling(daysUntilApprovalPeriodEnds)),
new SendPushNotificationFilter { IncludedIdentities = [identity.Address] },
SendPushNotificationFilter.AllDevicesOf(identity.Address),
cancellationToken
);
identity.DeletionProcessApprovalReminder3Sent();
Expand All @@ -75,7 +75,7 @@ private async Task SendReminder2(Identity identity, double daysUntilApprovalPeri
{
await _pushNotificationSender.SendNotification(
new DeletionProcessWaitingForApprovalReminderPushNotification((int)Math.Ceiling(daysUntilApprovalPeriodEnds)),
new SendPushNotificationFilter { IncludedIdentities = [identity.Address] },
SendPushNotificationFilter.AllDevicesOf(identity.Address),
cancellationToken
);
identity.DeletionProcessApprovalReminder2Sent();
Expand All @@ -87,7 +87,7 @@ private async Task SendReminder1(Identity identity, double daysUntilApprovalPeri
{
await _pushNotificationSender.SendNotification(
new DeletionProcessWaitingForApprovalReminderPushNotification((int)Math.Ceiling(daysUntilApprovalPeriodEnds)),
new SendPushNotificationFilter { IncludedIdentities = [identity.Address] },
SendPushNotificationFilter.AllDevicesOf(identity.Address),
cancellationToken
);
identity.DeletionProcessApprovalReminder1Sent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private async Task SendReminder3(Identity identity, double daysUntilGracePeriodE
{
await _pushSender.SendNotification(
new DeletionProcessGracePeriodReminderPushNotification((int)Math.Ceiling(daysUntilGracePeriodEnds)),
new SendPushNotificationFilter { IncludedIdentities = [identity.Address] },
SendPushNotificationFilter.AllDevicesOf(identity.Address),
cancellationToken
);
identity.DeletionGracePeriodReminder3Sent();
Expand All @@ -80,7 +80,7 @@ private async Task SendReminder2(Identity identity, double daysUntilGracePeriodE
{
await _pushSender.SendNotification(
new DeletionProcessGracePeriodReminderPushNotification((int)Math.Ceiling(daysUntilGracePeriodEnds)),
new SendPushNotificationFilter { IncludedIdentities = [identity.Address] },
SendPushNotificationFilter.AllDevicesOf(identity.Address),
cancellationToken
);
identity.DeletionGracePeriodReminder2Sent();
Expand All @@ -92,7 +92,7 @@ private async Task SendReminder1(Identity identity, double daysUntilGracePeriodE
{
await _pushSender.SendNotification(
new DeletionProcessGracePeriodReminderPushNotification((int)Math.Ceiling(daysUntilGracePeriodEnds)),
new SendPushNotificationFilter { IncludedIdentities = [identity.Address] },
SendPushNotificationFilter.AllDevicesOf(identity.Address),
cancellationToken
);
identity.DeletionGracePeriodReminder1Sent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task<StartDeletionProcessAsOwnerResponse> Handle(StartDeletionProce

await _notificationSender.SendNotification(
new DeletionProcessStartedPushNotification(),
new SendPushNotificationFilter { IncludedIdentities = [identity.Address] },
SendPushNotificationFilter.AllDevicesOf(identity.Address),
cancellationToken
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task<Unit> Handle(SendTestNotificationCommand request, Cancellation
{
await _pushSenderService.SendNotification(
new TestPushNotification { Data = request.Data },
new SendPushNotificationFilter { IncludedIdentities = [_activeIdentity] },
SendPushNotificationFilter.AllDevicesOf(_activeIdentity),
cancellationToken
);
return Unit.Value;
Expand Down

0 comments on commit 52f0d0f

Please sign in to comment.