Skip to content

Commit

Permalink
Merge pull request #12 from fabianoflorentino/development
Browse files Browse the repository at this point in the history
Development to Main
  • Loading branch information
fabianoflorentino authored Mar 7, 2024
2 parents 50c048d + cb06157 commit 076bf1c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/controllers/extracts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

# Extract controller
class ExtractsController < ApplicationController
def index
extract ||= TransactionUseCase::Extract.new(params[:customer_id]).call

render json: extract, status: :ok
end
end
3 changes: 3 additions & 0 deletions bin/migration
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/usr/bin/env sh

set -e

printf "Running migrations...\n"
bundle exec rails db:migrate
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
# Define a resource route for customers
resources :customers, only: %i[index show create destroy] do
resources :transactions, only: %i[create]
resources :extracts, only: %i[index]
end
end
42 changes: 42 additions & 0 deletions spec/requests/transactions/extract_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'GET - /customers/:id/extracts' do
let(:customer) { create(:customer) }
let(:url) { "/customers/#{customer.id}/extracts" }

context 'when the request is valid' do
it 'returns status code 200' do
get(url)

expect(response).to have_http_status(:ok)
end

it 'returns the customer extract' do
get(url)

expect(response.parsed_body['extract']['balance'].keys).to match_array(%w[total date limit])
expect(response.parsed_body['extract']['transactions']).to eq([])
end

it 'returns the customer extract with transactions' do
transactions = create_list(:transaction, 10, customer:)
transaction_attributes = %w[id kind date amount description]

get(url)

response_transactions_last_id = response.parsed_body['extract']['transactions'].last[:id]

expect(response.parsed_body['extract']['transactions'].size).to eq(10)
expect(response.parsed_body['extract']['transactions'].last.keys).to match_array(transaction_attributes)
expect(response_transactions_last_id).to be_in(transactions.map(&:id))
end

it 'raises an error when the customer does not exist' do
get("/customers/#{SecureRandom.uuid}/extracts")

expect(response).to have_http_status(:not_found)
end
end
end

0 comments on commit 076bf1c

Please sign in to comment.