-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver_userprofile.go
110 lines (83 loc) · 2.46 KB
/
resolver_userprofile.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
package main
import (
"errors"
"github.com/op/go-logging"
"go.mongodb.org/mongo-driver/mongo"
"golang.org/x/net/context"
//"go.mongodb.org/mongo-driver/bson"
graphql "github.com/graph-gophers/graphql-go"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type userProfileUpdateInput struct {
OrgId *graphql.ID
}
/////////// User Profile Resolver
type UserProfileResolver struct {
log *logging.Logger
db *mongo.Database
users *Users
up *UserProfile
}
func (r *UserProfileResolver) Email() string {
return r.up.Email
}
func (r *UserProfileResolver) IsAdmin() bool {
return r.up.IsAdmin
}
func (r *UserProfileResolver) OrgId() graphql.ID {
return graphql.ID(r.up.OrgId.Hex())
}
func (r *UserProfileResolver) Orgs() []*OrgResolver {
var result []*OrgResolver
// get all orgs assigned to user
orgs, err := r.users.FindUserOrgs(r.up.Id)
if err != nil {
r.log.Errorf("GQL: error : %v", err)
return result
}
// convert orgs to org resolvers
for i := 0; i < len(orgs); i++ {
result = append(result, &OrgResolver{r.log, r.db, r.users, &orgs[i]})
}
return result
}
/////////// Resolver
// get active user profile
func (r *Resolver) UserProfile(ctx context.Context) (*UserProfileResolver, error) {
// authorization checks
profileValue := ctx.Value("profile")
if profileValue == nil {
r.log.Errorf("GQL: Missing user profile")
return nil, errors.New("missing user profile")
}
profile := profileValue.(*UserProfile)
r.log.Debugf("ctx %v", profile)
return &UserProfileResolver{r.log, r.db, r.users, profile}, nil
}
func (r *Resolver) UpdateUserProfile(ctx context.Context, args struct{ Profile userProfileUpdateInput }) (*UserProfileResolver, error) {
r.log.Debugf("Updating user profile")
// get profile
profileValue := ctx.Value("profile")
if profileValue == nil {
r.log.Errorf("GQL: Missing user profile")
return nil, errors.New("missing user profile")
}
profile := profileValue.(*UserProfile)
// try to find similar user matching new email
if args.Profile.OrgId != nil {
r.log.Debugf("Updating user profile active org to <%s>", string(*args.Profile.OrgId))
// create ObjectID from string
orgId, err := primitive.ObjectIDFromHex(string(*args.Profile.OrgId))
if err != nil {
return nil, err
}
// set active org
err = r.users.SetActiveOrg(profile.Id, orgId)
if err != nil {
return nil, err
}
profile.OrgId = orgId
}
r.log.Debugf("User profile updated")
return &UserProfileResolver{r.log, r.db, r.users, profile}, nil
}