Files
ColaFlow/docs/plans/sprint_2_story_1_task_2.md
Yaojia Wang 2466cd4020 feat(backend): Add AuditLog repository interface and implementation
Implement repository pattern for AuditLog entity for Sprint 2 Story 1 Task 2.

Changes:
- Created IAuditLogRepository interface with 6 query methods
- Implemented AuditLogRepository with efficient querying
- Registered repository in DI container
- All queries use AsNoTracking for read-only operations

Query Methods:
- GetByIdAsync: Get single audit log by ID
- GetByEntityAsync: Get audit history for specific entity
- GetByUserAsync: Get user activity with pagination
- GetRecentAsync: Get recent audit logs
- AddAsync: Add new audit log
- GetCountAsync: Get total audit log count

Performance:
- All queries automatically filtered by TenantId (Global Query Filter)
- Efficient use of composite indexes
- AsNoTracking for read-only operations

Testing:
- All tests passing (192 domain + 113 identity + 8 arch + 32 app + 12 infra = 357 tests)
- No compilation errors
- Zero test failures

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-04 23:14:41 +01:00

3.6 KiB

task_id, story, status, estimated_hours, created_date, start_date, assignee
task_id story status estimated_hours created_date start_date assignee
sprint_2_story_1_task_2 sprint_2_story_1 in_progress 4 2025-11-05 2025-11-05 Backend Team

Task 2: Create AuditLog Entity and Repository

Story: Story 1 - Audit Log Foundation (Phase 1) Estimated: 4 hours

Description

Create the AuditLog domain entity and repository pattern implementation to support CRUD operations and querying audit logs with proper multi-tenant isolation.

Acceptance Criteria

  • AuditLog entity created in Domain layer
  • IAuditLogRepository interface defined
  • AuditLogRepository implementation with EF Core
  • Multi-tenant query filtering applied
  • Unit tests for repository methods

Implementation Details

Files to Create:

  1. Domain Entity: colaflow-api/src/ColaFlow.Domain/Entities/AuditLog.cs
public class AuditLog
{
    public Guid Id { get; set; }
    public Guid TenantId { get; set; }
    public string EntityType { get; set; } = string.Empty;
    public Guid EntityId { get; set; }
    public AuditAction Action { get; set; } // Enum: Create, Update, Delete
    public Guid? UserId { get; set; }
    public DateTime Timestamp { get; set; }
    public string? OldValues { get; set; } // JSONB stored as string
    public string? NewValues { get; set; } // JSONB stored as string
}

public enum AuditAction
{
    Create,
    Update,
    Delete
}
  1. Repository Interface: colaflow-api/src/ColaFlow.Domain/Repositories/IAuditLogRepository.cs
public interface IAuditLogRepository
{
    Task<AuditLog?> GetByIdAsync(Guid id);
    Task<List<AuditLog>> GetByEntityAsync(string entityType, Guid entityId);
    Task AddAsync(AuditLog auditLog);
    Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}
  1. Repository Implementation: colaflow-api/src/ColaFlow.Infrastructure/Repositories/AuditLogRepository.cs
public class AuditLogRepository : IAuditLogRepository
{
    private readonly ColaFlowDbContext _context;
    private readonly ITenantContext _tenantContext;

    public AuditLogRepository(ColaFlowDbContext context, ITenantContext tenantContext)
    {
        _context = context;
        _tenantContext = tenantContext;
    }

    public async Task<List<AuditLog>> GetByEntityAsync(string entityType, Guid entityId)
    {
        var tenantId = _tenantContext.TenantId;
        return await _context.AuditLogs
            .Where(a => a.TenantId == tenantId && a.EntityType == entityType && a.EntityId == entityId)
            .OrderByDescending(a => a.Timestamp)
            .ToListAsync();
    }

    // ... other methods
}
  1. EF Core Configuration: colaflow-api/src/ColaFlow.Infrastructure/Data/Configurations/AuditLogConfiguration.cs
public class AuditLogConfiguration : IEntityTypeConfiguration<AuditLog>
{
    public void Configure(EntityTypeBuilder<AuditLog> builder)
    {
        builder.ToTable("AuditLogs");
        builder.HasKey(a => a.Id);

        builder.Property(a => a.EntityType).IsRequired().HasMaxLength(100);
        builder.Property(a => a.Action).IsRequired();
        builder.Property(a => a.Timestamp).IsRequired();

        // JSONB columns
        builder.Property(a => a.OldValues).HasColumnType("jsonb");
        builder.Property(a => a.NewValues).HasColumnType("jsonb");

        // Multi-tenant global query filter
        builder.HasQueryFilter(a => a.TenantId == Guid.Empty); // Will be replaced by TenantContext
    }
}

Testing

  • Unit tests for repository methods
  • Verify multi-tenant filtering works
  • Test JSONB serialization/deserialization

Created: 2025-11-05 by Backend Agent