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(credit-note-export): Add new filters options to credit notes list endpoint #2982

Merged
merged 4 commits into from
Dec 23, 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
12 changes: 11 additions & 1 deletion app/controllers/api/v1/credit_notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,18 @@ def index
page: params[:page],
limit: params[:per_page] || PER_PAGE
},
search_term: params[:search_term],
filters: {
customer_external_id: params[:external_customer_id]
currency: params[:currency],
customer_external_id: params[:external_customer_id],
reason: params[:reason],
credit_status: params[:credit_status],
refund_status: params[:refund_status],
invoice_number: params[:invoice_number],
issuing_date_from: (Date.strptime(params[:issuing_date_from]) if valid_date?(params[:issuing_date_from])),
issuing_date_to: (Date.strptime(params[:issuing_date_to]) if valid_date?(params[:issuing_date_to])),
amount_from: params[:amount_from],
amount_to: params[:amount_to]
}
)

Expand Down
33 changes: 24 additions & 9 deletions app/queries/credit_notes_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ def call
credit_notes = paginate(credit_notes)
credit_notes = apply_consistent_ordering(credit_notes)

credit_notes = with_currency(credit_notes) if filters.currency
credit_notes = with_currency(credit_notes) if filters.currency.present?
credit_notes = with_customer_external_id(credit_notes) if filters.customer_external_id
credit_notes = with_customer_id(credit_notes) if filters.customer_id.present?
credit_notes = with_reason(credit_notes) if filters.reason.present?
credit_notes = with_credit_status(credit_notes) if filters.credit_status.present?
credit_notes = with_refund_status(credit_notes) if filters.refund_status.present?
credit_notes = with_invoice_number(credit_notes) unless filters.invoice_number.nil?
credit_notes = with_reason(credit_notes) if valid_reasons.present?
credit_notes = with_credit_status(credit_notes) if valid_credit_statuses.present?
credit_notes = with_refund_status(credit_notes) if valid_refund_statuses.present?
credit_notes = with_invoice_number(credit_notes) if filters.invoice_number.present?
credit_notes = with_issuing_date_range(credit_notes) if filters.issuing_date_from || filters.issuing_date_to
credit_notes = with_amount_range(credit_notes) if filters.amount_from || filters.amount_to
credit_notes = with_amount_range(credit_notes) if filters.amount_from.present? || filters.amount_to.present?

result.credit_notes = credit_notes
result
Expand Down Expand Up @@ -66,15 +66,15 @@ def with_customer_id(scope)
end

def with_reason(scope)
scope.where(reason: filters.reason)
scope.where(reason: valid_reasons)
end

def with_credit_status(scope)
scope.where(credit_status: filters.credit_status)
scope.where(credit_status: valid_credit_statuses)
end

def with_refund_status(scope)
scope.where(refund_status: filters.refund_status)
scope.where(refund_status: valid_refund_statuses)
end

def with_invoice_number(scope)
Expand All @@ -100,4 +100,19 @@ def issuing_date_from
def issuing_date_to
@issuing_date_to ||= parse_datetime_filter(:issuing_date_to)
end

def valid_credit_statuses
@valid_credit_statuses ||= Array(filters.credit_status)
.select { |credit_status| CreditNote.credit_statuses.key?(credit_status) }
end

def valid_refund_statuses
@valid_refund_statuses ||= Array(filters.refund_status)
.select { |refund_status| CreditNote.refund_statuses.key?(refund_status) }
end

def valid_reasons
@valid_reasons ||= Array(filters.reason)
.select { |reason| CreditNote.reasons.key?(reason) }
end
end
83 changes: 59 additions & 24 deletions spec/queries/credit_notes_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
described_class.call(organization:, search_term:, pagination:, filters:)
end

let(:membership) { create(:membership) }
let(:organization) { membership.organization }
let(:customer) { create(:customer, organization:) }
let(:organization) { customer.organization }
let(:customer) { create(:customer) }

let(:pagination) { nil }
let(:search_term) { nil }
Expand Down Expand Up @@ -85,31 +84,42 @@
end

context "when reason filter applied" do
let(:filters) { {reason: matching_reasons} }

let(:matching_reasons) { CreditNote::REASON.sample(2) }

let!(:matching_credit_notes) do
matching_reasons.map { |reason| create(:credit_note, reason:, customer:) }
end

before do
let!(:non_matching_credit_note) do
create(
:credit_note,
reason: CreditNote::REASON.excluding(matching_reasons).sample,
customer:
)
end

it "returns credit notes with matching reasons" do
expect(result).to be_success
expect(result.credit_notes.pluck(:id)).to match_array matching_credit_notes.pluck(:id)
context "with valid options" do
let(:filters) { {reason: matching_reasons} }

it "returns credit notes with matching reasons" do
expect(result).to be_success
expect(result.credit_notes.pluck(:id)).to match_array matching_credit_notes.pluck(:id)
end
end

context "with invalid options" do
let(:filters) { {reason: "invalid-reason"} }

it "returns all credit notes" do
expect(result).to be_success

expect(result.credit_notes.pluck(:id))
.to contain_exactly(*matching_credit_notes.pluck(:id), non_matching_credit_note.id)
end
end
end

context "when credit status filter applied" do
let(:filters) { {credit_status: matching_credit_statuses} }

let(:matching_credit_statuses) { CreditNote::CREDIT_STATUS.sample(2) }

let!(:matching_credit_notes) do
Expand All @@ -118,23 +128,36 @@
end
end

before do
let!(:non_matching_credit_note) do
create(
:credit_note,
credit_status: CreditNote::CREDIT_STATUS.excluding(matching_credit_statuses).sample,
customer:
)
end

it "returns credit notes with matching credit statuses" do
expect(result).to be_success
expect(result.credit_notes.pluck(:id)).to match_array matching_credit_notes.pluck(:id)
context "with valid options" do
let(:filters) { {credit_status: matching_credit_statuses} }

it "returns credit notes with matching credit statuses" do
expect(result).to be_success
expect(result.credit_notes.pluck(:id)).to match_array matching_credit_notes.pluck(:id)
end
end

context "with invalid options" do
let(:filters) { {credit_status: "invalid-credit-status"} }

it "returns all credit notes" do
expect(result).to be_success

expect(result.credit_notes.pluck(:id))
.to contain_exactly(*matching_credit_notes.pluck(:id), non_matching_credit_note.id)
end
end
end

context "when refund status filter applied" do
let(:filters) { {refund_status: matching_refund_statuses} }

let(:matching_refund_statuses) { CreditNote::REFUND_STATUS.sample(2) }

let!(:matching_credit_notes) do
Expand All @@ -143,17 +166,32 @@
end
end

before do
let!(:non_matching_credit_note) do
create(
:credit_note,
refund_status: CreditNote::REFUND_STATUS.excluding(matching_refund_statuses).sample,
customer:
)
end

it "returns credit notes with matching refund statuses" do
expect(result).to be_success
expect(result.credit_notes.pluck(:id)).to match_array matching_credit_notes.pluck(:id)
context "with valid options" do
let(:filters) { {refund_status: matching_refund_statuses} }

it "returns credit notes with matching refund statuses" do
expect(result).to be_success
expect(result.credit_notes.pluck(:id)).to match_array matching_credit_notes.pluck(:id)
end
end

context "with invalid options" do
let(:filters) { {refund_status: "invalid-refund-status"} }

it "returns all credit notes" do
expect(result).to be_success

expect(result.credit_notes.pluck(:id))
.to contain_exactly(*matching_credit_notes.pluck(:id), non_matching_credit_note.id)
end
end
end

Expand Down Expand Up @@ -307,7 +345,6 @@
let(:customer) do
create(
:customer,
organization:,
name: "Rick Sanchez",
firstname: "Rick Ramon",
lastname: "Sanchez Spencer"
Expand Down Expand Up @@ -344,7 +381,6 @@
let(:customer) do
create(
:customer,
organization:,
name: "Rick Sanchez",
firstname: "Rick Ramon",
lastname: "Sanchez Spencer"
Expand Down Expand Up @@ -383,7 +419,6 @@
let(:customer) do
create(
:customer,
organization:,
name: "Rick Sanchez",
firstname: "Rick Ramon",
lastname: "Sanchez Spencer"
Expand Down
Loading
Loading