mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-14 20:08:02 +00:00
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
|
package command
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"slices"
|
||
|
|
||
|
"github.com/zitadel/zitadel/internal/api/authz"
|
||
|
"github.com/zitadel/zitadel/internal/eventstore"
|
||
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
||
|
)
|
||
|
|
||
|
type InstanceTrustedDomainsWriteModel struct {
|
||
|
eventstore.WriteModel
|
||
|
|
||
|
Domains []string
|
||
|
}
|
||
|
|
||
|
func NewInstanceTrustedDomainsWriteModel(ctx context.Context) *InstanceTrustedDomainsWriteModel {
|
||
|
instanceID := authz.GetInstance(ctx).InstanceID()
|
||
|
return &InstanceTrustedDomainsWriteModel{
|
||
|
WriteModel: eventstore.WriteModel{
|
||
|
AggregateID: instanceID,
|
||
|
ResourceOwner: instanceID,
|
||
|
InstanceID: instanceID,
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (wm *InstanceTrustedDomainsWriteModel) Reduce() error {
|
||
|
for _, event := range wm.Events {
|
||
|
switch e := event.(type) {
|
||
|
case *instance.TrustedDomainAddedEvent:
|
||
|
wm.Domains = append(wm.Domains, e.Domain)
|
||
|
case *instance.TrustedDomainRemovedEvent:
|
||
|
wm.Domains = slices.DeleteFunc(wm.Domains, func(domain string) bool {
|
||
|
return domain == e.Domain
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
return wm.WriteModel.Reduce()
|
||
|
}
|
||
|
|
||
|
func (wm *InstanceTrustedDomainsWriteModel) Query() *eventstore.SearchQueryBuilder {
|
||
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
||
|
ResourceOwner(wm.ResourceOwner).
|
||
|
AddQuery().
|
||
|
AggregateTypes(instance.AggregateType).
|
||
|
AggregateIDs(wm.AggregateID).
|
||
|
EventTypes(
|
||
|
instance.TrustedDomainAddedEventType,
|
||
|
instance.TrustedDomainRemovedEventType,
|
||
|
).
|
||
|
Builder()
|
||
|
}
|