zitadel/internal/eventstore/v2/read_model.go
Livio Amstutz 21ffe1b0cb
new pkg structure (#1150)
* fix: split command query side

* fix: split command query side

* fix: members in correct pkg structure

* fix: label policy in correct pkg structure

* fix: structure

* fix: structure of login policy

* fix: identityprovider structure

* fix: org iam policy structure

* fix: password age policy structure

* fix: password complexity policy structure

* fix: password lockout policy structure

* fix: idp structure

* fix: user events structure

* fix: user write model

* fix: profile email changed command

* fix: address changed command

* fix: user states

* fix: user

* fix: org structure and add human

* begin iam setup command side

* setup

* step2

* step2

* fix: add user

* step2

* isvalid

* fix: folder structure v2 business

Co-authored-by: Fabiennne <fabienne.gerschwiler@gmail.com>
2021-01-04 14:52:13 +01:00

48 lines
1.4 KiB
Go

package eventstore
import "time"
//MemberReadModel is the minimum representation of a View model.
// It implements a basic reducer
// it might be saved in a database or in memory
type ReadModel struct {
AggregateID string `json:"-"`
ProcessedSequence uint64 `json:"-"`
CreationDate time.Time `json:"-"`
ChangeDate time.Time `json:"-"`
Events []EventReader `json:"-"`
ResourceOwner string `json:"-"`
}
//AppendEvents adds all the events to the read model.
// The function doesn't compute the new state of the read model
func (rm *ReadModel) AppendEvents(events ...EventReader) *ReadModel {
rm.Events = append(rm.Events, events...)
return rm
}
//Reduce is the basic implementaion of reducer
// If this function is extended the extending function should be the last step
func (rm *ReadModel) Reduce() error {
if len(rm.Events) == 0 {
return nil
}
if rm.AggregateID == "" {
rm.AggregateID = rm.Events[0].AggregateID()
}
if rm.ResourceOwner == "" {
rm.ResourceOwner = rm.Events[0].ResourceOwner()
}
if rm.CreationDate.IsZero() {
rm.CreationDate = rm.Events[0].CreationDate()
}
rm.ChangeDate = rm.Events[len(rm.Events)-1].CreationDate()
rm.ProcessedSequence = rm.Events[len(rm.Events)-1].Sequence()
// all events processed and not needed anymore
rm.Events = nil
rm.Events = []EventReader{}
return nil
}