Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement/allow updating invoices #4

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -59,6 +59,16 @@ type NewInvoiceRequest struct {
UblBytes []byte // or an invoice item
}

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 @@ -381,3 +391,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 @@ -92,5 +93,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")
}
})
}
Loading