Skip to content

Commit

Permalink
Merge pull request #26 from VictorAvelar/customers-resource-implement…
Browse files Browse the repository at this point in the history
…ation

Customers resource implementation
  • Loading branch information
VictorAvelar authored Dec 24, 2020
2 parents 7cf23da + 6a8b178 commit 2a96709
Show file tree
Hide file tree
Showing 36 changed files with 787 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
- uses: golangci/golangci-lint-action@v2
with:
version: v1.29
args: --issues-exit-code=0 --out-format=github-actions --new=true --sort-results --skip-dirs-use-default --tests=false
args: --issues-exit-code=0 --out-format=github-actions --new=true --sort-results --skip-dirs-use-default --tests=false --presets=bugs,complexity,format,performance,style,unused
2 changes: 2 additions & 0 deletions commands/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
CustomerIDArg = "customer"
DescriptionArg = "description"
EmbedArg = "embed"
EmailArg = "email"
FieldsArg = "fields"
FromArg = "from"
IDArg = "id"
Expand All @@ -19,6 +20,7 @@ const (
MandateIDArg = "mandate"
MetadataArg = "metadata"
MethodArg = "method"
NameArg = "name"
PaymentArg = "payment"
RedirectURLArg = "redirect-to"
RPMToCountryArg = "restrict-payment-to-country"
Expand Down
363 changes: 363 additions & 0 deletions commands/customers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,363 @@
package commands

import (
"strconv"

"github.com/VictorAvelar/mollie-api-go/v2/mollie"
"github.com/VictorAvelar/mollie-cli/commands/displayers"
"github.com/VictorAvelar/mollie-cli/internal/command"
"github.com/spf13/cobra"
)

var (
customerCols = []string{
"RESOURCE",
"ID",
"MODE",
"NAME",
"EMAIL",
"LOCALE",
"METADATA",
"CREATED_AT",
}
)

// Customers creates the customers command tree.
func Customers() *command.Command {
c := command.Builder(
nil,
command.Config{
Namespace: "customers",
ShortDesc: "Operations with customers API.",
Aliases: []string{"cust", "cstm"},
},
noCols,
)

gc := command.Builder(
c,
command.Config{
Namespace: "get",
ShortDesc: "Retrieve a single customer by its ID.",
Example: "mollie customers get --id=cs_token",
Execute: RunGetCustomer,
},
customerCols,
)

command.AddFlag(gc, command.FlagConfig{
Name: IDArg,
Usage: "the customer id/token",
Required: true,
})

lc := command.Builder(
c,
command.Config{
Namespace: "list",
ShortDesc: "Retrieves all customers created.",
Example: "mollie customers list",
Execute: RunListCustomers,
},
customerCols,
)

command.AddFlag(lc, command.FlagConfig{
Name: LimitArg,
FlagType: command.IntFlag,
Usage: "the number of customers to return (with a maximum of 250)",
Default: 50,
})

command.AddFlag(lc, command.FlagConfig{
Name: FromArg,
Usage: "offset the result set to the customer with this ID",
})

cc := command.Builder(
c,
command.Config{
Namespace: "create",
Aliases: []string{"new", "add"},
ShortDesc: "Creates a simple minimal representation of a customer.",
Example: "mollie customers create --name 'test customer' --email [email protected]",
Execute: RunCreateCustomer,
},
customerCols,
)

command.AddFlag(cc, command.FlagConfig{
Name: NameArg,
Usage: "the full name of the customer",
})

command.AddFlag(cc, command.FlagConfig{
Name: EmailArg,
Usage: "the email address of the customer",
})

command.AddFlag(cc, command.FlagConfig{
Name: LocaleArg,
Usage: "allows you to preset the language to be used in the hosted payment pages shown to the consumer",
})

command.AddFlag(cc, command.FlagConfig{
Name: MetadataArg,
Usage: "provide any data you like, and we will save the data alongside the customer",
})

uc := command.Builder(
c,
command.Config{
Namespace: "update",
Aliases: []string{"edit", "change", "mutate"},
ShortDesc: "Updates an existing customer.",
Example: "mollie customers update --name 'new name'",
Execute: RunUpdateCustomer,
},
customerCols,
)

command.AddFlag(uc, command.FlagConfig{
Name: IDArg,
Usage: "the customer id/token",
Required: true,
})

command.AddFlag(uc, command.FlagConfig{
Name: NameArg,
Usage: "the full name of the customer",
})

command.AddFlag(uc, command.FlagConfig{
Name: EmailArg,
Usage: "the email address of the customer",
})

command.AddFlag(uc, command.FlagConfig{
Name: LocaleArg,
Usage: "allows you to preset the language to be used in the hosted payment pages shown to the consumer",
})

command.AddFlag(uc, command.FlagConfig{
Name: MetadataArg,
Usage: "provide any data you like, and we will save the data alongside the customer",
})

dc := command.Builder(
c,
command.Config{
Namespace: "delete",
Aliases: []string{"remove", "del"},
ShortDesc: "Deletes a customer by its ID.",
LongDesc: "Deletes a customer. WARNING! All mandates and subscriptions created for this customer will be canceled as well.",
Example: "mollie customers delete --id cs_test",
Execute: RunDeleteCustomer,
},
customerCols,
)

command.AddFlag(dc, command.FlagConfig{
Name: IDArg,
Usage: "the customer id/token",
Required: true,
})

return c
}

// RunGetCustomer retrieves a customer by its id.
func RunGetCustomer(cmd *cobra.Command, args []string) {
id := ParseStringFromFlags(cmd, IDArg)

if verbose {
PrintNonemptyFlagValue(IDArg, id)
}

c, err := API.Customers.Get(id)
if err != nil {
logger.Fatal(err)
}

if verbose {
logger.Infof("request target: %s", c.Links.Self.Href)
logger.Infof("request docs: %s", c.Links.Documentation.Href)
}

disp := displayers.MollieCustomer{
Customer: c,
}

err = command.Display(
command.FilterColumns(
parseFieldsFromFlag(cmd),
customerCols,
),
disp.KV(),
)
if err != nil {
logger.Fatal(err)
}
}

// RunListCustomers retrieves all the created customers for the account.
func RunListCustomers(cmd *cobra.Command, args []string) {
var opts mollie.ListCustomersOptions
{
opts.Limit = ParseIntFromFlags(cmd, LimitArg)
opts.From = ParseStringFromFlags(cmd, FromArg)
}

if verbose {
PrintNonemptyFlagValue(LimitArg, strconv.Itoa(opts.Limit))
PrintNonemptyFlagValue(FromArg, opts.From)
}

cl, err := API.Customers.List(&opts)
if err != nil {
logger.Fatal(err)
}

if verbose {
logger.Infof("request target: %s", cl.Links.Self.Href)
logger.Infof("request docs: %s", cl.Links.Documentation.Href)
}

disp := displayers.MollieCustomerList{
CustomersList: cl,
}

err = command.Display(
command.FilterColumns(
parseFieldsFromFlag(cmd),
customerCols,
),
disp.KV(),
)
if err != nil {
logger.Fatal(err)
}
}

// RunCreateCustomer creates a simple minimal representation of a customer.
func RunCreateCustomer(cmd *cobra.Command, args []string) {
var c mollie.Customer
{
name := ParseStringFromFlags(cmd, NameArg)
email := ParseStringFromFlags(cmd, EmailArg)
locale := ParseStringFromFlags(cmd, LocaleArg)
meta := ParseStringFromFlags(cmd, MetadataArg)

if verbose {
PrintNonemptyFlagValue(NameArg, name)
PrintNonemptyFlagValue(EmailArg, email)
PrintNonemptyFlagValue(LocaleArg, locale)
PrintNonemptyFlagValue(MetadataArg, meta)
}

c = mollie.Customer{
Email: email,
Name: name,
Locale: mollie.Locale(locale),
Metadata: map[string]interface{}{"metadata": meta},
}
}

nc, err := API.Customers.Create(c)
if err != nil {
logger.Fatal(err)
}

if verbose {
logger.Infof("request target: %s", nc.Links.Self.Href)
logger.Infof("request docs: %s", nc.Links.Documentation.Href)
}

disp := displayers.MollieCustomer{
Customer: nc,
}

err = command.Display(
command.FilterColumns(
parseFieldsFromFlag(cmd),
customerCols,
),
disp.KV(),
)
if err != nil {
logger.Fatal(err)
}
}

// RunUpdateCustomer updates an existing customer.
func RunUpdateCustomer(cmd *cobra.Command, args []string) {
var c mollie.Customer
{
id := ParseStringFromFlags(cmd, IDArg)
name := ParseStringFromFlags(cmd, NameArg)
email := ParseStringFromFlags(cmd, EmailArg)
locale := ParseStringFromFlags(cmd, LocaleArg)
meta := ParseStringFromFlags(cmd, LocaleArg)

if verbose {
PrintNonemptyFlagValue(IDArg, id)
PrintNonemptyFlagValue(NameArg, name)
PrintNonemptyFlagValue(EmailArg, email)
PrintNonemptyFlagValue(LocaleArg, locale)
PrintNonemptyFlagValue(MetadataArg, meta)
}

c = mollie.Customer{
ID: id,
Email: email,
Name: name,
Locale: mollie.Locale(locale),
Metadata: map[string]interface{}{"metadata": meta},
}
}

uc, err := API.Customers.Update(c.ID, c)
if err != nil {
logger.Fatal(err)
}

if verbose {
logger.Infof("request target: %s", uc.Links.Self.Href)
logger.Infof("request docs: %s", uc.Links.Documentation.Href)
}

disp := displayers.MollieCustomer{
Customer: uc,
}

err = command.Display(
command.FilterColumns(
parseFieldsFromFlag(cmd),
customerCols,
),
disp.KV(),
)
if err != nil {
logger.Fatal(err)
}
}

// RunDeleteCustomer removes a customer by its id/token.
func RunDeleteCustomer(cmd *cobra.Command, args []string) {
id := ParseStringFromFlags(cmd, IDArg)

if verbose {
PrintNonemptyFlagValue(IDArg, id)
}

err := API.Customers.Delete(id)
if err != nil {
logger.Fatal(err)
}

if verbose {
logger.Infof("removed customer with id/token: %s", id)
}

logger.Info("customer successfully removed")
}
Loading

0 comments on commit 2a96709

Please sign in to comment.