Refactoring set using account
This commit is contained in:
+1
-1
Submodule grafana updated: 4e542d8b83...ad91093902
+40
-31
@@ -76,36 +76,45 @@ func GetOtherAccounts(c *middleware.Context) {
|
|||||||
c.JSON(200, result)
|
c.JSON(200, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateUsingAccount(accountId int64, otherId int64) bool {
|
||||||
|
if accountId == otherId {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
query := m.GetOtherAccountsQuery{AccountId: accountId}
|
||||||
|
err := bus.Dispatch(&query)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// validate that the account id in the list
|
||||||
|
valid := false
|
||||||
|
for _, other := range query.Result {
|
||||||
|
if other.Id == otherId {
|
||||||
|
valid = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return valid
|
||||||
|
}
|
||||||
|
|
||||||
func SetUsingAccount(c *middleware.Context) {
|
func SetUsingAccount(c *middleware.Context) {
|
||||||
// usingAccountId := c.ParamsInt64(":id")
|
usingAccountId := c.ParamsInt64(":id")
|
||||||
//
|
|
||||||
// account := c.UserAccount
|
if !validateUsingAccount(c.UserAccount.Id, usingAccountId) {
|
||||||
// otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id)
|
c.JsonApiErr(401, "Not a valid account", nil)
|
||||||
//
|
return
|
||||||
// if err != nil {
|
}
|
||||||
// c.JSON(500, utils.DynMap{"message": err.Error()})
|
|
||||||
// return
|
cmd := m.SetUsingAccountCommand{
|
||||||
// }
|
AccountId: c.UserAccount.Id,
|
||||||
//
|
UsingAccountId: usingAccountId,
|
||||||
// // validate that the account id in the list
|
}
|
||||||
// valid := false
|
|
||||||
// for _, other := range otherAccounts {
|
err := bus.Dispatch(&cmd)
|
||||||
// if other.Id == usingAccountId {
|
if err != nil {
|
||||||
// valid = true
|
c.JsonApiErr(500, "Failed to update account", err)
|
||||||
// }
|
return
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// if !valid {
|
c.JsonOK("Active account changed")
|
||||||
// c.Status(401)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// account.UsingAccountId = usingAccountId
|
|
||||||
// err = m.SaveAccount(account)
|
|
||||||
// if err != nil {
|
|
||||||
// c.JSON(500, utils.DynMap{"message": err.Error()})
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// c.Status(204)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,6 +64,11 @@ type CreateAccountCommand struct {
|
|||||||
Result Account `json:"-"`
|
Result Account `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SetUsingAccountCommand struct {
|
||||||
|
AccountId int64
|
||||||
|
UsingAccountId int64
|
||||||
|
}
|
||||||
|
|
||||||
// returns a view projection
|
// returns a view projection
|
||||||
type GetAccountInfoQuery struct {
|
type GetAccountInfoQuery struct {
|
||||||
Id int64
|
Id int64
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ func init() {
|
|||||||
bus.AddHandler("sql", AddCollaborator)
|
bus.AddHandler("sql", AddCollaborator)
|
||||||
bus.AddHandler("sql", GetOtherAccounts)
|
bus.AddHandler("sql", GetOtherAccounts)
|
||||||
bus.AddHandler("sql", CreateAccount)
|
bus.AddHandler("sql", CreateAccount)
|
||||||
|
bus.AddHandler("sql", SetUsingAccount)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||||
@@ -33,6 +34,17 @@ func CreateAccount(cmd *m.CreateAccountCommand) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
|
||||||
|
return inTransaction(func(sess *xorm.Session) error {
|
||||||
|
account := m.Account{}
|
||||||
|
sess.Id(cmd.AccountId).Get(&account)
|
||||||
|
|
||||||
|
account.UsingAccountId = cmd.UsingAccountId
|
||||||
|
_, err := sess.Id(account.Id).Update(&account)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func GetAccountInfo(query *m.GetAccountInfoQuery) error {
|
func GetAccountInfo(query *m.GetAccountInfoQuery) error {
|
||||||
var account m.Account
|
var account m.Account
|
||||||
has, err := x.Id(query.Id).Get(&account)
|
has, err := x.Id(query.Id).Get(&account)
|
||||||
|
|||||||
@@ -61,6 +61,12 @@ func TestAccountDataAccess(t *testing.T) {
|
|||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(query.Result[0].Email, ShouldEqual, "ac1@test.com")
|
So(query.Result[0].Email, ShouldEqual, "ac1@test.com")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("Can set using account", func() {
|
||||||
|
cmd := m.SetUsingAccountCommand{AccountId: ac2.Id, UsingAccountId: ac1.Id}
|
||||||
|
err := SetUsingAccount(&cmd)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user