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 query resolver #2920

Merged
merged 3 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
24 changes: 24 additions & 0 deletions app/graphql/resolvers/invoice_custom_sections_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

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

REQUIRED_PERMISSION = 'invoice_custom_sections:view'

description "Query invoice_custom_sections"

argument :limit, Integer, required: false
argument :page, Integer, required: false

type Types::InvoiceCustomSections::Object.collection_type, null: true

def resolve(page: nil, limit: nil)
current_organization.invoice_custom_sections.left_outer_joins(:invoice_custom_section_selections).order(
Arel.sql('CASE WHEN invoice_custom_section_selections.id IS NOT NULL THEN 0 ELSE 1 END'),
:name
).page(page).per(limit)
end
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_sections, resolver: Resolvers::InvoiceCustomSectionsResolver
field :invoiced_usages, resolver: Resolvers::Analytics::InvoicedUsagesResolver
field :invoices, resolver: Resolvers::InvoicesResolver
field :memberships, resolver: Resolvers::MembershipsResolver
Expand Down
1 change: 1 addition & 0 deletions app/models/invoice_custom_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class InvoiceCustomSection < ApplicationRecord
self.discard_column = :deleted_at

belongs_to :organization
has_many :invoice_custom_section_selections, dependent: :destroy

validates :name, presence: true
validates :code, presence: true, uniqueness: {scope: :organization_id}
Expand Down
20 changes: 20 additions & 0 deletions schema.graphql

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

94 changes: 94 additions & 0 deletions schema.json

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

58 changes: 58 additions & 0 deletions spec/graphql/resolvers/invoice_custom_sections_resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Resolvers::InvoiceCustomSectionsResolver, type: :graphql do
let(:required_permission) { 'invoice_custom_sections:view' }
let(:query) do
<<~GQL
query() {
invoiceCustomSections(limit: 5) {
collection { id, name }
metadata { currentPage, totalCount }
}
}
GQL
end

let(:membership) { create(:membership) }
let(:organization) { membership.organization }
let(:customer) { create(:customer, organization:) }
let(:invoice_custom_sections) do
[
create(:invoice_custom_section, organization:, name: 'x'),
create(:invoice_custom_section, organization:, name: 'r'),
create(:invoice_custom_section, organization:, name: 'c'),
create(:invoice_custom_section, organization:, name: 'a'),
create(:invoice_custom_section, organization:, name: 'z'),
create(:invoice_custom_section, organization:, name: 'n')
]
end

before do
organization.selected_invoice_custom_sections.concat(invoice_custom_sections[2..4])
end

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

it 'returns a list of sorted invoice_custom_sections: alphabetical, selected first' do
result = execute_graphql(
current_user: membership.user,
current_organization: organization,
permissions: required_permission,
query:
)

invoice_custom_sections_response = result['data']['invoiceCustomSections']

aggregate_failures do
expect(invoice_custom_sections_response['collection'].count).to eq(5)
expect(invoice_custom_sections_response['collection'].map { |ics| ics['name'] }.join('')).to eq('acznr')

expect(invoice_custom_sections_response['metadata']['currentPage']).to eq(1)
expect(invoice_custom_sections_response['metadata']['totalCount']).to eq(6)
end
end
end
Loading