2021-07-05 10:36:51 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-06 13:55:57 +02:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-03-28 21:36:52 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2021-07-05 10:36:51 +02:00
|
|
|
)
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
type InstancePrivacyPolicyWriteModel struct {
|
2021-07-05 10:36:51 +02:00
|
|
|
PrivacyPolicyWriteModel
|
|
|
|
}
|
|
|
|
|
2022-04-05 07:58:09 +02:00
|
|
|
func NewInstancePrivacyPolicyWriteModel(ctx context.Context) *InstancePrivacyPolicyWriteModel {
|
2022-03-24 17:21:34 +01:00
|
|
|
return &InstancePrivacyPolicyWriteModel{
|
2021-07-05 10:36:51 +02:00
|
|
|
PrivacyPolicyWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 07:58:09 +02:00
|
|
|
AggregateID: authz.GetInstance(ctx).InstanceID(),
|
|
|
|
ResourceOwner: authz.GetInstance(ctx).InstanceID(),
|
2021-07-05 10:36:51 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (wm *InstancePrivacyPolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-07-05 10:36:51 +02:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.PrivacyPolicyAddedEvent:
|
2021-07-05 10:36:51 +02:00
|
|
|
wm.PrivacyPolicyWriteModel.AppendEvents(&e.PrivacyPolicyAddedEvent)
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.PrivacyPolicyChangedEvent:
|
2021-07-05 10:36:51 +02:00
|
|
|
wm.PrivacyPolicyWriteModel.AppendEvents(&e.PrivacyPolicyChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (wm *InstancePrivacyPolicyWriteModel) Reduce() error {
|
2021-07-05 10:36:51 +02:00
|
|
|
return wm.PrivacyPolicyWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (wm *InstancePrivacyPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 13:55:57 +02:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-07-05 10:36:51 +02:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 13:55:57 +02:00
|
|
|
AddQuery().
|
2022-03-24 17:21:34 +01:00
|
|
|
AggregateTypes(instance.AggregateType).
|
2021-07-06 13:55:57 +02:00
|
|
|
AggregateIDs(wm.PrivacyPolicyWriteModel.AggregateID).
|
2021-07-05 10:36:51 +02:00
|
|
|
EventTypes(
|
2022-03-24 17:21:34 +01:00
|
|
|
instance.PrivacyPolicyAddedEventType,
|
|
|
|
instance.PrivacyPolicyChangedEventType).
|
2021-07-06 13:55:57 +02:00
|
|
|
Builder()
|
2021-07-05 10:36:51 +02:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (wm *InstancePrivacyPolicyWriteModel) NewChangedEvent(
|
2021-07-05 10:36:51 +02:00
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
tosLink,
|
2022-03-24 14:00:24 +01:00
|
|
|
privacyLink,
|
|
|
|
helpLink string,
|
2023-03-28 21:36:52 +02:00
|
|
|
supportEmail domain.EmailAddress,
|
2024-05-13 16:01:50 +02:00
|
|
|
docsLink, customLink, customLinkText string,
|
2022-03-24 17:21:34 +01:00
|
|
|
) (*instance.PrivacyPolicyChangedEvent, bool) {
|
2021-07-05 10:36:51 +02: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 14:00:24 +01:00
|
|
|
if wm.HelpLink != helpLink {
|
|
|
|
changes = append(changes, policy.ChangeHelpLink(helpLink))
|
|
|
|
}
|
2023-03-28 21:36:52 +02:00
|
|
|
if wm.SupportEmail != supportEmail {
|
|
|
|
changes = append(changes, policy.ChangeSupportEmail(supportEmail))
|
|
|
|
}
|
2024-05-13 16:01:50 +02:00
|
|
|
if wm.DocsLink != docsLink {
|
|
|
|
changes = append(changes, policy.ChangeDocsLink(docsLink))
|
|
|
|
}
|
|
|
|
if wm.CustomLink != customLink {
|
|
|
|
changes = append(changes, policy.ChangeCustomLink(customLink))
|
|
|
|
}
|
|
|
|
if wm.CustomLinkText != customLinkText {
|
|
|
|
changes = append(changes, policy.ChangeCustomLinkText(customLinkText))
|
|
|
|
}
|
2021-07-05 10:36:51 +02:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2022-03-24 17:21:34 +01:00
|
|
|
changedEvent, err := instance.NewPrivacyPolicyChangedEvent(ctx, aggregate, changes)
|
2021-07-05 10:36:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
|
|
|
}
|