/
estimate_message.go
209 lines (173 loc) · 5.9 KB
/
estimate_message.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
package harvest
import (
"context"
"fmt"
"net/http"
"time"
)
// Harvest API docs: https://help.getharvest.com/api-v2/estimates-api/estimates/estimate-messages/
type EstimateMessage struct {
// Unique ID for the message.
ID *int64 `json:"id,omitempty"`
// Name of the user that created the message.
SentBy *string `json:"sent_by,omitempty"`
// Email of the user that created the message.
SentByEmail *string `json:"sent_by_email,omitempty"`
// Name of the user that the message was sent from.
SentFrom *string `json:"sent_from,omitempty"`
// Email of the user that message was sent from.
SentFromEmail *string `json:"sent_from_email,omitempty"`
// Array of estimate message recipients.
Recipients *[]EstimateMessageRecipient `json:"recipients,omitempty"`
// The message subject.
Subject *string `json:"subject,omitempty"`
// The message body.
Body *string `json:"body,omitempty"`
// Whether to email a copy of the message to the current user.
SendMeACopy *bool `json:"send_me_a_copy,omitempty"`
// The type of estimate event that occurred with the message: send, accept, decline, re-open, view, or invoice.
EventType *bool `json:"event_type,omitempty"`
// Date and time the message was created.
CreatedAt *time.Time `json:"created_at,omitempty"`
// Date and time the message was last updated.
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
type EstimateMessageRecipient struct {
// Name of the message recipient.
Name *string `json:"name,omitempty"`
// Email of the message recipient.
Email *string `json:"email,omitempty"`
}
type EstimateMessageCreateRequest struct {
// required Array of recipient parameters. See below for details.
Recipients *[]EstimateMessageRecipientCreateRequest `json:"recipients,omitempty"`
// optional The message subject.
Subject *string `json:"subject,omitempty"`
// optional The message body.
Body *string `json:"body,omitempty"`
// optional If set to true, a copy of the message email will be sent to the current user. Defaults to false.
SendMeACopy *bool `json:"send_me_a_copy,omitempty"`
// optional If provided, runs an event against the estimate. Options: “accept”, “decline”, “re-open”, or “send”.
EventType *string `json:"event_type,omitempty"`
}
type EstimateMessageRecipientCreateRequest struct {
// optional Name of the message recipient.
Name *string `json:"name,omitempty"`
// required Email of the message recipient.
Email *string `json:"email"`
}
type EstimateMessageList struct {
Estimates []*Estimate `json:"estimates"`
Pagination
}
func (p EstimateMessage) String() string {
return Stringify(p)
}
func (p EstimateMessageList) String() string {
return Stringify(p)
}
type EstimateMessageListOptions struct {
UpdatedSince time.Time `url:"updated_since,omitempty"`
ListOptions
}
type EstimateEventTypeRequest struct {
EventType string `json:"event_type"`
}
// ListEstimateMessages returns a list of messages associated with a given estimate.
func (s *EstimateService) ListEstimateMessages(
ctx context.Context,
estimateID int64,
opt *EstimateMessageListOptions,
) (*EstimateList, *http.Response, error) {
u := fmt.Sprintf("estimates/%d/messages", estimateID)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}
estimateList := new(EstimateList)
resp, err := s.client.Do(ctx, req, &estimateList)
if err != nil {
return nil, resp, err
}
return estimateList, resp, nil
}
// CreateEstimateMessage creates a new estimate message object.
func (s *EstimateService) CreateEstimateMessage(
ctx context.Context,
data *EstimateMessageCreateRequest,
) (*EstimateMessage, *http.Response, error) {
u := "estimates"
req, err := s.client.NewRequest(ctx, "POST", u, data)
if err != nil {
return nil, nil, err
}
estimateMessage := new(EstimateMessage)
resp, err := s.client.Do(ctx, req, estimateMessage)
if err != nil {
return nil, resp, err
}
return estimateMessage, resp, nil
}
// DeleteEstimateMessage deletes an estimate message.
func (s *EstimateService) DeleteEstimateMessage(
ctx context.Context,
estimateID,
estimateMessageID int64,
) (*http.Response, error) {
u := fmt.Sprintf("estimates/%d/messages/%d", estimateID, estimateMessageID)
req, err := s.client.NewRequest(ctx, "DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// MarkAsSent marks a draft estimate as sent.
func (s *EstimateService) MarkAsSent(
ctx context.Context,
estimateID int64,
) (*EstimateMessage, *http.Response, error) {
return s.SendEvent(ctx, estimateID, &EstimateEventTypeRequest{EventType: "send"})
}
// MarkAsAccepted marks an open estimate as accepted.
func (s *EstimateService) MarkAsAccepted(
ctx context.Context,
estimateID int64,
) (*EstimateMessage, *http.Response, error) {
return s.SendEvent(ctx, estimateID, &EstimateEventTypeRequest{EventType: "accept"})
}
// MarkAsDeclined marks an open estimate as declined.
func (s *EstimateService) MarkAsDeclined(
ctx context.Context,
estimateID int64,
) (*EstimateMessage, *http.Response, error) {
return s.SendEvent(ctx, estimateID, &EstimateEventTypeRequest{EventType: "decline"})
}
// MarkAsReopen re-opens a closed estimate.
func (s *EstimateService) MarkAsReopen(
ctx context.Context,
estimateID int64,
) (*EstimateMessage, *http.Response, error) {
return s.SendEvent(ctx, estimateID, &EstimateEventTypeRequest{EventType: "re-open"})
}
// SendEvent will send an EstimateEventType.
func (s *EstimateService) SendEvent(
ctx context.Context,
estimateID int64,
data *EstimateEventTypeRequest,
) (*EstimateMessage, *http.Response, error) {
u := fmt.Sprintf("estimates/%d/messages", estimateID)
req, err := s.client.NewRequest(ctx, "POST", u, data)
if err != nil {
return nil, nil, err
}
estimateMessage := new(EstimateMessage)
resp, err := s.client.Do(ctx, req, estimateMessage)
if err != nil {
return nil, resp, err
}
return estimateMessage, resp, nil
}