2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-15 12:00:08 +00:00
|
|
|
// 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 (
|
2019-01-24 16:26:36 +00:00
|
|
|
// UserType is a graphql type for user
|
|
|
|
UserType = "user"
|
|
|
|
// UserInputType is a graphql type for user input
|
|
|
|
UserInputType = "userInput"
|
|
|
|
// FieldID is a field name for id
|
|
|
|
FieldID = "id"
|
|
|
|
// FieldEmail is a field name for email
|
|
|
|
FieldEmail = "email"
|
|
|
|
// FieldPassword is a field name for password
|
|
|
|
FieldPassword = "password"
|
|
|
|
// FieldFirstName is a field name for "first name"
|
|
|
|
FieldFirstName = "firstName"
|
|
|
|
// FieldLastName is a field name for "last name"
|
|
|
|
FieldLastName = "lastName"
|
|
|
|
// FieldCreatedAt is a field name for created at timestamp
|
|
|
|
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{
|
2019-01-24 16:26:36 +00:00
|
|
|
Name: UserType,
|
2018-11-14 10:50:15 +00:00
|
|
|
Fields: graphql.Fields{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldID: &graphql.Field{
|
2018-11-14 10:50:15 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldEmail: &graphql.Field{
|
2018-11-14 10:50:15 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldFirstName: &graphql.Field{
|
2018-11-14 10:50:15 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldLastName: &graphql.Field{
|
2018-11-14 10:50:15 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldCreatedAt: &graphql.Field{
|
2018-11-14 15:13:13 +00:00
|
|
|
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{
|
2019-01-24 16:26:36 +00:00
|
|
|
Name: UserInputType,
|
2018-11-21 15:51:43 +00:00
|
|
|
Fields: graphql.InputObjectConfigFieldMap{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldEmail: &graphql.InputObjectFieldConfig{
|
2018-11-21 15:51:43 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldFirstName: &graphql.InputObjectFieldConfig{
|
2018-11-21 15:51:43 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldLastName: &graphql.InputObjectFieldConfig{
|
2018-11-21 15:51:43 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldPassword: &graphql.InputObjectFieldConfig{
|
2018-11-21 15:51:43 +00:00
|
|
|
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) {
|
2019-01-24 16:26:36 +00:00
|
|
|
user.Email, _ = args[FieldEmail].(string)
|
|
|
|
user.FirstName, _ = args[FieldFirstName].(string)
|
|
|
|
user.LastName, _ = args[FieldLastName].(string)
|
2018-12-10 15:57:06 +00:00
|
|
|
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)
|
2019-01-24 16:26:36 +00:00
|
|
|
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 {
|
2019-01-24 16:26:36 +00:00
|
|
|
case FieldEmail:
|
2018-11-28 10:31:15 +00:00
|
|
|
info.Email = value
|
|
|
|
user.Email = value
|
2019-01-24 16:26:36 +00:00
|
|
|
case FieldFirstName:
|
2018-11-28 10:31:15 +00:00
|
|
|
info.FirstName = value
|
|
|
|
user.FirstName = value
|
2019-01-24 16:26:36 +00:00
|
|
|
case FieldLastName:
|
2018-11-28 10:31:15 +00:00
|
|
|
info.LastName = value
|
|
|
|
user.LastName = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return info
|
|
|
|
}
|