Skip to content

Commit

Permalink
feat: Implement account:company:use
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Jan 10, 2022
1 parent 6f8c00d commit 22d087d
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 9 deletions.
36 changes: 36 additions & 0 deletions account-api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,39 @@ type membership struct {
} `json:"permissions"`
} `json:"roles"`
}

type changeMembershipRequest struct {
SelectedMembership struct {
Id int `json:"id"`
} `json:"membership"`
}

func (c Client) ChangeActiveMembership(selected *membership) error {
s, err := json.Marshal(changeMembershipRequest{SelectedMembership: struct {
Id int `json:"id"`
}(struct{ Id int }{Id: selected.Id})})

if err != nil {
return fmt.Errorf("ChangeActiveMembership: %v", err)
}

r, err := c.NewAuthenticatedRequest("POST", fmt.Sprintf("%s/account/%d/memberships/change", ApiUrl, c.GetUserId()), bytes.NewBuffer(s))

if err != nil {
return err
}

resp, err := http.DefaultClient.Do(r)

if err != nil {
return err
}

if resp.StatusCode == 200 {
c.activeMembership = selected

return nil
}

return fmt.Errorf("could not change active membership due http error %d", resp.StatusCode)
}
45 changes: 45 additions & 0 deletions cmd/account_company_use.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
termColor "github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
"strconv"
)

var accountCompanyUseCmd = &cobra.Command{
Use: "account:company:use [companyId]",
Short: "Use another company for your Account",
Args: cobra.MinimumNArgs(1),
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
companyId, err := strconv.Atoi(args[0])

if err != nil {
log.Fatalln(err)
}

client := getAccountApiByConfig()

for _, membership := range *client.GetMemberships() {
if membership.Company.Id == companyId {
viper.Set(ConfigAccountCompany, companyId)
err := viper.WriteConfig()

if err != nil {
log.Fatalln(err)
}

termColor.Green("Successfully changed your company to %s (%d)", membership.Company.Name, membership.Company.CustomerNumber)
return
}
}

termColor.Red("Could noy find company by id %s", companyId)
},
}

func init() {
rootCmd.AddCommand(accountCompanyUseCmd)
}
19 changes: 14 additions & 5 deletions cmd/account_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ var loginCmd = &cobra.Command{
Short: "Login into your Shopware Account",
Long: "",
Run: func(cmd *cobra.Command, args []string) {
email := viper.GetString("account_email")
password := viper.GetString("account_password")
email := viper.GetString(ConfigAccountUser)
password := viper.GetString(ConfigAccountPassword)
newCredentials := false

if len(email) == 0 || len(password) == 0 {
email, password = askUserForEmailAndPassword()
newCredentials = true

viper.Set("account_email", email)
viper.Set("account_password", password)
viper.Set(ConfigAccountUser, email)
viper.Set(ConfigAccountPassword, password)
} else {
termColor.Blue("Using existing credentials. Use account:logout to logout")
}
Expand All @@ -38,8 +38,17 @@ var loginCmd = &cobra.Command{
os.Exit(1)
}

if viper.GetInt(ConfigAccountCompany) > 0 {
err = changeApiMembership(client, viper.GetInt(ConfigAccountCompany))

if err != nil {
termColor.Red(err.Error())
os.Exit(1)
}
}

if newCredentials {
viper.Set("account_membership", client.GetActiveCompanyId())
viper.Set(ConfigAccountCompany, client.GetActiveCompanyId())
err := viper.WriteConfig()

if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/account_logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ var logoutCmd = &cobra.Command{
Short: "Logout from Shopware Account",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
viper.Set("account_email", "")
viper.Set("account_password", "")
viper.Set(ConfigAccountUser, "")
viper.Set(ConfigAccountPassword, "")
viper.Set(ConfigAccountCompany, "")

err := viper.WriteConfig()

Expand Down
34 changes: 32 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
termColor "github.com/fatih/color"
"os"
accountApi "shopware-cli/account-api"
Expand All @@ -9,6 +10,12 @@ import (
"github.com/spf13/viper"
)

const (
ConfigAccountUser = "account.email"
ConfigAccountPassword = "account.password"
ConfigAccountCompany = "account.company"
)

var cfgFile string

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -53,8 +60,8 @@ func initConfig() {
}

func getAccountApiByConfig() *accountApi.Client {
email := viper.GetString("account_email")
password := viper.GetString("account_password")
email := viper.GetString(ConfigAccountUser)
password := viper.GetString(ConfigAccountPassword)

client, err := accountApi.NewApi(accountApi.LoginRequest{Email: email, Password: password})

Expand All @@ -63,5 +70,28 @@ func getAccountApiByConfig() *accountApi.Client {
os.Exit(1)
}

companyId := viper.GetInt(ConfigAccountCompany)

err = changeApiMembership(client, companyId)

if err != nil {
termColor.Red(err.Error())
os.Exit(1)
}

return client
}

func changeApiMembership(client *accountApi.Client, companyId int) error {
if companyId == 0 || client.GetActiveCompanyId() == companyId {
return nil
}

for _, membership := range *client.GetMemberships() {
if membership.Company.Id == companyId {
return client.ChangeActiveMembership(&membership)
}
}

return fmt.Errorf("could not find configured company with id %d", companyId)
}

0 comments on commit 22d087d

Please sign in to comment.