diff --git a/app/controllers/placements/providers/users_controller.rb b/app/controllers/placements/providers/users_controller.rb new file mode 100644 index 0000000000..d2cd881740 --- /dev/null +++ b/app/controllers/placements/providers/users_controller.rb @@ -0,0 +1,44 @@ +class Placements::Providers::UsersController < ApplicationController + before_action :set_provider + + def index + @users = @provider.users.order("LOWER(first_name)") + end + + def show + @user = @provider.users.find(params.require(:id)) + end + + def new + @user_form = params[:user_invite_form].present? ? user_form : UserInviteForm.new + end + + def check + render :new unless user_form.valid? + end + + def create + if user_form.invite + redirect_to placements_provider_users_path(@provider) + flash[:success] = t(".user_added") + else + render :new + end + end + + private + + def set_provider + @provider = Placements::Provider.find(params.fetch(:provider_id)) + end + + def user_params + params.require(:user_invite_form) + .permit(:first_name, :last_name, :email) + .merge({ service: current_service, organisation: @provider }) + end + + def user_form + @user_form ||= UserInviteForm.new(user_params) + end +end diff --git a/app/controllers/placements/schools/users_controller.rb b/app/controllers/placements/schools/users_controller.rb new file mode 100644 index 0000000000..0e801025e7 --- /dev/null +++ b/app/controllers/placements/schools/users_controller.rb @@ -0,0 +1,44 @@ +class Placements::Schools::UsersController < ApplicationController + before_action :set_school + + def index + @users = @school.users.order("LOWER(first_name)") + end + + def show + @user = @school.users.find(params.require(:id)) + end + + def new + @user_form = params[:user_invite_form].present? ? user_form : UserInviteForm.new + end + + def check + render :new unless user_form.valid? + end + + def create + if user_form.invite + redirect_to placements_school_users_path(@school) + flash[:success] = t(".user_added") + else + render :new + end + end + + private + + def set_school + @school = Placements::School.find(params.fetch(:school_id)) + end + + def user_params + params.require(:user_invite_form) + .permit(:first_name, :last_name, :email) + .merge({ service: current_service, organisation: @school }) + end + + def user_form + @user_form ||= UserInviteForm.new(user_params) + end +end diff --git a/app/helpers/routes_helper.rb b/app/helpers/routes_helper.rb index 859302661b..272300e09e 100644 --- a/app/helpers/routes_helper.rb +++ b/app/helpers/routes_helper.rb @@ -34,6 +34,15 @@ def organisations_path }.fetch current_service end + def organisation_users_path(organisation) + case organisation + when School + placements_school_users_path(organisation) + when Provider + placements_provider_users_path(organisation) + end + end + def placements_support_organisation_path(organisation) case organisation when School @@ -43,6 +52,15 @@ def placements_support_organisation_path(organisation) end end + def placements_organisation_user_path(organisation, user) + case organisation + when School + placements_school_user_path(organisation, user) + when Provider + placements_provider_user_path(organisation, user) + end + end + def placements_organisation_path(organisation) case organisation when School @@ -60,4 +78,31 @@ def placements_support_users_path(organisation) placements_support_provider_users_path(organisation) end end + + def check_placements_organisation_users_path(organisation) + case organisation + when School + check_placements_school_users_path + when Provider + check_placements_provider_users_path + end + end + + def placements_organisation_users_path(organisation) + case organisation + when School + placements_school_users_path(organisation) + when Provider + placements_provider_users_path(organisation) + end + end + + def new_placements_organisation_user_path(organisation, params = {}) + case organisation + when School + new_placements_school_user_path(organisation, params) + when Provider + new_placements_provider_user_path(organisation, params) + end + end end diff --git a/app/models/placements/user.rb b/app/models/placements/user.rb index 2dc4a7222e..e0ae372ebd 100644 --- a/app/models/placements/user.rb +++ b/app/models/placements/user.rb @@ -29,4 +29,8 @@ class Placements::User < User def service :placements end + + def organisation_count + providers.count + schools.count + end end diff --git a/app/views/placements/_primary_navigation.html.erb b/app/views/placements/_primary_navigation.html.erb index fc1a3acf1f..8cfcf95609 100644 --- a/app/views/placements/_primary_navigation.html.erb +++ b/app/views/placements/_primary_navigation.html.erb @@ -4,7 +4,7 @@ <%= render PrimaryNavigationComponent.new do |component| %> <% component.with_navigation_item t(".placements"), "#", current: current_navigation == :placements %> <% component.with_navigation_item t(".mentors"), "#", current: current_navigation == :mentors %> - <% component.with_navigation_item t(".users"), "#", current: current_navigation == :users %> + <% component.with_navigation_item t(".users"), organisation_users_path(organisation), current: current_navigation == :users %> <% component.with_navigation_item t(".organisation_details"), placements_organisation_path(organisation), current: current_navigation == :organisation_details %> diff --git a/app/views/placements/organisations/users/_check_form.html.erb b/app/views/placements/organisations/users/_check_form.html.erb new file mode 100644 index 0000000000..25b52a44f2 --- /dev/null +++ b/app/views/placements/organisations/users/_check_form.html.erb @@ -0,0 +1,58 @@ +<%# locals: (user: nil, organisation: nil) -%> +
+
+
+ <%= form_with(model: user, url: placements_organisation_users_path(organisation), method: :post) do |f| %> + <%= f.hidden_field :first_name, value: user.first_name %> + <%= f.hidden_field :last_name, value: user.last_name %> + <%= f.hidden_field :email, value: user.email %> + + + + <%= govuk_summary_list do |summary_list| %> + <% summary_list.with_row do |row| %> + <% row.with_key(text: User.human_attribute_name(:first_name)) %> + <% row.with_value(text: user.first_name) %> + <% row.with_action(text: t(".change"), + href: new_placements_organisation_user_path(organisation, user.as_form_params), + html_attributes: { + class: "govuk-link--no-visited-state", + }) %> + <% end %> + <% summary_list.with_row do |row| %> + <% row.with_key(text: User.human_attribute_name(:last_name)) %> + <% row.with_value(text: user.last_name) %> + <% row.with_action(text: t(".change"), + href: new_placements_organisation_user_path(organisation, user.as_form_params), + html_attributes: { + class: "govuk-link--no-visited-state", + }) %> + <% end %> + <% summary_list.with_row do |row| %> + <% row.with_key(text: User.human_attribute_name(:email)) %> + <% row.with_value(text: user.email) %> + <% row.with_action(text: t(".change"), + href: new_placements_organisation_user_path(organisation, user.as_form_params), + html_attributes: { + class: "govuk-link--no-visited-state", + }) %> + <% end %> + <% end %> + +
+ + <%= t(".warning", organisation_name: organisation.name) %> +
+ + <%= f.govuk_submit t(".add_user") %> + +

+ <%= govuk_link_to(t(".cancel"), placements_organisation_users_path(organisation)) %> +

+ <% end %> +
+
+
diff --git a/app/views/placements/organisations/users/_new_form.html.erb b/app/views/placements/organisations/users/_new_form.html.erb new file mode 100644 index 0000000000..bc62fc75d6 --- /dev/null +++ b/app/views/placements/organisations/users/_new_form.html.erb @@ -0,0 +1,33 @@ +<%# locals: (organisation: nil, user: nil) -%> +<%= form_with(model: user, url: check_placements_organisation_users_path(organisation), method: "get") do |f| %> + <%= f.govuk_error_summary %> + +
+
+ <%= t(".add_user", organisation_name: organisation.name) %> +

<%= t(".personal_details") %>

+ +
+ <%= f.govuk_text_field :first_name, + class: "govuk-input--width-20", + label: { text: User.human_attribute_name(:first_name), size: "s" } %> +
+ +
+ <%= f.govuk_text_field :last_name, + class: "govuk-input--width-20", + label: { text: User.human_attribute_name(:last_name), size: "s" } %> +
+ +
+ <%= f.govuk_text_field :email, label: { text: User.human_attribute_name(:email), size: "s" } %> +
+ + <%= f.govuk_submit t(".continue") %> + +

+ <%= govuk_link_to(t(".cancel"), placements_organisation_users_path(organisation)) %> +

+
+
+<% end %> diff --git a/app/views/placements/organisations/users/_user_details.html.erb b/app/views/placements/organisations/users/_user_details.html.erb new file mode 100644 index 0000000000..2d024d60f9 --- /dev/null +++ b/app/views/placements/organisations/users/_user_details.html.erb @@ -0,0 +1,14 @@ +<%= govuk_summary_list do |summary_list| %> + <% summary_list.with_row do |row| %> + <% row.with_key(text: User.human_attribute_name(:first_name)) %> + <% row.with_value(text: @user.first_name) %> + <% end %> + <% summary_list.with_row do |row| %> + <% row.with_key(text: User.human_attribute_name(:last_name)) %> + <% row.with_value(text: @user.last_name) %> + <% end %> + <% summary_list.with_row do |row| %> + <% row.with_key(text: User.human_attribute_name(:email)) %> + <% row.with_value(text: @user.email) %> + <% end %> +<% end %> diff --git a/app/views/placements/organisations/users/_users_list.html.erb b/app/views/placements/organisations/users/_users_list.html.erb new file mode 100644 index 0000000000..81e2483b8c --- /dev/null +++ b/app/views/placements/organisations/users/_users_list.html.erb @@ -0,0 +1,20 @@ +<% if users.any? %> + <%= govuk_table do |table| %> + <% table.with_head do |head| %> + <% head.with_row do |row| %> + <% row.with_cell(header: true, text: t(".name")) %> + <% row.with_cell(header: true, text: User.human_attribute_name(:email)) %> + <% end %> + <% end %> + <% table.with_body do |body| %> + <% users.each do |user| %> + <% body.with_row do |row| %> + <% row.with_cell(text: govuk_link_to(user.full_name, placements_organisation_user_path(organisation, user))) %> + <% row.with_cell(text: user.email) %> + <% end %> + <% end %> + <% end %> + <% end %> +<% else %> +

<%= t("no_users", organisation_name: organisation.name) %>

+<% end %> diff --git a/app/views/placements/providers/users/check.html.erb b/app/views/placements/providers/users/check.html.erb new file mode 100644 index 0000000000..b110592e0d --- /dev/null +++ b/app/views/placements/providers/users/check.html.erb @@ -0,0 +1,28 @@ +<%= content_for :page_title, sanitize(t(".check_your_answers", provider_name: @provider.name)) %> +<% if current_user.organisation_count > 1 %> + <%= content_for(:header_content) do %> + <%= render(ContentHeaderComponent.new( + title: @provider.name, + actions: [govuk_link_to(t(".change_organisation"), placements_organisations_path, no_visited_state: true)], + )) %> + <% end %> +<% end %> +<%= render "placements/primary_navigation", current_navigation: :users, organisation: @provider %> +<%= content_for(:before_content) do %> + <%= govuk_back_link(href: new_placements_provider_user_path( + @user_form.as_form_params.merge(provider_id: @provider.id), + )) %> +<% end %> + +
+
+
+ + + <%= render "placements/organisations/users/check_form", user: @user_form, organisation: @provider %> +
+
+
diff --git a/app/views/placements/providers/users/index.html.erb b/app/views/placements/providers/users/index.html.erb new file mode 100644 index 0000000000..4cc5bb6206 --- /dev/null +++ b/app/views/placements/providers/users/index.html.erb @@ -0,0 +1,20 @@ +<% content_for(:page_title) { t(".users") } %> +<% if current_user.organisation_count > 1 %> + <%= content_for(:header_content) do %> + <%= render(ContentHeaderComponent.new( + title: @provider.name, + actions: [govuk_link_to(t(".change_organisation"), placements_organisations_path, no_visited_state: true)], + )) %> + <% end %> +<% end %> +<%= render "placements/primary_navigation", organisation: @provider, current_navigation: :users %> + +
+

<%= t(".users") %>

+
+
+ <%= govuk_button_to(t(".add_user"), new_placements_provider_user_path, method: :get) %> + <%= render "placements/organisations/users/users_list", users: @users, organisation: @provider %> +
+
+
diff --git a/app/views/placements/providers/users/new.html.erb b/app/views/placements/providers/users/new.html.erb new file mode 100644 index 0000000000..b54bb8c537 --- /dev/null +++ b/app/views/placements/providers/users/new.html.erb @@ -0,0 +1,18 @@ +<%= content_for :page_title, t(".page_title") %> +<%= render "placements/primary_navigation", current_navigation: :users, organisation: @provider %> +<% if current_user.organisation_count > 1 %> + <%= content_for(:header_content) do %> + <%= render(ContentHeaderComponent.new( + title: @provider.name, + actions: [govuk_link_to(t(".change_organisation"), placements_organisations_path, no_visited_state: true)], + )) %> + <% end %> +<% end %> + +<%= content_for(:before_content) do %> + <%= govuk_back_link(href: placements_provider_users_path(@provider)) %> +<% end %> + +
+ <%= render "placements/organisations/users/new_form", user: @user_form, organisation: @provider %> +
diff --git a/app/views/placements/providers/users/show.html.erb b/app/views/placements/providers/users/show.html.erb new file mode 100644 index 0000000000..1e8e825b9e --- /dev/null +++ b/app/views/placements/providers/users/show.html.erb @@ -0,0 +1,22 @@ +<% if current_user.organisation_count > 1 %> + <%= content_for(:header_content) do %> + <%= render(ContentHeaderComponent.new( + title: @provider.name, + actions: [govuk_link_to(t(".change_organisation"), placements_organisations_path, no_visited_state: true)], + )) %> + <% end %> +<% end %> +<%= render "placements/primary_navigation", organisation: @provider, current_navigation: :users %> +<%= content_for :page_title, sanitize(@user.full_name) %> +<%= content_for(:before_content) do %> + <%= govuk_back_link(href: placements_provider_users_path(@provider)) %> +<% end %> + +
+

<%= @user.full_name %>

+
+
+ <%= render "placements/organisations/user_details", user: @user %> +
+
+
diff --git a/app/views/placements/schools/show.html.erb b/app/views/placements/schools/show.html.erb index f4d02a8936..bc1015036a 100644 --- a/app/views/placements/schools/show.html.erb +++ b/app/views/placements/schools/show.html.erb @@ -1,5 +1,5 @@ <%= content_for :page_title, t(".organisation_details") %> -<% if current_user.memberships.many? %> +<% if current_user.organisation_count > 1 %> <%= content_for(:header_content) do %> <%= render(ContentHeaderComponent.new( title: @school.name, diff --git a/app/views/placements/schools/users/check.html.erb b/app/views/placements/schools/users/check.html.erb new file mode 100644 index 0000000000..56260e6e48 --- /dev/null +++ b/app/views/placements/schools/users/check.html.erb @@ -0,0 +1,28 @@ +<%= content_for :page_title, sanitize(t(".check_your_answers", school_name: @school.name)) %> +<% if current_user.organisation_count > 1 %> + <%= content_for(:header_content) do %> + <%= render(ContentHeaderComponent.new( + title: @school.name, + actions: [govuk_link_to(t(".change_organisation"), placements_organisations_path, no_visited_state: true)], + )) %> + <% end %> +<% end %> +<%= render "placements/primary_navigation", current_navigation: :users, organisation: @school %> +<%= content_for(:before_content) do %> + <%= govuk_back_link(href: new_placements_school_user_path( + @user_form.as_form_params.merge(school_id: @school.id), + )) %> +<% end %> + +
+
+
+ + + <%= render "placements/organisations/users/check_form", user: @user_form, organisation: @school %> +
+
+
diff --git a/app/views/placements/schools/users/index.html.erb b/app/views/placements/schools/users/index.html.erb new file mode 100644 index 0000000000..e96cd2ca49 --- /dev/null +++ b/app/views/placements/schools/users/index.html.erb @@ -0,0 +1,20 @@ +<% content_for(:page_title) { t(".users") } %> +<% if current_user.organisation_count > 1 %> + <%= content_for(:header_content) do %> + <%= render(ContentHeaderComponent.new( + title: @school.name, + actions: [govuk_link_to(t(".change_organisation"), placements_organisations_path, no_visited_state: true)], + )) %> + <% end %> +<% end %> +<%= render "placements/primary_navigation", organisation: @school, current_navigation: :users %> + +
+

<%= t(".users") %>

+
+
+ <%= govuk_button_to(t(".add_user"), new_placements_school_user_path, method: :get) %> + <%= render "placements/organisations/users/users_list", users: @users, organisation: @school %> +
+
+
diff --git a/app/views/placements/schools/users/new.html.erb b/app/views/placements/schools/users/new.html.erb new file mode 100644 index 0000000000..28a7a309e6 --- /dev/null +++ b/app/views/placements/schools/users/new.html.erb @@ -0,0 +1,18 @@ +<%= content_for :page_title, t(".page_title") %> +<%= render "placements/primary_navigation", current_navigation: :users, organisation: @school %> +<% if current_user.organisation_count > 1 %> + <%= content_for(:header_content) do %> + <%= render(ContentHeaderComponent.new( + title: @school.name, + actions: [govuk_link_to(t(".change_organisation"), placements_organisations_path, no_visited_state: true)], + )) %> + <% end %> +<% end %> + +<%= content_for(:before_content) do %> + <%= govuk_back_link(href: placements_school_users_path(@school)) %> +<% end %> + +
+ <%= render "placements/organisations/users/new_form", user: @user_form, organisation: @school %> +
diff --git a/app/views/placements/schools/users/show.html.erb b/app/views/placements/schools/users/show.html.erb new file mode 100644 index 0000000000..4dad5dffa2 --- /dev/null +++ b/app/views/placements/schools/users/show.html.erb @@ -0,0 +1,22 @@ +<% if current_user.organisation_count > 1 %> + <%= content_for(:header_content) do %> + <%= render(ContentHeaderComponent.new( + title: @school.name, + actions: [govuk_link_to(t(".change_organisation"), placements_organisations_path, no_visited_state: true)], + )) %> + <% end %> +<% end %> +<%= render "placements/primary_navigation", organisation: @school, current_navigation: :users %> +<%= content_for :page_title, sanitize(@user.full_name) %> +<%= content_for(:before_content) do %> + <%= govuk_back_link(href: placements_school_users_path(@school)) %> +<% end %> + +
+

<%= @user.full_name %>

+
+
+ <%= render "placements/organisations/user_details", user: @user %> +
+
+
diff --git a/config/locales/en/placements/organisations/users/check_form.yml b/config/locales/en/placements/organisations/users/check_form.yml new file mode 100644 index 0000000000..3313485521 --- /dev/null +++ b/config/locales/en/placements/organisations/users/check_form.yml @@ -0,0 +1,11 @@ +en: + placements: + organisations: + users: + check_form: + add_user: Add user + check_your_answers: Check your answers + change: Change + continue: Continue + cancel: Cancel + warning: "The user will be sent an email to tell them you’ve added them to %{organisation_name}." diff --git a/config/locales/en/placements/organisations/users/new_form.yml b/config/locales/en/placements/organisations/users/new_form.yml new file mode 100644 index 0000000000..299ba954e4 --- /dev/null +++ b/config/locales/en/placements/organisations/users/new_form.yml @@ -0,0 +1,9 @@ +en: + placements: + organisations: + users: + new_form: + add_user: Add user + personal_details: Personal details + continue: Continue + cancel: Cancel diff --git a/config/locales/en/placements/organisations/users/users_list.yml b/config/locales/en/placements/organisations/users/users_list.yml new file mode 100644 index 0000000000..ea5bc69508 --- /dev/null +++ b/config/locales/en/placements/organisations/users/users_list.yml @@ -0,0 +1,6 @@ +en: + placements: + organisations: + users: + users_list: + name: Name diff --git a/config/locales/en/placements/providers/users.yml b/config/locales/en/placements/providers/users.yml new file mode 100644 index 0000000000..c29f0ed617 --- /dev/null +++ b/config/locales/en/placements/providers/users.yml @@ -0,0 +1,20 @@ +en: + placements: + providers: + users: + index: + users: Users + add_user: Add user + change_organisation: Change organisation + new: + page_title: Personal details - Add user + change_organisation: Change organisation + add_user: Add user + check: + change_organisation: Change organisation + check_your_answers: Check your answers + add_user: Add user + show: + change_organisation: Change organisation + create: + user_added: User added diff --git a/config/locales/en/placements/schools/users.yml b/config/locales/en/placements/schools/users.yml new file mode 100644 index 0000000000..f8c0874d2d --- /dev/null +++ b/config/locales/en/placements/schools/users.yml @@ -0,0 +1,20 @@ +en: + placements: + schools: + users: + index: + users: Users + add_user: Add user + change_organisation: Change organisation + new: + page_title: Personal details - Add user + change_organisation: Change organisation + add_user: Add user + check: + change_organisation: Change organisation + check_your_answers: Check your answers + add_user: Add user + show: + change_organisation: Change organisation + create: + user_added: User added diff --git a/config/locales/en/placements/support/providers/users.yml b/config/locales/en/placements/support/providers/users.yml index 65cfaa8791..752804772f 100644 --- a/config/locales/en/placements/support/providers/users.yml +++ b/config/locales/en/placements/support/providers/users.yml @@ -4,12 +4,6 @@ en: providers: users: index: - secondary_navigation: - details: Details - users: Users - mentors: Mentors - placements: Placements - providers: Providers add_user: Add user empty_state: There are no users for %{provider_name}. heading: Users @@ -23,3 +17,4 @@ en: page_title: "Personal details - Add user - %{provider_name}" check: page_title: "Check your answers - Add user - %{provider_name}" + check_your_answers: Check your answers diff --git a/config/locales/en/placements/support/schools/users.yml b/config/locales/en/placements/support/schools/users.yml index 14a2e5ad25..6cf40998ed 100644 --- a/config/locales/en/placements/support/schools/users.yml +++ b/config/locales/en/placements/support/schools/users.yml @@ -4,12 +4,6 @@ en: schools: users: index: - secondary_navigation: - details: Details - users: Users - mentors: Mentors - placements: Placements - providers: Providers add_user: Add user heading: Users attributes: @@ -22,4 +16,6 @@ en: new: page_title: "Personal details - Add user - %{school_name}" check: - page_title: "Check your answers - Add user - %{school_name}" \ No newline at end of file + page_title: "Check your answers - Add user - %{school_name}" + caption: Add user - %{organisation_name} + check_your_answers: Check your answers \ No newline at end of file diff --git a/config/routes/placements.rb b/config/routes/placements.rb index c64f32f57e..80ade376e7 100644 --- a/config/routes/placements.rb +++ b/config/routes/placements.rb @@ -42,6 +42,19 @@ end resources :organisations, only: [:index] - resources :schools, only: [:show] - resources :providers, only: [:show] + resources :schools, only: %i[show] do + scope module: :schools do + resources :users, only: %i[index new create show] do + get :check, on: :collection + end + end + end + + resources :providers, only: [:show] do + scope module: :providers do + resources :users, only: %i[index new create show] do + get :check, on: :collection + end + end + end end diff --git a/spec/models/placements/user_spec.rb b/spec/models/placements/user_spec.rb index fcce29cf1a..edd420c36e 100644 --- a/spec/models/placements/user_spec.rb +++ b/spec/models/placements/user_spec.rb @@ -43,4 +43,25 @@ expect(described_class.new.service).to eq(:placements) end end + + describe "#organisation_count" do + describe "returns the count of only placements organisations" do + it "returns 0 if user has no placement organisations" do + user = create(:placements_user) + expect(user.organisation_count).to eq 0 + + create(:membership, user:, organisation: create(:claims_school)) + expect(user.organisation_count).to eq 0 + end + + it "returns combined provider and school count" do + user = create(:placements_user) + create(:membership, user:, organisation: create(:placements_school)) + create(:membership, user:, organisation: create(:placements_provider)) + create(:membership, user:, organisation: create(:placements_provider)) + + expect(user.organisation_count).to eq 3 + end + end + end end diff --git a/spec/system/claims/create_claim_spec.rb b/spec/system/claims/create_claim_spec.rb index 40e593c010..ff21cdf501 100644 --- a/spec/system/claims/create_claim_spec.rb +++ b/spec/system/claims/create_claim_spec.rb @@ -102,10 +102,6 @@ def given_i_sign_in_as_anne and_i_click_sign_in_as("Anne") end - def and_there_is_an_existing_persona_for(persona_name) - create(:persona, persona_name.downcase.to_sym, service: :claims) - end - def and_i_visit_the_personas_page visit personas_path end diff --git a/spec/system/placements/organisations/add_users_spec.rb b/spec/system/placements/organisations/add_users_spec.rb new file mode 100644 index 0000000000..5675dd1372 --- /dev/null +++ b/spec/system/placements/organisations/add_users_spec.rb @@ -0,0 +1,247 @@ +require "rails_helper" + +RSpec.describe "Placements users invite other users to organisations", type: :system, service: :placements do + let(:anne) { create(:placements_user, :anne) } + let(:one_school) { create(:placements_school, name: "One School") } + let(:one_provider) { create(:placements_provider, name: "One Provider") } + let(:mary) { create(:placements_user, :mary) } + let(:another_school) { create(:placements_school, name: "Another School") } + let(:new_user) { create(:placements_user) } + + describe "Ann invites a member successfully " do + context "provider" do + before "user is sent an invitation" do + notify_mailer = double(:notify_mailer) + expect(NotifyMailer).to receive(:send_organisation_invite_email).with(kind_of(Placements::User), one_provider, "http://placements.localhost/sign-in") { notify_mailer } + expect(notify_mailer).to receive(:deliver_later).and_return true + end + + scenario "user invites a member to a provider" do + given_i_am_logged_in_as_a_user_with_one_organisation(one_provider) + when_i_click_users + then_i_see_the_users_page + when_i_click_add_user + and_i_enter_valid_user_details + then_i_can_check_my_answers(one_provider) + when_i_click_back + then_i_see_prepopulated_form + when_i_change_user_details + then_i_see_changes_in_check_form + when_i_click_add_user + then_the_user_is_added + end + end + + context "school" do + before "user is sent an invitation" do + notify_mailer = double(:notify_mailer) + expect(NotifyMailer).to receive(:send_organisation_invite_email).with(kind_of(Placements::User), one_school, "http://placements.localhost/sign-in") { notify_mailer } + expect(notify_mailer).to receive(:deliver_later).and_return true + end + + scenario "user invites a member to a school" do + given_i_am_logged_in_as_a_user_with_one_organisation(one_school) + when_i_click_users + then_i_see_the_users_page + when_i_click_add_user + and_i_enter_valid_user_details + then_i_can_check_my_answers(one_school) + when_i_click_back + then_i_see_prepopulated_form + when_i_change_user_details + then_i_see_changes_in_check_form + when_i_click_add_user + then_the_user_is_added + end + end + end + + describe "Mary invites a members to second organisation" do + before "user is sent an invitation" do + create(:membership, user: mary, organisation: one_school) + create(:membership, user: mary, organisation: another_school) + create(:membership, user: mary, organisation: one_provider) + + notify_mailer = double(:notify_mailer) + expect(NotifyMailer).to receive(:send_organisation_invite_email).with(kind_of(Placements::User), another_school, "http://placements.localhost/sign-in") { notify_mailer } + expect(notify_mailer).to receive(:deliver_later).and_return true + + expect(NotifyMailer).to receive(:send_organisation_invite_email).with(kind_of(Placements::User), one_provider, "http://placements.localhost/sign-in") { notify_mailer } + expect(notify_mailer).to receive(:deliver_later).and_return true + end + + scenario "user adds a user to multiple organisations" do + given_i_am_logged_in_as_a_user_with_multiple_organisations + and_user_is_already_assigned_to_a_school + when_i_navigate_to_that_schools_users + then_i_see_the_user_on_that_schools_user_list + when_i_change_organisation(another_school) + and_i_try_to_add_the_user + then_the_user_is_added_successfully + when_i_change_organisation(one_provider) + and_i_try_to_add_the_user + end + end + + scenario "user tries to submit invalid form" do + given_i_am_logged_in_as_a_user_with_one_organisation(one_school) + when_i_click_users + then_i_see_the_users_page + when_i_click_add_user + and_try_to_submit_invalid_form_data + then_i_see_form_errors + end + + scenario "user tries to add an existing user to the organisation" do + given_i_am_logged_in_as_a_user_with_one_organisation(one_school) + and_user_is_already_assigned_to_a_school + when_i_try_to_add_the_user_to_the_same_school + then_i_see_the_email_taken_error + end + + private + + def and_try_to_submit_invalid_form_data + fill_in "Email", with: "firsty_lasty" + click_on "Continue" + end + + def then_i_see_form_errors + expect(page.find(".govuk-error-summary")).to have_content "There is a problem" + expect(page).to have_content("Enter a first name").twice + expect(page).to have_content("Enter a last name").twice + expect(page).to have_content("Enter an email address in the correct format, like name@example.com").twice + end + + def then_i_see_the_email_taken_error + expect(page.find(".govuk-error-summary")).to have_content "There is a problem" + expect(page).to have_content("This email address is already in use. Try another email address") + end + + def given_i_am_logged_in_as_a_user_with_one_organisation(organisation) + create(:membership, user: anne, organisation:) + visit personas_path + click_on "Sign In as Anne" + end + + def given_i_am_logged_in_as_a_user_with_multiple_organisations + visit personas_path + click_on "Sign In as Mary" + end + + def and_user_is_already_assigned_to_a_school + create(:membership, user: new_user, organisation: one_school) + end + + def when_i_try_to_add_the_user_to_the_same_school + click_on "Users" + click_on "Add user" + fill_in "First name", with: new_user.first_name + fill_in "Last name", with: new_user.last_name + fill_in "Email", with: new_user.email + click_on "Continue" + end + + def when_i_navigate_to_that_schools_users + click_on "One School" + click_on "Users" + end + + def then_i_see_the_user_on_that_schools_user_list + users_is_selected_in_navigation + expect(page).to have_content(new_user.full_name) + expect(page).to have_content(new_user.email) + end + + def when_i_change_organisation(organisation) + click_on "Change organisation" + click_on organisation.name + click_on "Users" + end + + def and_i_try_to_add_the_user + click_on "Add user" + fill_in "First name", with: new_user.first_name + fill_in "Last name", with: new_user.last_name + fill_in "Email", with: new_user.email + click_on "Continue" + click_on "Add user" + end + + def then_the_user_is_added_successfully + users_is_selected_in_navigation + + expect(page.find(".govuk-notification-banner__content")).to have_content("User added") + expect(page).to have_content new_user.full_name + expect(page).to have_content new_user.email + end + + def when_i_click_users + click_on "Users" + end + + def then_i_see_the_users_page + users_is_selected_in_navigation + + expect(page).to have_content "Anne Wilson" + expect(page).to have_content "anne_wilson@example.org" + end + + def when_i_click_add_user + click_on "Add user" + end + + def and_i_enter_valid_user_details + users_is_selected_in_navigation + fill_in "First name", with: "First Namey" + fill_in "Last name", with: "Last Namey" + fill_in "Email", with: "firsty_lasty@email.co.uk" + click_on "Continue" + end + + def then_i_can_check_my_answers(organisation) + users_is_selected_in_navigation + expect(page).to have_content "First Namey" + expect(page).to have_content "Last Namey" + expect(page).to have_content "firsty_lasty@email.co.uk" + expect(page).to have_content "The user will be sent an email to tell them you’ve added them to #{organisation.name}." + end + + def when_i_click_back + click_on "Back" + end + + def then_i_see_prepopulated_form + users_is_selected_in_navigation + expect(page).to have_field("First name", with: "First Namey") + expect(page).to have_field("Last name", with: "Last Namey") + expect(page).to have_field("Email", with: "firsty_lasty@email.co.uk") + end + + def when_i_change_user_details + fill_in "First name", with: "New First Name" + click_on "Continue" + end + + def then_i_see_changes_in_check_form + expect(page).to have_content "New First Name" + expect(page).to have_content "Last Namey" + expect(page).to have_content "firsty_lasty@email.co.uk" + end + + def then_the_user_is_added + users_is_selected_in_navigation + expect(page.find(".govuk-notification-banner__content")).to have_content("User added") + expect(page).to have_content "New First Name Last Namey" + expect(page).to have_content "firsty_lasty@email.co.uk" + end + + def users_is_selected_in_navigation + within(".app-primary-navigation__nav") do + expect(page).to have_link "Placements", current: "false" + expect(page).to have_link "Mentors", current: "false" + expect(page).to have_link "Users", current: "page" + expect(page).to have_link "Organisation details", current: "false" + end + end +end