Class: Insika::Commands::RestoreSystemFile

Inherits:
Object
  • Object
show all
Defined in:
lib/insika/commands/restore_system_file.rb

Overview

Control command: restores an old version of a global system file as the current content (a new write — linear history). -> { file, updated_at }.

Instance Method Summary collapse

Constructor Details

#initialize(system_file_store:, event_stream:) ⇒ RestoreSystemFile

Returns a new instance of RestoreSystemFile.



11
12
13
14
# File 'lib/insika/commands/restore_system_file.rb', line 11

def initialize(system_file_store:, event_stream:)
  @system_files = system_file_store
  @event_stream = event_stream
end

Instance Method Details

#call(command) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/insika/commands/restore_system_file.rb', line 16

def call(command)
  p = AgentPayload.symbolize(command.payload)
  file = AgentPayload.presence(p[:file])
  raise Insika::ValidationError, "file is required" if file.nil?
  raise Insika::ValidationError, "version is required" if p[:version].nil?

  entry = @system_files.restore(file, p[:version])
  @event_stream.emit(Insika::Event.new(
                       type: :system_file_restored, data: { file: file, version: p[:version].to_s },
                       meta: { at: Time.now.utc.iso8601 }
                     ))
  { file: file, updated_at: entry["updated_at"] }
end