Skip to content

Commit

Permalink
Fix couple user preferences aspects
Browse files Browse the repository at this point in the history
  • Loading branch information
Bardin08 committed Jun 2, 2024
1 parent 24117d3 commit 74ffef7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/Core/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public static void AddGatewayCore(this IServiceCollection services, IConfigurati
{
services.AddScoped<ITemplatesService, TemplatesService>();
services.AddScoped<INotificationsService, NotificationService>();
services.AddScoped<IUserPreferencesService, UserPreferencesService>();
}
}
39 changes: 18 additions & 21 deletions src/Core/Mappers/UserPreferencesMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Core.Mappers;

public class UserPreferencesMapper
public static class UserPreferencesMapper
{
internal static UserPreferencesDto ToDto(UserPreferences e)
{
Expand All @@ -17,44 +17,41 @@ internal static UserPreferencesDto ToDto(UserPreferences e)
Enabled = d.Value.Enabled,
Description = d.Value.Description,
Metadata = d.Value.Metadata
}) ?? []
})
};
}

public static UserPreferences UpdateEntity(UserPreferences e, UserPreferencesDto dto)
{
var channels = e.Channels;
if (channels.Count > 0)
foreach (var ch in dto.Channels)
{
foreach (var ch in dto.Channels)
if (channels.TryGetValue(ch.Key, out var channel))
{
if (channels.TryGetValue(ch.Key, out var channel))
{
channel.Enabled = ch.Value.Enabled;
channel.Description = ch.Value.Description;
channel.Metadata = ch.Value.Metadata;
}
else
channel.Enabled = ch.Value.Enabled;
channel.Description = ch.Value.Description;
channel.Metadata = ch.Value.Metadata;
}
else
{
channels.Add(ch.Key, new ChannelDescriptorBase
{
channels.Add(ch.Key, new ChannelDescriptorBase
{
Enabled = ch.Value.Enabled,
Description = ch.Value.Description,
Metadata = ch.Value.Metadata
});
}
Enabled = ch.Value.Enabled,
Description = ch.Value.Description,
Metadata = ch.Value.Metadata
});
}
}

return e with { LastUpdated = DateTimeOffset.UtcNow, Channels = channels };
}

public static UserPreferences ToEntity(UserPreferencesDto dto)
{
var id = string.IsNullOrEmpty(dto.Id)
? ObjectId.GenerateNewId()
: ObjectId.Parse(dto.Id);

return new UserPreferences
{
Id = id,
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Services/UserPreferencesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Core.Services;

public class UserPreferencesService(
IUserAnalyticsClient userAnalyticsClient,
IMongoRepository<UserPreferences> userPreferencesRepository,
IUserPreferencesRepository userPreferencesRepository,
ILogger<UserPreferencesService> logger) : IUserPreferencesService
{
private readonly IUserAnalyticsClient _userAnalyticsClient = userAnalyticsClient;
Expand Down Expand Up @@ -64,7 +64,7 @@ public async Task<ErrorOr<UserPreferencesDto>> UpdateUserPreferences(

// here user mail should be sent
await _userAnalyticsClient.SendUserAction(
userPreferences.UserId, "UserPreferencesCreated", "recipient", "User's Preferences", ct);
userPreferences.UserId, "UserPreferencesUpdated", "recipient", "User's Preferences", ct);

return UserPreferencesMapper.ToDto(entity);
}
Expand Down

0 comments on commit 74ffef7

Please sign in to comment.