Skip to content

Commit

Permalink
Merge pull request #1530 from yeti-switch/1528-include-service-from-t…
Browse files Browse the repository at this point in the history
…ransaction-resource

1528, Customer API: improve transactions API resource
  • Loading branch information
dmitry-sinina committed Aug 23, 2024
2 parents e536c2e + c6e3d68 commit c93fd83
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/resources/api/rest/customer/v1/transaction_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class Api::Rest::Customer::V1::TransactionResource < Api::Rest::Customer::V1::Ba
attribute :created_at

has_one :account, class_name: 'Account', foreign_key_on: :related
has_one :service, class_name: 'Service', relation_name: :type, foreign_key_on: :related
has_one :service, class_name: 'Service', foreign_key_on: :related

ransack_filter :created_at, type: :datetime
association_uuid_filter :account_id, class_name: 'Account'
ransack_filter :service_id, type: :foreign_key
association_uuid_filter :service_id, class_name: 'Billing::Service'
ransack_filter :amount, type: :number
ransack_filter :description, type: :string

Expand Down
38 changes: 38 additions & 0 deletions spec/requests/api/rest/customer/v1/services_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

RSpec.describe Api::Rest::Customer::V1::ServicesController, type: :request do
include_context :json_api_customer_v1_helpers, type: :services

let!(:service) { FactoryBot.create(:service, uuid: SecureRandom.uuid) }
let(:json_api_request_query) { { include: :transactions } }

describe 'GET /api/rest/customer/v1/services?include=transactions' do
subject { get json_api_request_path, params: json_api_request_query, headers: json_api_request_headers }

it 'should render records with included service' do
subject

expect(response_json[:errors]).to eq nil
expect(response_json[:data].pluck(:id)).to match_array [service.uuid]
expect(response_json[:included]).to contain_exactly(
hash_including(
id: service.transactions.first.uuid,
type: 'transactions'
)
)
end
end

describe 'GET /api/rest/customer/v1/services/{id}?include=transactions' do
subject { get json_api_request_path, params: json_api_request_query, headers: json_api_request_headers }

let(:json_api_request_path) { "#{super()}/#{service.uuid}" }

it 'should return record with included service' do
subject

expect(response_json.dig(:data, :id)).to eq service.uuid
expect(response_json.dig(:included)).to contain_exactly hash_including id: service.transactions.first.uuid, type: 'transactions'
end
end
end
51 changes: 51 additions & 0 deletions spec/requests/api/rest/customer/v1/transactions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

RSpec.describe Api::Rest::Customer::V1::TransactionsController, type: :request do
include_context :json_api_customer_v1_helpers, type: :transactions

let!(:service) { FactoryBot.create(:service, uuid: SecureRandom.uuid) }
let(:json_api_request_query) { { include: :service } }

describe 'GET /api/rest/customer/v1/transactions?include=service' do
subject { get json_api_request_path, params: json_api_request_query, headers: json_api_request_headers }

it 'should render records with included service' do
subject

expect(response_json[:errors]).to eq nil
expect(response_json[:data].pluck(:id)).to match_array [service.transactions.first.uuid]
expect(response_json[:included]).to contain_exactly(
hash_including(
id: service.uuid,
type: 'services',
attributes: hash_including(name: service.name)
)
)
end
end

describe 'GET /api/rest/customer/v1/transactions?filter[service-id-eq]=uuid' do
subject { get json_api_request_path, params: json_api_request_query, headers: json_api_request_headers }

let(:json_api_request_query) { { filter: { 'service-id-eq' => service.uuid } } }

it 'should filter records by UUID of service' do
subject

expect(response_json[:data].pluck(:id)).to contain_exactly service.transactions.first.uuid
end
end

describe 'GET /api/rest/customer/v1/transactions/{id}?include=service' do
subject { get json_api_request_path, params: json_api_request_query, headers: json_api_request_headers }

let(:json_api_request_path) { "#{super()}/#{service.transactions.first.uuid}" }

it 'should return record with included service' do
subject

expect(response_json.dig(:data, :id)).to eq service.transactions.first.uuid
expect(response_json.dig(:included)).to contain_exactly hash_including id: service.uuid, type: 'services'
end
end
end

0 comments on commit c93fd83

Please sign in to comment.