2020-05-20 12:28:08 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/caos/logging"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
|
|
|
"github.com/caos/zitadel/internal/user/repository/eventsourcing"
|
|
|
|
view_model "github.com/caos/zitadel/internal/user/repository/view/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NotifyUser struct {
|
|
|
|
handler
|
|
|
|
eventstore eventstore.Eventstore
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
userTable = "notification.notify_users"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p *NotifyUser) MinimumCycleDuration() time.Duration { return p.cycleDuration }
|
|
|
|
|
|
|
|
func (p *NotifyUser) ViewModel() string {
|
|
|
|
return userTable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NotifyUser) EventQuery() (*models.SearchQuery, error) {
|
|
|
|
sequence, err := p.view.GetLatestNotifyUserSequence()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return eventsourcing.UserQuery(sequence), nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 12:47:47 +00:00
|
|
|
func (p *NotifyUser) Reduce(event *models.Event) (err error) {
|
2020-05-20 12:28:08 +00:00
|
|
|
user := new(view_model.NotifyUser)
|
|
|
|
switch event.Type {
|
|
|
|
case es_model.UserAdded,
|
|
|
|
es_model.UserRegistered:
|
|
|
|
user.AppendEvent(event)
|
|
|
|
case es_model.UserProfileChanged,
|
|
|
|
es_model.UserEmailChanged,
|
|
|
|
es_model.UserEmailVerified,
|
|
|
|
es_model.UserPhoneChanged,
|
2020-07-06 13:48:24 +00:00
|
|
|
es_model.UserPhoneVerified,
|
|
|
|
es_model.UserPhoneRemoved:
|
2020-05-20 12:28:08 +00:00
|
|
|
user, err = p.view.NotifyUserByID(event.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = user.AppendEvent(event)
|
2020-05-28 04:49:22 +00:00
|
|
|
case es_model.UserRemoved:
|
2020-05-20 12:28:08 +00:00
|
|
|
err = p.view.DeleteNotifyUser(event.AggregateID, event.Sequence)
|
|
|
|
default:
|
|
|
|
return p.view.ProcessedNotifyUserSequence(event.Sequence)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.view.PutNotifyUser(user)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NotifyUser) OnError(event *models.Event, err error) error {
|
|
|
|
logging.LogWithFields("SPOOL-9spwf", "id", event.AggregateID).WithError(err).Warn("something went wrong in notify user handler")
|
|
|
|
return spooler.HandleError(event, err, p.view.GetLatestNotifyUserFailedEvent, p.view.ProcessedNotifyUserFailedEvent, p.view.ProcessedNotifyUserSequence, p.errorCountUntilSkip)
|
|
|
|
}
|