-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompliance_workflows.go
273 lines (229 loc) · 9.59 KB
/
compliance_workflows.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
package rize
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/google/go-querystring/query"
)
// Handles all Compliance Workflow operations
type complianceWorkflowService service
// Workflow data type
type Workflow struct {
UID string `json:"uid,omitempty"`
Summary *WorkflowSummary `json:"summary,omitempty"`
Customer *WorkflowCustomer `json:"customer,omitempty"`
ProductUID string `json:"product_uid,omitempty"`
ProductCompliancePlanUID string `json:"product_compliance_plan_uid,omitempty"`
AcceptedDocuments []*WorkflowAcceptedDocument `json:"accepted_documents,omitempty"`
CurrentStepDocumentsPending []*WorkflowPendingDocument `json:"current_step_documents_pending,omitempty"`
AllDocuments []*WorkflowDocument `json:"all_documents,omitempty"`
}
// WorkflowSummary contains a status summary of the Compliance Workflow
type WorkflowSummary struct {
AcceptedQuantity int `json:"accepted_quantity,omitempty"`
BegunAt time.Time `json:"begun_at,omitempty"`
CompletedStep int `json:"completed_step,omitempty"`
CurrentStep int `json:"current_step,omitempty"`
Status string `json:"status,omitempty"`
}
// WorkflowCustomer contains Customer information related to this Compliance Workflow
type WorkflowCustomer struct {
Email string `json:"email,omitempty"`
ExternalUID string `json:"external_uid,omitempty"`
UID string `json:"uid,omitempty"`
}
// WorkflowAcceptedDocument contains information about accepted Compliance Workflow documents
type WorkflowAcceptedDocument struct {
ElectronicSignatureRequired string `json:"electronic_signature_required,omitempty"`
ExternalStorageName string `json:"external_storage_name,omitempty"`
ComplianceDocumentURL string `json:"compliance_document_url,omitempty"`
Name string `json:"name,omitempty"`
Step int `json:"step,omitempty"`
Version int `json:"version,omitempty"`
UID string `json:"uid,omitempty"`
AcceptedAt time.Time `json:"accepted_at,omitempty"`
}
// WorkflowPendingDocument contains information about pending Compliance Workflow documents
type WorkflowPendingDocument struct {
ElectronicSignatureRequired string `json:"electronic_signature_required,omitempty"`
ExternalStorageName string `json:"external_storage_name,omitempty"`
ComplianceDocumentURL string `json:"compliance_document_url,omitempty"`
Name string `json:"name,omitempty"`
Step int `json:"step,omitempty"`
Version int `json:"version,omitempty"`
UID string `json:"uid,omitempty"`
}
// WorkflowDocument contains information about all Compliance Workflow documents
type WorkflowDocument struct {
ElectronicSignatureRequired string `json:"electronic_signature_required,omitempty"`
ExternalStorageName string `json:"external_storage_name,omitempty"`
ComplianceDocumentURL string `json:"compliance_document_url,omitempty"`
Name string `json:"name,omitempty"`
Step int `json:"step,omitempty"`
Version int `json:"version,omitempty"`
}
// WorkflowListParams builds the query parameters used in querying Compliance Workflows
type WorkflowListParams struct {
CustomerUID string `url:"customer_uid,omitempty" json:"customer_uid,omitempty"`
ProductUID string `url:"product_uid,omitempty" json:"product_uid,omitempty"`
InProgress bool `url:"in_progress,omitempty" json:"in_progress,omitempty"`
Limit int `url:"limit,omitempty" json:"limit,omitempty"`
Offset int `url:"offset,omitempty" json:"offset,omitempty"`
}
// WorkflowLatestParams builds the query parameters used in querying the latest Compliance Workflow for a customer
type WorkflowLatestParams struct {
ProductCompliancePlanUID string `url:"product_compliance_plan_uid,omitempty" json:"product_compliance_plan_uid,omitempty"`
}
// WorkflowCreateParams are the body params used when creating a new Compliance Workflow
type WorkflowCreateParams struct {
CustomerUID string `json:"customer_uid"`
ProductCompliancePlanUID string `json:"product_compliance_plan_uid"`
}
// WorkflowDocumentParams are the body params used when acknowledging a compliance document
type WorkflowDocumentParams struct {
Accept string `json:"accept"`
DocumentUID string `json:"document_uid"`
IPAddress string `json:"ip_address,omitempty"`
UserName string `json:"user_name,omitempty"`
// Required for AcknowledgeDocument but omitted for BatchAcknowledgeDocuments
CustomerUID string `json:"customer_uid,omitempty"`
}
// WorkflowBatchDocumentsParams are the body params used when acknowledging multiple compliance documents
type WorkflowBatchDocumentsParams struct {
CustomerUID string `json:"customer_uid"`
Documents []*WorkflowDocumentParams `json:"documents"`
}
// WorkflowListResponse is an API response containing a list of Compliance Workflows
type WorkflowListResponse struct {
ListResponse
Data []*Workflow `json:"data"`
}
// Retrieves a list of Compliance Workflows filtered by the given parameters
func (c *complianceWorkflowService) List(ctx context.Context, params *WorkflowListParams) (*WorkflowListResponse, error) {
// Build WorkflowListParams into query string params
v, err := query.Values(params)
if err != nil {
return nil, err
}
res, err := c.client.doRequest(ctx, http.MethodGet, "compliance_workflows", v, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &WorkflowListResponse{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Associates a new Compliance Workflow and set of Compliance Documents (for acknowledgment) with a Customer
func (c *complianceWorkflowService) Create(ctx context.Context, params *WorkflowCreateParams) (*Workflow, error) {
if params.CustomerUID == "" || params.ProductCompliancePlanUID == "" {
return nil, fmt.Errorf("CustomerUID and ProductCompliancePlanUID values are required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := c.client.doRequest(ctx, http.MethodPost, "compliance_workflows", nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &Workflow{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// ViewLatest is a helper endpoint for retrieving the most recent Compliance Workflow for a Customer.
// A Customer UID must be supplied as the path parameter.
func (c *complianceWorkflowService) ViewLatest(ctx context.Context, customerUID string, params *WorkflowLatestParams) (*Workflow, error) {
if customerUID == "" {
return nil, fmt.Errorf("customerUID is required")
}
// Build query params
v, err := query.Values(params)
if err != nil {
return nil, err
}
res, err := c.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("compliance_workflows/latest/%s", customerUID), v, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &Workflow{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// AcknowledgeDocument is used to indicate acceptance or rejection of a Compliance Document within a given Compliance Workflow
func (c *complianceWorkflowService) AcknowledgeDocument(ctx context.Context, uid string, params *WorkflowDocumentParams) (*Workflow, error) {
if uid == "" || params.Accept == "" || params.DocumentUID == "" || params.CustomerUID == "" {
return nil, fmt.Errorf("UID, Accept, DocumentUID and CustomerUID values are required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := c.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("compliance_workflows/%s/acknowledge_document", uid), nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &Workflow{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// BatchAcknowledgeDocuments is used to indicate acceptance or rejection of multiple Compliance Documents within a given Compliance Workflow
func (c *complianceWorkflowService) BatchAcknowledgeDocuments(ctx context.Context, uid string, params *WorkflowBatchDocumentsParams) (*Workflow, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
for _, d := range params.Documents {
if d.Accept == "" || d.DocumentUID == "" {
return nil, fmt.Errorf("both Accept and DocumentUID values are required")
}
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := c.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("compliance_workflows/%s/batch_acknowledge_documents", uid), nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &Workflow{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}