Use org service instead of sqlstore (#56407)

* Use org service instead of sqlstore

* Remove methods from sqlstore

* Remove commented out code

* Fix lint

* Fix lint 2
This commit is contained in:
idafurjes
2022-10-13 08:40:46 -04:00
committed by GitHub
parent b0cb02568a
commit ef651daed2
20 changed files with 164 additions and 267 deletions
-73
View File
@@ -14,79 +14,6 @@ import (
// MainOrgName is the name of the main organization.
const MainOrgName = "Main Org."
func (ss *SQLStore) SearchOrgs(ctx context.Context, query *models.SearchOrgsQuery) error {
return ss.WithDbSession(ctx, func(dbSession *DBSession) error {
query.Result = make([]*models.OrgDTO, 0)
sess := dbSession.Table("org")
if query.Query != "" {
sess.Where("name LIKE ?", query.Query+"%")
}
if query.Name != "" {
sess.Where("name=?", query.Name)
}
if len(query.Ids) > 0 {
sess.In("id", query.Ids)
}
if query.Limit > 0 {
sess.Limit(query.Limit, query.Limit*query.Page)
}
sess.Cols("id", "name")
err := sess.Find(&query.Result)
return err
})
}
func (ss *SQLStore) GetOrgById(ctx context.Context, query *models.GetOrgByIdQuery) error {
return ss.WithDbSession(ctx, func(dbSession *DBSession) error {
var org models.Org
exists, err := dbSession.ID(query.Id).Get(&org)
if err != nil {
return err
}
if !exists {
return models.ErrOrgNotFound
}
query.Result = &org
return nil
})
}
func (ss *SQLStore) GetOrgByNameHandler(ctx context.Context, query *models.GetOrgByNameQuery) error {
return ss.WithDbSession(ctx, func(dbSession *DBSession) error {
var org models.Org
exists, err := dbSession.Where("name=?", query.Name).Get(&org)
if err != nil {
return err
}
if !exists {
return models.ErrOrgNotFound
}
query.Result = &org
return nil
})
}
// GetOrgByName gets an organization by name.
func (ss *SQLStore) GetOrgByName(name string) (*models.Org, error) {
var org models.Org
exists, err := ss.engine.Where("name=?", name).Get(&org)
if err != nil {
return nil, err
}
if !exists {
return nil, models.ErrOrgNotFound
}
return &org, nil
}
func isOrgNameTaken(name string, existingId int64, sess *DBSession) (bool, error) {
// check if org name is taken
var org models.Org
-53
View File
@@ -24,59 +24,6 @@ func TestIntegrationAccountDataAccess(t *testing.T) {
t.Run("Testing Account DB Access", func(t *testing.T) {
sqlStore := InitTestDB(t)
t.Run("Given we have organizations, we can query them by IDs", func(t *testing.T) {
var err error
var cmd *models.CreateOrgCommand
ids := []int64{}
for i := 1; i < 4; i++ {
cmd = &models.CreateOrgCommand{Name: fmt.Sprint("Org #", i)}
err = sqlStore.CreateOrg(context.Background(), cmd)
require.NoError(t, err)
ids = append(ids, cmd.Result.Id)
}
query := &models.SearchOrgsQuery{Ids: ids}
err = sqlStore.SearchOrgs(context.Background(), query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 3)
})
t.Run("Given we have organizations, we can limit and paginate search", func(t *testing.T) {
sqlStore = InitTestDB(t)
for i := 1; i < 4; i++ {
cmd := &models.CreateOrgCommand{Name: fmt.Sprint("Org #", i)}
err := sqlStore.CreateOrg(context.Background(), cmd)
require.NoError(t, err)
}
t.Run("Should be able to search with defaults", func(t *testing.T) {
query := &models.SearchOrgsQuery{}
err := sqlStore.SearchOrgs(context.Background(), query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 3)
})
t.Run("Should be able to limit search", func(t *testing.T) {
query := &models.SearchOrgsQuery{Limit: 1}
err := sqlStore.SearchOrgs(context.Background(), query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 1)
})
t.Run("Should be able to limit and paginate search", func(t *testing.T) {
query := &models.SearchOrgsQuery{Limit: 2, Page: 1}
err := sqlStore.SearchOrgs(context.Background(), query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 1)
})
})
t.Run("Given single org mode", func(t *testing.T) {
sqlStore.Cfg.AutoAssignOrg = true
sqlStore.Cfg.AutoAssignOrgId = 1
+4 -16
View File
@@ -80,39 +80,27 @@ func populateDB(t *testing.T, sqlStore *SQLStore) {
users[i] = *user
}
// get 1st user's organisation
getOrgByIdQuery := &models.GetOrgByIdQuery{Id: users[0].OrgID}
err := sqlStore.GetOrgById(context.Background(), getOrgByIdQuery)
require.NoError(t, err)
orga := getOrgByIdQuery.Result
// add 2nd user as editor
cmd := &models.AddOrgUserCommand{
OrgId: orga.Id,
OrgId: users[0].OrgID,
UserId: users[1].ID,
Role: org.RoleEditor,
}
err = sqlStore.AddOrgUser(context.Background(), cmd)
err := sqlStore.AddOrgUser(context.Background(), cmd)
require.NoError(t, err)
// add 3rd user as viewer
cmd = &models.AddOrgUserCommand{
OrgId: orga.Id,
OrgId: users[0].OrgID,
UserId: users[2].ID,
Role: org.RoleViewer,
}
err = sqlStore.AddOrgUser(context.Background(), cmd)
require.NoError(t, err)
// get 2nd user's organisation
getOrgByIdQuery = &models.GetOrgByIdQuery{Id: users[1].OrgID}
err = sqlStore.GetOrgById(context.Background(), getOrgByIdQuery)
require.NoError(t, err)
orga = getOrgByIdQuery.Result
// add 1st user as admin
cmd = &models.AddOrgUserCommand{
OrgId: orga.Id,
OrgId: users[1].OrgID,
UserId: users[0].ID,
Role: org.RoleAdmin,
}
-4
View File
@@ -18,9 +18,6 @@ type Store interface {
GetDialect() migrator.Dialect
GetDBType() core.DbType
GetSystemStats(ctx context.Context, query *models.GetSystemStatsQuery) error
GetOrgByName(name string) (*models.Org, error)
GetOrgById(context.Context, *models.GetOrgByIdQuery) error
GetOrgByNameHandler(ctx context.Context, query *models.GetOrgByNameQuery) error
CreateUser(ctx context.Context, cmd user.CreateUserCommand) (*user.User, error)
GetUserProfile(ctx context.Context, query *models.GetUserProfileQuery) error
GetSignedInUser(ctx context.Context, query *models.GetSignedInUserQuery) error
@@ -40,6 +37,5 @@ type Store interface {
Reset() error
Quote(value string) string
GetDBHealthQuery(ctx context.Context, query *models.GetDBHealthQuery) error
SearchOrgs(ctx context.Context, query *models.SearchOrgsQuery) error
GetSqlxSession() *session.SessionDB
}