diff --git a/README.md b/README.md index 750357965..a8bce7a91 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,40 @@ The yaml file contains a custom key of `:document_types` not used by wraith but bundle exec rake wraith:update_document_types[:sample_size] ``` +## Webchat + +### How to add a new webchat provider +1. Open to `lib/webchat.yaml` +2. Append new entry: +```yaml +- base_path: /government/contact/my-amazing-service + open_url: https://www.my-amazing-webchat.com/007/open-chat + availability_url: https://www.my-amazing-webchat.com/007/check-availability +``` +3. Deploy changes +4. Go to https://www.gov.uk/government/contact/my-amazing-service +5. Finished + +### Required configuration + +#### Base path +This is the base path of a contact page, for example, `/government/organisations/hm-revenue-customs/contact/child-benefit`. +This path should always be a contact page, any other content page type will result in the webchat component not being loaded. + +#### Availability URL +This URL is used to check the availability of agents at regular intervals. + +| Function | Required | +|-----------|-----------| +| Request Method | GET | +| Response Format | JSON | +| Request Example | {"status":"success","response":"BUSY"} | +| Valid statuses | ["BUSY", "UNAVAILABLE", "AVAILABLE", "ERROR"] | + +#### Open URL +This url is used to start a webchat session. +This url should not include session ids or require anything specific parameters to be generated. + ## Licence [MIT License](LICENCE) diff --git a/app/assets/javascripts/webchat/library.js b/app/assets/javascripts/webchat/library.js index 4103a013d..1733eb083 100644 --- a/app/assets/javascripts/webchat/library.js +++ b/app/assets/javascripts/webchat/library.js @@ -32,7 +32,7 @@ function handleOpenChat (evt) { evt.preventDefault() - global.open(openUrl, 'newwin', 'width=200,height=100') + global.open(openUrl, 'newwin', 'width=366,height=516') trackEvent('opened') } diff --git a/app/models/webchat.rb b/app/models/webchat.rb new file mode 100644 index 000000000..82234d754 --- /dev/null +++ b/app/models/webchat.rb @@ -0,0 +1,25 @@ +class Webchat + include ActiveModel::Validations + + CONFIG_PATH = Rails.root.join("lib/webchat.yaml") + + validates_presence_of :base_path, :open_url, :availability_url + + attr_reader :base_path, :open_url, :availability_url + + def initialize(attrs) + @base_path = attrs["base_path"] if attrs["base_path"].present? + @open_url = attrs["open_url"] if attrs["open_url"].present? + @availability_url = attrs["availability_url"] if attrs["availability_url"].present? + + validate! + end + + def self.find(base_path) + load_all.find { |w| w.base_path == base_path } + end + + def self.load_all + @load_all ||= YAML.load_file(CONFIG_PATH).map { |page_config| new(page_config) } + end +end diff --git a/app/presenters/contact_presenter.rb b/app/presenters/contact_presenter.rb index 778459985..5810a086b 100644 --- a/app/presenters/contact_presenter.rb +++ b/app/presenters/contact_presenter.rb @@ -8,10 +8,6 @@ def title_and_context end end - def webchat_body - content_item.dig("details", "more_info_webchat").try(:html_safe) - end - def online_form_links contact_form_links = content_item["details"]["contact_form_links"] || [] contact_form_links.map do |link| @@ -83,16 +79,18 @@ def email_body content_item.dig("details", "more_info_email_address").try(:html_safe) end - def show_webchat? - webchat_ids.include?(content_item["base_path"]) + # Webchat + + def webchat + Webchat.find(content_item["base_path"]) end - def webchat_availability_url - "https://www.tax.service.gov.uk/csp-partials/availability/#{webchat_id}" + def show_webchat? + webchat.present? end - def webchat_open_url - "https://www.tax.service.gov.uk/csp-partials/open/#{webchat_id}" + def webchat_body + content_item.dig("details", "more_info_webchat").try(:html_safe) end private @@ -118,30 +116,6 @@ def phone_numbers_in_group(group) ].select { |n| n[:number].present? } end - def webchat_id - webchat_ids[content_item["base_path"]] - end - - def webchat_ids - { - "/government/organisations/hm-revenue-customs/contact/child-benefit" => 1027, - "/government/organisations/hm-revenue-customs/contact/income-tax-enquiries-for-individuals-pensioners-and-employees" => 1030, - "/government/organisations/hm-revenue-customs/contact/vat-online-services-helpdesk" => 1026, - "/government/organisations/hm-revenue-customs/contact/national-insurance-numbers" => 1021, - "/government/organisations/hm-revenue-customs/contact/self-assessment" => 1004, - "/government/organisations/hm-revenue-customs/contact/tax-credits-enquiries" => 1016, - "/government/organisations/hm-revenue-customs/contact/vat-enquiries" => 1028, - "/government/organisations/hm-revenue-customs/contact/customs-international-trade-and-excise-enquiries" => 1034, - "/government/organisations/hm-revenue-customs/contact/employer-enquiries" => 1023, - "/government/organisations/hm-revenue-customs/contact/online-services-helpdesk" => 1003, - "/government/organisations/hm-revenue-customs/contact/charities-and-community-amateur-sports-clubs-cascs" => 1087, - "/government/organisations/hm-revenue-customs/contact/enquiries-from-employers-with-expatriate-employees" => 1089, - "/government/organisations/hm-revenue-customs/contact/share-schemes-for-employees" => 1088, - "/government/organisations/hm-revenue-customs/contact/non-uk-expatriate-employees-expats" => 1089, - "/government/organisations/hm-revenue-customs/contact/non-resident-landlords" => 1086, - } - end - def v_card_part(v_card_class, value) { v_card_class: v_card_class, diff --git a/app/views/shared/_webchat.html.erb b/app/views/shared/_webchat.html.erb index d2fb72863..d92d74f53 100644 --- a/app/views/shared/_webchat.html.erb +++ b/app/views/shared/_webchat.html.erb @@ -2,8 +2,9 @@

+ data-availability-url="<%= @content_item.webchat.availability_url %>" + data-open-url="<%= @content_item.webchat.open_url %>" + > <% if webchat_unavailable? %> <%= unavailability_message %> <% else %> diff --git a/lib/webchat.yaml b/lib/webchat.yaml new file mode 100644 index 000000000..08162ccee --- /dev/null +++ b/lib/webchat.yaml @@ -0,0 +1,45 @@ +- base_path: /government/organisations/hm-revenue-customs/contact/child-benefit + open_url: https://www.tax.service.gov.uk/csp-partials/open/1027 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1027 +- base_path: /government/organisations/hm-revenue-customs/contact/income-tax-enquiries-for-individuals-pensioners-and-employees + open_url: https://www.tax.service.gov.uk/csp-partials/open/1030 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1030 +- base_path: /government/organisations/hm-revenue-customs/contact/vat-online-services-helpdesk + open_url: https://www.tax.service.gov.uk/csp-partials/open/1026 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1026 +- base_path: /government/organisations/hm-revenue-customs/contact/national-insurance-numbers + open_url: https://www.tax.service.gov.uk/csp-partials/open/1021 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1021 +- base_path: /government/organisations/hm-revenue-customs/contact/self-assessment + open_url: https://www.tax.service.gov.uk/csp-partials/open/1004 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1004 +- base_path: /government/organisations/hm-revenue-customs/contact/tax-credits-enquiries + open_url: https://www.tax.service.gov.uk/csp-partials/open/1016 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1016 +- base_path: /government/organisations/hm-revenue-customs/contact/vat-enquiries + open_url: https://www.tax.service.gov.uk/csp-partials/open/1028 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1028 +- base_path: /government/organisations/hm-revenue-customs/contact/customs-international-trade-and-excise-enquiries + open_url: https://www.tax.service.gov.uk/csp-partials/open/1034 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1034 +- base_path: /government/organisations/hm-revenue-customs/contact/employer-enquiries + open_url: https://www.tax.service.gov.uk/csp-partials/open/1023 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1023 +- base_path: /government/organisations/hm-revenue-customs/contact/online-services-helpdesk + open_url: https://www.tax.service.gov.uk/csp-partials/open/1003 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1003 +- base_path: /government/organisations/hm-revenue-customs/contact/charities-and-community-amateur-sports-clubs-cascs + open_url: https://www.tax.service.gov.uk/csp-partials/open/1087 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1087 +- base_path: /government/organisations/hm-revenue-customs/contact/enquiries-from-employers-with-expatriate-employees + open_url: https://www.tax.service.gov.uk/csp-partials/open/1089 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1089 +- base_path: /government/organisations/hm-revenue-customs/contact/share-schemes-for-employees + open_url: https://www.tax.service.gov.uk/csp-partials/open/1088 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1088 +- base_path: /government/organisations/hm-revenue-customs/contact/non-uk-expatriate-employees-expats + open_url: https://www.tax.service.gov.uk/csp-partials/open/1089 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1089 +- base_path: /government/organisations/hm-revenue-customs/contact/non-resident-landlords + open_url: https://www.tax.service.gov.uk/csp-partials/open/1086 + availability_url: https://www.tax.service.gov.uk/csp-partials/availability/1086 diff --git a/test/models/webchat_test.rb b/test/models/webchat_test.rb new file mode 100644 index 000000000..d87b09c7d --- /dev/null +++ b/test/models/webchat_test.rb @@ -0,0 +1,28 @@ +require "test_helper" + +class WebchatTest < ActiveSupport::TestCase + webchat_config = { + "base_path" => "/government/contact/my-amazing-service", + "open_url" => "https://www.tax.service.gov.uk/csp-partials/open/1023", + "availability_url" => "https://www.tax.service.gov.uk/csp-partials/availability/1023", + } + + test "should create instance correctly" do + instance = Webchat.new(webchat_config) + + assert_equal(instance.base_path, webchat_config["base_path"]) + assert_equal(instance.open_url, webchat_config["open_url"]) + assert_equal(instance.availability_url, webchat_config["availability_url"]) + end + + test "should return error if config is invalid" do + webchat_invalid_config = { + "base_path" => "/government/contact/my-amazing-service", + "availability_url" => "https://www.tax.service.gov.uk/csp-partials/availability/1023", + } + + assert_raises ActiveModel::ValidationError do + Webchat.new(webchat_invalid_config) + end + end +end diff --git a/test/presenters/contact_presenter_test.rb b/test/presenters/contact_presenter_test.rb index 53a692709..67ef8c498 100644 --- a/test/presenters/contact_presenter_test.rb +++ b/test/presenters/contact_presenter_test.rb @@ -88,8 +88,8 @@ def schema_name schema = schema_item("contact_with_webchat") presented = present_example(schema) assert_equal true, presented.show_webchat? - assert_equal presented.webchat_availability_url, "https://www.tax.service.gov.uk/csp-partials/availability/1030" - assert_equal presented.webchat_open_url, "https://www.tax.service.gov.uk/csp-partials/open/1030" + assert_equal presented.webchat.availability_url, "https://www.tax.service.gov.uk/csp-partials/availability/1030" + assert_equal presented.webchat.open_url, "https://www.tax.service.gov.uk/csp-partials/open/1030" end end end