Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions app/controllers/api/v1/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
class Api::V1::ReportsController < ApplicationController
def features
feature_stats = Reports::FeatureUsageService.new.report

render json: feature_stats.to_json, status: :ok
end

def add_another_answer_forms
data = Reports::FeatureUsageService.new.add_another_answer_forms
data = Reports::AddAnotherAnswerUsageService.new.add_another_answer_forms

render json: data.to_json, status: :ok
end
Expand Down
21 changes: 21 additions & 0 deletions app/services/reports/add_another_answer_usage_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Reports::AddAnotherAnswerUsageService
def add_another_answer_forms
forms = Form.includes(:pages)
.where(pages: { is_repeatable: true })
.map(&method(:form_data))

# adding the count even though forms-admin doesn't use it as ActiveResource doesn't like parsing JSON with a single root key
{ forms:, count: forms.length }
end

private

def form_data(form)
{
form_id: form.id,
name: form.name,
state: form.state,
repeatable_pages: form.pages.map { |page| { page_id: page.id, question_text: page.question_text } if page.is_repeatable },
}
end
end
69 changes: 0 additions & 69 deletions app/services/reports/feature_usage_service.rb

This file was deleted.

1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
end

scope :reports do
get "/features", to: "api/v1/reports#features"
get "/add-another-answer-forms", to: "api/v1/reports#add_another_answer_forms"
get "/selection-questions-summary", to: "api/v1/reports#selection_questions_summary"
get "/selection-questions-with-autocomplete", to: "api/v1/reports#selection_questions_with_autocomplete"
Expand Down
68 changes: 0 additions & 68 deletions spec/requests/api/v1/reports_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,74 +1,6 @@
require "rails_helper"

RSpec.describe Api::V1::ReportsController, type: :request do
describe "GET /features" do
let!(:form_with_repeatable_question) { create(:form, state: :live, pages: [repeatable_page]) }
let(:repeatable_page) { build(:page, answer_type: "text", is_repeatable: true) }

before do
create :form, state: :live, payment_url: Faker::Internet.url(host: "gov.uk"), submission_type: "email_with_csv", pages: [
(build :page, answer_type: "text"),
(build :page, answer_type: "text"),
]

create :form, state: :live, pages: [
(build :page, answer_type: "name"),
(build :page, answer_type: "organisation_name"),
(build :page, answer_type: "phone_number"),
(build :page, answer_type: "email"),
(build :page, answer_type: "address"),
(build :page, answer_type: "national_insurance_number"),
(build :page, answer_type: "date"),
(build :page, answer_type: "number"),
(build :page, answer_type: "selection", routing_conditions: [(build :condition)]),
(build :page, answer_type: "text"),
]

get "/api/v1/reports/features"
end

it "returns the breakdown of form features used" do
response_hash = JSON.parse(response.body, symbolize_names: true)

expect(response_hash).to eq({
total_live_forms: 3,
live_forms_with_answer_type: {
name: 1,
organisation_name: 1,
phone_number: 1,
email: 1,
address: 1,
national_insurance_number: 1,
date: 1,
number: 1,
selection: 1,
text: 3,
},
live_pages_with_answer_type: {
name: 1,
organisation_name: 1,
phone_number: 1,
email: 1,
address: 1,
national_insurance_number: 1,
date: 1,
number: 1,
selection: 1,
text: 4,
},
live_forms_with_payment: 1,
live_forms_with_routing: 1,
live_forms_with_add_another_answer: 1,
live_forms_with_csv_submission_enabled: 1,
all_forms_with_add_another_answer: [{ form_id: form_with_repeatable_question.id, name: form_with_repeatable_question.name, state: form_with_repeatable_question.state, repeatable_pages: [{ page_id: repeatable_page.id, question_text: repeatable_page.question_text }] }],
})
end

it "returns http success" do
expect(response).to have_http_status(:success)
end
end

describe "GET /add-another-answer-forms" do
let!(:form_with_repeatable_question) { create(:form, state: :live, pages: [repeatable_page]) }
let(:repeatable_page) { build(:page, answer_type: "text", is_repeatable: true) }
Expand Down
45 changes: 45 additions & 0 deletions spec/services/reports/add_another_answer_usage_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "rails_helper"

describe Reports::AddAnotherAnswerUsageService do
subject(:features_report_service) { described_class.new }

describe "#add_another_answer_forms" do
let!(:add_another_answer_draft_form) { create(:form, state: "draft", pages: draft_form_pages) }
let(:draft_form_pages) do
[
(build :page, answer_type: "name", is_repeatable: true),
]
end
let!(:add_another_answer_live_form) { create(:form, state: "live", pages: live_form_pages) }
let(:live_form_pages) do
[
(build :page, answer_type: "name", is_repeatable: true),
(build :page, answer_type: "text", is_repeatable: true),
]
end

it "obtains all forms in the add another answer report" do
report = features_report_service.add_another_answer_forms

expect(report[:forms]).to contain_exactly({
form_id: add_another_answer_draft_form.id,
name: add_another_answer_draft_form.name,
state: add_another_answer_draft_form.state,
repeatable_pages: contain_exactly({ page_id: draft_form_pages.first.id, question_text: draft_form_pages.first.question_text }),
}, {
form_id: add_another_answer_live_form.id,
name: add_another_answer_live_form.name,
state: add_another_answer_live_form.state,
repeatable_pages: contain_exactly(
{ page_id: live_form_pages.first.id, question_text: live_form_pages.first.question_text },
{ page_id: live_form_pages.second.id, question_text: live_form_pages.second.question_text },
),
})
end

it "returns the count" do
report = features_report_service.add_another_answer_forms
expect(report[:count]).to eq 2
end
end
end
Loading