161 lines
3.4 KiB
Go
Raw Normal View History

feat: org command sides (#96) * start org * refactor(eventstore): filter in sql for querier * feat(eventstore): Aggregate precondition preconditions are checked right before insert. Insert is still transaction save * feat(eventstore): check preconditions in repository * test(eventstore): test precondition in models * test(eventstore): precondition-tests * start org * refactor(eventstore): filter in sql for querier * feat(eventstore): Aggregate precondition preconditions are checked right before insert. Insert is still transaction save * feat(admin): start implement org * feat(eventstore): check preconditions in repository * fix(eventstore): data as NULL if empty refactor(eventstore): naming in sequence methods * feat(admin): org command side * feat(management): start org-repo * feat(org): member * fix: replace ObjectRoot.ID with ObjectRoot.AggregateID * aggregateID * add remove,change member * refactor(org): namings * refactor(eventstore): querier as type * fix(precondition): rename validation from precondition to validation * test(eventstore): isErr func instead of wantErr bool * fix(tests): Data * fix(eventstore): correct check for existing events in push, simplify insert statement * fix(eventstore): aggregate id public * test(org): eventsourcing * test(org): eventstore * test(org): deactivate, reactivate, orgbyid * test(org): getMemberByIDs * tests * running tests * add user repo to admin * thorw not found if no org found * eventstore tests done * lauft * validate if user is already member of org * modules * delete unused file * add member validation test * return error if unable to validat member * generate org id once, set resourceowner of org * Update internal/admin/repository/eventsourcing/eventstore/org.go * Update internal/admin/repository/eventsourcing/eventstore/org.go * Update internal/org/repository/eventsourcing/member_model.go * Update internal/org/repository/eventsourcing/org.go * Update internal/org/repository/eventsourcing/org.go * Update internal/org/repository/eventsourcing/org_member.go * Update internal/org/repository/eventsourcing/org_member.go * Update internal/org/repository/eventsourcing/org_model.go * Update internal/org/repository/eventsourcing/org.go * Update internal/org/repository/eventsourcing/org_model.go * Update internal/org/repository/eventsourcing/org_model.go * typo * correct user events * usercreate for setuporg instead of userregister * set data * mod * mod * tests * cleanup code * code styling * return member on add and change * change username in startup * girignore * orgID as parameter in re-/deactive org * startup config * migration for admin_api-user * probes fro admin * move unique org Co-authored-by: Fabiennne <fabienne.gerschwiler@gmail.com>
2020-05-13 14:22:29 +02:00
package eventsourcing
import (
"encoding/json"
"github.com/caos/zitadel/internal/errors"
es_models "github.com/caos/zitadel/internal/eventstore/models"
org_model "github.com/caos/zitadel/internal/org/model"
)
const (
orgVersion = "v1"
)
type Org struct {
es_models.ObjectRoot `json:"-"`
Name string `json:"name,omitempty"`
Domain string `json:"domain,omitempty"`
State int32 `json:"-"`
Members []*OrgMember `json:"-"`
}
func OrgFromModel(org *org_model.Org) *Org {
members := OrgMembersFromModel(org.Members)
return &Org{
ObjectRoot: org.ObjectRoot,
Domain: org.Domain,
Name: org.Name,
State: int32(org.State),
Members: members,
}
}
func OrgToModel(org *Org) *org_model.Org {
return &org_model.Org{
ObjectRoot: org.ObjectRoot,
Domain: org.Domain,
Name: org.Name,
State: org_model.OrgState(org.State),
Members: OrgMembersToModel(org.Members),
}
}
func OrgFromEvents(org *Org, events ...*es_models.Event) (*Org, error) {
if org == nil {
org = new(Org)
}
return org, org.AppendEvents(events...)
}
func (o *Org) AppendEvents(events ...*es_models.Event) error {
for _, event := range events {
err := o.AppendEvent(event)
if err != nil {
return err
}
}
return nil
}
func (o *Org) AppendEvent(event *es_models.Event) error {
switch event.Type {
case org_model.OrgAdded:
*o = Org{}
err := o.setData(event)
if err != nil {
return err
}
case org_model.OrgChanged:
err := o.setData(event)
if err != nil {
return err
}
case org_model.OrgDeactivated:
o.State = int32(org_model.ORGSTATE_INACTIVE)
case org_model.OrgReactivated:
o.State = int32(org_model.ORGSTATE_ACTIVE)
case org_model.OrgMemberAdded:
member, err := OrgMemberFromEvent(nil, event)
if err != nil {
return err
}
member.CreationDate = event.CreationDate
o.setMember(member)
case org_model.OrgMemberChanged:
member, err := OrgMemberFromEvent(nil, event)
if err != nil {
return err
}
existingMember := o.getMember(member.UserID)
member.CreationDate = existingMember.CreationDate
o.setMember(member)
case org_model.OrgMemberRemoved:
member, err := OrgMemberFromEvent(nil, event)
if err != nil {
return err
}
o.removeMember(member.UserID)
}
o.ObjectRoot.AppendEvent(event)
return nil
}
func (o *Org) setData(event *es_models.Event) error {
err := json.Unmarshal(event.Data, o)
if err != nil {
return errors.ThrowInternal(err, "EVENT-BpbQZ", "unable to unmarshal event")
}
return nil
}
func (o *Org) getMember(userID string) *OrgMember {
for _, member := range o.Members {
if member.UserID == userID {
return member
}
}
return nil
}
func (o *Org) setMember(member *OrgMember) {
for i, existingMember := range o.Members {
if existingMember.UserID == member.UserID {
o.Members[i] = member
return
}
}
o.Members = append(o.Members, member)
}
func (o *Org) removeMember(userID string) {
for i := len(o.Members) - 1; i >= 0; i-- {
if o.Members[i].UserID == userID {
copy(o.Members[i:], o.Members[i+1:])
o.Members[len(o.Members)-1] = nil
o.Members = o.Members[:len(o.Members)-1]
}
}
}
func (o *Org) Changes(changed *Org) map[string]interface{} {
changes := make(map[string]interface{}, 2)
if changed.Name != "" && changed.Name != o.Name {
changes["name"] = changed.Name
}
if changed.Domain != "" && changed.Domain != o.Domain {
changes["domain"] = changed.Domain
}
return changes
}