-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
153 lines (113 loc) · 3.85 KB
/
user.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
package client
import (
"context"
"errors"
"github.com/bangladesh-data/terraform-provider-soc2bd/soc2bd/internal/client/query"
"github.com/bangladesh-data/terraform-provider-soc2bd/soc2bd/internal/model"
)
func (client *Client) ReadUsers(ctx context.Context) ([]*model.User, error) {
opr := resourceUser.read()
variables := newVars(
cursor(query.CursorUsers),
pageLimit(client.pageLimit),
)
response := query.ReadUsers{}
if err := client.query(ctx, &response, variables, opr.withCustomName("readUsers"), attr{id: "All"}); err != nil {
if errors.Is(err, ErrGraphqlResultIsEmpty) {
return nil, nil
}
return nil, err
}
if err := response.FetchPages(ctx, client.readUsersAfter, variables); err != nil {
return nil, err //nolint
}
return response.ToModel(), nil
}
func (client *Client) readUsersAfter(ctx context.Context, variables map[string]interface{}, cursor string) (*query.PaginatedResource[*query.UserEdge], error) {
opr := resourceUser.read()
variables[query.CursorUsers] = cursor
response := query.ReadUsers{}
if err := client.query(ctx, &response, variables, opr.withCustomName("readUsers"), attr{id: "All"}); err != nil {
return nil, err
}
return &response.PaginatedResource, nil
}
func (client *Client) ReadUser(ctx context.Context, userID string) (*model.User, error) {
opr := resourceUser.read()
if userID == "" {
return nil, opr.apiError(ErrGraphqlIDIsEmpty)
}
variables := newVars(gqlID(userID))
response := query.ReadUser{}
if err := client.query(ctx, &response, variables, opr, attr{id: userID}); err != nil {
return nil, err
}
return response.ToModel(), nil
}
func (client *Client) CreateUser(ctx context.Context, input *model.User) (*model.User, error) {
opr := resourceUser.create()
if input == nil || input.Email == "" {
return nil, opr.apiError(ErrGraphqlEmailIsEmpty)
}
variables := newVars(
gqlVar(input.Email, "email"),
gqlNullable(input.FirstName, "firstName"),
gqlNullable(input.LastName, "lastName"),
gqlVar(query.UserRole(input.Role), "role"),
gqlVar(input.SendInvite, "shouldSendInvite"),
)
response := query.CreateUser{}
if err := client.mutate(ctx, &response, variables, opr, attr{name: input.Email}); err != nil {
return nil, err
}
return response.ToModel(), nil
}
func (client *Client) UpdateUser(ctx context.Context, input *model.UserUpdate) (*model.User, error) {
opr := resourceUser.update()
if input == nil || input.ID == "" {
return nil, opr.apiError(ErrGraphqlIDIsEmpty)
}
if input.FirstName != nil || input.LastName != nil || input.IsActive != nil {
variables := newVars(
gqlID(input.ID),
gqlNullable(input.FirstName, "firstName"),
gqlNullable(input.LastName, "lastName"),
gqlNullable(query.NewUserStateUpdateInput(input.State()), "state"),
)
response := query.UpdateUser{}
if err := client.mutate(ctx, &response, variables, opr, attr{id: input.ID}); err != nil {
return nil, err
}
user := response.ToModel()
if input.Role == nil {
return user, nil
}
}
return client.UpdateUserRole(ctx, input)
}
func (client *Client) UpdateUserRole(ctx context.Context, input *model.UserUpdate) (*model.User, error) {
opr := resourceUser.update()
if input == nil || input.ID == "" {
return nil, opr.apiError(ErrGraphqlIDIsEmpty)
}
if input.Role == nil {
return client.ReadUser(ctx, input.ID)
}
variables := newVars(
gqlID(input.ID),
gqlVar(query.UserRole(*input.Role), "role"),
)
response := query.UpdateUserRole{}
if err := client.mutate(ctx, &response, variables, opr, attr{id: input.ID}); err != nil {
return nil, err
}
return response.ToModel(), nil
}
func (client *Client) DeleteUser(ctx context.Context, userID string) error {
opr := resourceUser.delete()
if userID == "" {
return opr.apiError(ErrGraphqlIDIsEmpty)
}
response := query.DeleteUser{}
return client.mutate(ctx, &response, newVars(gqlID(userID)), opr, attr{id: userID})
}