fix(notify): notify user in projection (#3889)

* start implement notify user in projection

* fix(stmt): add copy to multi stmt

* use projections for notify users

* feat: notifications from projections

* feat: notifications from projections

* cleanup

* pre-release

* fix tests

* fix types

* fix command

* fix queryNotifyUser

* fix: build version

* fix: HumanPasswordlessInitCodeSent

Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
This commit is contained in:
Livio Spring
2022-07-06 14:09:49 +02:00
committed by GitHub
parent d15a15c809
commit a1d404291d
46 changed files with 2018 additions and 1839 deletions

View File

@@ -17,43 +17,43 @@ import (
)
var (
userQuery = `SELECT projections.users.id,` +
` projections.users.creation_date,` +
` projections.users.change_date,` +
` projections.users.resource_owner,` +
` projections.users.sequence,` +
` projections.users.state,` +
` projections.users.type,` +
` projections.users.username,` +
userQuery = `SELECT projections.users2.id,` +
` projections.users2.creation_date,` +
` projections.users2.change_date,` +
` projections.users2.resource_owner,` +
` projections.users2.sequence,` +
` projections.users2.state,` +
` projections.users2.type,` +
` projections.users2.username,` +
` login_names.loginnames,` +
` preferred_login_name.login_name,` +
` projections.users_humans.user_id,` +
` projections.users_humans.first_name,` +
` projections.users_humans.last_name,` +
` projections.users_humans.nick_name,` +
` projections.users_humans.display_name,` +
` projections.users_humans.preferred_language,` +
` projections.users_humans.gender,` +
` projections.users_humans.avatar_key,` +
` projections.users_humans.email,` +
` projections.users_humans.is_email_verified,` +
` projections.users_humans.phone,` +
` projections.users_humans.is_phone_verified,` +
` projections.users_machines.user_id,` +
` projections.users_machines.name,` +
` projections.users_machines.description` +
` FROM projections.users` +
` LEFT JOIN projections.users_humans ON projections.users.id = projections.users_humans.user_id` +
` LEFT JOIN projections.users_machines ON projections.users.id = projections.users_machines.user_id` +
` projections.users2_humans.user_id,` +
` projections.users2_humans.first_name,` +
` projections.users2_humans.last_name,` +
` projections.users2_humans.nick_name,` +
` projections.users2_humans.display_name,` +
` projections.users2_humans.preferred_language,` +
` projections.users2_humans.gender,` +
` projections.users2_humans.avatar_key,` +
` projections.users2_humans.email,` +
` projections.users2_humans.is_email_verified,` +
` projections.users2_humans.phone,` +
` projections.users2_humans.is_phone_verified,` +
` projections.users2_machines.user_id,` +
` projections.users2_machines.name,` +
` projections.users2_machines.description` +
` FROM projections.users2` +
` LEFT JOIN projections.users2_humans ON projections.users2.id = projections.users2_humans.user_id` +
` LEFT JOIN projections.users2_machines ON projections.users2.id = projections.users2_machines.user_id` +
` LEFT JOIN` +
` (SELECT login_names.user_id, ARRAY_AGG(login_names.login_name) as loginnames` +
` FROM projections.login_names as login_names` +
` WHERE login_names.instance_id = $1` +
` GROUP BY login_names.user_id) as login_names` +
` on login_names.user_id = projections.users.id` +
` on login_names.user_id = projections.users2.id` +
` LEFT JOIN` +
` (SELECT preferred_login_name.user_id, preferred_login_name.login_name FROM projections.login_names as preferred_login_name WHERE preferred_login_name.instance_id = $2 AND preferred_login_name.is_primary = $3) as preferred_login_name` +
` on preferred_login_name.user_id = projections.users.id`
` on preferred_login_name.user_id = projections.users2.id`
userCols = []string{
"id",
"creation_date",
@@ -83,21 +83,21 @@ var (
"name",
"description",
}
profileQuery = `SELECT projections.users.id,` +
` projections.users.creation_date,` +
` projections.users.change_date,` +
` projections.users.resource_owner,` +
` projections.users.sequence,` +
` projections.users_humans.user_id,` +
` projections.users_humans.first_name,` +
` projections.users_humans.last_name,` +
` projections.users_humans.nick_name,` +
` projections.users_humans.display_name,` +
` projections.users_humans.preferred_language,` +
` projections.users_humans.gender,` +
` projections.users_humans.avatar_key` +
` FROM projections.users` +
` LEFT JOIN projections.users_humans ON projections.users.id = projections.users_humans.user_id`
profileQuery = `SELECT projections.users2.id,` +
` projections.users2.creation_date,` +
` projections.users2.change_date,` +
` projections.users2.resource_owner,` +
` projections.users2.sequence,` +
` projections.users2_humans.user_id,` +
` projections.users2_humans.first_name,` +
` projections.users2_humans.last_name,` +
` projections.users2_humans.nick_name,` +
` projections.users2_humans.display_name,` +
` projections.users2_humans.preferred_language,` +
` projections.users2_humans.gender,` +
` projections.users2_humans.avatar_key` +
` FROM projections.users2` +
` LEFT JOIN projections.users2_humans ON projections.users2.id = projections.users2_humans.user_id`
profileCols = []string{
"id",
"creation_date",
@@ -113,16 +113,16 @@ var (
"gender",
"avatar_key",
}
emailQuery = `SELECT projections.users.id,` +
` projections.users.creation_date,` +
` projections.users.change_date,` +
` projections.users.resource_owner,` +
` projections.users.sequence,` +
` projections.users_humans.user_id,` +
` projections.users_humans.email,` +
` projections.users_humans.is_email_verified` +
` FROM projections.users` +
` LEFT JOIN projections.users_humans ON projections.users.id = projections.users_humans.user_id`
emailQuery = `SELECT projections.users2.id,` +
` projections.users2.creation_date,` +
` projections.users2.change_date,` +
` projections.users2.resource_owner,` +
` projections.users2.sequence,` +
` projections.users2_humans.user_id,` +
` projections.users2_humans.email,` +
` projections.users2_humans.is_email_verified` +
` FROM projections.users2` +
` LEFT JOIN projections.users2_humans ON projections.users2.id = projections.users2_humans.user_id`
emailCols = []string{
"id",
"creation_date",
@@ -133,16 +133,16 @@ var (
"email",
"is_email_verified",
}
phoneQuery = `SELECT projections.users.id,` +
` projections.users.creation_date,` +
` projections.users.change_date,` +
` projections.users.resource_owner,` +
` projections.users.sequence,` +
` projections.users_humans.user_id,` +
` projections.users_humans.phone,` +
` projections.users_humans.is_phone_verified` +
` FROM projections.users` +
` LEFT JOIN projections.users_humans ON projections.users.id = projections.users_humans.user_id`
phoneQuery = `SELECT projections.users2.id,` +
` projections.users2.creation_date,` +
` projections.users2.change_date,` +
` projections.users2.resource_owner,` +
` projections.users2.sequence,` +
` projections.users2_humans.user_id,` +
` projections.users2_humans.phone,` +
` projections.users2_humans.is_phone_verified` +
` FROM projections.users2` +
` LEFT JOIN projections.users2_humans ON projections.users2.id = projections.users2_humans.user_id`
phoneCols = []string{
"id",
"creation_date",
@@ -153,15 +153,14 @@ var (
"phone",
"is_phone_verified",
}
userUniqueQuery = `SELECT projections.users.id,` +
` projections.users.state,` +
` projections.users.username,` +
` projections.users_humans.user_id,` +
` projections.users_humans.email,` +
` projections.users_humans.is_email_verified` +
` FROM projections.users` +
` LEFT JOIN projections.users_humans ON projections.users.id = projections.users_humans.user_id`
userUniqueQuery = `SELECT projections.users2.id,` +
` projections.users2.state,` +
` projections.users2.username,` +
` projections.users2_humans.user_id,` +
` projections.users2_humans.email,` +
` projections.users2_humans.is_email_verified` +
` FROM projections.users2` +
` LEFT JOIN projections.users2_humans ON projections.users2.id = projections.users2_humans.user_id`
userUniqueCols = []string{
"id",
"state",
@@ -170,43 +169,107 @@ var (
"email",
"is_email_verified",
}
usersQuery = `SELECT projections.users.id,` +
` projections.users.creation_date,` +
` projections.users.change_date,` +
` projections.users.resource_owner,` +
` projections.users.sequence,` +
` projections.users.state,` +
` projections.users.type,` +
` projections.users.username,` +
notifyUserQuery = `SELECT projections.users2.id,` +
` projections.users2.creation_date,` +
` projections.users2.change_date,` +
` projections.users2.resource_owner,` +
` projections.users2.sequence,` +
` projections.users2.state,` +
` projections.users2.type,` +
` projections.users2.username,` +
` login_names.loginnames,` +
` preferred_login_name.login_name,` +
` projections.users_humans.user_id,` +
` projections.users_humans.first_name,` +
` projections.users_humans.last_name,` +
` projections.users_humans.nick_name,` +
` projections.users_humans.display_name,` +
` projections.users_humans.preferred_language,` +
` projections.users_humans.gender,` +
` projections.users_humans.avatar_key,` +
` projections.users_humans.email,` +
` projections.users_humans.is_email_verified,` +
` projections.users_humans.phone,` +
` projections.users_humans.is_phone_verified,` +
` projections.users_machines.user_id,` +
` projections.users_machines.name,` +
` projections.users_machines.description,` +
` projections.users2_humans.user_id,` +
` projections.users2_humans.first_name,` +
` projections.users2_humans.last_name,` +
` projections.users2_humans.nick_name,` +
` projections.users2_humans.display_name,` +
` projections.users2_humans.preferred_language,` +
` projections.users2_humans.gender,` +
` projections.users2_humans.avatar_key,` +
` projections.users2_notifications.user_id,` +
` projections.users2_notifications.last_email,` +
` projections.users2_notifications.verified_email,` +
` projections.users2_notifications.last_phone,` +
` projections.users2_notifications.verified_phone,` +
` projections.users2_notifications.password_set` +
` FROM projections.users2` +
` LEFT JOIN projections.users2_humans ON projections.users2.id = projections.users2_humans.user_id` +
` LEFT JOIN projections.users2_notifications ON projections.users2.id = projections.users2_notifications.user_id` +
` LEFT JOIN` +
` (SELECT login_names.user_id, ARRAY_AGG(login_names.login_name) as loginnames` +
` FROM projections.login_names as login_names` +
` WHERE login_names.instance_id = $1` +
` GROUP BY login_names.user_id) as login_names` +
` on login_names.user_id = projections.users2.id` +
` LEFT JOIN` +
` (SELECT preferred_login_name.user_id, preferred_login_name.login_name FROM projections.login_names as preferred_login_name WHERE preferred_login_name.instance_id = $2 AND preferred_login_name.is_primary = $3) as preferred_login_name` +
` on preferred_login_name.user_id = projections.users2.id`
notifyUserCols = []string{
"id",
"creation_date",
"change_date",
"resource_owner",
"sequence",
"state",
"type",
"username",
"loginnames",
"login_name",
//human
"user_id",
"first_name",
"last_name",
"nick_name",
"display_name",
"preferred_language",
"gender",
"avatar_key",
//machine
"user_id",
"last_email",
"verified_email",
"last_phone",
"verified_phone",
"password_set",
}
usersQuery = `SELECT projections.users2.id,` +
` projections.users2.creation_date,` +
` projections.users2.change_date,` +
` projections.users2.resource_owner,` +
` projections.users2.sequence,` +
` projections.users2.state,` +
` projections.users2.type,` +
` projections.users2.username,` +
` login_names.loginnames,` +
` preferred_login_name.login_name,` +
` projections.users2_humans.user_id,` +
` projections.users2_humans.first_name,` +
` projections.users2_humans.last_name,` +
` projections.users2_humans.nick_name,` +
` projections.users2_humans.display_name,` +
` projections.users2_humans.preferred_language,` +
` projections.users2_humans.gender,` +
` projections.users2_humans.avatar_key,` +
` projections.users2_humans.email,` +
` projections.users2_humans.is_email_verified,` +
` projections.users2_humans.phone,` +
` projections.users2_humans.is_phone_verified,` +
` projections.users2_machines.user_id,` +
` projections.users2_machines.name,` +
` projections.users2_machines.description,` +
` COUNT(*) OVER ()` +
` FROM projections.users` +
` LEFT JOIN projections.users_humans ON projections.users.id = projections.users_humans.user_id` +
` LEFT JOIN projections.users_machines ON projections.users.id = projections.users_machines.user_id` +
` FROM projections.users2` +
` LEFT JOIN projections.users2_humans ON projections.users2.id = projections.users2_humans.user_id` +
` LEFT JOIN projections.users2_machines ON projections.users2.id = projections.users2_machines.user_id` +
` LEFT JOIN` +
` (SELECT login_names.user_id, ARRAY_AGG(login_names.login_name) as loginnames` +
` FROM projections.login_names as login_names` +
` GROUP BY login_names.user_id) as login_names` +
` on login_names.user_id = projections.users.id` +
` on login_names.user_id = projections.users2.id` +
` LEFT JOIN` +
` (SELECT preferred_login_name.user_id, preferred_login_name.login_name FROM projections.login_names as preferred_login_name WHERE preferred_login_name.is_primary = $1) as preferred_login_name` +
` on preferred_login_name.user_id = projections.users.id`
` on preferred_login_name.user_id = projections.users2.id`
usersCols = []string{
"id",
"creation_date",
@@ -760,6 +823,155 @@ func Test_UserPrepares(t *testing.T) {
},
object: nil,
},
{
name: "prepareNotifyUserQuery no result",
prepare: func() (sq.SelectBuilder, func(*sql.Row) (*NotifyUser, error)) {
return prepareNotifyUserQuery("instanceID")
},
want: want{
sqlExpectations: mockQuery(
regexp.QuoteMeta(notifyUserQuery),
nil,
nil,
),
err: func(err error) (error, bool) {
if !errs.IsNotFound(err) {
return fmt.Errorf("err should be zitadel.NotFoundError got: %w", err), false
}
return nil, true
},
},
object: (*NotifyUser)(nil),
},
{
name: "prepareNotifyUserQuery notify found",
prepare: func() (sq.SelectBuilder, func(*sql.Row) (*NotifyUser, error)) {
return prepareNotifyUserQuery("instanceID")
},
want: want{
sqlExpectations: mockQuery(
regexp.QuoteMeta(notifyUserQuery),
notifyUserCols,
[]driver.Value{
"id",
testNow,
testNow,
"resource_owner",
uint64(20211108),
domain.UserStateActive,
domain.UserTypeHuman,
"username",
pq.StringArray{"login_name1", "login_name2"},
"login_name1",
//human
"id",
"first_name",
"last_name",
"nick_name",
"display_name",
"de",
domain.GenderUnspecified,
"avatar_key",
//notify
"id",
"lastEmail",
"verifiedEmail",
"lastPhone",
"verifiedPhone",
true,
},
),
},
object: &NotifyUser{
ID: "id",
CreationDate: testNow,
ChangeDate: testNow,
ResourceOwner: "resource_owner",
Sequence: 20211108,
State: domain.UserStateActive,
Type: domain.UserTypeHuman,
Username: "username",
LoginNames: []string{"login_name1", "login_name2"},
PreferredLoginName: "login_name1",
FirstName: "first_name",
LastName: "last_name",
NickName: "nick_name",
DisplayName: "display_name",
AvatarKey: "avatar_key",
PreferredLanguage: language.German,
Gender: domain.GenderUnspecified,
LastEmail: "lastEmail",
VerifiedEmail: "verifiedEmail",
LastPhone: "lastPhone",
VerifiedPhone: "verifiedPhone",
PasswordSet: true,
},
},
{
name: "prepareNotifyUserQuery not notify found (error)",
prepare: func() (sq.SelectBuilder, func(*sql.Row) (*NotifyUser, error)) {
return prepareNotifyUserQuery("instanceID")
},
want: want{
sqlExpectations: mockQuery(
regexp.QuoteMeta(notifyUserQuery),
notifyUserCols,
[]driver.Value{
"id",
testNow,
testNow,
"resource_owner",
uint64(20211108),
domain.UserStateActive,
domain.UserTypeHuman,
"username",
pq.StringArray{"login_name1", "login_name2"},
"login_name1",
//human
"id",
"first_name",
"last_name",
"nick_name",
"display_name",
"de",
domain.GenderUnspecified,
"avatar_key",
nil,
nil,
nil,
nil,
nil,
nil,
},
),
err: func(err error) (error, bool) {
if !errs.IsPreconditionFailed(err) {
return fmt.Errorf("err should be zitadel.PredconditionError got: %w", err), false
}
return nil, true
},
},
object: (*NotifyUser)(nil),
},
{
name: "prepareNotifyUserQuery sql err",
prepare: func() (sq.SelectBuilder, func(*sql.Row) (*NotifyUser, error)) {
return prepareNotifyUserQuery("instanceID")
},
want: want{
sqlExpectations: mockQueryErr(
regexp.QuoteMeta(notifyUserQuery),
sql.ErrConnDone,
),
err: func(err error) (error, bool) {
if !errors.Is(err, sql.ErrConnDone) {
return fmt.Errorf("err should be sql.ErrConnDone got: %w", err), false
}
return nil, true
},
},
object: nil,
},
{
name: "prepareUsersQuery no result",
prepare: prepareUsersQuery,