forked from r0busta/go-shopify-graphql
/
inventory.go
93 lines (76 loc) · 2.79 KB
/
inventory.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
package shopify
import (
"context"
"fmt"
"github.com/r0busta/go-shopify-graphql-model/v3/graph/model"
)
//go:generate mockgen -destination=./mock/inventory_service.go -package=mock . InventoryService
type InventoryService interface {
Update(id string, input model.InventoryItemUpdateInput) error
Adjust(locationID string, input []model.InventoryAdjustItemInput) error
ActivateInventory(locationID string, id string) error
}
type InventoryServiceOp struct {
client *Client
}
var _ InventoryService = &InventoryServiceOp{}
type mutationInventoryItemUpdate struct {
InventoryItemUpdateResult struct {
UserErrors []model.UserError `json:"userErrors,omitempty"`
} `graphql:"inventoryItemUpdate(id: $id, input: $input)" json:"inventoryItemUpdate"`
}
type mutationInventoryBulkAdjustQuantityAtLocation struct {
InventoryBulkAdjustQuantityAtLocationResult struct {
UserErrors []model.UserError `json:"userErrors,omitempty"`
} `graphql:"inventoryBulkAdjustQuantityAtLocation(locationId: $locationId, inventoryItemAdjustments: $inventoryItemAdjustments)" json:"inventoryBulkAdjustQuantityAtLocation"`
}
type mutationInventoryActivate struct {
InventoryActivateResult struct {
UserErrors []model.UserError `json:"userErrors,omitempty"`
} `graphql:"inventoryActivate(inventoryItemId: $itemID, locationId: $locationId)" json:"inventoryActivate"`
}
func (s *InventoryServiceOp) Update(id string, input model.InventoryItemUpdateInput) error {
m := mutationInventoryItemUpdate{}
vars := map[string]interface{}{
"id": id,
"input": input,
}
err := s.client.gql.Mutate(context.Background(), &m, vars)
if err != nil {
return fmt.Errorf("mutation: %w", err)
}
if len(m.InventoryItemUpdateResult.UserErrors) > 0 {
return fmt.Errorf("%+v", m.InventoryItemUpdateResult.UserErrors)
}
return nil
}
func (s *InventoryServiceOp) Adjust(locationID string, input []model.InventoryAdjustItemInput) error {
m := mutationInventoryBulkAdjustQuantityAtLocation{}
vars := map[string]interface{}{
"locationId": locationID,
"inventoryItemAdjustments": input,
}
err := s.client.gql.Mutate(context.Background(), &m, vars)
if err != nil {
return fmt.Errorf("mutation: %w", err)
}
if len(m.InventoryBulkAdjustQuantityAtLocationResult.UserErrors) > 0 {
return fmt.Errorf("%+v", m.InventoryBulkAdjustQuantityAtLocationResult.UserErrors)
}
return nil
}
func (s *InventoryServiceOp) ActivateInventory(locationID string, id string) error {
m := mutationInventoryActivate{}
vars := map[string]interface{}{
"itemID": id,
"locationId": locationID,
}
err := s.client.gql.Mutate(context.Background(), &m, vars)
if err != nil {
return fmt.Errorf("mutation: %w", err)
}
if len(m.InventoryActivateResult.UserErrors) > 0 {
return fmt.Errorf("%+v", m.InventoryActivateResult.UserErrors)
}
return nil
}