-
Notifications
You must be signed in to change notification settings - Fork 3
/
itembasedeclarations.go
601 lines (497 loc) · 16.6 KB
/
itembasedeclarations.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package itembase
import (
"encoding/json"
"log"
"strings"
"time"
)
// TODO: Some entities/models don't have the full set of fields from the API.
// Some of the implementation detail structs (Contacts, Billing, pagination
// containers, etc.) could perhaps be unexported.
type ProfileID string
func (profileID ProfileID) String() string {
return string(profileID)
}
// A Profile represents a user profile entity from the itembase API.
//
// See http://sandbox.api.itembase.io/swagger-ui/
type Profile struct {
Active bool `json:"active,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
Contact struct {
Contact []Contact `json:"contact,omitempty"`
} `json:"contact,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
Currency string `json:"currency,omitempty"`
DisplayName string `json:"display_name,omitempty"`
ID ProfileID `json:"id"`
Language string `json:"language,omitempty"`
Locale string `json:"locale,omitempty"`
OriginalReference string `json:"original_reference,omitempty"`
PlatformID string `json:"platform_id,omitempty"`
PlatformName string `json:"platform_name,omitempty"`
SourceID string `json:"source_id,omitempty"`
Status string `json:"status,omitempty"`
Type string `json:"type,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
URL string `json:"url,omitempty"`
}
// An Address represents a mailing address model from the itembase API.
type Address struct {
City string `json:"city,omitempty"`
Country string `json:"country,omitempty"`
Line1 string `json:"line_1,omitempty"`
Name string `json:"name,omitempty"`
Zip string `json:"zip,omitempty"`
}
// A Contact represents a container of contact information from itembase API
// models.
type Contact struct {
Addresses []Address `json:"addresses,omitempty"`
Emails []struct {
Value string `json:"value,omitempty"`
} `json:"emails,omitempty"`
Phones []interface{} `json:"phones,omitempty"`
}
// GetName returns a string with a combined FirstName and
// LastName of a Buyer Profile
func (buyer *Buyer) GetName() string {
return buyer.FirstName + " " + buyer.LastName
}
type BuyerID string
func (buyerID BuyerID) String() string {
return string(buyerID)
}
// A Buyer represents a buyer entity from the itembase API.
//
// See http://sandbox.api.itembase.io/swagger-ui/
type Buyer struct {
Active bool `json:"active,omitempty"`
Contact Contact `json:"contact,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
Currency string `json:"currency,omitempty"`
DateOfBirth string `json:"date_of_birth,omitempty"`
FirstName string `json:"first_name,omitempty"`
ID BuyerID `json:"id"`
Language string `json:"language,omitempty"`
LastName string `json:"last_name,omitempty"`
Locale string `json:"locale,omitempty"`
Note string `json:"note,omitempty"`
OptOut bool `json:"opt_out,omitempty"`
OriginalReference string `json:"original_reference,omitempty"`
SourceID string `json:"source_id,omitempty"`
Status string `json:"status,omitempty"`
Type string `json:"type,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
URL string `json:"url,omitempty"`
}
// GetEmail returns an Email for a Profile
func (buyer *Buyer) GetEmail() string {
if len(buyer.Contact.Emails) > 0 {
for _, EmailValue := range buyer.Contact.Emails {
return EmailValue.Value
}
}
return ""
}
// GetEmails returns all Emails for a Profile
func (buyer *Buyer) GetEmails() (emails []string) {
if len(buyer.Contact.Emails) > 0 {
for _, EmailValue := range buyer.Contact.Emails {
emails = append(emails, EmailValue.Value)
}
}
return
}
// A Category represents a product category model from the itembase API.
type Category struct {
CategoryID string `json:"category_id,omitempty"`
Language string `json:"language,omitempty"`
Value string `json:"value,omitempty"`
}
// A ProductDescription represents a product description model from the itembase
// API, which may be in a specified language.
type ProductDescription struct {
Language string `json:"language,omitempty"`
Value string `json:"value,omitempty"`
}
// A Brand represents a product brand model from the itembase API.
type Brand struct {
Name struct {
Language string `json:"language,omitempty"`
Value string `json:"value,omitempty"`
} `json:"name,omitempty"`
}
type Identifier struct {
ID string `json:"id,omitempty"`
}
type StockInformation struct {
InStock bool `json:"in_stock,omitempty"`
InventoryLevel float64 `json:"inventory_level,omitempty"`
InventoryUnit string `json:"inventory_unit,omitempty"`
}
type ProductID string
func (productID ProductID) String() string {
return string(productID)
}
// A Product represents a product entity from the itembase API.
//
// See http://sandbox.api.itembase.io/swagger-ui/
type Product struct {
Active bool `json:"active,omitempty"`
Brand Brand `json:"brand,omitempty"`
Categories []Category `json:"categories,omitempty"`
Condition string `json:"condition,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
Currency string `json:"currency,omitempty"`
Description []ProductDescription `json:"description,omitempty"`
ID ProductID `json:"id"`
Identifier Identifier `json:"identifier,omitempty"`
Name []struct {
Language string `json:"language,omitempty"`
Value string `json:"value,omitempty"`
} `json:"name,omitempty"`
OriginalReference string `json:"original_reference,omitempty"`
PictureUrls []struct {
URLOriginal string `json:"url_original,omitempty"`
} `json:"picture_urls,omitempty"`
PricePerUnit float64 `json:"price_per_unit,omitempty"`
Shipping []struct {
Price float64 `json:"price,omitempty"`
ShippingService string `json:"shipping_service,omitempty"`
} `json:"shipping,omitempty"`
SourceID string `json:"source_id,omitempty"`
StockInformation StockInformation `json:"stock_information,omitempty"`
Tax float64 `json:"tax,omitempty"`
TaxRate float64 `json:"tax_rate,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
URL string `json:"url,omitempty"`
Variants []interface{} `json:"variants,omitempty"`
}
func (product *Product) InStock() bool {
return product.StockInformation.InStock
}
// Returns name for specified preferred language if present
func (product *Product) GetName(preferredLanguage string) (name string, ok bool) {
for _, productName := range product.Name {
if preferredLanguage == productName.Language {
return cleanItembaseUnicode(productName.Value), true
}
}
// if []struct{} is empty, return empty string
return "", false
}
// Returns any name for Product
func (product *Product) GetDefaultName() (name string, ok bool) {
for _, productName := range product.Name {
return cleanItembaseUnicode(productName.Value), true
}
return "", false
}
func cleanItembaseUnicode(str string) string {
str = strings.Replace(str, "\u00a0", " ", -1)
str = strings.Replace(str, "\ufeff", "", -1)
return str
}
// Billing represents a model from the itembase API containing the billing
// address of a Transaction.
type Billing struct {
Address Address `json:"address,omitempty"`
}
type Shipping struct {
Address Address `json:"address,omitempty"`
}
// Status describes a transactions' status
type Status struct {
Global string `json:"global,omitempty"`
Payment string `json:"payment,omitempty"`
Shipping string `json:"shipping,omitempty"`
}
type TransactionID string
func (transactionID TransactionID) String() string {
return string(transactionID)
}
// A Transaction represents a transaction entity from the itembase API.
//
// See http://sandbox.api.itembase.io/swagger-ui/
type Transaction struct {
Billing Billing `json:"billing,omitempty"`
Buyer Buyer `json:"buyer,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
Currency string `json:"currency,omitempty"`
ID TransactionID `json:"id"`
OriginalReference string `json:"original_reference,omitempty"`
Products []Product `json:"products,omitempty"`
Shipping Shipping `json:"shipping,omitempty"`
SourceID string `json:"source_id,omitempty"`
Status Status `json:"status,omitempty"`
TotalPrice float64 `json:"total_price,omitempty"`
TotalPriceNet float64 `json:"total_price_net,omitempty"`
TotalTax float64 `json:"total_tax,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
func (t *Transaction) Completed() bool {
if t.Status.Global == "completed" {
return true
}
return false
}
// ItembaseResponse is a container for any Itembase response.
// It returns the resultset, Number of found documents and Number of documents returned
type ItembaseResponse struct {
Documents []interface{} `json:"documents"`
NumDocumentsFound int `json:"num_documents_found"`
NumDocumentsReturned int `json:"num_documents_returned"`
}
// Transactions is a container for pagination of Transaction entities.
type Transactions struct {
Transactions []Transaction `json:"documents"`
}
func (transactions *Transactions) Add(transaction interface{}) error {
var newTransaction Transaction
err := ConvertTo(transaction, &newTransaction)
if err != nil {
log.Println("Error when converting", "error", err)
return err
}
if !transactions.Exists(newTransaction) {
transactions.Transactions = append(transactions.Transactions, newTransaction)
}
return nil
}
func (transactions *Transactions) Exists(searchTransaction Transaction) bool {
for _, transaction := range transactions.Transactions {
if searchTransaction.ID == transaction.ID {
log.Println("Transaction already exists in collection")
return true
}
}
return false
}
func (transactions *Transactions) Count() int {
return len(transactions.Transactions)
}
// Return date of heighest Created At transaction
func (transactions *Transactions) MaxCreatedAt() time.Time {
var maxCreatedAt time.Time
for _, transaction := range transactions.Transactions {
if transaction.CreatedAt.After(maxCreatedAt) {
maxCreatedAt = *transaction.CreatedAt
}
}
return maxCreatedAt
}
// Return date of heighest Updated At transaction
func (transactions *Transactions) MaxUpdatedAt() time.Time {
var maxUpdatedAt time.Time
for _, transaction := range transactions.Transactions {
if transaction.UpdatedAt != nil && transaction.UpdatedAt.After(maxUpdatedAt) {
maxUpdatedAt = *transaction.UpdatedAt
}
}
return maxUpdatedAt
}
// Return only completed transactions
func (transactions *Transactions) Completed() (filteredTransactions Transactions) {
for _, transaction := range transactions.Transactions {
if transaction.Completed() {
filteredTransactions.Add(transaction)
}
}
return
}
// Profiles is a container for pagination of Profile entities.
type Profiles struct {
Profiles []Profile `json:"documents"`
}
func (profiles *Profiles) Add(profile interface{}) error {
var newProfile Profile
err := ConvertTo(profile, &newProfile)
if err != nil {
log.Println("Error when converting", "error", err)
return err
}
if !profiles.Exists(newProfile) {
profiles.Profiles = append(profiles.Profiles, newProfile)
}
return nil
}
func (profiles *Profiles) Exists(searchProfile Profile) bool {
for _, profile := range profiles.Profiles {
if searchProfile.ID == profile.ID {
log.Println("Profile already exists in collection")
return true
}
}
return false
}
func (profiles *Profiles) Count() int {
return len(profiles.Profiles)
}
// Return date of heighest Created At profile
func (profiles *Profiles) MaxCreatedAt() time.Time {
var maxCreatedAt time.Time
for _, profile := range profiles.Profiles {
if profile.CreatedAt.After(maxCreatedAt) {
maxCreatedAt = *profile.CreatedAt
}
}
return maxCreatedAt
}
// Return date of heighest Updated At profile
func (profiles *Profiles) MaxUpdatedAt() time.Time {
var maxUpdatedAt time.Time
for _, profile := range profiles.Profiles {
if profile.UpdatedAt != nil && profile.UpdatedAt.After(maxUpdatedAt) {
maxUpdatedAt = *profile.UpdatedAt
}
}
return maxUpdatedAt
}
// Products is a container for pagination of Product entities.
type Products struct {
Products []Product `json:"documents"`
}
func (products *Products) Add(product interface{}) error {
var newProduct Product
err := ConvertTo(product, &newProduct)
if err != nil {
log.Println("Error when converting", "error", err)
return err
}
if !products.Exists(newProduct) {
products.Products = append(products.Products, newProduct)
}
return nil
}
func (products *Products) Exists(searchProduct Product) bool {
for _, product := range products.Products {
if searchProduct.ID == product.ID {
log.Println("Product already exists in collection", searchProduct.ID, product.ID)
return true
}
}
return false
}
func (products *Products) Count() int {
return len(products.Products)
}
// Return date of heighest Created At product
func (products *Products) MaxCreatedAt() time.Time {
var maxCreatedAt time.Time
for _, product := range products.Products {
if product.CreatedAt.After(maxCreatedAt) {
maxCreatedAt = *product.CreatedAt
}
}
return maxCreatedAt
}
// Return date of heighest Updated At product
func (products *Products) MaxUpdatedAt() time.Time {
var maxUpdatedAt time.Time
for _, product := range products.Products {
if product.UpdatedAt != nil && product.UpdatedAt.After(maxUpdatedAt) {
maxUpdatedAt = *product.UpdatedAt
}
}
return maxUpdatedAt
}
func (products *Products) InStock() (filteredProducts Products) {
for _, product := range products.Products {
if product.InStock() {
filteredProducts.Add(product)
}
}
return
}
// Get Products based on shopID
func (products *Products) ByShop(shopID string) (filteredProducts Products) {
for _, product := range products.Products {
if product.SourceID == shopID {
filteredProducts.Add(product)
}
}
return
}
// Buyers is a container for pagination of Buyer entities.
type Buyers struct {
Buyers []Buyer `json:"documents"`
}
func (buyers *Buyers) Add(buyer interface{}) error {
var newBuyer Buyer
err := ConvertTo(buyer, &newBuyer)
if err != nil {
log.Println("Error when converting", "error", err)
return err
}
if !buyers.Exists(newBuyer) {
buyers.Buyers = append(buyers.Buyers, newBuyer)
}
return nil
}
func (buyers *Buyers) Exists(searchBuyer Buyer) bool {
for _, buyer := range buyers.Buyers {
if searchBuyer.ID == buyer.ID {
log.Println("Buyer already exists in collection")
return true
}
}
return false
}
func (buyers *Buyers) Count() int {
return len(buyers.Buyers)
}
// Return date of heighest Created At buyer
func (buyers *Buyers) MaxCreatedAt() time.Time {
var maxCreatedAt time.Time
for _, buyer := range buyers.Buyers {
if buyer.CreatedAt.After(maxCreatedAt) {
maxCreatedAt = *buyer.CreatedAt
}
}
return maxCreatedAt
}
// Return date of heighest Updated At buyer
func (buyers *Buyers) MaxUpdatedAt() time.Time {
var maxUpdatedAt time.Time
for _, buyer := range buyers.Buyers {
if buyer.UpdatedAt != nil && buyer.UpdatedAt.After(maxUpdatedAt) {
maxUpdatedAt = *buyer.UpdatedAt
}
}
return maxUpdatedAt
}
func (buyers *Buyers) ByShop(shopID string) (filteredBuyers Buyers) {
for _, buyer := range buyers.Buyers {
if buyer.SourceID == shopID {
filteredBuyers.Add(buyer)
}
}
return
}
// A User represents a user entity from the itembase API, such as returned from
// the "me" endpoint.
type User struct {
UUID string `json:"uuid"`
Username string `json:"username,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
MiddleName string `json:"middle_name,omitempty"`
NameFormat string `json:"name_format,omitempty"`
Locale string `json:"locale,omitempty"`
Email string `json:"email,omitempty"`
PreferredCurrency string `json:"preferred_currency,omitempty"`
}
func ConvertTo(inputInterface, outputType interface{}) error {
jsonBLOB, err := json.Marshal(inputInterface)
if err != nil {
log.Fatal(err)
return err
}
err = json.Unmarshal(jsonBLOB, &outputType)
if err != nil {
log.Fatal(err)
return err
}
return nil
}