2022-03-07 13:22:37 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
errs "errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-02-27 21:36:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/call"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
2022-12-01 08:18:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2022-03-07 13:22:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
2022-10-27 06:08:36 +00:00
|
|
|
name: projection.DebugNotificationProviderTable,
|
|
|
|
instanceIDCol: projection.DebugNotificationProviderInstanceIDCol,
|
2022-03-07 13:22:37 +00:00
|
|
|
}
|
|
|
|
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,
|
|
|
|
}
|
2022-03-23 08:02:39 +00:00
|
|
|
NotificationProviderColumnInstanceID = Column{
|
|
|
|
name: projection.DebugNotificationProviderInstanceIDCol,
|
|
|
|
table: notificationProviderTable,
|
|
|
|
}
|
2022-03-07 13:22:37 +00:00
|
|
|
NotificationProviderColumnState = Column{
|
|
|
|
name: projection.DebugNotificationProviderStateCol,
|
|
|
|
table: notificationProviderTable,
|
|
|
|
}
|
|
|
|
NotificationProviderColumnType = Column{
|
|
|
|
name: projection.DebugNotificationProviderTypeCol,
|
|
|
|
table: notificationProviderTable,
|
|
|
|
}
|
|
|
|
NotificationProviderColumnCompact = Column{
|
|
|
|
name: projection.DebugNotificationProviderCompactCol,
|
|
|
|
table: notificationProviderTable,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
func (q *Queries) NotificationProviderByIDAndType(ctx context.Context, aggID string, providerType domain.NotificationProviderType) (provider *DebugNotificationProvider, err error) {
|
2022-12-01 08:18:53 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
query, scan := prepareDebugNotificationProviderQuery(ctx, q.client)
|
2022-03-07 13:22:37 +00:00
|
|
|
stmt, args, err := query.Where(
|
2022-03-23 08:02:39 +00:00
|
|
|
sq.And{
|
2022-11-30 16:01:17 +00:00
|
|
|
sq.Eq{NotificationProviderColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID()},
|
2022-03-23 08:02:39 +00:00
|
|
|
sq.Or{
|
|
|
|
sq.Eq{
|
|
|
|
NotificationProviderColumnAggID.identifier(): aggID,
|
|
|
|
NotificationProviderColumnType.identifier(): providerType,
|
|
|
|
},
|
2022-03-07 13:22:37 +00:00
|
|
|
},
|
|
|
|
}).
|
|
|
|
Limit(1).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-f9jSf", "Errors.Query.SQLStatement")
|
|
|
|
}
|
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
err = q.client.QueryRowContext(ctx, func(row *sql.Row) error {
|
|
|
|
provider, err = scan(row)
|
|
|
|
return err
|
|
|
|
}, stmt, args...)
|
|
|
|
return provider, err
|
2022-03-07 13:22:37 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
func prepareDebugNotificationProviderQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Row) (*DebugNotificationProvider, error)) {
|
2022-03-07 13:22:37 +00:00
|
|
|
return sq.Select(
|
|
|
|
NotificationProviderColumnAggID.identifier(),
|
|
|
|
NotificationProviderColumnCreationDate.identifier(),
|
|
|
|
NotificationProviderColumnChangeDate.identifier(),
|
|
|
|
NotificationProviderColumnSequence.identifier(),
|
|
|
|
NotificationProviderColumnResourceOwner.identifier(),
|
|
|
|
NotificationProviderColumnState.identifier(),
|
|
|
|
NotificationProviderColumnType.identifier(),
|
|
|
|
NotificationProviderColumnCompact.identifier(),
|
2023-02-27 21:36:43 +00:00
|
|
|
).From(notificationProviderTable.identifier() + db.Timetravel(call.Took(ctx))).
|
|
|
|
PlaceholderFormat(sq.Dollar),
|
2022-03-07 13:22:37 +00:00
|
|
|
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 errs.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, errors.ThrowNotFound(err, "QUERY-s9ujf", "Errors.NotificationProvider.NotFound")
|
|
|
|
}
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-2liu0", "Errors.Internal")
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|