2020-05-20 12:28:08 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2022-04-19 06:26:12 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
usr_model "github.com/zitadel/zitadel/internal/user/model"
|
|
|
|
"github.com/zitadel/zitadel/internal/user/repository/view/model"
|
|
|
|
"github.com/zitadel/zitadel/internal/view/repository"
|
2020-05-20 12:28:08 +00:00
|
|
|
)
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func NotifyUserByID(db *gorm.DB, table, userID, instanceID string) (*model.NotifyUser, error) {
|
2020-05-20 12:28:08 +00:00
|
|
|
user := new(model.NotifyUser)
|
2022-04-19 06:26:12 +00:00
|
|
|
query := repository.PrepareGetByQuery(table,
|
|
|
|
model.NotifyUserSearchQuery{Key: usr_model.NotifyUserSearchKeyUserID, Method: domain.SearchMethodEquals, Value: userID},
|
|
|
|
model.NotifyUserSearchQuery{Key: usr_model.NotifyUserSearchKeyInstanceID, Method: domain.SearchMethodEquals, Value: instanceID},
|
|
|
|
)
|
2020-05-20 12:28:08 +00:00
|
|
|
err := query(db, user)
|
2020-07-06 11:18:10 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-Gad31", "Errors.User.NotFound")
|
|
|
|
}
|
2020-05-20 12:28:08 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func NotifyUsersByOrgID(db *gorm.DB, table, orgID, instanceID string) ([]*model.NotifyUser, error) {
|
2020-07-07 17:31:51 +00:00
|
|
|
users := make([]*model.NotifyUser, 0)
|
|
|
|
orgIDQuery := &usr_model.NotifyUserSearchQuery{
|
|
|
|
Key: usr_model.NotifyUserSearchKeyResourceOwner,
|
2021-03-01 07:48:50 +00:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-07-07 17:31:51 +00:00
|
|
|
Value: orgID,
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
instanceIDQuery := &usr_model.NotifyUserSearchQuery{
|
|
|
|
Key: usr_model.NotifyUserSearchKeyInstanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: instanceID,
|
|
|
|
}
|
2020-07-07 17:31:51 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.NotifyUserSearchRequest{
|
2022-04-19 06:26:12 +00:00
|
|
|
Queries: []*usr_model.NotifyUserSearchQuery{orgIDQuery, instanceIDQuery},
|
2020-07-07 17:31:51 +00:00
|
|
|
})
|
|
|
|
_, err := query(db, &users)
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:28:08 +00:00
|
|
|
func PutNotifyUser(db *gorm.DB, table string, project *model.NotifyUser) error {
|
2020-06-25 06:01:13 +00:00
|
|
|
save := repository.PrepareSave(table)
|
2020-05-20 12:28:08 +00:00
|
|
|
return save(db, project)
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func DeleteNotifyUser(db *gorm.DB, table, userID, instanceID string) error {
|
|
|
|
delete := repository.PrepareDeleteByKeys(table,
|
|
|
|
repository.Key{model.UserSearchKey(usr_model.NotifyUserSearchKeyUserID), userID},
|
|
|
|
repository.Key{model.UserSearchKey(usr_model.NotifyUserSearchKeyInstanceID), instanceID},
|
|
|
|
)
|
2020-05-20 12:28:08 +00:00
|
|
|
return delete(db)
|
|
|
|
}
|