2023-03-28 22:09:06 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2023-03-28 22:09:06 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/notification/channels/webhook"
|
|
|
|
_ "github.com/zitadel/zitadel/internal/notification/statik"
|
|
|
|
"github.com/zitadel/zitadel/internal/notification/types"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/quota"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
QuotaNotificationsProjectionTable = "projections.notifications_quota"
|
|
|
|
)
|
|
|
|
|
|
|
|
type quotaNotifier struct {
|
2023-10-10 13:20:53 +00:00
|
|
|
commands *command.Commands
|
|
|
|
queries *NotificationQueries
|
|
|
|
channels types.ChannelChains
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewQuotaNotifier(
|
|
|
|
ctx context.Context,
|
2023-10-19 10:19:10 +00:00
|
|
|
config handler.Config,
|
2023-03-28 22:09:06 +00:00
|
|
|
commands *command.Commands,
|
|
|
|
queries *NotificationQueries,
|
2023-10-10 13:20:53 +00:00
|
|
|
channels types.ChannelChains,
|
2023-10-19 10:19:10 +00:00
|
|
|
) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, "aNotifier{
|
|
|
|
commands: commands,
|
|
|
|
queries: queries,
|
|
|
|
channels: channels,
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*quotaNotifier) Name() string {
|
|
|
|
return QuotaNotificationsProjectionTable
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (u *quotaNotifier) Reducers() []handler.AggregateReducer {
|
2023-03-28 22:09:06 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: quota.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2023-03-28 22:09:06 +00:00
|
|
|
{
|
|
|
|
Event: quota.NotificationDueEventType,
|
|
|
|
Reduce: u.reduceNotificationDue,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *quotaNotifier) reduceNotificationDue(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*quota.NotificationDueEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-DLxdE", "reduce.wrong.event.type %s", quota.NotificationDueEventType)
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
|
|
|
|
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
|
|
|
|
ctx := HandlerContext(event.Aggregate())
|
|
|
|
alreadyHandled, err := u.queries.IsAlreadyHandled(ctx, event, map[string]interface{}{"dueEventID": e.ID}, quota.AggregateType, quota.NotifiedEventType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if alreadyHandled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = types.SendJSON(ctx, webhook.Config{CallURL: e.CallURL, Method: http.MethodPost}, u.channels, e, e).WithoutTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return u.commands.UsageNotificationSent(ctx, e)
|
|
|
|
}), nil
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|