This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders.go
178 lines (152 loc) · 5.37 KB
/
orders.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
package workwave
import (
"context"
"fmt"
"net/http"
"github.com/pkg/errors"
)
const (
ordersBasePath = "/api/v1/territories/%s/orders"
)
// OrdersService is an interface to orders in the WorkWave API.
type OrdersService interface {
List(context.Context, OrdersListInput) ([]Order, error)
Get(context.Context, OrdersGetInput) ([]Order, error)
Add(context.Context, OrdersAddInput) (string, error)
}
type ordersService struct {
client *Client
}
// Order represents an Order in the WorkWave API
// This structure can be used as input for order calls by omitting ID.
type Order struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Eligibility Eligibility `json:"eligibility,omitempty"`
ForceVehicleID interface{} `json:"forceVehicleId,omitempty"`
Priority int `json:"priority,omitempty"`
Loads map[string]int `json:"loads,omitempty"`
Pickup *OrderStep `json:"pickup,omitempty"`
Delivery *OrderStep `json:"delivery,omitempty"`
IsService bool `json:"isService,omitempty"`
}
// Eligibility represents Eligibility for an Order in the WorkWave API.
type Eligibility struct {
Type string `json:"type,omitempty"` // One of: on, by, any
ByDate string `json:"byDate,omitempty"` // Used when type = by, format: yyyyMMdd
OnDates []string `json:"onDates,omitempty"` // Used when type = on, format: yyyyMMdd
}
// Location represents a Location in the WorkWave API.
type Location struct {
Address string `json:"address,omitempty"`
LatLng *[2]int `json:"latLng,omitempty"` // ie, {33817872, -87266893}
Status string `json:"status,omitempty"`
}
// TimeWindow represents a time window in the WorkWave API.
type TimeWindow struct {
StartSec int `json:"startSec,omitempty"`
EndSec int `json:"endSec,omitempty"`
}
// OrderStep represents an OrderStep within an Order in the WorkWave API.
// An OrderStep can be `pickup` or `delivery`.
type OrderStep struct {
DepotID string `json:"depotId,omitempty"`
Location Location `json:"location,omitempty"`
TimeWindows []TimeWindow `json:"timeWindows,omitempty"`
TimeWindowExceptions map[string]TimeWindow `json:"timeWindowExceptions,omitempty"`
Notes string `json:"notes,omitempty"`
ServiceTimeSec int `json:"serviceTimeSec,omitempty"`
TagsIn []string `json:"tagsIn,omitempty"`
TagsOut []string `json:"tagsOut,omitempty"`
CustomFields map[string]string `json:"customFields,omitempty"`
}
type ordersResponse struct {
Orders map[string]Order `json:"orders"`
}
// OrdersListInput is used to populate a call to List Orders on the WorkWave API.
type OrdersListInput struct {
TerritoryID string
Include string
EligibleOn string
AssignedOn string
}
// List retrieves the orders matching the filters provided in the given OrderListInput.
func (svc *ordersService) List(ctx context.Context, i OrdersListInput) ([]Order, error) {
u := fmt.Sprintf(ordersBasePath, i.TerritoryID)
req, err := svc.client.NewRequest(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create orders list request")
}
// Query params
q := req.URL.Query()
if i.Include != "" {
q.Add("include", i.Include)
}
if i.EligibleOn != "" {
q.Add("eligibleOn", i.EligibleOn)
}
if i.AssignedOn != "" {
q.Add("assignedOn", i.AssignedOn)
}
req.URL.RawQuery = q.Encode()
olr := &ordersResponse{}
if _, err := svc.client.Do(ctx, req, olr); err != nil {
return nil, err
}
var orders []Order
for _, order := range olr.Orders {
orders = append(orders, order)
}
return orders, nil
}
// OrdersGetInput is used to populate a call Get Orders on the WorkWave API.
type OrdersGetInput struct {
TerritoryID string
IDs []string
}
// Get orders for the given IDs.
func (svc *ordersService) Get(ctx context.Context, i OrdersGetInput) ([]Order, error) {
u := fmt.Sprintf(ordersBasePath, i.TerritoryID)
b := struct {
IDs []string `json:"ids"`
}{
IDs: i.IDs,
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, u, b)
if err != nil {
return nil, errors.Wrap(err, "failed to create orders get request")
}
olr := &ordersResponse{}
if _, err := svc.client.Do(ctx, req, olr); err != nil {
return nil, err
}
var orders []Order
for _, order := range olr.Orders {
orders = append(orders, order)
}
return orders, nil
}
// OrdersAddInput is used to populate a call Add Orders on the WorkWave API.
type OrdersAddInput struct {
TerritoryID string `json:"-"`
Orders []Order `json:"orders"`
Strict bool `json:"strict"`
AcceptBadGeocodes bool `json:"acceptBadGeocodes"`
}
type ordersAddResponse struct {
RequestID string `json:"requestId"`
}
// Add the given orders to WorkWave via the API.
// This API call is asynchronous and the WorkWave API `requestId` value will be returned.
func (svc *ordersService) Add(ctx context.Context, i OrdersAddInput) (string, error) {
u := fmt.Sprintf(ordersBasePath, i.TerritoryID)
req, err := svc.client.NewRequest(ctx, http.MethodPost, u, i)
if err != nil {
return "", errors.Wrap(err, "failed to create orders add request")
}
oar := &ordersAddResponse{}
if _, err := svc.client.Do(ctx, req, oar); err != nil {
return "", err
}
return oar.RequestID, nil
}