Skip to content

Commit

Permalink
search api for clients (#246)
Browse files Browse the repository at this point in the history
* search api for clients

* fixed tests and moved ransack logic to controller

* fix reveiw comments

* minor fix

Co-authored-by: “Apoorv <“tiwari.apoorv1316@gmail.com”>
  • Loading branch information
apoorv1316 and “Apoorv authored Apr 14, 2022
1 parent 2e88c4b commit c7ea5ff
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
4 changes: 3 additions & 1 deletion app/controllers/internal_api/v1/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
class InternalApi::V1::ClientsController < InternalApi::V1::ApplicationController
def index
authorize Client
client_details = current_user.current_workspace.client_details(params[:time_frame])
query = current_company.clients.ransack({ name_or_email_cont: params[:q] })
clients = query.result(distinct: true)
client_details = clients.map { |client| client.client_detail(params[:time_frame]) }
total_minutes = (client_details.map { |client| client[:minutes_spent] }).sum
render json: { client_details:, total_minutes: }, status: :ok
end
Expand Down
9 changes: 9 additions & 0 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ def week_month_year(time_frame)
end
end

def client_detail(time_frame = "week")
{
id: id,
name: name,
email: email,
minutes_spent: total_hours_logged(time_frame)
}
end

private

def discard_projects
Expand Down
9 changes: 1 addition & 8 deletions app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,7 @@ class Company < ApplicationRecord
validates :company_code, presence: true, uniqueness: true, length: { is: 2 }

def client_details(time_frame = "week")
clients.kept.map do |client|
{
id: client.id,
name: client.name,
email: client.email,
minutes_spent: client.total_hours_logged(time_frame)
}
end
clients.kept.map { |client| client.client_detail(time_frame) }
end

def client_list
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 30 additions & 2 deletions spec/requests/internal_api/v1/clients/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
let(:client_2) { create(:client, company:) }
let(:project_1) { create(:project, client: client_1) }
let(:project_2) { create(:project, client: client_2) }
let(:time_frame) { "last_week" }

context "when user is admin" do
before do
Expand All @@ -21,8 +22,6 @@
end

context "when time_frame is week" do
let(:time_frame) { "last_week" }

it "returns the total hours logged for a Company in the last_week" do
client_details = user.current_workspace.clients.kept.map do |client|
{
Expand All @@ -36,6 +35,35 @@
expect(json_response["total_minutes"]).to eq(JSON.parse(total_minutes.to_json))
end
end

context "for ransack search" do
before do
create(:company_user, company:, user:)
user.add_role :admin, company
sign_in user
send_request :get, internal_api_v1_clients_path, params: { q: client_1.name }
end

it "finds specific client by name" do
client_details = [{
id: client_1.id, name: client_1.name, email: client_1.email,
minutes_spent: client_1.total_hours_logged(time_frame)
}]
expect(response).to have_http_status(:ok)
expect(json_response["client_details"]).to eq(JSON.parse(client_details.to_json))
end
end

it "returns all the clients when query params are empty" do
client_details = [{
id: client_1.id, name: client_1.name, email: client_1.email,
minutes_spent: client_1.total_hours_logged(time_frame)
}, id: client_2.id, name: client_2.name, email: client_2.email,
minutes_spent: client_2.total_hours_logged(time_frame) ]

expect(response).to have_http_status(:ok)
expect(json_response["client_details"]).to eq(JSON.parse(client_details.to_json))
end
end

context "when user is employee" do
Expand Down

0 comments on commit c7ea5ff

Please sign in to comment.