-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathorganizationmember.go
162 lines (130 loc) · 4.92 KB
/
organizationmember.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
package kanka
import (
"bytes"
"encoding/json"
"fmt"
"time"
)
// OrganizationMember contains information about a specific organization member.
// For more information, visit: https://kanka.io/en-US/docs/1.0/organisations#organisation-members
type OrganizationMember struct {
SimpleOrganizationMember
CreatedAt time.Time `json:"created_at"`
CreatedBy int `json:"created_by"`
ID int `json:"id"`
UpdatedAt time.Time `json:"updated_at"`
UpdatedBy int `json:"updated_by"`
}
// SimpleOrganizationMember contains only the simple information about an
// organization member.
// SimpleOrganizationMember is primarily used to create new organization
// members for posting to Kanka.
type SimpleOrganizationMember struct {
CharacterID int `json:"character_id"`
OrganizationID int `json:"organisation_id"`
Role string `json:"role,omitempty"`
IsPrivate bool `json:"is_private,omitempty"`
}
// OrganizationMemberService handles communication with the OrganizationMember endpoint.
type OrganizationMemberService service
// Index returns the list of all OrganizationMembers for the organization
// associated with orgID in the Campaign associated with campID.
// If a non-nil time is provided, Index will only return OrganizationMembers
// that have been changed since that time.
func (os *OrganizationMemberService) Index(campID int, orgID int, sync *time.Time) ([]*OrganizationMember, error) {
var err error
end := EndpointCampaign
if end, err = end.id(campID); err != nil {
return nil, fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(EndpointOrganization)
if end, err = end.id(orgID); err != nil {
return nil, fmt.Errorf("invalid Organization ID: %w", err)
}
end = end.concat(os.end)
if sync != nil {
end = end.sync(*sync)
}
var wrap struct {
Data []*OrganizationMember `json:"data"`
}
if err = os.client.get(end, &wrap); err != nil {
return nil, fmt.Errorf("cannot get OrganizationMember Index from Campaign (ID: %d): %w", campID, err)
}
return wrap.Data, nil
}
// Get returns the OrganizationMember associated with memID for the organization
// associated with orgID from the Campaign associated with campID.
func (os *OrganizationMemberService) Get(campID int, orgID int, memID int) (*OrganizationMember, error) {
var err error
end := EndpointCampaign
if end, err = end.id(campID); err != nil {
return nil, fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(EndpointOrganization)
if end, err = end.id(orgID); err != nil {
return nil, fmt.Errorf("invalid Organization ID: %w", err)
}
end = end.concat(os.end)
if end, err = end.id(memID); err != nil {
return nil, fmt.Errorf("invalid OrganizationMember ID: %w", err)
}
var wrap struct {
Data *OrganizationMember `json:"data"`
}
if err = os.client.get(end, &wrap); err != nil {
return nil, fmt.Errorf("cannot get OrganizationMember (ID: %d) from Campaign (ID: %d): %w", memID, campID, err)
}
return wrap.Data, nil
}
// Update updates an existing OrganizationMember associated with memID for the
// organization associated with orgID from the Campaign associated with campID
// using the provided SimpleOrganizationMember data.
// Update returns the newly updated OrganizationMember.
func (os *OrganizationMemberService) Update(campID int, orgID int, memID int, mem SimpleOrganizationMember) (*OrganizationMember, error) {
var err error
end := EndpointCampaign
if end, err = end.id(campID); err != nil {
return nil, fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(EndpointOrganization)
if end, err = end.id(orgID); err != nil {
return nil, fmt.Errorf("invalid Organization ID: %w", err)
}
end = end.concat(os.end)
if end, err = end.id(memID); err != nil {
return nil, fmt.Errorf("invalid OrganizationMember ID: %w", err)
}
b, err := json.Marshal(mem)
if err != nil {
return nil, fmt.Errorf("cannot marshal SimpleOrganizationMember: %w", err)
}
var wrap struct {
Data *OrganizationMember `json:"data"`
}
if err = os.client.put(end, bytes.NewReader(b), &wrap); err != nil {
return nil, fmt.Errorf("cannot update OrganizationMember for Campaign (ID: %d): '%w'", campID, err)
}
return wrap.Data, nil
}
// Delete deletes an existing OrganizationMember associated with memID from the
// Campaign associated with campID.
func (os *OrganizationMemberService) Delete(campID int, orgID int, memID int) error {
var err error
end := EndpointCampaign
if end, err = end.id(campID); err != nil {
return fmt.Errorf("invalid Campaign ID: %w", err)
}
end = end.concat(EndpointOrganization)
if end, err = end.id(orgID); err != nil {
return fmt.Errorf("invalid Organization ID: %w", err)
}
end = end.concat(os.end)
if end, err = end.id(memID); err != nil {
return fmt.Errorf("invalid OrganizationMember ID: %w", err)
}
if err = os.client.delete(end); err != nil {
return fmt.Errorf("cannot delete OrganizationMember (ID: %d) for Campaign (ID: %d): %w", memID, campID, err)
}
return nil
}