/
user.go
120 lines (116 loc) · 3.25 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
package gql
import (
"errors"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/db"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/db/models"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)
// UserType defines a graphql type for User
var UserType = graphql.NewObject(
graphql.ObjectConfig{
Name: "User",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.NewNonNull(graphql.ID),
},
"email": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
},
"name": &graphql.Field{
Type: graphql.String,
},
"passwordResetToken": &graphql.Field{
Type: graphql.String,
},
"passwordResetTokenExpiry": &graphql.Field{
Type: graphql.DateTime,
},
"createdAt": &graphql.Field{
Type: graphql.DateTime,
},
"updatedAt": &graphql.Field{
Type: graphql.DateTime,
},
"defaultStoreId": &graphql.Field{
Type: graphql.ID,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
userID := p.Source.(models.User).ID
//TODO add index for this query?
var storeIDs []uuid.UUID
query := db.Manager.
Model(&models.Store{}).
Select("stores.id").
Joins("INNER JOIN store_users ON store_users.store_id = stores.id").
Joins("INNER JOIN store_user_preferences ON store_user_preferences.store_user_id = store_users.id").
Where("store_users.user_id = ?", userID).
Where("store_user_preferences.default_store = ?", true).
Pluck("stores.id", &storeIDs).
Error
if err := query; err != nil {
return nil, errors.New("default store id not found for user")
}
if len(storeIDs) > 0 {
return storeIDs[0], nil
}
return nil, nil
},
},
"accessToken": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
userID := p.Source.(*models.User).ID
var authToken models.AuthToken
query := db.Manager.
Select("access_token").
Where("user_id = ?", userID).
Order("created_at DESC").
Last(&authToken).
Error
if err := query; err != nil {
return nil, errors.New("token not found for user")
}
return authToken.AccessToken, nil
},
},
// DEPRECATED in favour of accessToken
"token": &graphql.Field{
Type: AuthTokenType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
userID := p.Source.(*models.User).ID
authToken := &models.AuthToken{}
query := db.Manager.
Where("user_id = ?", userID).
Order("created_at DESC").
Last(&authToken).
Error
if err := query; err != nil {
return nil, errors.New("token not found for user")
}
return authToken, nil
},
},
},
},
)
// BasicUserType is similar to UserType except it does not include fields for anything
// except basic user info. Used in sharableStore query
var BasicUserType = graphql.NewObject(
graphql.ObjectConfig{
Name: "BasicUser",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.NewNonNull(graphql.ID),
},
"email": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
},
"firstName": &graphql.Field{
Type: graphql.String,
},
"lastName": &graphql.Field{
Type: graphql.String,
},
},
},
)