Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #108 Add organization update mutation #109

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions app/graphql/mutations/organizations/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Mutations
module Organizations
class Update < BaseMutation
include AuthenticableApiUser
include RequiredOrganization

graphql_name 'UpdateOrganization'
description 'Updates an Organization'

argument :webhook_url, String

type Types::OrganizationType

def resolve(**args)
validate_organization!

result = ::Organizations::UpdateService
.new(current_organization)
.update(**args)

result.success? ? result.organization : result_error(result)
end
end
end
end
2 changes: 2 additions & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class MutationType < Types::BaseObject
field :login_user, mutation: Mutations::LoginUser
field :register_user, mutation: Mutations::RegisterUser

field :update_organization, mutation: Mutations::Organizations::Update

field :create_billable_metric, mutation: Mutations::BillableMetrics::Create
field :update_billable_metric, mutation: Mutations::BillableMetrics::Update
field :destroy_billable_metric, mutation: Mutations::BillableMetrics::Destroy
Expand Down
1 change: 1 addition & 0 deletions app/graphql/types/organization_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class OrganizationType < Types::BaseObject
field :id, ID, null: false
field :name, String, null: false
field :api_key, String, null: false
field :webhook_url, String
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
end
Expand Down
24 changes: 24 additions & 0 deletions app/services/organizations/update_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Organizations
class UpdateService < BaseService
def initialize(organization)
@organization = organization

super(nil)
end

def update(**args)
organization.update!(webhook_url: args[:webhook_url])
result.organization = organization

result
rescue ActiveRecord::RecordInvalid => e
result.fail_with_validations!(e.record)
end

private

attr_reader :organization
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ en:
invalid_date_range: invalid_date_range
required: relation_must_exist
taken: value_already_exist
url_invalid: url_is_invalid
22 changes: 22 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,16 @@ type Mutation {
input: UpdateCustomerInput!
): Customer

"""
Updates an Organization
"""
updateOrganization(
"""
Parameters for UpdateOrganization
"""
input: UpdateOrganizationInput!
): Organization

"""
Updates an existing Plan
"""
Expand All @@ -1735,6 +1745,7 @@ type Organization {
id: ID!
name: String!
updatedAt: ISO8601DateTime!
webhookUrl: String
}

type Plan {
Expand Down Expand Up @@ -1980,6 +1991,17 @@ input UpdateCustomerInput {
zipcode: String
}

"""
Autogenerated input type of UpdateOrganization
"""
input UpdateOrganizationInput {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
webhookUrl: String!
}

"""
Autogenerated input type of UpdatePlan
"""
Expand Down
82 changes: 82 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4780,6 +4780,35 @@
}
]
},
{
"name": "updateOrganization",
"description": "Updates an Organization",
"type": {
"kind": "OBJECT",
"name": "Organization",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null,
"args": [
{
"name": "input",
"description": "Parameters for UpdateOrganization",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "INPUT_OBJECT",
"name": "UpdateOrganizationInput",
"ofType": null
}
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
}
]
},
{
"name": "updatePlan",
"description": "Updates an existing Plan",
Expand Down Expand Up @@ -4910,6 +4939,20 @@
"deprecationReason": null,
"args": [

]
},
{
"name": "webhookUrl",
"description": null,
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null,
"args": [

]
}
],
Expand Down Expand Up @@ -6674,6 +6717,45 @@
],
"enumValues": null
},
{
"kind": "INPUT_OBJECT",
"name": "UpdateOrganizationInput",
"description": "Autogenerated input type of UpdateOrganization",
"interfaces": null,
"possibleTypes": null,
"fields": null,
"inputFields": [
{
"name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "webhookUrl",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
}
],
"enumValues": null
},
{
"kind": "INPUT_OBJECT",
"name": "UpdatePlanInput",
Expand Down
82 changes: 82 additions & 0 deletions spec/graphql/mutations/organizations/update_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Mutations::Organizations::Update, type: :graphql do
let(:membership) { create(:membership) }
let(:mutation) do
<<~GQL
mutation($input: UpdateOrganizationInput!) {
updateOrganization(input: $input) {
webhookUrl
}
}
GQL
end

it 'updates an organization' do
result = execute_graphql(
current_user: membership.user,
current_organization: membership.organization,
query: mutation,
variables: {
input: {
webhookUrl: 'http://foo.bar',
},
},
)

result_data = result['data']['updateOrganization']

expect(result_data['webhookUrl']).to eq('http://foo.bar')
end

context 'with invalid webhook url' do
it 'returns an error' do
result = execute_graphql(
current_user: membership.user,
current_organization: membership.organization,
query: mutation,
variables: {
input: {
webhookUrl: 'bad_url',
},
},
)

expect_graphql_error(result: result, message: :unprocessable_entity)
end
end

context 'without current user' do
it 'returns an error' do
result = execute_graphql(
current_organization: membership.organization,
query: mutation,
variables: {
input: {
webhookUrl: 'http://foo.bar',
},
},
)

expect_unauthorized_error(result)
end
end

context 'without current organization' do
it 'returns an error' do
result = execute_graphql(
current_user: membership.user,
query: mutation,
variables: {
input: {
webhookUrl: 'http://foo.bar',
},
},
)

expect_forbidden_error(result)
end
end
end
17 changes: 17 additions & 0 deletions spec/services/organizations/update_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Organizations::UpdateService do
subject(:organization_update_service) { described_class.new(organization) }

let(:organization) { create(:organization) }

describe '.update' do
it 'updates the organization' do
result = organization_update_service.update(webhook_url: 'http://foo.bar')

expect(result.organization.webhook_url).to eq('http://foo.bar')
end
end
end