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-26 10:47:23 +00:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-03-26 15:56:16 +00:00
|
|
|
"go.uber.org/zap"
|
2018-11-14 10:50:15 +00:00
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/post"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-03-02 15:22:20 +00:00
|
|
|
"storj.io/storj/satellite/mailservice"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Mutation is graphql request that modifies data
|
|
|
|
Mutation = "mutation"
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
// CreateProjectMutation is a mutation name for project creation
|
|
|
|
CreateProjectMutation = "createProject"
|
|
|
|
// DeleteProjectMutation is a mutation name for project deletion
|
|
|
|
DeleteProjectMutation = "deleteProject"
|
|
|
|
// UpdateProjectDescriptionMutation is a mutation name for project updating
|
|
|
|
UpdateProjectDescriptionMutation = "updateProjectDescription"
|
|
|
|
|
|
|
|
// AddProjectMembersMutation is a mutation name for adding new project members
|
|
|
|
AddProjectMembersMutation = "addProjectMembers"
|
|
|
|
// DeleteProjectMembersMutation is a mutation name for deleting project members
|
|
|
|
DeleteProjectMembersMutation = "deleteProjectMembers"
|
|
|
|
|
|
|
|
// CreateAPIKeyMutation is a mutation name for api key creation
|
|
|
|
CreateAPIKeyMutation = "createAPIKey"
|
2019-02-13 11:34:40 +00:00
|
|
|
// DeleteAPIKeysMutation is a mutation name for api key deleting
|
|
|
|
DeleteAPIKeysMutation = "deleteAPIKeys"
|
2019-01-24 16:26:36 +00:00
|
|
|
|
2019-07-10 21:29:26 +01:00
|
|
|
// AddPaymentMethodMutation is mutation name for adding new payment method
|
|
|
|
AddPaymentMethodMutation = "addPaymentMethod"
|
|
|
|
// DeletePaymentMethodMutation is mutation name for deleting payment method
|
|
|
|
DeletePaymentMethodMutation = "deletePaymentMethod"
|
|
|
|
// SetDefaultPaymentMethodMutation is mutation name setting payment method as default payment method
|
|
|
|
SetDefaultPaymentMethodMutation = "setDefaultPaymentMethod"
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
// InputArg is argument name for all input types
|
|
|
|
InputArg = "input"
|
|
|
|
// FieldProjectID is field name for projectID
|
|
|
|
FieldProjectID = "projectID"
|
|
|
|
// FieldNewPassword is a field name for new password
|
|
|
|
FieldNewPassword = "newPassword"
|
2019-03-19 17:55:43 +00:00
|
|
|
// Secret is a field name for registration token for user creation during Vanguard release
|
|
|
|
Secret = "secret"
|
2019-07-23 17:08:07 +01:00
|
|
|
// ReferrerUserID is a field name for passing referrer's user id
|
2019-07-26 18:58:17 +01:00
|
|
|
ReferrerUserID = "referrerUserId"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// rootMutation creates mutation for graphql populated by AccountsClient
|
2019-04-04 15:56:20 +01:00
|
|
|
func rootMutation(log *zap.Logger, service *console.Service, mailService *mailservice.Service, types *TypeCreator) *graphql.Object {
|
2018-11-14 10:50:15 +00:00
|
|
|
return graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: Mutation,
|
|
|
|
Fields: graphql.Fields{
|
2018-11-26 10:47:23 +00:00
|
|
|
// creates project from input params
|
2019-01-24 16:26:36 +00:00
|
|
|
CreateProjectMutation: &graphql.Field{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: types.project,
|
2018-11-26 10:47:23 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2019-01-24 16:26:36 +00:00
|
|
|
InputArg: &graphql.ArgumentConfig{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: graphql.NewNonNull(types.projectInput),
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-01-24 16:26:36 +00:00
|
|
|
var projectInput = fromMapProjectInfo(p.Args[InputArg].(map[string]interface{}))
|
2018-11-26 10:47:23 +00:00
|
|
|
|
2019-09-04 16:02:39 +01:00
|
|
|
project, err := service.CreateProject(p.Context, projectInput)
|
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2019-09-04 16:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return project, nil
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// deletes project by id, taken from input params
|
2019-01-24 16:26:36 +00:00
|
|
|
DeleteProjectMutation: &graphql.Field{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: types.project,
|
2018-11-26 10:47:23 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldID: &graphql.ArgumentConfig{
|
2018-11-28 16:20:23 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-01-24 16:26:36 +00:00
|
|
|
inputID := p.Args[FieldID].(string)
|
2018-11-26 10:47:23 +00:00
|
|
|
projectID, err := uuid.Parse(inputID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-08 14:54:35 +00:00
|
|
|
project, err := service.GetProject(p.Context, *projectID)
|
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2019-01-08 14:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = service.DeleteProject(p.Context, project.ID); err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2019-01-08 14:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return project, nil
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
2018-11-28 16:20:23 +00:00
|
|
|
// updates project description
|
2019-01-24 16:26:36 +00:00
|
|
|
UpdateProjectDescriptionMutation: &graphql.Field{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: types.project,
|
2018-11-26 10:47:23 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldID: &graphql.ArgumentConfig{
|
2018-11-28 16:20:23 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldDescription: &graphql.ArgumentConfig{
|
2018-11-28 16:20:23 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-01-24 16:26:36 +00:00
|
|
|
description := p.Args[FieldDescription].(string)
|
2018-11-26 10:47:23 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
inputID := p.Args[FieldID].(string)
|
2018-11-26 10:47:23 +00:00
|
|
|
projectID, err := uuid.Parse(inputID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-04 16:02:39 +01:00
|
|
|
project, err := service.UpdateProject(p.Context, *projectID, description)
|
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2019-09-04 16:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return project, nil
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-06 14:40:32 +00:00
|
|
|
// add user as member of given project
|
2019-01-24 16:26:36 +00:00
|
|
|
AddProjectMembersMutation: &graphql.Field{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: types.project,
|
2018-12-06 14:40:32 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldProjectID: &graphql.ArgumentConfig{
|
2018-12-06 14:40:32 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldEmail: &graphql.ArgumentConfig{
|
2018-12-20 15:36:32 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.NewList(graphql.String)),
|
2018-12-06 14:40:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-01-24 16:26:36 +00:00
|
|
|
pID, _ := p.Args[FieldProjectID].(string)
|
|
|
|
emails, _ := p.Args[FieldEmail].([]interface{})
|
2018-12-06 14:40:32 +00:00
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
projectID, err := uuid.Parse(pID)
|
2018-12-06 14:40:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
var userEmails []string
|
|
|
|
for _, email := range emails {
|
|
|
|
userEmails = append(userEmails, email.(string))
|
2018-12-20 15:36:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 15:42:19 +00:00
|
|
|
project, err := service.GetProject(p.Context, *projectID)
|
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2019-03-06 15:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
users, err := service.AddProjectMembers(p.Context, *projectID, userEmails)
|
2018-12-21 15:41:53 +00:00
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2018-12-20 15:36:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 15:56:16 +00:00
|
|
|
rootObject := p.Info.RootValue.(map[string]interface{})
|
|
|
|
origin := rootObject["origin"].(string)
|
|
|
|
signIn := origin + rootObject[SignInPath].(string)
|
|
|
|
|
2019-05-17 13:29:35 +01:00
|
|
|
for _, user := range users {
|
|
|
|
userName := user.ShortName
|
|
|
|
if user.ShortName == "" {
|
|
|
|
userName = user.FullName
|
2019-03-06 15:42:19 +00:00
|
|
|
}
|
2019-05-17 13:29:35 +01:00
|
|
|
|
2019-09-27 17:48:53 +01:00
|
|
|
contactInfoURL := rootObject[ContactInfoURL].(string)
|
|
|
|
letUsKnowURL := rootObject[LetUsKnowURL].(string)
|
|
|
|
termsAndConditionsURL := rootObject[TermsAndConditionsURL].(string)
|
|
|
|
|
2019-05-17 13:29:35 +01:00
|
|
|
mailService.SendRenderedAsync(
|
|
|
|
p.Context,
|
|
|
|
[]post.Address{{Address: user.Email, Name: userName}},
|
|
|
|
&ProjectInvitationEmail{
|
2019-09-27 17:48:53 +01:00
|
|
|
Origin: origin,
|
|
|
|
UserName: userName,
|
|
|
|
ProjectName: project.Name,
|
|
|
|
SignInLink: signIn,
|
|
|
|
LetUsKnowURL: letUsKnowURL,
|
|
|
|
TermsAndConditionsURL: termsAndConditionsURL,
|
|
|
|
ContactInfoURL: contactInfoURL,
|
2019-05-17 13:29:35 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2019-03-06 15:42:19 +00:00
|
|
|
|
2019-03-26 15:56:16 +00:00
|
|
|
return project, nil
|
2018-12-06 14:40:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// delete user membership for given project
|
2019-01-24 16:26:36 +00:00
|
|
|
DeleteProjectMembersMutation: &graphql.Field{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: types.project,
|
2018-12-06 14:40:32 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldProjectID: &graphql.ArgumentConfig{
|
2018-12-06 14:40:32 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldEmail: &graphql.ArgumentConfig{
|
2018-12-20 15:36:32 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.NewList(graphql.String)),
|
2018-12-06 14:40:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-01-24 16:26:36 +00:00
|
|
|
pID, _ := p.Args[FieldProjectID].(string)
|
|
|
|
emails, _ := p.Args[FieldEmail].([]interface{})
|
2018-12-06 14:40:32 +00:00
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
projectID, err := uuid.Parse(pID)
|
2018-12-06 14:40:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
var userEmails []string
|
|
|
|
for _, email := range emails {
|
|
|
|
userEmails = append(userEmails, email.(string))
|
2018-12-20 15:36:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
err = service.DeleteProjectMembers(p.Context, *projectID, userEmails)
|
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2019-09-04 16:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
project, err := service.GetProject(p.Context, *projectID)
|
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2018-12-20 15:36:32 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 16:02:39 +01:00
|
|
|
return project, nil
|
2018-12-06 14:40:32 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-27 15:30:15 +00:00
|
|
|
// creates new api key
|
2019-01-24 16:26:36 +00:00
|
|
|
CreateAPIKeyMutation: &graphql.Field{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: types.createAPIKey,
|
2018-12-27 15:30:15 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldProjectID: &graphql.ArgumentConfig{
|
2018-12-27 15:30:15 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldName: &graphql.ArgumentConfig{
|
2018-12-27 15:30:15 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-01-24 16:26:36 +00:00
|
|
|
projectID, _ := p.Args[FieldProjectID].(string)
|
|
|
|
name, _ := p.Args[FieldName].(string)
|
2018-12-27 15:30:15 +00:00
|
|
|
|
|
|
|
pID, err := uuid.Parse(projectID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, key, err := service.CreateAPIKey(p.Context, *pID, name)
|
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2018-12-27 15:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return createAPIKey{
|
2019-05-24 17:51:27 +01:00
|
|
|
Key: key.Serialize(),
|
2018-12-27 15:30:15 +00:00
|
|
|
KeyInfo: info,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// deletes api key
|
2019-02-13 11:34:40 +00:00
|
|
|
DeleteAPIKeysMutation: &graphql.Field{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: graphql.NewList(types.apiKeyInfo),
|
2018-12-27 15:30:15 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldID: &graphql.ArgumentConfig{
|
2019-02-13 11:34:40 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.NewList(graphql.String)),
|
2018-12-27 15:30:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-02-13 11:34:40 +00:00
|
|
|
paramKeysID, _ := p.Args[FieldID].([]interface{})
|
|
|
|
|
|
|
|
var keyIds []uuid.UUID
|
|
|
|
var keys []console.APIKeyInfo
|
|
|
|
for _, id := range paramKeysID {
|
|
|
|
keyID, err := uuid.Parse(id.(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := service.GetAPIKeyInfo(p.Context, *keyID)
|
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2019-02-13 11:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyIds = append(keyIds, *keyID)
|
|
|
|
keys = append(keys, *key)
|
2018-12-27 15:30:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 11:34:40 +00:00
|
|
|
err := service.DeleteAPIKeys(p.Context, keyIds)
|
2018-12-27 15:30:15 +00:00
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2018-12-27 15:30:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 11:34:40 +00:00
|
|
|
return keys, nil
|
2018-12-27 15:30:15 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 21:29:26 +01:00
|
|
|
AddPaymentMethodMutation: &graphql.Field{
|
|
|
|
Type: graphql.Boolean,
|
2019-09-27 10:46:37 +01:00
|
|
|
Args: graphql.FieldConfigArgument{},
|
2019-07-10 21:29:26 +01:00
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-09-27 10:46:37 +01:00
|
|
|
return nil, nil
|
2019-07-10 21:29:26 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
DeletePaymentMethodMutation: &graphql.Field{
|
|
|
|
Type: graphql.Boolean,
|
2019-09-27 10:46:37 +01:00
|
|
|
Args: graphql.FieldConfigArgument{},
|
2019-07-10 21:29:26 +01:00
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-09-27 10:46:37 +01:00
|
|
|
return nil, nil
|
2019-07-10 21:29:26 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
SetDefaultPaymentMethodMutation: &graphql.Field{
|
|
|
|
Type: graphql.Boolean,
|
2019-09-27 10:46:37 +01:00
|
|
|
Args: graphql.FieldConfigArgument{},
|
2019-07-10 21:29:26 +01:00
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-09-27 10:46:37 +01:00
|
|
|
return nil, nil
|
2019-07-10 21:29:26 +01:00
|
|
|
},
|
|
|
|
},
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|