storj/satellite/console/consoleweb/consoleql/user.go

124 lines
3.1 KiB
Go
Raw Normal View History

2019-01-24 16:26:36 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package consoleql
import (
"github.com/graphql-go/graphql"
2018-11-21 15:51:43 +00:00
"storj.io/storj/satellite/console"
)
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"
// FieldFullName is a field name for "first name"
FieldFullName = "fullName"
// FieldShortName is a field name for "last name"
FieldShortName = "shortName"
2019-01-24 16:26:36 +00:00
// FieldCreatedAt is a field name for created at timestamp
FieldCreatedAt = "createdAt"
// FieldPartnerID is a field name for partnerID
FieldPartnerID = "partnerId"
)
// base graphql config for user
func baseUserConfig() graphql.ObjectConfig {
return graphql.ObjectConfig{
2019-01-24 16:26:36 +00:00
Name: UserType,
Fields: graphql.Fields{
2019-01-24 16:26:36 +00:00
FieldID: &graphql.Field{
Type: graphql.String,
},
2019-01-24 16:26:36 +00:00
FieldEmail: &graphql.Field{
Type: graphql.String,
},
FieldFullName: &graphql.Field{
Type: graphql.String,
},
FieldShortName: &graphql.Field{
Type: graphql.String,
},
2019-01-24 16:26:36 +00:00
FieldCreatedAt: &graphql.Field{
Type: graphql.DateTime,
},
FieldPartnerID: &graphql.Field{
Type: graphql.String,
},
},
}
}
// graphqlUser creates *graphql.Object type representation of satellite.User
// TODO: simplify
func graphqlUser() *graphql.Object {
return graphql.NewObject(baseUserConfig())
}
2018-11-21 15:51:43 +00:00
// graphqlUserInput creates graphql.InputObject type needed to register/update satellite.User
2019-04-04 15:56:20 +01:00
func graphqlUserInput() *graphql.InputObject {
2018-11-21 15:51:43 +00:00
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,
},
FieldFullName: &graphql.InputObjectFieldConfig{
2018-11-21 15:51:43 +00:00
Type: graphql.String,
},
FieldShortName: &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,
},
FieldPartnerID: &graphql.InputObjectFieldConfig{
Type: graphql.String,
},
2018-11-21 15:51:43 +00:00
},
})
}
func fromMapCreateUser(args map[string]interface{}) (user console.CreateUser) {
2019-01-24 16:26:36 +00:00
user.Email, _ = args[FieldEmail].(string)
user.FullName, _ = args[FieldFullName].(string)
user.ShortName, _ = args[FieldShortName].(string)
2019-01-24 16:26:36 +00:00
user.Password, _ = args[FieldPassword].(string)
user.PartnerID, _ = args[FieldPartnerID].(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
func fillUserInfo(user *console.User, args map[string]interface{}) console.UserInfo {
info := console.UserInfo{
FullName: user.FullName,
ShortName: user.ShortName,
2018-11-28 10:31:15 +00:00
}
for fieldName, fieldValue := range args {
value, ok := fieldValue.(string)
if !ok {
continue
}
switch fieldName {
case FieldFullName:
info.FullName = value
user.FullName = value
case FieldShortName:
info.ShortName = value
user.ShortName = value
2018-11-28 10:31:15 +00:00
}
}
return info
}