Files
schemas/domain/events.go
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

95 lines
2.5 KiB
Go

package domain
import "gitlab.com/unboundsoftware/eventsourced/eventsourced"
type OrganizationAdded struct {
eventsourced.BaseEvent
Name string `json:"name"`
Initiator string `json:"initiator"`
}
func (a *OrganizationAdded) UpdateOrganization(o *Organization) {
o.Name = a.Name
o.Users = []string{a.Initiator}
o.CreatedBy = a.Initiator
o.CreatedAt = a.When()
o.ChangedBy = a.Initiator
o.ChangedAt = a.When()
}
type UserAddedToOrganization struct {
eventsourced.BaseEvent
UserId string `json:"userId"`
Initiator string `json:"initiator"`
}
func (a *UserAddedToOrganization) UpdateOrganization(o *Organization) {
// Check if user is already in the organization
for _, user := range o.Users {
if user == a.UserId {
return // User already exists, no need to add
}
}
o.Users = append(o.Users, a.UserId)
o.ChangedBy = a.Initiator
o.ChangedAt = a.When()
}
type APIKeyAdded struct {
eventsourced.BaseEvent
OrganizationId string `json:"organizationId"`
Name string `json:"name"`
Key string `json:"key"`
Refs []string `json:"refs"`
Read bool `json:"read"`
Publish bool `json:"publish"`
Initiator string `json:"initiator"`
}
func (a *APIKeyAdded) EnrichFromAggregate(aggregate eventsourced.Aggregate) {
a.OrganizationId = aggregate.Identity().String()
}
var _ eventsourced.EnrichableEvent = &APIKeyAdded{}
type APIKeyRemoved struct {
eventsourced.BaseEvent
KeyName string `json:"keyName"`
Initiator string `json:"initiator"`
}
func (a *APIKeyRemoved) UpdateOrganization(o *Organization) {
// Remove the API key from the organization
for i, key := range o.APIKeys {
if key.Name == a.KeyName {
o.APIKeys = append(o.APIKeys[:i], o.APIKeys[i+1:]...)
break
}
}
o.ChangedBy = a.Initiator
o.ChangedAt = a.When()
}
type OrganizationRemoved struct {
eventsourced.BaseEvent
Initiator string `json:"initiator"`
}
func (a *OrganizationRemoved) UpdateOrganization(o *Organization) {
// Mark organization as removed by clearing critical fields
// The aggregate will still exist in the event store, but it's logically deleted
o.ChangedBy = a.Initiator
o.ChangedAt = a.When()
}
type SubGraphUpdated struct {
eventsourced.BaseEvent
OrganizationId string `json:"organizationId"`
Ref string `json:"ref"`
Service string `json:"service"`
Url *string `json:"url"`
WSUrl *string `json:"wsUrl"`
Sdl string `json:"sdl"`
Initiator string `json:"initiator"`
}