Skip to content

Commit

Permalink
Merge pull request #4 from LucasRouckhout/improvement/allow_updating_…
Browse files Browse the repository at this point in the history
…invoices

Improvement/allow updating invoices
  • Loading branch information
koen-serry authored May 27, 2024
2 parents 752a23f + 9e83f6d commit 4548ba9
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
3 changes: 3 additions & 0 deletions example/simple.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build exclude
// +build exclude

package main

import (
Expand Down
47 changes: 47 additions & 0 deletions invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ type NewInvoiceRequest struct {
Extra map[string]string // extra attributes
}

type UpdateInvoiceRequest struct {
ID string `json:"-"`
Date string `json:"date,omitempty"`
DueDate string `json:"duedate,omitempty"`
Title string `json:"title,omitempty"`
Ref string `json:"ref,omitempty"`
Pdf []byte `json:"pdf,omitempty"`
Extra map[string]string `json:"extra,omitempty"`
}

// Customer is a json wrapper for usage inside the Invoice object
type Customer struct {
CustomerNumber string `json:"customerNumber,omitempty"`
Expand Down Expand Up @@ -402,3 +412,40 @@ func (c *Client) InvoicePayment(ctx context.Context, invoiceIdOrNumber string, m
}
return NewTwikeyErrorFromResponse(res)
}

func (c *Client) InvoiceUpdate(ctx context.Context, request *UpdateInvoiceRequest) error {
if err := c.refreshTokenIfRequired(); err != nil {
return err
}

if request.ID == "" {
return errors.New("missing invoice id")
}

body, err := json.Marshal(request)
if err != nil {
return err
}

_url := c.BaseURL + "/creditor/invoice/" + request.ID
req, err := http.NewRequestWithContext(ctx, http.MethodPut, _url, bytes.NewReader(body))
if err != nil {
return err
}

req.Header.Add("Accept-Language", "en")
req.Header.Add("Content-Type", "application/json")
req.Header.Set("User-Agent", c.UserAgent)
req.Header.Add("Authorization", c.apiToken)

res, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()

if res.StatusCode == 204 {
return nil
}
return NewTwikeyErrorFromResponse(res)
}
60 changes: 57 additions & 3 deletions invoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ func TestInvoiceFeed(t *testing.T) {
})
}

func TestInvoiceAdd(t *testing.T) {
func TestInvoiceAddAndUpdate(t *testing.T) {
if os.Getenv("TWIKEY_API_KEY") == "" {
t.Skip("No TWIKEY_API_KEY available")
}

c := newTestClient()
t.Run("Invoice", func(t *testing.T) {
t.Run("InvoiceAddAndUpdate", func(t *testing.T) {
invoice, err := c.InvoiceAdd(context.Background(), &NewInvoiceRequest{
Invoice: &Invoice{
Number: "123",
Expand All @@ -72,7 +72,8 @@ func TestInvoiceAdd(t *testing.T) {
t.Log("New invoice", invoice.Id)
}

cnote, err := c.InvoiceAdd(context.Background(), &NewInvoiceRequest{
ctx := context.Background()
cnote, err := c.InvoiceAdd(ctx, &NewInvoiceRequest{
Invoice: &Invoice{
Number: "124",
RelatedInvoice: "123",
Expand All @@ -95,5 +96,58 @@ func TestInvoiceAdd(t *testing.T) {
} else {
t.Log("New CreditNote", cnote.Id)
}

if err := c.InvoiceUpdate(ctx, &UpdateInvoiceRequest{
ID: invoice.Id,
Title: "Some updated title",
}); err != nil {
if err != nil {
t.Error(err)
} else {
t.Log("Updated invoice", cnote.Id)
}
}
})
}

func TestInvoiceUpdateWithInvalidRequest(t *testing.T) {
if os.Getenv("TWIKEY_API_KEY") == "" {
t.Skip("No TWIKEY_API_KEY available")
}

c := newTestClient()
t.Run("InvoiceUpdateWithInvalidRequest", func(t *testing.T) {
invoice, err := c.InvoiceAdd(context.Background(), &NewInvoiceRequest{
Invoice: &Invoice{
Number: "123123",
Title: "TestInvoice 123123",
Date: "2021-01-01",
Duedate: "2021-03-01",
Remittance: "123123",
Amount: 10.00,
Customer: &Customer{
CustomerNumber: "123123",
Email: "[email protected]",
Address: "Derbystraat 43",
City: "Gent",
Zip: "9051",
Country: "BE",
Language: "nl",
},
},
Origin: "Go-Test",
})
if err != nil {
t.Error(err)
} else {
t.Log("New invoice", invoice.Id)
}

ctx := context.Background()
if err := c.InvoiceUpdate(ctx, &UpdateInvoiceRequest{
Title: "Some updated title",
}); err == nil {
t.Error("Update invoice call did not return an error even though we send no ID")
}
})
}

0 comments on commit 4548ba9

Please sign in to comment.