Added support for notifications

This commit is contained in:
Khwezi Mngoma
2026-05-05 14:36:46 +02:00
parent f5c9557f59
commit 4b822c63b2
19 changed files with 230 additions and 12 deletions
@@ -0,0 +1,21 @@
namespace LiteCharms.Entities.Configuration;
public class NotificationConfiguration : IEntityTypeConfiguration<Notification>
{
public void Configure(EntityTypeBuilder<Notification> builder)
{
builder.ToTable(nameof(Notification));
builder.HasKey(f => f.Id);
builder.Property(f => f.CreatedAt).IsRequired().ValueGeneratedOnAdd();
builder.Property(f => f.Direction).IsRequired();
builder.Property(f => f.Author).IsRequired();
builder.Property(f => f.Title).IsRequired();
builder.Property(f => f.Description).IsRequired();
builder.Property(f => f.Platform).IsRequired();
builder.Property(f => f.PlatformAddress).IsRequired();
builder.Property(f => f.CorrelationId).IsRequired();
builder.Property(f => f.CorrelationIdType).IsRequired();
builder.Property(f => f.IsInternal).IsRequired();
}
}
+6
View File
@@ -0,0 +1,6 @@
using LiteCharms.Entities.Configuration;
namespace LiteCharms.Entities;
[EntityTypeConfiguration<NotificationConfiguration, Notification>]
public class Notification : Models.Notification;
@@ -4,6 +4,22 @@ namespace LiteCharms.Extensions;
public static class EntityModeMappers
{
public static Notification ToModel(this Entities.Notification entity) =>
new()
{
Id = entity.Id,
CreatedAt = entity.CreatedAt,
Description = entity.Description,
Direction = entity.Direction,
CorrelationId = entity.CorrelationId,
CorrelationIdType = entity.CorrelationIdType,
IsInternal = entity.IsInternal,
Author = entity.Author,
Platform = entity.Platform,
PlatformAddress = entity.PlatformAddress,
Title = entity.Title
};
public static Customer ToModel(this Entities.Customer entity) =>
new()
{
@@ -8,17 +8,23 @@ public class GetCustomersQuery : IRequest<Result<Customer[]>>
public DateOnly To { get; set; }
private GetCustomersQuery(DateOnly from, DateOnly to)
public int MaxRecords { get; set; }
private GetCustomersQuery(DateOnly from, DateOnly to, int maxRecords = 1000)
{
From = from;
To = to;
MaxRecords = maxRecords;
}
public static GetCustomersQuery Create(DateOnly from, DateOnly to)
public static GetCustomersQuery Create(DateOnly from, DateOnly to, int maxRecords = 1000)
{
if (from > to)
throw new ArgumentException("From date cannot be greater than To date.");
return new(from, to);
if(maxRecords <= 0)
throw new ArgumentException("MaxRecords must be a positive integer.");
return new(from, to, maxRecords);
}
}
@@ -18,6 +18,7 @@ public class GetCustomersQueryHandler(IDbContextFactory<LeadGeneratorDbContext>
var customers = await context.Customers.AsNoTracking()
.OrderByDescending(o => o.CreatedAt)
.Where(c => c.CreatedAt >= fromDate && c.CreatedAt <= toDate)
.Take(request.MaxRecords)
.ToArrayAsync(cancellationToken);
return customers?.Length > 0
@@ -8,17 +8,23 @@ public class GetLeadsQuery : IRequest<Result<Lead[]>>
public DateOnly To { get; set; }
private GetLeadsQuery(DateOnly from, DateOnly to)
public int MaxRecords { get; set; }
private GetLeadsQuery(DateOnly from, DateOnly to, int maxRecords = 1000)
{
From = from;
To = to;
MaxRecords = maxRecords;
}
public static GetLeadsQuery Create(DateOnly from, DateOnly to)
public static GetLeadsQuery Create(DateOnly from, DateOnly to, int maxRecords = 1000)
{
if (from > to)
throw new ArgumentException("From date cannot be greater than To date.");
return new(from, to);
if(maxRecords <= 0)
throw new ArgumentException("MaxRecords must be a positive integer.");
return new(from, to, maxRecords);
}
}
@@ -18,6 +18,7 @@ public class GetLeadsQueryHandler(IDbContextFactory<LeadGeneratorDbContext> cont
var leads = await context.Leads.AsNoTracking()
.OrderByDescending(o => o.CreatedAt)
.Where(l => l.CreatedAt.Date >= fromDate && l.CreatedAt.Date <= toDate)
.Take(request.MaxRecords)
.ToArrayAsync(cancellationToken);
return leads?.Length > 0
@@ -34,8 +34,8 @@
<PackageReference Include="MimeKit" Version="4.16.0" />
<!-- Global Usings-->
<Using Include="MimeKit"/>
<Using Include="MailKit.Net.Smtp"/>
<Using Include="MimeKit" />
<Using Include="MailKit.Net.Smtp" />
</ItemGroup>
<!-- CQRS -->
@@ -0,0 +1,30 @@
using LiteCharms.Models;
namespace LiteCharms.Features.Notifications.Queries;
public class GetNotificationsQuery : IRequest<Result<Notification[]>>
{
public DateOnly From { get; set; }
public DateOnly To { get; set; }
public int MaxRecords { get; set; }
private GetNotificationsQuery(DateOnly from, DateOnly to, int maxRecords = 1000)
{
From = from;
To = to;
MaxRecords = maxRecords;
}
public static GetNotificationsQuery Create(DateOnly from, DateOnly to, int maxRecords = 1000)
{
if (from > to)
throw new ArgumentException("From date cannot be greater than To date.");
if(maxRecords <= 0)
throw new ArgumentException("MaxRecords must be a positive integer.", nameof(maxRecords));
return new(from, to, maxRecords);
}
}
@@ -0,0 +1,33 @@
using LiteCharms.Extensions;
using LiteCharms.Infrastructure.Database;
using LiteCharms.Models;
namespace LiteCharms.Features.Notifications.Queries.Handlers;
public class GetNotificationsQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetNotificationsQuery, Result<Notification[]>>
{
public async ValueTask<Result<Notification[]>> Handle(GetNotificationsQuery request, CancellationToken cancellationToken)
{
try
{
var fromDate = request.From.ToDateTime(TimeOnly.MinValue);
var toDate = request.To.ToDateTime(TimeOnly.MaxValue);
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var notifications = await context.Notifications
.Where(n => n.CreatedAt >= fromDate && n.CreatedAt <= toDate)
.OrderByDescending(n => n.CreatedAt)
.Take(request.MaxRecords)
.ToArrayAsync(cancellationToken);
return notifications?.Length > 0
? Result.Ok(notifications.Select(n => n.ToModel()).ToArray())
: Result.Fail(new Error($"No notifications found for the specified date range {request.From} to {request.To}."));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -8,17 +8,23 @@ public class GetOrdersQuery : IRequest<Result<Order[]>>
public DateOnly To { get; set; }
private GetOrdersQuery(DateOnly from, DateOnly to)
public int MaxRecords { get; set; }
private GetOrdersQuery(DateOnly from, DateOnly to, int maxRecords = 1000)
{
From = from;
To = to;
MaxRecords = maxRecords;
}
public static GetOrdersQuery Create(DateOnly from, DateOnly to)
public static GetOrdersQuery Create(DateOnly from, DateOnly to, int maxRecords = 1000)
{
if (from > to)
throw new ArgumentException("From date cannot be greater than To date.");
return new(from, to);
if(maxRecords <= 0)
throw new ArgumentException("MaxRecords must be a positive integer.");
return new(from, to, maxRecords);
}
}
@@ -18,6 +18,7 @@ public class GetOrdersQueryHandler(IDbContextFactory<LeadGeneratorDbContext> con
var orders = await context.Orders
.OrderByDescending(o => o.CreatedAt)
.Where(o => o.CreatedAt >= fromDate && o.CreatedAt <= toDate)
.Take(request.MaxRecords)
.ToArrayAsync(cancellationToken);
return orders?.Length > 0
@@ -0,0 +1,18 @@
using LiteCharms.Models;
namespace LiteCharms.Features.Products.Queries;
public class GetProductPricesQuery : IRequest<Result<ProductPrice[]>>
{
public int MaxRecords { get; set; }
private GetProductPricesQuery(int maxRecords = 1000) => MaxRecords = maxRecords;
public static GetProductPricesQuery Create(int maxRecords = 1000)
{
if (maxRecords <= 0)
throw new ArgumentOutOfRangeException(nameof(maxRecords), "MaxRecords must be greater than zero.");
return new(maxRecords);
}
}
@@ -4,5 +4,15 @@ namespace LiteCharms.Features.Products.Queries;
public class GetProductsQuery : IRequest<Result<Product[]>>
{
public static GetProductsQuery Create() => new();
public int MaxRecords { get; set; }
private GetProductsQuery(int maxRecords = 1000) => MaxRecords = maxRecords;
public static GetProductsQuery Create(int maxRecords = 1000)
{
if (maxRecords <= 0)
throw new ArgumentException("MaxRecords must be a positive integer.");
return new(maxRecords);
}
}
@@ -0,0 +1,27 @@
using LiteCharms.Extensions;
using LiteCharms.Infrastructure.Database;
using LiteCharms.Models;
namespace LiteCharms.Features.Products.Queries.Handlers;
public class GetProductPricesQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetProductPricesQuery, Result<ProductPrice[]>>
{
public async ValueTask<Result<ProductPrice[]>> Handle(GetProductPricesQuery request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var products = await context.ProductPrices.AsNoTracking()
.OrderByDescending(o => o.Id)
.Take(request.MaxRecords)
.ToArrayAsync(cancellationToken);
return Result.Ok(products.Select(p => p.ToModel()).ToArray());
}
catch (Exception ex)
{
return Result.Fail<ProductPrice[]>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -14,6 +14,7 @@ public class GetProductsQueryHandler(IDbContextFactory<LeadGeneratorDbContext> c
var products = await context.Products.AsNoTracking()
.OrderByDescending(o => o.Id)
.Take(request.MaxRecords)
.ToArrayAsync(cancellationToken);
return Result.Ok(products.Select(p => p.ToModel()).ToArray());
@@ -15,4 +15,6 @@ public class LeadGeneratorDbContext(DbContextOptions<LeadGeneratorDbContext> opt
public DbSet<Product> Products { get; set; }
public DbSet<ProductPrice> ProductPrices { get; set; }
public DbSet<Notification> Notifications { get; set; }
}
+7
View File
@@ -19,3 +19,10 @@ public enum LeadStatus : int
Converted = 4,
Lost = 5
}
public enum NotificationDirection : int
{
Incoming = 0,
Outgoing = 1,
Neutral = 2
}
+26
View File
@@ -0,0 +1,26 @@
namespace LiteCharms.Models;
public class Notification
{
public Guid Id { get; set; }
public DateTimeOffset CreatedAt { get; set; }
public NotificationDirection Direction { get; set; }
public string? Author { get; set; }
public string? Title { get; set; }
public string? Description { get; set; }
public string? Platform { get; set; }
public string? PlatformAddress { get; set; }
public string? CorrelationId { get; set; }
public string? CorrelationIdType { get; set; }
public bool IsInternal { get; set; }
}