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: invoice custom sections - update mutation #2927

Merged
merged 5 commits into from
Dec 13, 2024
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
29 changes: 29 additions & 0 deletions app/graphql/mutations/invoice_custom_sections/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

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

REQUIRED_PERMISSION = 'invoice_custom_sections:update'

graphql_name 'UpdateInvoiceCustomSection'
description 'Updates an InvoiceCustomSection'

input_object_class Types::InvoiceCustomSections::UpdateInput

type Types::InvoiceCustomSections::Object

def resolve(**args)
selected = args.delete(:selected) || false
invoice_custom_section = ::InvoiceCustomSection.find(args.delete(:id))
result = ::InvoiceCustomSections::UpdateService.call(
invoice_custom_section:, update_params: args.to_h, selected: selected
)

result.success? ? result.invoice_custom_section : result_error(result)
end
end
end
end
18 changes: 18 additions & 0 deletions app/graphql/types/invoice_custom_sections/update_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Types
module InvoiceCustomSections
class UpdateInput < Types::BaseInputObject
graphql_name 'UpdateInvoiceCustomSectionInput'

argument :id, ID, required: true

argument :description, String, required: false
argument :details, String, required: false
argument :display_name, String, required: false
argument :name, String, required: false

argument :selected, Boolean, required: false
end
end
end
1 change: 1 addition & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,6 @@ class MutationType < Types::BaseObject
field :update_api_key, mutation: Mutations::ApiKeys::Update

field :create_invoice_custom_section, mutation: Mutations::InvoiceCustomSections::Create
field :update_invoice_custom_section, mutation: Mutations::InvoiceCustomSections::Update
end
end
26 changes: 26 additions & 0 deletions schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

128 changes: 128 additions & 0 deletions schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions spec/graphql/mutations/invoice_custom_sections/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,28 @@
expect(result_data['selected']).to eq(true)
end
end

context 'when fail to create invoice_custom_section' do
it 'returns an error' do
result = execute_graphql(
current_user: membership.user,
current_organization: membership.organization,
permissions: required_permission,
query: mutation,
variables: {
input: {
code: nil,
name: 'First Invoice Custom Section',
description: 'this invoice custom section will be added in the PDF',
details: 'This is the exact information shown in the invoice',
displayName: 'Section name displayed in the invoice',
selected: true
}
}
)

expect(result['errors']).to be_present
expect(result['errors'].first['message']).to include('invalid value for code')
end
end
end
92 changes: 92 additions & 0 deletions spec/graphql/mutations/invoice_custom_sections/update_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Mutations::InvoiceCustomSections::Update, type: :graphql do
let(:required_permission) { 'invoice_custom_sections:update' }
let(:membership) { create(:membership) }

let(:mutation) do
<<~GQL
mutation($input: UpdateInvoiceCustomSectionInput!) {
updateInvoiceCustomSection(input: $input) {
id,
code,
name,
description,
details,
displayName,
selected
}
}
GQL
end

it_behaves_like 'requires current user'
it_behaves_like 'requires current organization'
it_behaves_like 'requires permission', 'invoice_custom_sections:update'

context 'when there is a invoice_custom_section' do
let(:invoice_custom_section) { create(:invoice_custom_section) }

before { invoice_custom_section }

it 'updates a invoice_custom_section' do
result = execute_graphql(
current_user: membership.user,
current_organization: membership.organization,
permissions: required_permission,
query: mutation,
variables: {
input: {
id: invoice_custom_section.id,
name: 'First Invoice Custom Section',
description: 'this invoice custom section will be added in the PDF',
details: 'This is the exact information shown in the invoice',
displayName: 'Section name displayed in the invoice',
selected: true
}
}
)

result_data = result['data']['updateInvoiceCustomSection']

aggregate_failures do
expect(result_data['id']).to be_present
expect(result_data['name']).to eq('First Invoice Custom Section')
expect(result_data['displayName']).to eq('Section name displayed in the invoice')
expect(result_data['code']).to eq(invoice_custom_section.code)
expect(result_data['description']).to eq('this invoice custom section will be added in the PDF')
expect(result_data['details']).to eq('This is the exact information shown in the invoice')
expect(result_data['selected']).to eq(true)
end
end
end

context 'when updating to wrong values' do
let(:invoice_custom_section) { create(:invoice_custom_section) }

before { invoice_custom_section }

it 'returns an error' do
result = execute_graphql(
current_user: membership.user,
current_organization: membership.organization,
permissions: required_permission,
query: mutation,
variables: {
input: {
id: invoice_custom_section.id,
name: nil,
description: 'this invoice custom section will be added in the PDF',
details: 'This is the exact information shown in the invoice',
displayName: 'Section name displayed in the invoice',
selected: true
}
}
)

expect(result['errors']).to be_present
end
end
end
15 changes: 15 additions & 0 deletions spec/graphql/types/invoice_custom_sections/update_input_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Types::InvoiceCustomSections::UpdateInput do
subject { described_class }

it { is_expected.to accept_argument(:id).of_type('ID!') }

it { is_expected.to accept_argument(:description).of_type('String') }
it { is_expected.to accept_argument(:details).of_type('String') }
it { is_expected.to accept_argument(:display_name).of_type('String') }
it { is_expected.to accept_argument(:name).of_type('String') }
it { is_expected.to accept_argument(:selected).of_type('Boolean') }
end
Loading