Skip to content

Commit

Permalink
As a Support User, I should be able to invite new Users to a School O…
Browse files Browse the repository at this point in the history
…rganisation
  • Loading branch information
gms-gs committed Jan 8, 2024
1 parent 346bf36 commit b533536
Show file tree
Hide file tree
Showing 14 changed files with 284 additions and 5 deletions.
39 changes: 39 additions & 0 deletions app/controllers/claims/support/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,48 @@ def index
@users = @school.users
end

def new
@user = Claims::User.new
end

def check
if user.valid?
@user = user.decorate
else
render :new
end
end

def create
if user.save
attach_user_to_school
send_invite_email_to_user
redirect_to claims_support_school_users_path(@school)
flash[:success] = "User added"
else
render :new
end
end

private

def set_school
@school = Claims::School.find(params[:school_id])
end

def user
@user ||= Claims::User.new(user_params)
end

def attach_user_to_school
Membership.create!(user_id: user.id, organisation_type: "School", organisation_id: @school.id)
end

def send_invite_email_to_user
NotifyMailer.send_school_invite_email(user, @school, sign_in_url).deliver_now
end

def user_params
params.require(:claims_user).permit(:first_name, :last_name, :email)
end
end
3 changes: 3 additions & 0 deletions app/decorators/claims/user_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Claims::UserDecorator < Draper::Decorator
delegate_all
end
10 changes: 10 additions & 0 deletions app/mailers/notify_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class NotifyMailer < ApplicationMailer
# NotifyMailer.send_school_invite_email.deliver_now
def send_school_invite_email(user, school, sign_in_url)
body = "Dear #{user.full_name} \n\n You have been invited to join the claims service for #{school.name}.\n\n Sign in here #{sign_in_url}"

mailer_options = { to: user.email, subject: "You have been invited to #{school.name}", body: }

notify_email(mailer_options)
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class User < ApplicationRecord

validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :service, presence: true
validates :email, uniqueness: { scope: :service, case_sensitive: false }

Expand Down
2 changes: 1 addition & 1 deletion app/views/claims/support/schools/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl"><%= t("organisations") %></h1>
<h1 class="govuk-heading-xl"><%= t(".heading") %></h1>

<%= govuk_button_to(t("add_organisation"), new_claims_support_school_path, method: :get) %>

Expand Down
65 changes: 65 additions & 0 deletions app/views/claims/support/users/check.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<%= content_for(:before_content) do %>
<%= govuk_back_link(href: new_claims_support_school_user_path(@school)) %>
<% end %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_with(model: @user, url: claims_support_school_users_path, 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 %>

<label class="govuk-label govuk-label--l">
<span class="govuk-caption-l"><%= t(".caption") %></span>
<%= t(".title") %>
</label>

<dl class="govuk-summary-list">
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key"><%= t(".first_name") %></dt>
<dd class="govuk-summary-list__value">
<p class="govuk-body"><%= @user.first_name %></p>
</dd>
<dd class="govuk-summary-list__actions">
<ul class="govuk-summary-list__actions-list">
<li class="govuk-summary-list__actions-list-item"><a class="govuk-link" href="#">Change<span class="govuk-visually-hidden"></span></a></li>
</ul>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key"><%= t(".last_name") %></dt>
<dd class="govuk-summary-list__value">
<p class="govuk-body"><%= @user.last_name %></p>
</dd>
<dd class="govuk-summary-list__actions">
<ul class="govuk-summary-list__actions-list">
<li class="govuk-summary-list__actions-list-item"><a class="govuk-link" href="#">Change<span class="govuk-visually-hidden"></span></a></li>
</ul>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key"><%= t(".email") %></dt>
<dd class="govuk-summary-list__value">
<p class="govuk-body"><%= @user.email %></p>
</dd>
<dd class="govuk-summary-list__actions">
<ul class="govuk-summary-list__actions-list">
<li class="govuk-summary-list__actions-list-item"><a class="govuk-link" href="#">Change<span class="govuk-visually-hidden"></span></a></li>
</ul>
</dd>
</div>
</dl>

<div class="govuk-warning-text">
<span class="govuk-warning-text__icon" aria-hidden="true">!</span>
<strong class="govuk-warning-text__text"><%= t(".warning", school_name: @school.name) %></strong>
</div>

<%= f.govuk_submit t(".add_user") %>

<p class="govuk-body">
<%= govuk_link_to(t(".cancel"), claims_support_school_users_path(@school)) %>
</p>
<% end %>
<div>
<div>
1 change: 1 addition & 0 deletions app/views/claims/support/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l"><%= t(".heading") %></h1>
<%= govuk_button_to(t("invite_user"), new_claims_support_school_user_path, method: :get) %>
</div>
</div>

Expand Down
36 changes: 36 additions & 0 deletions app/views/claims/support/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<%= content_for(:before_content) do %>
<%= govuk_back_link(href: claims_support_school_users_path(@school)) %>
<% end %>

<%= form_with(model: @user, url: check_claims_support_school_users_path, method: "get") do |f| %>
<%= f.govuk_error_summary %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<span class="govuk-caption-l"><%= t(".caption") %></span>
<h2 class="govuk-heading-l"><%= t(".title") %></h2>

<div class="govuk-form-group">
<%= f.govuk_text_field :first_name,
class: "govuk-input--width-20",
label: { text: t(".attributes.users.first_name"), size: "s" } %>
</div>

<div class="govuk-form-group">
<%= f.govuk_text_field :last_name,
class: "govuk-input--width-20",
label: { text: t(".attributes.users.last_name"), size: "s" } %>
</div>

<div class="govuk-form-group">
<%= f.govuk_text_field :email, label: { text: t(".attributes.users.email"), size: "s" } %>
</div>

<%= f.govuk_submit t("continue") %>

<p class="govuk-body">
<%= govuk_link_to(t("cancel"), claims_support_school_users_path(@school)) %>
</p>
</div>
</div>
<% end %>
12 changes: 12 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@

<div class="govuk-width-container">
<%= yield :before_content %>

<% flash.each do |key, message| %>
<div class="govuk-notification-banner govuk-notification-banner--success govuk-!-margin-top-6 govuk-!-margin-bottom-0" role="alert" aria-labelledby="govuk-notification-banner-title" data-module="govuk-notification-banner">
<div class="govuk-notification-banner__header">
<h2 class="govuk-notification-banner__title" id="govuk-notification-banner-title">
</div>
<div class="govuk-notification-banner__content">
<h3 class="govuk-notification-banner__heading"><%= message %></h3>
</div>
</div>
<% end %>

<main class="govuk-main-wrapper" id="main-content" role="main">
<%= yield %>
</main>
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions config/locales/claims/en/support/schools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ en:
claims:
support:
schools:
index:
heading: Organisations
new:
caption: Add organisation
cancel: Cancel
Expand Down
33 changes: 31 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ en:
taken: This provider has already been added. Try another provider
blank: Enter a provider name, UKPRN, URN or postcode
school:
attributes:
urn:
attributes:
urn:
taken: This school has already been added. Try another school
blank: Enter a school name, URN or postcode
gias_school:
required: ""
user:
attributes:
first_name:
blank: Enter a first name
last_name:
blank: Enter a last name
email:
blank: Enter an email address
taken: This email address is already in use. Try another email address
invalid: Enter an email address in the correct format, like [email protected]
add_organisation: Add organisation
organisations: Organisations
school: School
Expand Down Expand Up @@ -83,11 +93,30 @@ en:
support:
users:
index:
invite_user: Invite User
heading: Users
attributes:
users:
name: Name
email: Email
new:
attributes:
users:
first_name: First name
last_name: Last name
email: Email
title: Invite a new user
caption: Add user
description: Personal details
check:
title: Check your answers
caption: Add user
add_user: Add user
cancel: Cancel
first_name: First name
last_name: Last name
email: Email
warning: "The user will be sent an email to tell them you’ve added them to %{school_name}."
navigation_bar:
organisation_details: Organisation details
users: Users
Expand Down
4 changes: 3 additions & 1 deletion config/routes/claims.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
collection { get :check }

resources :claims
resources :users, only: [:index]
resources :users, only: %i[index new create] do
collection { get :check }
end
end
end
end
80 changes: 80 additions & 0 deletions spec/system/claims/invite_a_user_to_a_school_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require "rails_helper"

RSpec.describe "Invite a user to a school", type: :system do
before do
setup_school
mailer_double = instance_double(ActionMailer::MessageDelivery, deliver_now: nil)
allow(NotifyMailer).to receive(:send_school_invite_email).and_return(mailer_double)
end

scenario "I sign in as a support user and invite a user to a school" do
sign_in_as_support_user
visit_claims_support_school_users_page
click_on_invite_user_button
fill_in_user_details
check_user_details
click_on_add_user
verify_user_added
end

scenario "I sign in as a support user and invalid bad user details" do
sign_in_as_support_user
visit_claims_support_school_users_page
click_on_invite_user_button
fill_in_invalid_user_details
end

private

def setup_school
@school = create(:school, :claims, urn: "123456")
end

def sign_in_as_support_user
create(:persona, :colin, service: "claims")
visit personas_path
click_on "Sign In as Colin"
end

def visit_claims_support_school_users_page
visit claims_support_school_users_path(@school)
end

def click_on_invite_user_button
click_on "Invite User"
end

def fill_in_user_details
fill_in "First name", with: "Barry"
fill_in "Last name", with: "Garlow"
fill_in "Email", with: "[email protected]"
click_on "Continue"
end

def fill_in_invalid_user_details
fill_in "First name", with: "Barry"
fill_in "Last name", with: "Garlow"
fill_in "Email", with: "not a valid email"
click_on "Continue"
end

def show_error_messages
expect(page).to have_content("Enter an email address in the correct format, like [email protected]")
end

def check_user_details
expect(page).to have_content("Barry")
expect(page).to have_content("Garlow")
expect(page).to have_content("[email protected]")
end

def click_on_add_user
click_on "Add user"
end

def verify_user_added
visit_claims_support_school_users_page
expect(page).to have_content("Barry Garlow")
expect(page).to have_content("[email protected]")
end
end

0 comments on commit b533536

Please sign in to comment.