Skip to content

Commit

Permalink
feat: add with_payload parameter
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo committed Apr 4, 2024
1 parent d4f5683 commit 1717398
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 31 deletions.
10 changes: 10 additions & 0 deletions api/openapi/event-logs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ paths:
- $ref: "#/components/parameters/limit"
- $ref: "#/components/parameters/offset"
- $ref: "#/components/parameters/operation"
- $ref: "#/components/parameters/with_payload"
- $ref: "#/components/parameters/from"
- $ref: "#/components/parameters/to"
security:
Expand Down Expand Up @@ -223,6 +224,15 @@ components:
type: string
required: false
example: users.create

with_payload:
name: with_payload
description: Include event payload.
in: query
schema:
type: boolean
required: false
example: true

from:
name: from
Expand Down
15 changes: 15 additions & 0 deletions eventlogs/api/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -160,6 +161,20 @@ func TestListEventsEndpoint(t *testing.T) {
status: http.StatusBadRequest,
svcErr: nil,
},
{
desc: "with payload",
token: validToken,
url: validID + "/" + auth.UserType + fmt.Sprintf("?payload=%s", strconv.FormatBool(true)),
status: http.StatusOK,
svcErr: nil,
},
{
desc: "with invalid payload",
token: validToken,
url: validID + "/" + auth.UserType + "?payload=ten",
status: http.StatusBadRequest,
svcErr: nil,
},
{
desc: "with empty id",
token: validToken,
Expand Down
20 changes: 13 additions & 7 deletions eventlogs/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

const (
operationKey = "operation"
payloadKey = "with_payload"
fromKey = "from"
toKey = "to"
)
Expand Down Expand Up @@ -74,17 +75,22 @@ func decodeListEventsReq(_ context.Context, r *http.Request) (interface{}, error
if to == 0 {
to = time.Now().UnixNano()
}
payload, err := apiutil.ReadBoolQuery(r, payloadKey, false)
if err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

req := listEventsReq{
token: apiutil.ExtractBearerToken(r),
page: eventlogs.Page{
Offset: offset,
Limit: limit,
ID: chi.URLParam(r, "id"),
EntityType: chi.URLParam(r, "type"),
Operation: operation,
From: time.Unix(0, from),
To: time.Unix(0, to),
Offset: offset,
Limit: limit,
ID: chi.URLParam(r, "id"),
EntityType: chi.URLParam(r, "type"),
Operation: operation,
From: time.Unix(0, from),
To: time.Unix(0, to),
WithPayload: payload,
},
}

Expand Down
15 changes: 8 additions & 7 deletions eventlogs/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ type EventsPage struct {

// Page is used to filter events.
type Page struct {
Offset uint64 `json:"offset" db:"offset"`
Limit uint64 `json:"limit" db:"limit"`
ID string `json:"id,omitempty" db:"id,omitempty"`
EntityType string `json:"entity_type,omitempty"`
Operation string `json:"operation,omitempty" db:"operation,omitempty"`
From time.Time `json:"from,omitempty" db:"from,omitempty"`
To time.Time `json:"to,omitempty" db:"to,omitempty"`
Offset uint64 `json:"offset" db:"offset"`
Limit uint64 `json:"limit" db:"limit"`
ID string `json:"id,omitempty" db:"id,omitempty"`
EntityType string `json:"entity_type,omitempty"`
Operation string `json:"operation,omitempty" db:"operation,omitempty"`
From time.Time `json:"from,omitempty" db:"from,omitempty"`
To time.Time `json:"to,omitempty" db:"to,omitempty"`
WithPayload bool `json:"with_payload,omitempty"`
}

func (page EventsPage) MarshalJSON() ([]byte, error) {
Expand Down
6 changes: 5 additions & 1 deletion eventlogs/postgres/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ func (repo *repository) Save(ctx context.Context, event eventlogs.Event) (err er
func (repo *repository) RetrieveAll(ctx context.Context, page eventlogs.Page) (eventlogs.EventsPage, error) {
query := pageQuery(page)

q := fmt.Sprintf("SELECT id, operation, occurred_at, payload FROM events %s ORDER BY occurred_at LIMIT :limit OFFSET :offset;", query)
sq := "id, operation, occurred_at"
if page.WithPayload {
sq += ", payload"
}
q := fmt.Sprintf("SELECT %s FROM events %s ORDER BY occurred_at LIMIT :limit OFFSET :offset;", sq, query)

rows, err := repo.db.NamedQueryContext(ctx, q, page)
if err != nil {
Expand Down
41 changes: 25 additions & 16 deletions eventlogs/postgres/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ func TestEventsRetrieveAll(t *testing.T) {
}
err := repo.Save(context.Background(), event)
require.Nil(t, err, fmt.Sprintf("create event unexpected error: %s", err))
event.Payload = nil
items = append(items, event)
}

Expand Down Expand Up @@ -359,6 +360,20 @@ func TestEventsRetrieveAll(t *testing.T) {
Events: []eventlogs.Event{items[0]},
},
},
{
desc: "with payload",
page: eventlogs.Page{
WithPayload: true,
Offset: 0,
Limit: 10,
},
response: eventlogs.EventsPage{
Total: uint64(num),
Offset: 0,
Limit: 10,
Events: items[:10],
},
},
{
desc: "with from",
page: eventlogs.Page{
Expand Down Expand Up @@ -405,12 +420,13 @@ func TestEventsRetrieveAll(t *testing.T) {
{
desc: "with all filters",
page: eventlogs.Page{
ID: items[0].ID,
Operation: items[0].Operation,
From: items[0].OccurredAt,
To: items[num-1].OccurredAt,
Offset: 0,
Limit: 10,
ID: items[0].ID,
Operation: items[0].Operation,
From: items[0].OccurredAt,
To: items[num-1].OccurredAt,
WithPayload: true,
Offset: 0,
Limit: 10,
},
response: eventlogs.EventsPage{
Total: 1,
Expand Down Expand Up @@ -482,17 +498,10 @@ func TestEventsRetrieveAll(t *testing.T) {
assert.Equal(t, tc.response.Total, page.Total)
assert.Equal(t, tc.response.Offset, page.Offset)
assert.Equal(t, tc.response.Limit, page.Limit)
assert.ElementsMatch(t, removePayload(t, page.Events), removePayload(t, tc.response.Events))
if !tc.page.WithPayload {
assert.ElementsMatch(t, page.Events, tc.response.Events)
}
assert.Equal(t, tc.err, err)
})
}
}

func removePayload(t *testing.T, events []eventlogs.Event) []eventlogs.Event {
var items []eventlogs.Event
for _, e := range events {
e.Payload = nil
items = append(items, e)
}
return items
}
11 changes: 11 additions & 0 deletions pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ type PageMetadata struct {
Operation string `json:"operation,omitempty"`
From int64 `json:"from,omitempty"`
To int64 `json:"to,omitempty"`
WithPayload bool `json:"with_payload,omitempty"`
}

// Credentials represent client credentials: it contains
Expand Down Expand Up @@ -1382,6 +1383,16 @@ func (pm PageMetadata) query() (string, error) {
if pm.Relation != "" {
q.Add("relation", pm.Relation)
}
if pm.Operation != "" {
q.Add("operation", pm.Operation)
}
if pm.From != 0 {
q.Add("from", strconv.FormatInt(pm.From, 10))
}
if pm.To != 0 {
q.Add("to", strconv.FormatInt(pm.To, 10))
}
q.Add("with_payload", strconv.FormatBool(pm.WithPayload))

return q.Encode(), nil
}

0 comments on commit 1717398

Please sign in to comment.