Skip to content

Commit

Permalink
Substitute delivery info to the notification message
Browse files Browse the repository at this point in the history
  • Loading branch information
Bardin08 committed Jun 3, 2024
1 parent bc95a39 commit af9969f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/Core/Abstractions/IUserPreferencesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface IUserPreferencesService
Task<ErrorOr<UserPreferencesDto>> CreateUserPreferences(string userId, CancellationToken ct);
Task<ErrorOr<UserPreferencesDto>> UpdateUserPreferences(
UserPreferencesDto userPreferences, CancellationToken ct);
}
Task<ErrorOr<ChannelDescriptorBaseDto>> GetChannelDeliveryInfo(
string recipientUserId, string channel, CancellationToken ct);
}
5 changes: 4 additions & 1 deletion src/Core/Errors/UserPreferencesErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ internal static ErrorOr<UserPreferencesDto> FailedToUpdate

internal static ErrorOr<UserPreferencesDto> NotFound
=> Error.NotFound("UserPreferences.NotFound", "User preferences not found");
}

internal static ErrorOr<ChannelDescriptorBaseDto> ChannelNotFound
=> Error.NotFound("UserPreferences.Channel.NotFound", "User preferences for channel not found");
}
37 changes: 25 additions & 12 deletions src/Core/Mappers/UserPreferencesMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@

namespace Core.Mappers;

public static class UserPreferencesChannelMapper
{
public static ChannelDescriptorBaseDto ToDto(ChannelDescriptorBase e)
{
return new ChannelDescriptorBaseDto
{
Enabled = e.Enabled,
Description = e.Description,
Metadata = e.Metadata
};
}

public static ChannelDescriptorBase ToEntity(ChannelDescriptorBaseDto dto)
{
return new ChannelDescriptorBase
{
Enabled = dto.Enabled,
Description = dto.Description,
Metadata = dto.Metadata
};
}
}

public static class UserPreferencesMapper
{
internal static UserPreferencesDto ToDto(UserPreferences e)
Expand All @@ -13,12 +36,7 @@ internal static UserPreferencesDto ToDto(UserPreferences e)
{
Id = e.Id.ToString(),
UserId = e.UserId,
Channels = e.Channels.ToDictionary(x => x.Key, d => new ChannelDescriptorBaseDto
{
Enabled = d.Value.Enabled,
Description = d.Value.Description,
Metadata = d.Value.Metadata
})
Channels = e.Channels.ToDictionary(x => x.Key, d => UserPreferencesChannelMapper.ToDto(d.Value))
};
}

Expand Down Expand Up @@ -70,12 +88,7 @@ public static UserPreferences ToEntity(UserPreferencesDto dto)
{
Id = id,
UserId = dto.UserId,
Channels = dto.Channels.ToDictionary(x => x.Key, d => new ChannelDescriptorBase
{
Enabled = d.Value.Enabled,
Description = d.Value.Description,
Metadata = d.Value.Metadata
}),
Channels = dto.Channels.ToDictionary(x => x.Key, d => UserPreferencesChannelMapper.ToEntity(d.Value)),
LastUpdated = DateTimeOffset.UtcNow
};
}
Expand Down
34 changes: 30 additions & 4 deletions src/Core/Services/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Core.Services;

internal class NotificationService(
ITemplateFillerClient massTransitClient,
IUserPreferencesService userPreferencesService,
INotificationsAnalyticsClient notificationsAnalyticsClient,
INotificationsRepository notificationsRepository)
: INotificationsService
Expand All @@ -24,7 +25,7 @@ public async Task<ErrorOr<NotificationDto>> CreateNotification(
await notificationsRepository.InsertOne(notification);
dto = dto with { Id = notification.Id.ToString() };

var deliveryRequests = CreateDeliveryRequests(dto).ToList();
var deliveryRequests = await CreateDeliveryRequests(dto, ct);

await massTransitClient.SendMessages(deliveryRequests, string.Empty);
foreach (var deliveryRequest in deliveryRequests)
Expand All @@ -36,10 +37,35 @@ await notificationsAnalyticsClient
return dto;
}

private IEnumerable<Notification> CreateDeliveryRequests(NotificationDto dto)
private async Task<List<Notification>> CreateDeliveryRequests(NotificationDto dto, CancellationToken ct)
{
return dto.Recipients!
return await dto.Recipients!
.ToAsyncEnumerable()
.Select(recipient => Mappers.External.DeliveryRequestMapper.ToRequest(dto, recipient))
.ToList();
.SelectAwait(deliveryRequest => PopulateDeliveryInfo(deliveryRequest, ct))
.ToListAsync(cancellationToken: ct);
}

private async ValueTask<Notification> PopulateDeliveryInfo(Notification notification, CancellationToken ct)
{
ct.ThrowIfCancellationRequested();

var channel = notification.Recipient.Channel;
var deliveryInfo = await userPreferencesService
.GetChannelDeliveryInfo(notification.Recipient.UserId, channel, ct);
if (deliveryInfo.IsError)
{
return notification;
}

notification = notification with
{
Recipient = notification.Recipient with
{
DeliveryInfo = deliveryInfo.Value
}
};

return notification;
}
}
19 changes: 19 additions & 0 deletions src/Core/Services/UserPreferencesService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Core.Abstractions;
using Core.Errors;
using Core.Extensions;
using Core.Mappers;
using Core.Models.UserPreferences;
using ErrorOr;
Expand Down Expand Up @@ -74,4 +75,22 @@ await _userAnalyticsClient.SendUserAction(
return UserPreferencesErrors.FailedToUpdate;
}
}

public async Task<ErrorOr<ChannelDescriptorBaseDto>> GetChannelDeliveryInfo(
string recipientUserId, string channel, CancellationToken ct)
{
var byUserId = new AdHocSpecification<UserPreferences>(
x => x.UserId == recipientUserId);
var currentUserPreferences = await _userPreferencesRepository.FirstOrDefault(byUserId);
if (currentUserPreferences is null)
{
return UserPreferencesErrors.ChannelNotFound;
}

var channelKey = channel.ToPascalCase();
var channelDescriptor = currentUserPreferences.Channels.GetValueOrDefault(channelKey);
return channelDescriptor is null
? UserPreferencesErrors.ChannelNotFound
: UserPreferencesChannelMapper.ToDto(channelDescriptor);
}
}

0 comments on commit af9969f

Please sign in to comment.