zitadel/internal/query/notification_provider.go
Livio Amstutz 958362e6c9
feat: handle instance from context (#3382)
* commander

* commander

* selber!

* move to packages

* fix(errors): implement Is interface

* test: command

* test: commands

* add init steps

* setup tenant

* add default step yaml

* possibility to set password

* merge v2 into v2-commander

* fix: rename iam command side to instance

* fix: rename iam command side to instance

* fix: rename iam command side to instance

* fix: rename iam command side to instance

* fix: search query builder can filter events in memory

* fix: filters for add member

* fix(setup): add `ExternalSecure` to config

* chore: name iam to instance

* fix: matching

* remove unsued func

* base url

* base url

* test(command): filter funcs

* test: commands

* fix: rename orgiampolicy to domain policy

* start from init

* commands

* config

* fix indexes and add constraints

* fixes

* fix: merge conflicts

* fix: protos

* fix: md files

* setup

* add deprecated org iam policy again

* typo

* fix search query

* fix filter

* Apply suggestions from code review

* remove custom org from org setup

* add todos for verification

* change apps creation

* simplify package structure

* fix error

* move preparation helper for tests

* fix unique constraints

* fix config mapping in setup

* fix error handling in encryption_keys.go

* fix projection config

* fix query from old views to projection

* fix setup of mgmt api

* set iam project and fix instance projection

* fix tokens view

* fix steps.yaml and defaults.yaml

* fix projections

* change instance context to interface

* instance interceptors and additional events in setup

* cleanup

* tests for interceptors

* fix label policy

* add todo

* single api endpoint in environment.json

Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2022-03-29 11:53:19 +02:00

126 lines
3.8 KiB
Go

package query
import (
"context"
"database/sql"
errs "errors"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/caos/zitadel/internal/api/authz"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/query/projection"
)
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,
}
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) (*DebugNotificationProvider, error) {
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, errors.ThrowInternal(err, "QUERY-f9jSf", "Errors.Query.SQLStatement")
}
row := q.client.QueryRowContext(ctx, stmt, args...)
return scan(row)
}
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 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
}
}