-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package account_api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type producerEndpoint struct { | ||
c Client | ||
producerId int | ||
} | ||
|
||
func (e producerEndpoint) GetId() int { | ||
return e.producerId | ||
} | ||
|
||
func (c Client) Producer() (*producerEndpoint, error) { | ||
r, err := c.NewAuthenticatedRequest("GET", fmt.Sprintf("%s/companies/%d/allocations", ApiUrl, c.GetActiveCompanyId()), nil) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.doRequest(r) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var allocation companyAllocation | ||
if err := json.Unmarshal(body, &allocation); err != nil { | ||
return nil, fmt.Errorf("my_profile: %v", err) | ||
} | ||
|
||
if !allocation.IsProducer { | ||
return nil, fmt.Errorf("this company is not unlocked as producer") | ||
} | ||
|
||
return &producerEndpoint{producerId: allocation.ProducerId, c: c}, nil | ||
} | ||
|
||
type companyAllocation struct { | ||
HasShops bool `json:"hasShops"` | ||
HasCommercialShop bool `json:"hasCommercialShop"` | ||
IsEducationMember bool `json:"isEducationMember"` | ||
IsPartner bool `json:"isPartner"` | ||
IsProducer bool `json:"isProducer"` | ||
ProducerId int `json:"producerId"` | ||
} | ||
|
||
func (e producerEndpoint) Profile() (*producer, error) { | ||
// Fetch the producer | ||
r, err := e.c.NewAuthenticatedRequest("GET", fmt.Sprintf("%s/producers?companyId=%d", ApiUrl, e.c.GetActiveCompanyId()), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := e.c.doRequest(r) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var producers []producer | ||
if err := json.Unmarshal(body, &producers); err != nil { | ||
return nil, fmt.Errorf("my_profile: %v", err) | ||
} | ||
|
||
for _, profile := range producers { | ||
return &profile, nil | ||
} | ||
|
||
return nil, fmt.Errorf("cannot find a profile") | ||
} | ||
|
||
type producer struct { | ||
Id int `json:"id"` | ||
Prefix string `json:"prefix"` | ||
Contract struct { | ||
Id int `json:"id"` | ||
Path string `json:"path"` | ||
} `json:"contract"` | ||
Name string `json:"name"` | ||
Details []struct { | ||
Id int `json:"id"` | ||
Locale struct { | ||
Id int `json:"id"` | ||
Name string `json:"name"` | ||
} `json:"locale"` | ||
Description string `json:"description"` | ||
} `json:"details"` | ||
Website string `json:"website"` | ||
Fixed bool `json:"fixed"` | ||
HasCancelledContract bool `json:"hasCancelledContract"` | ||
IconPath string `json:"iconPath"` | ||
IconIsSet bool `json:"iconIsSet"` | ||
ShopwareId string `json:"shopwareId"` | ||
UserId int `json:"userId"` | ||
CompanyId int `json:"companyId"` | ||
CompanyName string `json:"companyName"` | ||
SaleMail string `json:"saleMail"` | ||
SupportMail string `json:"supportMail"` | ||
RatingMail string `json:"ratingMail"` | ||
SupportedLanguages []struct { | ||
Id int `json:"id"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
} `json:"supportedLanguages"` | ||
IconUrl string `json:"iconUrl"` | ||
CancelledContract interface{} `json:"cancelledContract"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var accountCompanyProducerCmd = &cobra.Command{ | ||
Use: "company", | ||
Short: "Manage your Shopware manufacturer", | ||
} | ||
|
||
func init() { | ||
accountRootCmd.AddCommand(accountCompanyProducerCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cmd | ||
|
||
import ( | ||
termColor "github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
var accountProducerInfoCmd = &cobra.Command{ | ||
Use: "info", | ||
Short: "List information about your producer account", | ||
Long: ``, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
client := getAccountApiByConfig() | ||
|
||
p, err := client.Producer() | ||
|
||
if err != nil { | ||
termColor.Red(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
profile, err := p.Profile() | ||
if err != nil { | ||
termColor.Red(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
termColor.Blue("Name: %s", profile.Name) | ||
termColor.Blue("Prefix: %s", profile.Prefix) | ||
termColor.Blue("Website: %s", profile.Website) | ||
}, | ||
} | ||
|
||
func init() { | ||
accountCompanyProducerCmd.AddCommand(accountProducerInfoCmd) | ||
} |