2021-07-05 08:36:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-06 11:55:57 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2021-07-05 08:36:51 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2021-07-05 08:36:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OrgPrivacyPolicyWriteModel struct {
|
|
|
|
PrivacyPolicyWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgPrivacyPolicyWriteModel(orgID string) *OrgPrivacyPolicyWriteModel {
|
|
|
|
return &OrgPrivacyPolicyWriteModel{
|
|
|
|
PrivacyPolicyWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func (wm *OrgPrivacyPolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-07-05 08:36:51 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.PrivacyPolicyAddedEvent:
|
|
|
|
wm.PrivacyPolicyWriteModel.AppendEvents(&e.PrivacyPolicyAddedEvent)
|
|
|
|
case *org.PrivacyPolicyChangedEvent:
|
|
|
|
wm.PrivacyPolicyWriteModel.AppendEvents(&e.PrivacyPolicyChangedEvent)
|
|
|
|
case *org.PrivacyPolicyRemovedEvent:
|
|
|
|
wm.PrivacyPolicyWriteModel.AppendEvents(&e.PrivacyPolicyRemovedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgPrivacyPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.PrivacyPolicyWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgPrivacyPolicyWriteModel) 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().
|
|
|
|
AggregateIDs(wm.PrivacyPolicyWriteModel.AggregateID).
|
|
|
|
AggregateTypes(org.AggregateType).
|
2021-07-05 08:36:51 +00:00
|
|
|
EventTypes(org.PrivacyPolicyAddedEventType,
|
|
|
|
org.PrivacyPolicyChangedEventType,
|
2021-07-06 11:55:57 +00:00
|
|
|
org.PrivacyPolicyRemovedEventType).
|
|
|
|
Builder()
|
2021-07-05 08:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgPrivacyPolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
tosLink,
|
2022-03-24 13:00:24 +00:00
|
|
|
privacyLink,
|
|
|
|
helpLink string,
|
2021-07-05 08:36:51 +00:00
|
|
|
) (*org.PrivacyPolicyChangedEvent, bool) {
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
changedEvent, err := org.NewPrivacyPolicyChangedEvent(ctx, aggregate, changes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
|
|
|
}
|