forked from Invoiced/invoiced-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
159 lines (114 loc) · 3.11 KB
/
item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package invdapi
import (
"errors"
"github.com/gabrielsoaressantos/invoiced-go/invdendpoint"
)
type Item struct {
*Connection
*invdendpoint.Item
}
type Items []*Item
func (c *Connection) NewItem() *Item {
item := new(invdendpoint.Item)
return &Item{c, item}
}
func (c *Item) Create(item *Item) (*Item, error) {
endpoint := invdendpoint.ItemEndpoint
itemResp := new(Item)
if item == nil {
return nil, errors.New("item is nil")
}
// safe prune file data for creation
invdItemDataToCreate, err := SafeItemForCreation(item.Item)
if err != nil {
return nil, err
}
apiErr := c.create(endpoint, invdItemDataToCreate, itemResp)
if apiErr != nil {
return nil, apiErr
}
itemResp.Connection = c.Connection
return itemResp, nil
}
func (c *Item) Save() error {
endpoint := invdendpoint.ItemEndpoint + "/" + c.Id
itemResp := new(Item)
itemDataToUpdate, err := SafeItemForUpdating(c.Item)
if err != nil {
return err
}
apiErr := c.update(endpoint, itemDataToUpdate, itemResp)
if apiErr != nil {
return apiErr
}
c.Item = itemResp.Item
return nil
}
func (c *Item) Delete() error {
endpoint := invdendpoint.ItemEndpoint + "/" + c.Id
err := c.delete(endpoint)
if err != nil {
return err
}
return nil
}
func (c *Item) Retrieve(id string) (*Item, error) {
endpoint := invdendpoint.ItemEndpoint + "/" + id
itemEndpoint := new(invdendpoint.Item)
item := &Item{c.Connection, itemEndpoint}
_, err := c.retrieveDataFromAPI(endpoint, item)
if err != nil {
return nil, err
}
return item, nil
}
func (c *Item) ListAll(filter *invdendpoint.Filter, sort *invdendpoint.Sort) (Items, error) {
endpoint := invdendpoint.ItemEndpoint
endpoint = addFilterAndSort(endpoint, filter, sort)
items := make(Items, 0)
NEXT:
tmpItems := make(Items, 0)
endpointTmp, apiErr := c.retrieveDataFromAPI(endpoint, &tmpItems)
if apiErr != nil {
return nil, apiErr
}
items = append(items, tmpItems...)
if endpointTmp != "" {
goto NEXT
}
for _, item := range items {
item.Connection = c.Connection
}
return items, nil
}
// SafeForCreation prunes item data for just fields that can be used for creation of an item
func SafeItemForCreation(item *invdendpoint.Item) (*invdendpoint.Item, error) {
if item == nil {
return nil, errors.New("task is nil")
}
itemData := new(invdendpoint.Item)
itemData.Id = item.Id
itemData.Name = item.Name
itemData.Currency = item.Currency
itemData.UnitCost = item.UnitCost
itemData.Description = item.Description
itemData.Type = item.Type
itemData.Taxable = item.Taxable
itemData.AvalaraTaxCode = item.AvalaraTaxCode
itemData.GlAccount = item.GlAccount
itemData.Discountable = item.Discountable
itemData.Metadata = item.Metadata
return itemData, nil
}
// SafeForUpdating prunes item data for just fields that can be used for updating of an item
func SafeItemForUpdating(item *invdendpoint.Item) (*invdendpoint.Item, error) {
if item == nil {
return nil, errors.New("task is nil")
}
itemData := new(invdendpoint.Item)
itemData.Name = item.Name
itemData.Description = item.Description
itemData.Type = item.Type
itemData.Metadata = item.Metadata
return itemData, nil
}