Skip to content

Commit 700108e

Browse files
committed
feat: return matched destinations in publish api
1 parent c9a9aa5 commit 700108e

File tree

7 files changed

+37
-12
lines changed

7 files changed

+37
-12
lines changed

cmd/e2e/delivery_pipeline_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ func (s *basicSuite) TestDeliveryPipeline_PublishDeliversToWebhook() {
1111
tenant := s.createTenant()
1212
dest := s.createWebhookDestination(tenant.ID, "*", withSecret(testSecret))
1313

14-
s.publish(tenant.ID, "user.created", map[string]any{
14+
resp := s.publish(tenant.ID, "user.created", map[string]any{
1515
"event_id": "delivery_test_1",
1616
})
17+
s.Equal([]string{dest.ID}, resp.DestinationIDs)
1718

1819
// Verify mock server received the event
1920
events := s.waitForNewMockServerEvents(dest.mockID, 1)

cmd/e2e/helpers_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ type destinationResponse struct {
5757
}
5858

5959
type publishResponse struct {
60-
ID string `json:"id"`
61-
Duplicate bool `json:"duplicate"`
60+
ID string `json:"id"`
61+
Duplicate bool `json:"duplicate"`
62+
DestinationIDs []string `json:"destination_ids"`
6263
}
6364

6465
type mockServerEvent struct {

docs/apis/openapi.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,7 @@ components:
16981698
required:
16991699
- id
17001700
- duplicate
1701+
- destination_ids
17011702
properties:
17021703
id:
17031704
type: string
@@ -1707,6 +1708,12 @@ components:
17071708
type: boolean
17081709
description: Whether this event was already processed (idempotency hit). If true, the event was not queued again.
17091710
example: false
1711+
destination_ids:
1712+
type: array
1713+
items:
1714+
type: string
1715+
description: The IDs of destinations that matched this event. Empty array if no destinations matched.
1716+
example: ["des_456", "des_789"]
17101717
Event:
17111718
type: object
17121719
properties:

internal/apirouter/publish_handlers_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ func TestAPI_Publish(t *testing.T) {
147147
})
148148

149149
t.Run("Success", func(t *testing.T) {
150-
t.Run("returns event ID", func(t *testing.T) {
150+
t.Run("returns event ID and destination_ids", func(t *testing.T) {
151151
h := newAPITest(t)
152-
h.eventHandler.result = &publishmq.HandleResult{EventID: "evt-123"}
152+
h.eventHandler.result = &publishmq.HandleResult{EventID: "evt-123", DestinationIDs: []string{"d1"}}
153153

154154
req := h.jsonReq(http.MethodPost, "/api/v1/publish", map[string]any{
155155
"tenant_id": "t1",
@@ -162,11 +162,12 @@ func TestAPI_Publish(t *testing.T) {
162162
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &result))
163163
assert.Equal(t, "evt-123", result.EventID)
164164
assert.False(t, result.Duplicate)
165+
assert.Equal(t, []string{"d1"}, result.DestinationIDs)
165166
})
166167

167-
t.Run("returns duplicate flag", func(t *testing.T) {
168+
t.Run("returns duplicate flag with empty destination_ids", func(t *testing.T) {
168169
h := newAPITest(t)
169-
h.eventHandler.result = &publishmq.HandleResult{EventID: "evt-123", Duplicate: true}
170+
h.eventHandler.result = &publishmq.HandleResult{EventID: "evt-123", Duplicate: true, DestinationIDs: []string{}}
170171

171172
req := h.jsonReq(http.MethodPost, "/api/v1/publish", map[string]any{
172173
"tenant_id": "t1",
@@ -178,6 +179,7 @@ func TestAPI_Publish(t *testing.T) {
178179
var result publishmq.HandleResult
179180
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &result))
180181
assert.True(t, result.Duplicate)
182+
assert.Empty(t, result.DestinationIDs)
181183
})
182184
})
183185

internal/apirouter/router_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (m *mockEventHandler) Handle(_ context.Context, event *models.Event) (*publ
195195
if m.result != nil {
196196
return m.result, nil
197197
}
198-
return &publishmq.HandleResult{EventID: event.ID}, nil
198+
return &publishmq.HandleResult{EventID: event.ID, DestinationIDs: []string{}}, nil
199199
}
200200

201201
// stubRegistry is a minimal destregistry.Registry for test setup.

internal/publishmq/eventhandler.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ type EventHandler interface {
2626
}
2727

2828
type HandleResult struct {
29-
EventID string `json:"id"`
30-
Duplicate bool `json:"duplicate"`
29+
EventID string `json:"id"`
30+
Duplicate bool `json:"duplicate"`
31+
DestinationIDs []string `json:"destination_ids"`
3132
}
3233

3334
type eventHandler struct {
@@ -101,9 +102,14 @@ func (h *eventHandler) Handle(ctx context.Context, event *models.Event) (*Handle
101102
}
102103
}
103104

105+
if matchedDestinations == nil {
106+
matchedDestinations = []string{}
107+
}
108+
104109
result := &HandleResult{
105-
EventID: event.ID,
106-
Duplicate: false,
110+
EventID: event.ID,
111+
Duplicate: false,
112+
DestinationIDs: matchedDestinations,
107113
}
108114

109115
// Early return if no destinations matched

internal/publishmq/eventhandler_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ func TestEventHandler_HandleResult(t *testing.T) {
259259
require.NotNil(t, result)
260260
require.Equal(t, event.ID, result.EventID)
261261
require.False(t, result.Duplicate)
262+
require.Len(t, result.DestinationIDs, 3)
262263
})
263264

264265
t.Run("no destinations matched", func(t *testing.T) {
@@ -272,6 +273,7 @@ func TestEventHandler_HandleResult(t *testing.T) {
272273
require.NotNil(t, result)
273274
require.Equal(t, event.ID, result.EventID)
274275
require.False(t, result.Duplicate)
276+
require.Empty(t, result.DestinationIDs)
275277
})
276278

277279
t.Run("duplicate event - idempotency", func(t *testing.T) {
@@ -291,11 +293,13 @@ func TestEventHandler_HandleResult(t *testing.T) {
291293
result1, err := eventHandler.Handle(ctx, event)
292294
require.NoError(t, err)
293295
require.False(t, result1.Duplicate)
296+
require.Len(t, result1.DestinationIDs, 1)
294297

295298
// Duplicate request
296299
result2, err := eventHandler.Handle(ctx, event)
297300
require.NoError(t, err)
298301
require.True(t, result2.Duplicate) // Duplicate due to idempotency
302+
require.Len(t, result2.DestinationIDs, 1)
299303
})
300304

301305
t.Run("with destination_id - queued", func(t *testing.T) {
@@ -314,6 +318,7 @@ func TestEventHandler_HandleResult(t *testing.T) {
314318
result, err := eventHandler.Handle(ctx, event)
315319
require.NoError(t, err)
316320
require.False(t, result.Duplicate)
321+
require.Equal(t, []string{dest.ID}, result.DestinationIDs)
317322
})
318323

319324
t.Run("with destination_id - duplicate event", func(t *testing.T) {
@@ -358,6 +363,7 @@ func TestEventHandler_HandleResult(t *testing.T) {
358363
result, err := eventHandler.Handle(ctx, event)
359364
require.NoError(t, err)
360365
require.False(t, result.Duplicate)
366+
require.Empty(t, result.DestinationIDs)
361367
})
362368

363369
t.Run("with destination_id - not found", func(t *testing.T) {
@@ -370,6 +376,7 @@ func TestEventHandler_HandleResult(t *testing.T) {
370376
result, err := eventHandler.Handle(ctx, event)
371377
require.NoError(t, err)
372378
require.False(t, result.Duplicate)
379+
require.Empty(t, result.DestinationIDs)
373380
})
374381

375382
t.Run("with destination_id - topic mismatch", func(t *testing.T) {
@@ -388,6 +395,7 @@ func TestEventHandler_HandleResult(t *testing.T) {
388395
result, err := eventHandler.Handle(ctx, event)
389396
require.NoError(t, err)
390397
require.False(t, result.Duplicate)
398+
require.Empty(t, result.DestinationIDs)
391399
})
392400
}
393401

0 commit comments

Comments
 (0)