2018-11-15 12:00:08 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
package consoleql
|
2018-11-14 10:50:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/graphql-go/graphql"
|
2018-11-21 15:51:43 +00:00
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-11-21 15:51:43 +00:00
|
|
|
userType = "user"
|
|
|
|
userInputType = "userInput"
|
2018-11-14 10:50:15 +00:00
|
|
|
|
|
|
|
fieldID = "id"
|
|
|
|
fieldEmail = "email"
|
|
|
|
fieldPassword = "password"
|
|
|
|
fieldFirstName = "firstName"
|
|
|
|
fieldLastName = "lastName"
|
2018-11-14 15:13:13 +00:00
|
|
|
fieldCreatedAt = "createdAt"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
2018-11-27 14:20:58 +00:00
|
|
|
// base graphql config for user
|
|
|
|
func baseUserConfig() graphql.ObjectConfig {
|
|
|
|
return graphql.ObjectConfig{
|
2018-11-14 10:50:15 +00:00
|
|
|
Name: userType,
|
|
|
|
Fields: graphql.Fields{
|
|
|
|
fieldID: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
fieldEmail: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
fieldFirstName: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
fieldLastName: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2018-11-14 15:13:13 +00:00
|
|
|
fieldCreatedAt: &graphql.Field{
|
|
|
|
Type: graphql.DateTime,
|
|
|
|
},
|
2018-11-27 14:20:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// graphqlUser creates *graphql.Object type representation of satellite.User
|
2018-12-20 16:18:08 +00:00
|
|
|
// TODO: simplify
|
|
|
|
func graphqlUser() *graphql.Object {
|
|
|
|
return graphql.NewObject(baseUserConfig())
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
2018-11-21 15:51:43 +00:00
|
|
|
|
|
|
|
// graphqlUserInput creates graphql.InputObject type needed to register/update satellite.User
|
|
|
|
func graphqlUserInput(types Types) *graphql.InputObject {
|
|
|
|
return graphql.NewInputObject(graphql.InputObjectConfig{
|
|
|
|
Name: userInputType,
|
|
|
|
Fields: graphql.InputObjectConfigFieldMap{
|
|
|
|
fieldEmail: &graphql.InputObjectFieldConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
fieldFirstName: &graphql.InputObjectFieldConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
fieldLastName: &graphql.InputObjectFieldConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
fieldPassword: &graphql.InputObjectFieldConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromMapUserInfo creates UserInput from input args
|
2019-01-15 13:03:24 +00:00
|
|
|
func fromMapUserInfo(args map[string]interface{}) (user console.UserInfo) {
|
2018-12-10 15:57:06 +00:00
|
|
|
user.Email, _ = args[fieldEmail].(string)
|
|
|
|
user.FirstName, _ = args[fieldFirstName].(string)
|
|
|
|
user.LastName, _ = args[fieldLastName].(string)
|
|
|
|
return
|
|
|
|
}
|
2018-11-21 15:51:43 +00:00
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
func fromMapCreateUser(args map[string]interface{}) (user console.CreateUser) {
|
2018-12-10 15:57:06 +00:00
|
|
|
user.UserInfo = fromMapUserInfo(args)
|
|
|
|
user.Password, _ = args[fieldPassword].(string)
|
2018-11-21 15:51:43 +00:00
|
|
|
return
|
|
|
|
}
|
2018-11-28 10:31:15 +00:00
|
|
|
|
2018-11-28 12:30:38 +00:00
|
|
|
// fillUserInfo fills satellite.UserInfo from satellite.User and input args
|
2019-01-15 13:03:24 +00:00
|
|
|
func fillUserInfo(user *console.User, args map[string]interface{}) console.UserInfo {
|
|
|
|
info := console.UserInfo{
|
2018-11-28 10:31:15 +00:00
|
|
|
Email: user.Email,
|
|
|
|
FirstName: user.FirstName,
|
|
|
|
LastName: user.LastName,
|
|
|
|
}
|
|
|
|
|
|
|
|
for fieldName, fieldValue := range args {
|
|
|
|
value, ok := fieldValue.(string)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch fieldName {
|
|
|
|
case fieldEmail:
|
|
|
|
info.Email = value
|
|
|
|
user.Email = value
|
|
|
|
case fieldFirstName:
|
|
|
|
info.FirstName = value
|
|
|
|
user.FirstName = value
|
|
|
|
case fieldLastName:
|
|
|
|
info.LastName = value
|
|
|
|
user.LastName = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return info
|
|
|
|
}
|