Skip to content

Commit

Permalink
Merge pull request #1550 from senid231/fix-customer-api-service-trans…
Browse files Browse the repository at this point in the history
…action

fix customer api service and transactions
  • Loading branch information
dmitry-sinina authored Sep 7, 2024
2 parents e86639a + 795daeb commit ae84842
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/resources/api/rest/customer/v1/service_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ def self.sortable_fields(_ctx = nil)
def self.required_model_includes(_ctx = nil)
[:type]
end

def self.apply_allowed_accounts(records, options)
context = options[:context]
if context[:allowed_account_ids].present?
records.where(account_id: context[:allowed_account_ids])
else
records.joins(:account).where(accounts: { contractor_id: context[:customer_id] })
end
end
end
9 changes: 9 additions & 0 deletions app/resources/api/rest/customer/v1/transaction_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ class Api::Rest::Customer::V1::TransactionResource < Api::Rest::Customer::V1::Ba
def self.sortable_fields(_ctx = nil)
%i[id created_at service_id amount]
end

def self.apply_allowed_accounts(records, options)
context = options[:context]
if context[:allowed_account_ids].present?
records.where(account_id: context[:allowed_account_ids])
else
records.joins(:account).where(accounts: { contractor_id: context[:customer_id] })
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
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(:account) { create(:account, contractor: customer) }
let!(:service) { FactoryBot.create(:service, uuid: SecureRandom.uuid, account:) }
let(:json_api_request_query) { { include: :transactions } }

before do
# other customer's service
FactoryBot.create(:service)
end

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 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
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(:account) { create(:account, contractor: customer) }
let!(:service) { FactoryBot.create(:service, account:, uuid: SecureRandom.uuid) }
let(:json_api_request_query) { { include: :service } }

before do
# other customer's service with transaction
FactoryBot.create(:service)
end

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 }

Expand Down

0 comments on commit ae84842

Please sign in to comment.