zitadel/internal/query/notification_provider.go
Fabienne Bühler 07ce3b6905
chore!: Introduce ZITADEL v3 (#9645)
This PR summarizes multiple changes specifically only available with
ZITADEL v3:

- feat: Web Keys management
(https://github.com/zitadel/zitadel/pull/9526)
- fix(cmd): ensure proper working of mirror
(https://github.com/zitadel/zitadel/pull/9509)
- feat(Authz): system user support for permission check v2
(https://github.com/zitadel/zitadel/pull/9640)
- chore(license): change from Apache to AGPL
(https://github.com/zitadel/zitadel/pull/9597)
- feat(console): list v2 sessions
(https://github.com/zitadel/zitadel/pull/9539)
- fix(console): add loginV2 feature flag
(https://github.com/zitadel/zitadel/pull/9682)
- fix(feature flags): allow reading "own" flags
(https://github.com/zitadel/zitadel/pull/9649)
- feat(console): add Actions V2 UI
(https://github.com/zitadel/zitadel/pull/9591)

BREAKING CHANGE
- feat(webkey): migrate to v2beta API
(https://github.com/zitadel/zitadel/pull/9445)
- chore!: remove CockroachDB Support
(https://github.com/zitadel/zitadel/pull/9444)
- feat(actions): migrate to v2beta API
(https://github.com/zitadel/zitadel/pull/9489)

---------

Co-authored-by: Livio Spring <livio.a@gmail.com>
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
Co-authored-by: Silvan <27845747+adlerhurst@users.noreply.github.com>
Co-authored-by: Ramon <mail@conblem.me>
Co-authored-by: Elio Bischof <elio@zitadel.com>
Co-authored-by: Kenta Yamaguchi <56732734+KEY60228@users.noreply.github.com>
Co-authored-by: Harsha Reddy <harsha.reddy@klaviyo.com>
Co-authored-by: Livio Spring <livio@zitadel.com>
Co-authored-by: Max Peintner <max@caos.ch>
Co-authored-by: Iraq <66622793+kkrime@users.noreply.github.com>
Co-authored-by: Florian Forster <florian@zitadel.com>
Co-authored-by: Tim Möhlmann <tim+github@zitadel.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Max Peintner <peintnerm@gmail.com>
2025-04-02 16:53:06 +02:00

132 lines
4.1 KiB
Go

package query
import (
"context"
"database/sql"
"errors"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/query/projection"
"github.com/zitadel/zitadel/internal/telemetry/tracing"
"github.com/zitadel/zitadel/internal/zerrors"
)
type DebugNotificationProvider struct {
AggregateID string
CreationDate time.Time
ChangeDate time.Time
Sequence uint64
ResourceOwner string
State domain.NotificationProviderState
Type domain.NotificationProviderType
Compact bool
}
var (
notificationProviderTable = table{
name: projection.DebugNotificationProviderTable,
instanceIDCol: projection.DebugNotificationProviderInstanceIDCol,
}
NotificationProviderColumnAggID = Column{
name: projection.DebugNotificationProviderAggIDCol,
table: notificationProviderTable,
}
NotificationProviderColumnCreationDate = Column{
name: projection.DebugNotificationProviderCreationDateCol,
table: notificationProviderTable,
}
NotificationProviderColumnChangeDate = Column{
name: projection.DebugNotificationProviderChangeDateCol,
table: notificationProviderTable,
}
NotificationProviderColumnSequence = Column{
name: projection.DebugNotificationProviderSequenceCol,
table: notificationProviderTable,
}
NotificationProviderColumnResourceOwner = Column{
name: projection.DebugNotificationProviderResourceOwnerCol,
table: notificationProviderTable,
}
NotificationProviderColumnInstanceID = Column{
name: projection.DebugNotificationProviderInstanceIDCol,
table: notificationProviderTable,
}
NotificationProviderColumnState = Column{
name: projection.DebugNotificationProviderStateCol,
table: notificationProviderTable,
}
NotificationProviderColumnType = Column{
name: projection.DebugNotificationProviderTypeCol,
table: notificationProviderTable,
}
NotificationProviderColumnCompact = Column{
name: projection.DebugNotificationProviderCompactCol,
table: notificationProviderTable,
}
)
func (q *Queries) NotificationProviderByIDAndType(ctx context.Context, aggID string, providerType domain.NotificationProviderType) (provider *DebugNotificationProvider, err error) {
ctx, span := tracing.NewSpan(ctx)
defer func() { span.EndWithError(err) }()
query, scan := prepareDebugNotificationProviderQuery()
stmt, args, err := query.Where(
sq.And{
sq.Eq{NotificationProviderColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID()},
sq.Or{
sq.Eq{
NotificationProviderColumnAggID.identifier(): aggID,
NotificationProviderColumnType.identifier(): providerType,
},
},
}).
Limit(1).ToSql()
if err != nil {
return nil, zerrors.ThrowInternal(err, "QUERY-f9jSf", "Errors.Query.SQLStatement")
}
err = q.client.QueryRowContext(ctx, func(row *sql.Row) error {
provider, err = scan(row)
return err
}, stmt, args...)
return provider, err
}
func prepareDebugNotificationProviderQuery() (sq.SelectBuilder, func(*sql.Row) (*DebugNotificationProvider, error)) {
return sq.Select(
NotificationProviderColumnAggID.identifier(),
NotificationProviderColumnCreationDate.identifier(),
NotificationProviderColumnChangeDate.identifier(),
NotificationProviderColumnSequence.identifier(),
NotificationProviderColumnResourceOwner.identifier(),
NotificationProviderColumnState.identifier(),
NotificationProviderColumnType.identifier(),
NotificationProviderColumnCompact.identifier(),
).From(notificationProviderTable.identifier()).
PlaceholderFormat(sq.Dollar),
func(row *sql.Row) (*DebugNotificationProvider, error) {
p := new(DebugNotificationProvider)
err := row.Scan(
&p.AggregateID,
&p.CreationDate,
&p.ChangeDate,
&p.Sequence,
&p.ResourceOwner,
&p.State,
&p.Type,
&p.Compact,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, zerrors.ThrowNotFound(err, "QUERY-s9ujf", "Errors.NotificationProvider.NotFound")
}
return nil, zerrors.ThrowInternal(err, "QUERY-2liu0", "Errors.Internal")
}
return p, nil
}
}