Bladeren bron

Refactoring set using account

Torkel Ödegaard 11 jaren geleden
bovenliggende
commit
5dcf6ff2d3
5 gewijzigde bestanden met toevoegingen van 64 en 32 verwijderingen
  1. 1 1
      grafana
  2. 40 31
      pkg/api/api_account.go
  3. 5 0
      pkg/models/account.go
  4. 12 0
      pkg/stores/sqlstore/accounts.go
  5. 6 0
      pkg/stores/sqlstore/accounts_test.go

+ 1 - 1
grafana

@@ -1 +1 @@
-Subproject commit 4e542d8b83844f8faa4d5ae3edab593950aaa344
+Subproject commit ad91093902bdfc0d2a87bb362a76a9057aef4361

+ 40 - 31
pkg/api/api_account.go

@@ -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")
-	//
-	// account := c.UserAccount
-	// otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id)
-	//
-	// if err != nil {
-	// 	c.JSON(500, utils.DynMap{"message": err.Error()})
-	// 	return
-	// }
-	//
-	// // validate that the account id in the list
-	// valid := false
-	// for _, other := range otherAccounts {
-	// 	if other.Id == usingAccountId {
-	// 		valid = true
-	// 	}
-	// }
-	//
-	// if !valid {
-	// 	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)
+	usingAccountId := c.ParamsInt64(":id")
+
+	if !validateUsingAccount(c.UserAccount.Id, usingAccountId) {
+		c.JsonApiErr(401, "Not a valid account", nil)
+		return
+	}
+
+	cmd := m.SetUsingAccountCommand{
+		AccountId:      c.UserAccount.Id,
+		UsingAccountId: usingAccountId,
+	}
+
+	err := bus.Dispatch(&cmd)
+	if err != nil {
+		c.JsonApiErr(500, "Failed to update account", err)
+		return
+	}
+
+	c.JsonOK("Active account changed")
 }
 }

+ 5 - 0
pkg/models/account.go

@@ -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

+ 12 - 0
pkg/stores/sqlstore/accounts.go

@@ -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)

+ 6 - 0
pkg/stores/sqlstore/accounts_test.go

@@ -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)
+				})
 			})
 			})
 		})
 		})
 	})
 	})