Skip to content

Commit

Permalink
Feat: invoice custom sections discard mutation (#2939)
Browse files Browse the repository at this point in the history
## Description

- Added a mutation to trigger InvoiceCustomSection::DestroyService;
- Added single invoice_custom_section resolver
  • Loading branch information
annvelents authored Dec 17, 2024
1 parent 73c0021 commit ade839d
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 2 deletions.
26 changes: 26 additions & 0 deletions app/graphql/mutations/invoice_custom_sections/destroy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

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

REQUIRED_PERMISSION = 'invoice_custom_sections:delete'

graphql_name 'DestroyInvoiceCustomSection'
description 'Deletes an invoice_custom_section'

argument :id, ID, required: true

field :id, ID, null: true

def resolve(id:)
invoice_custom_section = current_organization.invoice_custom_sections.find_by(id:)
result = ::InvoiceCustomSections::DestroyService.call(invoice_custom_section:)

result.success? ? result.invoice_custom_section : result_error(result)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Resolvers
module Customers
class InvoiceCustomSectionsResolver < Resolvers::BaseResolver
end
end
end
20 changes: 20 additions & 0 deletions app/graphql/resolvers/invoice_custom_section_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Resolvers
class InvoiceCustomSectionResolver < Resolvers::BaseResolver
include AuthenticableApiUser
include RequiredOrganization

description 'Query a single invoice_custom_section of an organization'

argument :id, ID, required: true, description: 'Uniq ID of the invoice_custom_section'

type Types::InvoiceCustomSections::Object, null: false

def resolve(id: nil)
current_organization.invoice_custom_sections.find(id)
rescue ActiveRecord::RecordNotFound
not_found_error(resource: 'invoice_custom_section')
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,6 +161,7 @@ class MutationType < Types::BaseObject
field :update_api_key, mutation: Mutations::ApiKeys::Update

field :create_invoice_custom_section, mutation: Mutations::InvoiceCustomSections::Create
field :destroy_invoice_custom_section, mutation: Mutations::InvoiceCustomSections::Destroy
field :update_invoice_custom_section, mutation: Mutations::InvoiceCustomSections::Update
end
end
1 change: 1 addition & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class QueryType < Types::BaseObject
field :invoice, resolver: Resolvers::InvoiceResolver
field :invoice_collections, resolver: Resolvers::Analytics::InvoiceCollectionsResolver
field :invoice_credit_notes, resolver: Resolvers::InvoiceCreditNotesResolver
field :invoice_custom_section, resolver: Resolvers::InvoiceCustomSectionResolver
field :invoice_custom_sections, resolver: Resolvers::InvoiceCustomSectionsResolver
field :invoiced_usages, resolver: Resolvers::Analytics::InvoicedUsagesResolver
field :invoices, resolver: Resolvers::InvoicesResolver
Expand Down
4 changes: 3 additions & 1 deletion app/models/invoice_custom_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ class InvoiceCustomSection < ApplicationRecord
validates :name, presence: true
validates :code, presence: true, uniqueness: {scope: :organization_id}

default_scope -> { kept }

def selected_for_organization?
organization.invoice_custom_sections.exists?(id: id)
organization.selected_invoice_custom_sections.exists?(id: id)
end
end

Expand Down
4 changes: 3 additions & 1 deletion app/services/invoice_custom_sections/destroy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ def initialize(invoice_custom_section:)
end

def call
return result.not_found_failure!(resource: 'invoice_custom_section') unless invoice_custom_section

ActiveRecord::Base.transaction do
invoice_custom_section.discard
Deselect::ForAllUsagesService.call(section: invoice_custom_section).raise_if_error
Deselect::ForAllUsagesService.call(section: invoice_custom_section).raise_if_error!
result.invoice_custom_section = invoice_custom_section
result
end
Expand Down
42 changes: 42 additions & 0 deletions schema.graphql

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

142 changes: 142 additions & 0 deletions schema.json

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

51 changes: 51 additions & 0 deletions spec/graphql/mutations/invoice_custom_sections/destroy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Mutations::InvoiceCustomSections::Destroy, type: :graphql do
subject(:result) do
execute_graphql(
current_user: membership.user,
current_organization: membership.organization,
permissions: required_permission,
query:,
variables: {input: {id: invoice_custom_section.id}}
)
end

let(:query) do
<<-GQL
mutation($input: DestroyInvoiceCustomSectionInput!) {
destroyInvoiceCustomSection(input: $input) { id }
}
GQL
end

let(:required_permission) { 'invoice_custom_sections:delete' }
let(:membership) { create(:membership) }
let(:invoice_custom_section) { create(:invoice_custom_section, organization: membership.organization) }

before { invoice_custom_section }

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

context 'when invoice custom section with such ID exists in the current organization' do
it 'destroys the invoice custom section' do
expect { result }.to change(InvoiceCustomSection, :count).from(1).to(0)
end
end

context 'when invoice_custom_section with such ID does not exist in the current organization' do
let(:invoice_custom_section) { create(:invoice_custom_section) }

it 'does not delete the invoice_custom_section' do
expect { result }.not_to change(InvoiceCustomSection, :count)
end

it 'returns an error' do
expect_graphql_error(result:, message: 'Resource not found')
end
end
end
Loading

0 comments on commit ade839d

Please sign in to comment.