2021-07-05 08:36:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-06 11:55:57 +00:00
|
|
|
|
2021-07-05 08:36:51 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2022-03-24 16:21:34 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/instance"
|
2021-07-05 08:36:51 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/policy"
|
|
|
|
)
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
type InstancePrivacyPolicyWriteModel struct {
|
2021-07-05 08:36:51 +00:00
|
|
|
PrivacyPolicyWriteModel
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func NewInstancePrivacyPolicyWriteModel() *InstancePrivacyPolicyWriteModel {
|
|
|
|
return &InstancePrivacyPolicyWriteModel{
|
2021-07-05 08:36:51 +00:00
|
|
|
PrivacyPolicyWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: domain.IAMID,
|
|
|
|
ResourceOwner: domain.IAMID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstancePrivacyPolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-07-05 08:36:51 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.PrivacyPolicyAddedEvent:
|
2021-07-05 08:36:51 +00:00
|
|
|
wm.PrivacyPolicyWriteModel.AppendEvents(&e.PrivacyPolicyAddedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.PrivacyPolicyChangedEvent:
|
2021-07-05 08:36:51 +00:00
|
|
|
wm.PrivacyPolicyWriteModel.AppendEvents(&e.PrivacyPolicyChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstancePrivacyPolicyWriteModel) Reduce() error {
|
2021-07-05 08:36:51 +00:00
|
|
|
return wm.PrivacyPolicyWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstancePrivacyPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-07-05 08:36:51 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
2022-03-24 16:21:34 +00:00
|
|
|
AggregateTypes(instance.AggregateType).
|
2021-07-06 11:55:57 +00:00
|
|
|
AggregateIDs(wm.PrivacyPolicyWriteModel.AggregateID).
|
2021-07-05 08:36:51 +00:00
|
|
|
EventTypes(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.PrivacyPolicyAddedEventType,
|
|
|
|
instance.PrivacyPolicyChangedEventType).
|
2021-07-06 11:55:57 +00:00
|
|
|
Builder()
|
2021-07-05 08:36:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstancePrivacyPolicyWriteModel) NewChangedEvent(
|
2021-07-05 08:36:51 +00:00
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
tosLink,
|
2022-03-24 13:00:24 +00:00
|
|
|
privacyLink,
|
|
|
|
helpLink string,
|
2022-03-24 16:21:34 +00:00
|
|
|
) (*instance.PrivacyPolicyChangedEvent, bool) {
|
2021-07-05 08:36:51 +00:00
|
|
|
|
|
|
|
changes := make([]policy.PrivacyPolicyChanges, 0)
|
|
|
|
if wm.TOSLink != tosLink {
|
|
|
|
changes = append(changes, policy.ChangeTOSLink(tosLink))
|
|
|
|
}
|
|
|
|
if wm.PrivacyLink != privacyLink {
|
|
|
|
changes = append(changes, policy.ChangePrivacyLink(privacyLink))
|
|
|
|
}
|
2022-03-24 13:00:24 +00:00
|
|
|
if wm.HelpLink != helpLink {
|
|
|
|
changes = append(changes, policy.ChangeHelpLink(helpLink))
|
|
|
|
}
|
2021-07-05 08:36:51 +00:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
changedEvent, err := instance.NewPrivacyPolicyChangedEvent(ctx, aggregate, changes)
|
2021-07-05 08:36:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
|
|
|
}
|