zitadel/internal/user/repository/view/notify_user.go
Tim Möhlmann f680dd934d
refactor: rename package errors to zerrors (#7039)
* chore: rename package errors to zerrors

* rename package errors to gerrors

* fix error related linting issues

* fix zitadel error assertion

* fix gosimple linting issues

* fix deprecated linting issues

* resolve gci linting issues

* fix import structure

---------

Co-authored-by: Elio Bischof <elio@zitadel.com>
2023-12-08 15:30:55 +01:00

57 lines
2.0 KiB
Go

package view
import (
"github.com/jinzhu/gorm"
"github.com/zitadel/zitadel/internal/domain"
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"
"github.com/zitadel/zitadel/internal/zerrors"
)
func NotifyUserByID(db *gorm.DB, table, userID, instanceID string) (*model.NotifyUser, error) {
user := new(model.NotifyUser)
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},
)
err := query(db, user)
if zerrors.IsNotFound(err) {
return nil, zerrors.ThrowNotFound(nil, "VIEW-Gad31", "Errors.User.NotFound")
}
return user, err
}
func NotifyUsersByOrgID(db *gorm.DB, table, orgID, instanceID string) ([]*model.NotifyUser, error) {
users := make([]*model.NotifyUser, 0)
orgIDQuery := &usr_model.NotifyUserSearchQuery{
Key: usr_model.NotifyUserSearchKeyResourceOwner,
Method: domain.SearchMethodEquals,
Value: orgID,
}
instanceIDQuery := &usr_model.NotifyUserSearchQuery{
Key: usr_model.NotifyUserSearchKeyInstanceID,
Method: domain.SearchMethodEquals,
Value: instanceID,
}
query := repository.PrepareSearchQuery(table, model.NotifyUserSearchRequest{
Queries: []*usr_model.NotifyUserSearchQuery{orgIDQuery, instanceIDQuery},
})
_, err := query(db, &users)
return users, err
}
func PutNotifyUser(db *gorm.DB, table string, project *model.NotifyUser) error {
save := repository.PrepareSave(table)
return save(db, project)
}
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},
)
return delete(db)
}