Skip to content

Commit 18d44ce

Browse files
iammukeshmjarvis
andauthored
feat: Add domain events to Identity module (#1167)
Add domain events for identity-related operations: - UserRegisteredEvent: Raised when a new user registers - PasswordChangedEvent: Raised when a user changes password - UserRoleAssignedEvent: Raised when roles are assigned to a user - UserActivatedEvent: Raised when a user account is activated - UserDeactivatedEvent: Raised when a user account is deactivated - SessionRevokedEvent: Raised when a user session is revoked All events inherit from DomainEvent base record and include: - EventId, OccurredOnUtc, CorrelationId, TenantId (from base) - Relevant domain-specific data - Static factory method for convenient creation Co-authored-by: jarvis <jarvis@codewithmukesh.com>
1 parent a5846b4 commit 18d44ce

6 files changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using FSH.Framework.Core.Domain;
2+
3+
namespace FSH.Modules.Identity.Domain.Events;
4+
5+
/// <summary>Raised when a user changes their password.</summary>
6+
public sealed record PasswordChangedEvent(
7+
Guid EventId,
8+
DateTimeOffset OccurredOnUtc,
9+
string UserId,
10+
bool WasReset,
11+
string? CorrelationId = null,
12+
string? TenantId = null
13+
) : DomainEvent(EventId, OccurredOnUtc, CorrelationId, TenantId)
14+
{
15+
public static PasswordChangedEvent Create(string userId, bool wasReset = false, string? correlationId = null, string? tenantId = null)
16+
=> new(Guid.NewGuid(), DateTimeOffset.UtcNow, userId, wasReset, correlationId, tenantId);
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using FSH.Framework.Core.Domain;
2+
3+
namespace FSH.Modules.Identity.Domain.Events;
4+
5+
/// <summary>Raised when a user session is revoked.</summary>
6+
public sealed record SessionRevokedEvent(
7+
Guid EventId,
8+
DateTimeOffset OccurredOnUtc,
9+
string UserId,
10+
Guid SessionId,
11+
string? RevokedBy,
12+
string? Reason,
13+
string? CorrelationId = null,
14+
string? TenantId = null
15+
) : DomainEvent(EventId, OccurredOnUtc, CorrelationId, TenantId)
16+
{
17+
public static SessionRevokedEvent Create(string userId, Guid sessionId, string? revokedBy = null, string? reason = null, string? correlationId = null, string? tenantId = null)
18+
=> new(Guid.NewGuid(), DateTimeOffset.UtcNow, userId, sessionId, revokedBy, reason, correlationId, tenantId);
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using FSH.Framework.Core.Domain;
2+
3+
namespace FSH.Modules.Identity.Domain.Events;
4+
5+
/// <summary>Raised when a user account is activated.</summary>
6+
public sealed record UserActivatedEvent(
7+
Guid EventId,
8+
DateTimeOffset OccurredOnUtc,
9+
string UserId,
10+
string? ActivatedBy,
11+
string? CorrelationId = null,
12+
string? TenantId = null
13+
) : DomainEvent(EventId, OccurredOnUtc, CorrelationId, TenantId)
14+
{
15+
public static UserActivatedEvent Create(string userId, string? activatedBy = null, string? correlationId = null, string? tenantId = null)
16+
=> new(Guid.NewGuid(), DateTimeOffset.UtcNow, userId, activatedBy, correlationId, tenantId);
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using FSH.Framework.Core.Domain;
2+
3+
namespace FSH.Modules.Identity.Domain.Events;
4+
5+
/// <summary>Raised when a user account is deactivated.</summary>
6+
public sealed record UserDeactivatedEvent(
7+
Guid EventId,
8+
DateTimeOffset OccurredOnUtc,
9+
string UserId,
10+
string? DeactivatedBy,
11+
string? Reason,
12+
string? CorrelationId = null,
13+
string? TenantId = null
14+
) : DomainEvent(EventId, OccurredOnUtc, CorrelationId, TenantId)
15+
{
16+
public static UserDeactivatedEvent Create(string userId, string? deactivatedBy = null, string? reason = null, string? correlationId = null, string? tenantId = null)
17+
=> new(Guid.NewGuid(), DateTimeOffset.UtcNow, userId, deactivatedBy, reason, correlationId, tenantId);
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using FSH.Framework.Core.Domain;
2+
3+
namespace FSH.Modules.Identity.Domain.Events;
4+
5+
/// <summary>Raised when a new user registers in the system.</summary>
6+
public sealed record UserRegisteredEvent(
7+
Guid EventId,
8+
DateTimeOffset OccurredOnUtc,
9+
string UserId,
10+
string Email,
11+
string? FirstName,
12+
string? LastName,
13+
string? CorrelationId = null,
14+
string? TenantId = null
15+
) : DomainEvent(EventId, OccurredOnUtc, CorrelationId, TenantId)
16+
{
17+
public static UserRegisteredEvent Create(string userId, string email, string? firstName = null, string? lastName = null, string? correlationId = null, string? tenantId = null)
18+
=> new(Guid.NewGuid(), DateTimeOffset.UtcNow, userId, email, firstName, lastName, correlationId, tenantId);
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using FSH.Framework.Core.Domain;
2+
3+
namespace FSH.Modules.Identity.Domain.Events;
4+
5+
/// <summary>Raised when roles are assigned to a user.</summary>
6+
public sealed record UserRoleAssignedEvent(
7+
Guid EventId,
8+
DateTimeOffset OccurredOnUtc,
9+
string UserId,
10+
IReadOnlyList<string> AssignedRoles,
11+
string? CorrelationId = null,
12+
string? TenantId = null
13+
) : DomainEvent(EventId, OccurredOnUtc, CorrelationId, TenantId)
14+
{
15+
public static UserRoleAssignedEvent Create(string userId, IEnumerable<string> assignedRoles, string? correlationId = null, string? tenantId = null)
16+
=> new(Guid.NewGuid(), DateTimeOffset.UtcNow, userId, assignedRoles.ToList().AsReadOnly(), correlationId, tenantId);
17+
}

0 commit comments

Comments
 (0)