Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add make live validation #332

Merged
merged 4 commits into from
Sep 20, 2023
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: 6 additions & 2 deletions app/controllers/api/v1/forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ def destroy
end

def make_live
form.make_live!
render json: { success: true }.to_json, status: :ok
if form.ready_for_live
form.make_live!
render json: { success: true }.to_json, status: :ok
else
render json: form.incomplete_tasks.to_json, status: :forbidden
end
end

def show_live
Expand Down
16 changes: 15 additions & 1 deletion app/models/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def name=(val)
end

def as_json(options = {})
options[:methods] ||= %i[live_at start_page has_draft_version has_live_version has_routing_errors]
options[:methods] ||= %i[live_at start_page has_draft_version has_live_version has_routing_errors ready_for_live incomplete_tasks task_statuses]
jamie-o-wilkinson marked this conversation as resolved.
Show resolved Hide resolved
super(options)
end

Expand All @@ -78,4 +78,18 @@ def has_routing_errors
def marking_complete_with_errors
errors.add(:base, :has_validation_errors, message: "Form has routing validation errors") if question_section_completed && has_routing_errors
end

def ready_for_live
task_status_service.mandatory_tasks_completed?
end

delegate :incomplete_tasks, to: :task_status_service

delegate :task_statuses, to: :task_status_service

private

def task_status_service
@task_status_service ||= TaskStatusService.new(form: self)
end
end
85 changes: 85 additions & 0 deletions app/service/task_status_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
class TaskStatusService
def initialize(form:)
@form = form
end

def mandatory_tasks_completed?
incomplete_tasks.empty?
end

def incomplete_tasks
{ missing_pages: pages_status,
missing_what_happens_next: what_happens_next_status,
missing_privacy_policy_url: privacy_policy_status,
missing_contact_details: support_contact_details_status }.reject { |_k, v| v == :completed }.map { |k, _v| k }
end

def task_statuses
{
name_status:,
pages_status:,
declaration_status:,
what_happens_next_status:,
privacy_policy_status:,
support_contact_details_status:,
make_live_status:,
}
end

private
jamie-o-wilkinson marked this conversation as resolved.
Show resolved Hide resolved

def name_status
:completed
end

def pages_status
if @form.question_section_completed && @form.pages.any?
:completed
elsif @form.pages.any?
:in_progress
else
:not_started
end
end

def declaration_status
if @form.declaration_section_completed
:completed
elsif @form.declaration_text.present?
:in_progress
else
:not_started
end
end

def what_happens_next_status
if @form.what_happens_next_text.present?
:completed
else
:not_started
end
end

def privacy_policy_status
if @form.privacy_policy_url.present?
:completed
else
:not_started
end
end

def support_contact_details_status
if @form.support_email.present? || @form.support_phone.present? || (@form.support_url_text.present? && @form.support_url)
:completed
else
:not_started
end
end

def make_live_status
if @form.has_draft_version
return mandatory_tasks_completed? ? :not_started : :cannot_start
end
return :completed if @form.has_live_version
end
end
2 changes: 2 additions & 0 deletions spec/factories/forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
end

trait :ready_for_live do
with_pages
support_email { Faker::Internet.email(domain: "example.gov.uk") }
what_happens_next_text { "We usually respond to applications within 10 working days." }
question_section_completed { true }
Expand All @@ -50,6 +51,7 @@

trait :live do
ready_for_live
after(:create, &:make_live!)
end

trait :with_support do
Expand Down
73 changes: 73 additions & 0 deletions spec/models/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,77 @@
end
end
end

describe "#ready_for_live" do
context "when a form is complete and ready to be made live" do
let(:completed_form) { create(:form, :live) }

it "returns true" do
expect(completed_form.ready_for_live).to eq true
end
end

context "when a form is incomplete and should still be in draft state" do
let(:new_form) { build :form, :new_form }

[
{
attribute: :pages,
attribute_value: [],
},
{
attribute: :what_happens_next_text,
attribute_value: nil,
},
{
attribute: :privacy_policy_url,
attribute_value: nil,
},
{
attribute: :support_email,
attribute_value: nil,
},
].each do |scenario|
it "returns false if #{scenario[:attribute]} is missing" do
new_form.send("#{scenario[:attribute]}=", scenario[:attribute_value])
expect(new_form.ready_for_live).to eq false
end
end
end
end

describe "#incomplete_tasks" do
context "when a form is complete and ready to be made live" do
let(:completed_form) { build :form, :live }

it "returns no missing sections" do
expect(completed_form.incomplete_tasks).to be_empty
end
end

context "when a form is incomplete and should still be in draft state" do
let(:new_form) { build :form, :new_form }

it "returns a set of keys related to missing fields" do
expect(new_form.incomplete_tasks).to match_array(%i[missing_pages missing_privacy_policy_url missing_contact_details missing_what_happens_next])
end
end
end

describe "#task_statuses" do
let(:completed_form) { create(:form, :live) }

it "returns a hash with each of the task statuses" do
expected_hash = {
name_status: :completed,
pages_status: :completed,
declaration_status: :completed,
what_happens_next_status: :completed,
privacy_policy_status: :completed,
support_contact_details_status: :completed,
make_live_status: :completed,
}
expect(completed_form.task_statuses).to eq expected_hash
end
end
end
15 changes: 14 additions & 1 deletion spec/request/api/v1/forms_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@
created_at: form1.created_at.as_json,
updated_at: form1.updated_at.as_json,
has_routing_errors: false,
incomplete_tasks: %w[missing_pages missing_what_happens_next missing_privacy_policy_url missing_contact_details],
ready_for_live: false,
task_statuses: { declaration_status: "not_started", make_live_status: "cannot_start", name_status: "completed", pages_status: "not_started", privacy_policy_status: "not_started", support_contact_details_status: "not_started", what_happens_next_status: "not_started" },
)
end
end
Expand Down Expand Up @@ -251,8 +254,18 @@
end

describe "#make_live" do
context "when given a form with missing sections" do
it "doesn't make the form live" do
form_to_be_made_live = create(:form, :new_form)
post make_live_form_path(form_to_be_made_live), as: :json
expect(response.status).to eq(403)
expect(response.headers["Content-Type"]).to eq("application/json")
expect(json_body).to eq(%w[missing_pages missing_what_happens_next missing_privacy_policy_url missing_contact_details])
end
end

it "when given a form, sets live_at to current time" do
form_to_be_made_live = create :form
form_to_be_made_live = create(:form, :ready_for_live)
post make_live_form_path(form_to_be_made_live), as: :json
expect(response.status).to eq(200)
expect(response.headers["Content-Type"]).to eq("application/json")
Expand Down
Loading