Files
schemas/domain/aggregates.go
T
argoyle ffcf41b85a feat: add commands for managing organizations and users
Introduce `AddUserToOrganization`, `RemoveAPIKey`, and 
`RemoveOrganization` commands to enhance organization 
management. Implement validation for user addition and 
API key removal. Update GraphQL schema to support new 
mutations and add caching for the new events, ensuring 
that organizations and their relationships are accurately 
represented in the cache.
2025-11-22 18:37:07 +01:00

108 lines
2.2 KiB
Go

package domain
import (
"fmt"
"strings"
"time"
"gitlab.com/unboundsoftware/eventsourced/eventsourced"
)
type Organization struct {
eventsourced.BaseAggregate
Name string
Users []string
APIKeys []APIKey
CreatedBy string
CreatedAt time.Time
ChangedBy string
ChangedAt time.Time
}
func (o *Organization) Apply(event eventsourced.Event) error {
switch e := event.(type) {
case *OrganizationAdded:
e.UpdateOrganization(o)
case *UserAddedToOrganization:
e.UpdateOrganization(o)
case *APIKeyAdded:
o.APIKeys = append(o.APIKeys, APIKey{
Name: e.Name,
OrganizationId: o.ID.String(),
Key: e.Key,
Refs: e.Refs,
Read: e.Read,
Publish: e.Publish,
CreatedBy: e.Initiator,
CreatedAt: e.When(),
})
o.ChangedBy = e.Initiator
o.ChangedAt = e.When()
case *APIKeyRemoved:
e.UpdateOrganization(o)
case *OrganizationRemoved:
e.UpdateOrganization(o)
default:
return fmt.Errorf("unexpected event type: %+v", event)
}
return nil
}
var _ eventsourced.Aggregate = &Organization{}
type APIKey struct {
Name string
OrganizationId string
Key string
Refs []string
Read bool
Publish bool
CreatedBy string
CreatedAt time.Time
}
type SubGraph struct {
eventsourced.BaseAggregate
OrganizationId string
Ref string
Service string
Url *string
WSUrl *string
Sdl string
CreatedBy string
CreatedAt time.Time
ChangedBy string
ChangedAt time.Time
}
func (s *SubGraph) Apply(event eventsourced.Event) error {
switch e := event.(type) {
case *SubGraphUpdated:
if s.Ref == "" {
s.CreatedBy = e.Initiator
s.CreatedAt = e.When()
}
if s.OrganizationId == "" {
s.OrganizationId = e.OrganizationId
}
s.ChangedBy = e.Initiator
s.ChangedAt = e.When()
s.Ref = e.Ref
s.Service = e.Service
if e.Url != nil {
url := strings.TrimSpace(*e.Url)
s.Url = &url
}
if e.WSUrl != nil {
url := strings.TrimSpace(*e.WSUrl)
s.WSUrl = &url
}
s.Sdl = e.Sdl
default:
return fmt.Errorf("unexpected event type: %+v", event)
}
return nil
}
var _ eventsourced.Aggregate = &SubGraph{}