Class: Lyra::AuditProjection

Inherits:
Projection show all
Defined in:
lib/lyra/projection.rb

Overview

Audit trail projection

Class Method Summary collapse

Methods inherited from Projection

handle, #handle, subscribe_to

Class Method Details

.audit_trail(model_class, model_id) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lyra/projection.rb', line 75

def self.audit_trail(model_class, model_id)
  stream_name = "#{model_class.name}$#{model_id}"
  events = Lyra.config.event_store.read.stream(stream_name).to_a

  events.map do |event|
    # Access data with both symbol and string keys (JSON serializer uses strings)
    data = event.data
     = data[:metadata] || data["metadata"] || {}

    {
      operation: data[:operation] || data["operation"],
      timestamp: data[:timestamp] || data["timestamp"] || event.[:timestamp],
      user_id: [:user_id] || ["user_id"],
      changes: data[:changes] || data["changes"] || {},
      attributes: data[:attributes] || data["attributes"] || {}
    }
  end
end