diff --git a/compose/service/event/record.go b/compose/service/event/record.go index 1ddfd031d..932ecbc25 100644 --- a/compose/service/event/record.go +++ b/compose/service/event/record.go @@ -1,8 +1,39 @@ package event -import "github.com/cortezaproject/corteza-server/pkg/eventbus" +import ( + "github.com/cortezaproject/corteza-server/compose/types" + "github.com/cortezaproject/corteza-server/pkg/eventbus" + "strings" + "time" +) + +const ( + recordMatchValues = "record.values." +) // Match returns false if given conditions do not match event & resource internals func (res recordBase) Match(c eventbus.ConstraintMatcher) bool { - return namespaceMatch(res.namespace, c, moduleMatch(res.module, c, false)) + return recordMatch(res.record, c, namespaceMatch(res.namespace, c, moduleMatch(res.module, c, false))) +} + +func recordMatch(r *types.Record, c eventbus.ConstraintMatcher, def bool) bool { + switch c.Name() { + case "record.updatedAt": + return c.Match(r.UpdatedAt.Format(time.RFC3339)) + case "record.createdAt": + return c.Match(r.CreatedAt.Format(time.RFC3339)) + case "record.deletedAt": + return c.Match(r.DeletedAt.Format(time.RFC3339)) + } + + if strings.HasPrefix(c.Name(), recordMatchValues) { + fieldName := c.Name()[len(recordMatchValues):] + for _, v := range r.Values.FilterByName(fieldName) { + if c.Match(v.Value) { + return true + } + } + } + + return def } diff --git a/compose/service/event/record_test.go b/compose/service/event/record_test.go index 8bf716b82..4fff39035 100644 --- a/compose/service/event/record_test.go +++ b/compose/service/event/record_test.go @@ -1,11 +1,11 @@ package event import ( + "fmt" "github.com/cortezaproject/corteza-server/compose/types" "github.com/cortezaproject/corteza-server/pkg/eventbus" - "testing" - "github.com/stretchr/testify/assert" + "testing" ) func TestRecordMatching(t *testing.T) { @@ -23,3 +23,36 @@ func TestRecordMatching(t *testing.T) { a.True(res.Match(cMod)) a.True(res.Match(cNms)) } + +func TestRecordMatchValues(t *testing.T) { + var ( + rec = &types.Record{ + Values: types.RecordValueSet{ + &types.RecordValue{Name: "fld1", Value: "val1"}, + &types.RecordValue{Name: "fld2", Value: "val2"}, + }, + } + + res = &recordBase{record: rec} + + cases = []struct { + match bool + name string + op string + v string + }{ + {true, "fld1", "eq", "val1"}, + {false, "fld2", "eq", "val1"}, + {false, "fld1", "!=", "val1"}, + } + ) + + for _, c := range cases { + t.Run( + fmt.Sprintf("(%s %s %s) == %v", c.name, c.op, c.v, c.match), + func(t *testing.T) { + assert.Equal(t, c.match, res.Match(eventbus.MustMakeConstraint("record.values."+c.name, c.op, c.v))) + }, + ) + } +}