/
person.go
95 lines (76 loc) · 2.31 KB
/
person.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
package delighted
import (
"errors"
"fmt"
)
// Person representation in golang.
type Person struct {
ID string `json:"id,omitempty"`
Email string `json:"email"`
Name string `json:"name,omitempty"`
Delay int `json:"delay,omitempty"`
Properties map[string]string `json:"properties,omitempty"`
Send bool `json:"send,omitempty"`
LastSentAt int `json:"last_sent_at,omitempty"`
}
// PersonSurveyResponse from action against a person's survey
type PersonSurveyResponse struct {
Ok string `json:"ok,omitempty"`
}
// PersonUnsubscribePayload for unsubscribing a person.
type PersonUnsubscribePayload struct {
Email string `json:"person_email"`
}
// PeopleService struct
type PersonService struct {
client *Client
}
// Create a new person for a survey.
func (s *PersonService) Create(p *Person) (*Person, error) {
if p.Email == "" {
return p, errors.New("Email address is required when creating a person.")
}
newPerson := Person{}
req, err := s.client.NewRequest("POST", "people.json", p)
if err != nil {
return p, err
}
_, err = s.client.Do(req, &newPerson)
if err != nil {
return &newPerson, err
}
return &newPerson, nil
}
// Unsubscribe a person from a survey, returns true of false if person was sucessfully Unsubscribed
func (s *PersonService) Unsubscribe(p *Person) (bool, error) {
if p.Email == "" {
return false, errors.New("Email address is required when ubsubscribing a person.")
}
PersonUnsubscribePayload := PersonUnsubscribePayload{Email: p.Email}
req, err := s.client.NewRequest("POST", "unsubscribes.json", PersonUnsubscribePayload)
if err != nil {
return false, err
}
_, err = s.client.Do(req, nil)
if err != nil {
return false, err
}
return true, nil
}
// RemovePendingSurvey from a person who already has a pending survey
func (s *PersonService) RemovePendingSurvey(p *Person) (bool, error) {
if p.Email == "" {
return false, errors.New("Email address is required when unsubscribing a person.")
}
psr := PersonSurveyResponse{}
url := fmt.Sprintf("people/%s/survey_requests/pending.json", p.Email)
req, err := s.client.NewRequest("DELETE", url, nil)
if err != nil {
return false, err
}
_, err = s.client.Do(req, &psr)
if err != nil {
return false, err
}
return true, nil
}