Skip to content

Commit

Permalink
Public URL with Checkin Statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
salimhb committed Aug 27, 2020
1 parent 6911192 commit 8f595f0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/controllers/owners/companies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Owners
class CompaniesController < Owners::ApplicationController
before_action :authenticate_owner!
before_action :authenticate_owner!, except: %i[stats]

def index
companies = current_owner.companies
Expand Down Expand Up @@ -28,6 +28,16 @@ def show
render json: company
end

def stats
company = Company.find(params[:company_id])

stats = company.areas.map do |a|
{ area_name: a.name, checkin_count: a.tickets.open.count }
end

render json: stats
end

private

def company_params
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
resources :areas, only: %i[index create update show], shallow: true
resources :tickets, only: :index
resources :data_requests, only: %i[show index], shallow: true
get :stats
end
resource :owner, only: %i[show update]
post :checkout, only: :create, controller: :checkouts, action: :create
Expand Down
22 changes: 22 additions & 0 deletions spec/requests/owners/companies_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,26 @@
expect(JSON.parse(response.body)['id']).to eq(company.id)
end
end

context 'GET stats' do
let!(:company) { FactoryBot.create(:company, owner: owner) }
let!(:area_1) { FactoryBot.create(:area, company: company) }
let!(:area_2) { FactoryBot.create(:area, company: company) }
let!(:closed_ticket) { FactoryBot.create(:ticket, area: area_1) }
let!(:open_ticket_1) { FactoryBot.create(:ticket, area: area_1, left_at: nil) }
let!(:open_ticket_2) { FactoryBot.create(:ticket, area: area_1, left_at: nil) }
let!(:open_ticket_3) { FactoryBot.create(:ticket, area: area_2, left_at: nil) }
let(:response_json) { JSON.parse(response.body) }

before { get owners_company_stats_path(company) }

it 'return stats for open tickets by area' do
expect(response_json.size).to eq(2)
expect(response_json.first['area_name']).to eq(area_1.name)
expect(response_json.first['checkin_count']).to eq(2)
expect(response_json.second['area_name']).to eq(area_2.name)
expect(response_json.second['checkin_count']).to eq(1)
end
end

end

0 comments on commit 8f595f0

Please sign in to comment.