Skip to content

Commit

Permalink
fix: abstract user with channel as a valid type
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo committed Jun 13, 2024
1 parent c77bbf5 commit cf80d28
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 21 deletions.
7 changes: 1 addition & 6 deletions activitylog/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package activitylog
import (
"context"
"encoding/json"
"errors"
"time"

"github.com/absmach/magistrala/auth"
Expand All @@ -30,8 +29,6 @@ const (
channelEntityType = "channel"
)

var ErrMissingOccurredAt = errors.New("missing occurred_at")

// String converts entity type to string literal.
func (e EntityType) String() string {
switch e {
Expand Down Expand Up @@ -83,12 +80,10 @@ func (e EntityType) Query() string {
switch e {
case UserEntity:
return "((operation LIKE 'user.%' AND attributes->>'id' = :entity_id) OR (attributes->>'user_id' = :entity_id))"
case GroupEntity:
case GroupEntity, ChannelEntity:
return "((operation LIKE 'group.%' AND attributes->>'id' = :entity_id) OR (attributes->>'group_id' = :entity_id))"
case ThingEntity:
return "((operation LIKE 'thing.%' AND attributes->>'id' = :entity_id) OR (attributes->>'thing_id' = :entity_id))"
case ChannelEntity:
return "((operation LIKE 'channel.%' AND attributes->>'id' = :entity_id) OR (attributes->>'group_id' = :entity_id))"
default:
return ""
}
Expand Down
4 changes: 4 additions & 0 deletions activitylog/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/slog"
"math"
"net/http"
"strings"
"time"

"github.com/absmach/magistrala"
Expand Down Expand Up @@ -104,6 +105,9 @@ func decodeRetrieveActivitiesReq(_ context.Context, r *http.Request) (interface{
if err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, err)
}
if entityType == activitylog.ChannelEntity {
operation = strings.ReplaceAll(operation, "channel", "group")
}

req := retrieveActivitiesReq{
token: apiutil.ExtractBearerToken(r),
Expand Down
11 changes: 7 additions & 4 deletions activitylog/consumer.go → activitylog/events/consumer.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package activitylog
package events

import (
"context"
"errors"
"time"

"github.com/absmach/magistrala/activitylog"
"github.com/absmach/magistrala/pkg/events"
"github.com/absmach/magistrala/pkg/events/store"
)

var ErrMissingOccurredAt = errors.New("missing occurred_at")

// Start method starts consuming messages received from Event store.
func Start(ctx context.Context, consumer string, sub events.Subscriber, service Service) error {
func Start(ctx context.Context, consumer string, sub events.Subscriber, service activitylog.Service) error {
subCfg := events.SubscriberConfig{
Consumer: consumer,
Stream: store.StreamAllEvents,
Expand All @@ -23,7 +26,7 @@ func Start(ctx context.Context, consumer string, sub events.Subscriber, service
return sub.Subscribe(ctx, subCfg)
}

func Handle(service Service) handleFunc {
func Handle(service activitylog.Service) handleFunc {
return func(ctx context.Context, event events.Event) error {
data, err := event.Encode()
if err != nil {
Expand Down Expand Up @@ -60,7 +63,7 @@ func Handle(service Service) handleFunc {
return errors.New("missing attributes")
}

activity := Activity{
activity := activitylog.Activity{
Operation: operation,
OccurredAt: time.Unix(0, int64(occurredAt)),
Attributes: data,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package activitylog_test
package events_test

import (
"context"
Expand All @@ -13,6 +13,7 @@ import (
"time"

"github.com/absmach/magistrala/activitylog"
aevents "github.com/absmach/magistrala/activitylog/events"
"github.com/absmach/magistrala/activitylog/mocks"
"github.com/absmach/magistrala/internal/testsutil"
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
Expand Down Expand Up @@ -126,7 +127,7 @@ func TestHandle(t *testing.T) {
"number": float64(rand.Intn(1000)),
"metadata": payload,
},
err: activitylog.ErrMissingOccurredAt,
err: aevents.ErrMissingOccurredAt,
},
{
desc: "with empty occurred_at",
Expand All @@ -138,7 +139,7 @@ func TestHandle(t *testing.T) {
"number": float64(rand.Intn(1000)),
"metadata": payload,
},
err: activitylog.ErrMissingOccurredAt,
err: aevents.ErrMissingOccurredAt,
},
{
desc: "with invalid occurred_at",
Expand All @@ -150,7 +151,7 @@ func TestHandle(t *testing.T) {
"number": float64(rand.Intn(1000)),
"metadata": payload,
},
err: activitylog.ErrMissingOccurredAt,
err: aevents.ErrMissingOccurredAt,
},
{
desc: "with missing metadata",
Expand Down Expand Up @@ -275,7 +276,7 @@ func TestHandle(t *testing.T) {
Metadata: metadata,
}
repoCall := repo.On("Save", context.Background(), activity).Return(tc.repoErr)
err = activitylog.Handle(svc)(context.Background(), NewTestEvent(event, tc.encodeErr))
err = aevents.Handle(svc)(context.Background(), NewTestEvent(event, tc.encodeErr))
switch {
case tc.err == nil:
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion activitylog/postgres/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ type dbActivity struct {

func toDBActivity(activity activitylog.Activity) (dbActivity, error) {
if activity.OccurredAt.IsZero() {
return dbActivity{}, errors.Wrap(repoerr.ErrMalformedEntity, activitylog.ErrMissingOccurredAt)
activity.OccurredAt = time.Now()
}

attributes := []byte("{}")
Expand Down
4 changes: 2 additions & 2 deletions activitylog/postgres/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestActivitySave(t *testing.T) {
Attributes: payload,
Metadata: payload,
},
err: repoerr.ErrCreateEntity,
err: nil,
},
{
desc: "with nil activity attributes",
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestActivitySave(t *testing.T) {
{
desc: "with empty activity",
activity: activitylog.Activity{},
err: repoerr.ErrMalformedEntity,
err: repoerr.ErrCreateEntity,
},
}
for _, tc := range cases {
Expand Down
5 changes: 3 additions & 2 deletions api/openapi/activitylog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ components:
properties:
operation:
type: string
example: users.create
example: user.create
description: Activity operation.
occurred_at:
type: string
Expand Down Expand Up @@ -155,6 +155,7 @@ components:
- user
- group
- thing
- channel
required: true
example: user

Expand Down Expand Up @@ -198,7 +199,7 @@ components:
schema:
type: string
required: false
example: users.create
example: user.create

with_attributes:
name: with_attributes
Expand Down
3 changes: 2 additions & 1 deletion cmd/activitylog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/absmach/magistrala"
"github.com/absmach/magistrala/activitylog"
"github.com/absmach/magistrala/activitylog/api"
"github.com/absmach/magistrala/activitylog/events"
"github.com/absmach/magistrala/activitylog/middleware"
activitylogpg "github.com/absmach/magistrala/activitylog/postgres"
"github.com/absmach/magistrala/internal"
Expand Down Expand Up @@ -131,7 +132,7 @@ func main() {

logger.Info("Subscribed to Event Store")

if err := activitylog.Start(ctx, svcName, subscriber, svc); err != nil {
if err := events.Start(ctx, svcName, subscriber, svc); err != nil {
logger.Error("failed to start %s service: %s", svcName, err)
exitCode = 1
return
Expand Down

0 comments on commit cf80d28

Please sign in to comment.