Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link to parent event in Browser #614

Merged
merged 3 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ruby_event_store-browser/elm/src/Api.elm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type alias Event =
, rawMetadata : String
, correlationStreamName : Maybe String
, causationStreamName : Maybe String
, parentEventId : Maybe String
}


Expand Down Expand Up @@ -61,6 +62,7 @@ eventDecoder_ =
|> requiredAt [ "attributes", "metadata" ] (value |> Json.Decode.map (encode 2))
|> optionalAt [ "attributes", "correlation_stream_name" ] (maybe string) Nothing
|> optionalAt [ "attributes", "causation_stream_name" ] (maybe string) Nothing
|> optionalAt [ "attributes", "parent_event_id" ] (maybe string) Nothing


getEvents : (Result Http.Error (PaginatedList Event) -> msg) -> String -> Cmd msg
Expand Down
24 changes: 22 additions & 2 deletions ruby_event_store-browser/elm/src/Page/ShowEvent.elm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type alias Event =
, eventId : String
, correlationStreamName : Maybe String
, causationStreamName : Maybe String
, parentEventId : Maybe String
, rawData : String
, rawMetadata : String
, dataTreeState : JsonTree.State
Expand Down Expand Up @@ -104,6 +105,7 @@ apiEventToEvent e =
, rawMetadata = e.rawMetadata
, correlationStreamName = e.correlationStreamName
, causationStreamName = e.causationStreamName
, parentEventId = e.parentEventId
, dataTreeState = JsonTree.defaultState
, metadataTreeState = JsonTree.defaultState
}
Expand Down Expand Up @@ -197,15 +199,16 @@ relatedStreams event =

else
div [ class "event__related-streams" ]
[ h2 [] [ text "Related streams:" ]
[ h2 [] [ text "Related streams / events:" ]
, ul [] (relatedStreamsList event)
]


relatedStreamsList : Event -> List (Html Msg)
relatedStreamsList event =
values
[ correlationStreamLink event
[ parentEventLink event
, correlationStreamLink event
, causationStreamLink event
]

Expand Down Expand Up @@ -234,11 +237,28 @@ causationStreamLink event =
event.causationStreamName


parentEventLink : Event -> Maybe (Html Msg)
parentEventLink event =
Maybe.map
(\parentEventId ->
li []
[ text "Parent event: "
, eventLink parentEventId
]
)
event.parentEventId


streamLink : String -> Html Msg
streamLink streamName =
a [ class "event__stream-link", href ("/#streams/" ++ streamName) ] [ text streamName ]


eventLink : String -> Html Msg
eventLink eventId =
a [ class "event__event-link", href ("/#events/" ++ eventId) ] [ text eventId ]


renderCausedEvents : List Api.Event -> Html Msg
renderCausedEvents causedEvents =
case causedEvents of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ $browser-layout: (columns: 2);
}
}

.event__stream-link {
.event__stream-link, .event__event-link {
text-decoration: none;
}
8 changes: 6 additions & 2 deletions ruby_event_store-browser/elm/tests/DecodersTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ suite =
},
"metadata": {
"timestamp": "2017-12-20T23:49:45.273Z"
}
},
"parent_event_id": null
}
}
]
Expand All @@ -56,6 +57,7 @@ suite =
, rawMetadata = "{\n \"timestamp\": \"2017-12-20T23:49:45.273Z\"\n}"
, correlationStreamName = Nothing
, causationStreamName = Nothing
, parentEventId = Nothing
}
]
, links =
Expand Down Expand Up @@ -91,7 +93,8 @@ suite =
"timestamp": "2017-12-20T23:49:45.273Z"
},
"correlation_stream_name": "$by_correlation_id_a7243789-999f-4ef2-8511-b1c686b83fad",
"causation_stream_name": "$by_causation_id_664ada1e-2f01-4ed0-9c16-63dbc82269d2"
"causation_stream_name": "$by_causation_id_664ada1e-2f01-4ed0-9c16-63dbc82269d2",
"parent_event_id": "cb12f84b-b9e4-439b-8442-50fae6244dc9"
}
}
}
Expand All @@ -109,6 +112,7 @@ suite =
, rawMetadata = "{\n \"timestamp\": \"2017-12-20T23:49:45.273Z\"\n}"
, correlationStreamName = Just "$by_correlation_id_a7243789-999f-4ef2-8511-b1c686b83fad"
, causationStreamName = Just "$by_causation_id_664ada1e-2f01-4ed0-9c16-63dbc82269d2"
, parentEventId = Just "cb12f84b-b9e4-439b-8442-50fae6244dc9"
}
)
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ def initialize(event_store:, params:)

def as_json
{
data: JsonApiEvent.new(event).to_h,
data: JsonApiEvent.new(event, parent_event_id).to_h,
}
end

def event
@event ||= event_store.read.event!(event_id)
end

def parent_event_id
if event.metadata.has_key?(:causation_id)
event_store.read.event(event.metadata.fetch(:causation_id))&.event_id
end
end

def event_id
params.fetch(:id)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module RubyEventStore
module Browser
class JsonApiEvent
def initialize(event)
def initialize(event, parent_event_id)
@event = event
@parent_event_id = parent_event_id
end

def to_h
Expand All @@ -15,12 +16,13 @@ def to_h
metadata: metadata,
correlation_stream_name: correlation_stream_name,
causation_stream_name: causation_stream_name,
parent_event_id: parent_event_id,
},
}
end

private
attr_reader :event
attr_reader :event, :parent_event_id

def metadata
event.metadata.to_h.tap do |m|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(event_store:, params:, url_builder:)

def as_json
{
data: events.map { |e| JsonApiEvent.new(e).to_h },
data: events.map { |e| JsonApiEvent.new(e, nil).to_h },
links: links
}
end
Expand Down
41 changes: 40 additions & 1 deletion ruby_event_store-browser/spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,43 @@ module RubyEventStore
)
end

specify "requesting event with parent event id" do
parent_event = DummyEvent.new(
event_id: "44427ded-e8a7-4ee4-bf31-09f34433d506",
data: {},
)
event_store.publish(parent_event, stream_name: "dummy")
caused_event = DummyEvent.new(
event_id: "a562dc5c-97c0-4fe9-8b81-10f9bd0e825f",
data: {},
metadata: {
causation_id: parent_event.event_id,
}
)
event_store.publish(caused_event, stream_name: "dummy")
test_client.get "/events/#{caused_event.event_id}"

expect(test_client.last_response).to be_ok
expect(test_client.parsed_body["data"]["attributes"]["parent_event_id"]).to eq(
"44427ded-e8a7-4ee4-bf31-09f34433d506",
)
end

specify "requesting event which is caused by something other than event" do
caused_event = DummyEvent.new(
event_id: "a562dc5c-97c0-4fe9-8b81-10f9bd0e825f",
data: {},
metadata: {
causation_id: "44427ded-e8a7-4ee4-bf31-09f34433d506",
}
)
event_store.publish(caused_event, stream_name: "dummy")
test_client.get "/events/#{caused_event.event_id}"

expect(test_client.last_response).to be_ok
expect(test_client.parsed_body["data"]["attributes"]["parent_event_id"]).to be_nil
end

specify "requesting non-existing event" do
test_client.get "/events/73947fbd-90d7-4e1c-be2a-d7ff1900c409"

Expand All @@ -63,7 +100,7 @@ module RubyEventStore
end

specify do
json = Browser::JsonApiEvent.new(dummy_event("a562dc5c-97c0-4fe9-8b81-10f9bd0e825f")).to_h
json = Browser::JsonApiEvent.new(dummy_event("a562dc5c-97c0-4fe9-8b81-10f9bd0e825f"), nil).to_h

expect(json).to match(
id: "a562dc5c-97c0-4fe9-8b81-10f9bd0e825f",
Expand All @@ -78,6 +115,7 @@ module RubyEventStore
metadata: {},
correlation_stream_name: nil,
causation_stream_name: "$by_causation_id_a562dc5c-97c0-4fe9-8b81-10f9bd0e825f",
parent_event_id: nil,
},
)
end
Expand Down Expand Up @@ -264,6 +302,7 @@ def event_resource
},
"correlation_stream_name" => nil,
"causation_stream_name" => "$by_causation_id_#{dummy_event.event_id}",
"parent_event_id" => nil,
},
}
end
Expand Down