feat: pivot to social media workflow app
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
namespace Socialize.Modules.Notifications.Contracts;
|
||||
|
||||
public record NotificationEventWriteModel(
|
||||
Guid WorkspaceId,
|
||||
Guid? ContentItemId,
|
||||
string EventType,
|
||||
string EntityType,
|
||||
Guid EntityId,
|
||||
string Message,
|
||||
Guid? RecipientUserId,
|
||||
string? RecipientEmail,
|
||||
string? MetadataJson);
|
||||
|
||||
public interface INotificationEventWriter
|
||||
{
|
||||
Task WriteAsync(NotificationEventWriteModel model, CancellationToken cancellationToken = default);
|
||||
}
|
||||
17
backend/Modules/Notifications/Data/NotificationEvent.cs
Normal file
17
backend/Modules/Notifications/Data/NotificationEvent.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Socialize.Modules.Notifications.Data;
|
||||
|
||||
public class NotificationEvent
|
||||
{
|
||||
public Guid Id { get; init; }
|
||||
public Guid WorkspaceId { get; set; }
|
||||
public Guid? ContentItemId { get; set; }
|
||||
public required string EventType { get; set; }
|
||||
public required string EntityType { get; set; }
|
||||
public Guid EntityId { get; set; }
|
||||
public required string Message { get; set; }
|
||||
public Guid? RecipientUserId { get; set; }
|
||||
public string? RecipientEmail { get; set; }
|
||||
public string? MetadataJson { get; set; }
|
||||
public DateTimeOffset CreatedAt { get; init; }
|
||||
public DateTimeOffset? ReadAt { get; set; }
|
||||
}
|
||||
16
backend/Modules/Notifications/DependencyInjection.cs
Normal file
16
backend/Modules/Notifications/DependencyInjection.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Socialize.Modules.Notifications.Contracts;
|
||||
using Socialize.Modules.Notifications.Data;
|
||||
using Socialize.Modules.Notifications.Services;
|
||||
|
||||
namespace Socialize.Modules.Notifications;
|
||||
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static WebApplicationBuilder AddNotificationsModule(
|
||||
this WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.AddScoped<INotificationEventWriter, NotificationEventWriter>();
|
||||
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
88
backend/Modules/Notifications/Handlers/GetNotifications.cs
Normal file
88
backend/Modules/Notifications/Handlers/GetNotifications.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using Socialize.Infrastructure.Security;
|
||||
namespace Socialize.Modules.Notifications.Handlers;
|
||||
|
||||
public record GetNotificationsRequest(Guid? WorkspaceId, Guid? ContentItemId);
|
||||
|
||||
public record NotificationEventDto(
|
||||
Guid Id,
|
||||
Guid WorkspaceId,
|
||||
Guid? ContentItemId,
|
||||
string EventType,
|
||||
string EntityType,
|
||||
Guid EntityId,
|
||||
string Message,
|
||||
Guid? RecipientUserId,
|
||||
string? RecipientEmail,
|
||||
string? MetadataJson,
|
||||
DateTimeOffset CreatedAt,
|
||||
DateTimeOffset? ReadAt);
|
||||
|
||||
public class GetNotificationsHandler(
|
||||
AppDbContext dbContext,
|
||||
AccessScopeService accessScopeService)
|
||||
: Endpoint<GetNotificationsRequest, IReadOnlyCollection<NotificationEventDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/notifications");
|
||||
Options(o => o.WithTags("Notifications"));
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetNotificationsRequest request, CancellationToken ct)
|
||||
{
|
||||
if (request.ContentItemId.HasValue)
|
||||
{
|
||||
ContentItem? item = await dbContext.ContentItems
|
||||
.SingleOrDefaultAsync(candidate => candidate.Id == request.ContentItemId.Value, ct);
|
||||
if (item is null)
|
||||
{
|
||||
await SendNotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!accessScopeService.CanReviewContent(User, item.WorkspaceId, item.ClientId, item.ProjectId))
|
||||
{
|
||||
await SendForbiddenAsync(ct);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
IQueryable<NotificationEvent> query = dbContext.NotificationEvents.AsQueryable();
|
||||
|
||||
if (!accessScopeService.IsManager(User))
|
||||
{
|
||||
IReadOnlyCollection<Guid> workspaceScopeIds = User.GetWorkspaceScopeIds();
|
||||
query = query.Where(notificationEvent => workspaceScopeIds.Contains(notificationEvent.WorkspaceId));
|
||||
}
|
||||
|
||||
if (request.WorkspaceId.HasValue)
|
||||
{
|
||||
query = query.Where(notificationEvent => notificationEvent.WorkspaceId == request.WorkspaceId.Value);
|
||||
}
|
||||
|
||||
if (request.ContentItemId.HasValue)
|
||||
{
|
||||
query = query.Where(notificationEvent => notificationEvent.ContentItemId == request.ContentItemId.Value);
|
||||
}
|
||||
|
||||
List<NotificationEventDto> notifications = await query
|
||||
.OrderByDescending(notificationEvent => notificationEvent.CreatedAt)
|
||||
.Take(100)
|
||||
.Select(notificationEvent => new NotificationEventDto(
|
||||
notificationEvent.Id,
|
||||
notificationEvent.WorkspaceId,
|
||||
notificationEvent.ContentItemId,
|
||||
notificationEvent.EventType,
|
||||
notificationEvent.EntityType,
|
||||
notificationEvent.EntityId,
|
||||
notificationEvent.Message,
|
||||
notificationEvent.RecipientUserId,
|
||||
notificationEvent.RecipientEmail,
|
||||
notificationEvent.MetadataJson,
|
||||
notificationEvent.CreatedAt,
|
||||
notificationEvent.ReadAt))
|
||||
.ToListAsync(ct);
|
||||
|
||||
await SendOkAsync(notifications, ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Socialize.Infrastructure.Security;
|
||||
using Socialize.Modules.Notifications.Data;
|
||||
|
||||
namespace Socialize.Modules.Notifications.Handlers;
|
||||
|
||||
public class MarkNotificationAsReadHandler(
|
||||
AppDbContext dbContext,
|
||||
AccessScopeService accessScopeService)
|
||||
: EndpointWithoutRequest
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/api/notifications/{id}/read");
|
||||
Options(o => o.WithTags("Notifications"));
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
Guid id = Route<Guid>("id");
|
||||
|
||||
NotificationEvent? notificationEvent = await dbContext.NotificationEvents.SingleOrDefaultAsync(candidate => candidate.Id == id, ct);
|
||||
if (notificationEvent is null)
|
||||
{
|
||||
await SendNotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!accessScopeService.CanAccessWorkspace(User, notificationEvent.WorkspaceId))
|
||||
{
|
||||
await SendForbiddenAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
notificationEvent.ReadAt = notificationEvent.ReadAt ?? DateTimeOffset.UtcNow;
|
||||
await dbContext.SaveChangesAsync(ct);
|
||||
|
||||
await SendNoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Socialize.Modules.Notifications.Contracts;
|
||||
using Socialize.Modules.Notifications.Data;
|
||||
|
||||
namespace Socialize.Modules.Notifications.Services;
|
||||
|
||||
public class NotificationEventWriter(
|
||||
AppDbContext dbContext)
|
||||
: INotificationEventWriter
|
||||
{
|
||||
public async Task WriteAsync(NotificationEventWriteModel model, CancellationToken cancellationToken = default)
|
||||
{
|
||||
NotificationEvent notificationEvent = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
WorkspaceId = model.WorkspaceId,
|
||||
ContentItemId = model.ContentItemId,
|
||||
EventType = model.EventType,
|
||||
EntityType = model.EntityType,
|
||||
EntityId = model.EntityId,
|
||||
Message = model.Message,
|
||||
RecipientUserId = model.RecipientUserId,
|
||||
RecipientEmail = model.RecipientEmail,
|
||||
MetadataJson = model.MetadataJson,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
|
||||
dbContext.NotificationEvents.Add(notificationEvent);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user