2021-12-16 14:21: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
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2021-12-16 14:21:37 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/query/projection"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MailTemplate struct {
|
|
|
|
AggregateID string
|
|
|
|
Sequence uint64
|
|
|
|
CreationDate time.Time
|
|
|
|
ChangeDate time.Time
|
|
|
|
State domain.PolicyState
|
|
|
|
|
|
|
|
Template []byte
|
|
|
|
IsDefault bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
mailTemplateTable = table{
|
|
|
|
name: projection.MailTemplateTable,
|
|
|
|
}
|
|
|
|
MailTemplateColAggregateID = Column{
|
|
|
|
name: projection.MailTemplateAggregateIDCol,
|
|
|
|
table: mailTemplateTable,
|
|
|
|
}
|
2022-03-23 08:02:39 +00:00
|
|
|
MailTemplateColInstanceID = Column{
|
|
|
|
name: projection.MailTemplateInstanceIDCol,
|
|
|
|
table: mailTemplateTable,
|
|
|
|
}
|
2021-12-16 14:21:37 +00:00
|
|
|
MailTemplateColSequence = Column{
|
|
|
|
name: projection.MailTemplateSequenceCol,
|
|
|
|
table: mailTemplateTable,
|
|
|
|
}
|
|
|
|
MailTemplateColCreationDate = Column{
|
|
|
|
name: projection.MailTemplateCreationDateCol,
|
|
|
|
table: mailTemplateTable,
|
|
|
|
}
|
|
|
|
MailTemplateColChangeDate = Column{
|
|
|
|
name: projection.MailTemplateChangeDateCol,
|
|
|
|
table: mailTemplateTable,
|
|
|
|
}
|
|
|
|
MailTemplateColTemplate = Column{
|
|
|
|
name: projection.MailTemplateTemplateCol,
|
|
|
|
table: mailTemplateTable,
|
|
|
|
}
|
|
|
|
MailTemplateColIsDefault = Column{
|
|
|
|
name: projection.MailTemplateIsDefaultCol,
|
|
|
|
table: mailTemplateTable,
|
|
|
|
}
|
|
|
|
MailTemplateColState = Column{
|
|
|
|
name: projection.MailTemplateStateCol,
|
|
|
|
table: mailTemplateTable,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (q *Queries) MailTemplateByOrg(ctx context.Context, orgID string) (*MailTemplate, error) {
|
|
|
|
stmt, scan := prepareMailTemplateQuery()
|
|
|
|
query, args, err := stmt.Where(
|
2022-03-23 08:02:39 +00:00
|
|
|
sq.And{
|
2021-12-16 14:21:37 +00:00
|
|
|
sq.Eq{
|
2022-03-29 09:53:19 +00:00
|
|
|
MailTemplateColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
2021-12-16 14:21:37 +00:00
|
|
|
},
|
2022-03-23 08:02:39 +00:00
|
|
|
sq.Or{
|
|
|
|
sq.Eq{
|
|
|
|
MailTemplateColAggregateID.identifier(): orgID,
|
|
|
|
},
|
|
|
|
sq.Eq{
|
|
|
|
MailTemplateColAggregateID.identifier(): domain.IAMID,
|
|
|
|
},
|
2021-12-16 14:21:37 +00:00
|
|
|
},
|
|
|
|
}).
|
|
|
|
OrderBy(MailTemplateColIsDefault.identifier()).
|
|
|
|
Limit(1).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-m0sJg", "Errors.Query.SQLStatement")
|
|
|
|
}
|
|
|
|
|
|
|
|
row := q.client.QueryRowContext(ctx, query, args...)
|
|
|
|
return scan(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) DefaultMailTemplate(ctx context.Context) (*MailTemplate, error) {
|
|
|
|
stmt, scan := prepareMailTemplateQuery()
|
|
|
|
query, args, err := stmt.Where(sq.Eq{
|
2022-02-16 15:49:17 +00:00
|
|
|
MailTemplateColAggregateID.identifier(): domain.IAMID,
|
2022-03-29 09:53:19 +00:00
|
|
|
MailTemplateColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
2021-12-16 14:21:37 +00:00
|
|
|
}).
|
|
|
|
OrderBy(MailTemplateColIsDefault.identifier()).
|
|
|
|
Limit(1).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-2m0fH", "Errors.Query.SQLStatement")
|
|
|
|
}
|
|
|
|
|
|
|
|
row := q.client.QueryRowContext(ctx, query, args...)
|
|
|
|
return scan(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareMailTemplateQuery() (sq.SelectBuilder, func(*sql.Row) (*MailTemplate, error)) {
|
|
|
|
return sq.Select(
|
|
|
|
MailTemplateColAggregateID.identifier(),
|
|
|
|
MailTemplateColSequence.identifier(),
|
|
|
|
MailTemplateColCreationDate.identifier(),
|
|
|
|
MailTemplateColChangeDate.identifier(),
|
|
|
|
MailTemplateColTemplate.identifier(),
|
|
|
|
MailTemplateColIsDefault.identifier(),
|
|
|
|
MailTemplateColState.identifier(),
|
|
|
|
).
|
|
|
|
From(mailTemplateTable.identifier()).PlaceholderFormat(sq.Dollar),
|
|
|
|
func(row *sql.Row) (*MailTemplate, error) {
|
|
|
|
policy := new(MailTemplate)
|
|
|
|
err := row.Scan(
|
|
|
|
&policy.AggregateID,
|
|
|
|
&policy.Sequence,
|
|
|
|
&policy.CreationDate,
|
|
|
|
&policy.ChangeDate,
|
|
|
|
&policy.Template,
|
|
|
|
&policy.IsDefault,
|
|
|
|
&policy.State,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if errs.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, errors.ThrowNotFound(err, "QUERY-2NO0g", "Errors.MailTemplate.NotFound")
|
|
|
|
}
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-4Nisf", "Errors.Internal")
|
|
|
|
}
|
|
|
|
return policy, nil
|
|
|
|
}
|
|
|
|
}
|