Skip to content

Commit

Permalink
Added new files for adding contract recipients
Browse files Browse the repository at this point in the history
  • Loading branch information
edwin-jebaraj committed Feb 25, 2025
1 parent 71eccbd commit b2af171
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 1 deletion.
65 changes: 65 additions & 0 deletions app/controllers/support/cases/contract_recipients_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module Support
class Cases::ContractRecipientsController < Cases::ApplicationController
before_action :set_current_case
before_action :set_contract_recipient, only: %i[edit update destroy]

before_action only: %i[new create edit update destroy] do
@back_url = support_case_contract_recipients_path(@current_case)
end

before_action only: [:index] do
@back_url = support_case_path(@current_case, anchor: "tasklist")
end
def index
@contract_recipients = @current_case.contract_recipients.all
end

def new
@contract_recipient = @current_case.contract_recipients.new
end

def create
@contract_recipient = @current_case.contract_recipients.new(contract_recipient_params)

if @contract_recipient.save
redirect_to support_case_contract_recipients_path(case_id: @current_case),
notice: I18n.t("support.contract_recipients.flash.success", name: @contract_recipient.name)
else
render :new
end
end

def edit; end

def update
if @contract_recipient.update(contract_recipient_params)
redirect_to support_case_contract_recipients_path(case_id: @current_case),
notice: I18n.t("support.contract_recipients.flash.updated", name: @contract_recipient.name)
else
render :edit
end
end

def destroy
return unless params[:confirm]

@contract_recipient.destroy!
redirect_to support_case_contract_recipients_path(case_id: @current_case),
notice: I18n.t("support.contract_recipients.flash.destroyed", name: @contract_recipient.name)
end

private

def set_contract_recipient
@contract_recipient = current_case.contract_recipients.find(params[:id])
end

def set_current_case
@current_case = Support::Case.find(params[:case_id])
end

def contract_recipient_params
params.require(:support_contract_recipient).permit(:first_name, :last_name, :email)
end
end
end
2 changes: 2 additions & 0 deletions app/models/support/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class Case < ApplicationRecord

has_many :evaluators_upload_documents, class_name: "Support::EvaluatorsUploadDocument", foreign_key: :support_case_id

has_many :contract_recipients, class_name: "Support::ContractRecipient", foreign_key: :support_case_id

has_many :upload_contract_handovers, class_name: "Support::UploadContractHandover", foreign_key: :support_case_id

# Support level
Expand Down
16 changes: 16 additions & 0 deletions app/models/support/contract_recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Support
class ContractRecipient < ApplicationRecord
belongs_to :support_case, class_name: "Support::Case"

validates :email,
presence: true,
format: { with: URI::MailTo::EMAIL_REGEXP },
uniqueness: { case_sensitive: false, scope: :support_case_id }
validates :first_name, :last_name, presence: true
belongs_to :user, foreign_key: "dsi_uid", primary_key: "dfe_sign_in_uid", optional: true

def name
[first_name, last_name].compact_blank.join(" ")
end
end
end
27 changes: 27 additions & 0 deletions app/views/support/cases/contract_recipients/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= form_with(
model: @contract_recipient,
url: @contract_recipient.new_record? ? support_case_contract_recipients_path(case_id: @current_case)
: support_case_contract_recipient_path(@contract_recipient, case_id: @current_case)
) do |form| %>

<%= form.govuk_error_summary %>

<%= form.govuk_text_field :first_name, autofocus: true, width: "one-half" %>
<%= form.govuk_text_field :last_name, width: "one-half" %>
<%= form.govuk_text_field :email, width: "one-half" %>

<div class="govuk-button-group flex-align-center">
<%= form.submit I18n.t("support.contract_recipients.submit"), class: "govuk-button" %>

<% unless @contract_recipient.new_record? %>
<%= link_to I18n.t("generic.button.remove"),
support_case_contract_recipient_path(@contract_recipient, case_id: @current_case),
method: :delete,
class: "govuk-button govuk-button--secondary" %>
<%end%>

<%= link_to I18n.t("generic.button.cancel"),
support_case_contract_recipients_path(case_id: @current_case),
class: "govuk-link govuk-link--no-visited-state" %>
</div>
<% end %>
15 changes: 15 additions & 0 deletions app/views/support/cases/contract_recipients/destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%= render partial: "support/cases/components/case_header", locals: { current_case: @current_case } %>
<h1 class="govuk-heading-l">
<%= I18n.t("support.contract_recipients.delete_confirmation", name: @contract_recipient.name) %>
</h1>

<div class="govuk-button-group flex-align-center">
<%= link_to I18n.t("generic.button.remove"),
support_case_contract_recipient_path(@contract_recipient, case_id: @current_case, confirm: true),
method: :delete,
class: "govuk-button govuk-button--warning" %>

<%= link_to I18n.t("generic.button.cancel"),
support_case_contract_recipients_path(case_id: @current_case),
class: "govuk-link govuk-link--no-visited-state" %>
</div>
4 changes: 4 additions & 0 deletions app/views/support/cases/contract_recipients/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= render partial: "support/cases/components/case_header", locals: { current_case: @current_case } %>
<h1 class="govuk-heading-l"><%= I18n.t("support.contract_recipients.header.edit") %></h1>

<%= render "form" %>
32 changes: 32 additions & 0 deletions app/views/support/cases/contract_recipients/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%= render partial: "support/cases/components/case_header", locals: { current_case: @current_case } %>
<h1 class="govuk-heading-l"><%= I18n.t("support.contract_recipients.header.index") %></h1>

<table class="govuk-table messages-table">
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th scope="col" class="govuk-table__header"
><%= I18n.t(:name, scope: "helpers.label.support_contract_recipient") %></th>
<th scope="col" class="govuk-table__header"
><%= I18n.t(:email, scope: "helpers.label.support_contract_recipient") %></th>
<th scope="col" class="govuk-table__header"></th>

<th></th>
</tr>
</thead>
<tbody class="govuk-table__body">
<% @contract_recipients.each do |contract_recipient| %>
<div class="govuk-summary-list__row">
<tr>
<td class="govuk-table__cell"><%= contract_recipient.name %></td>
<td class="govuk-table__cell"><%= contract_recipient.email %></td>
<td class="govuk-table__cell"
><%= link_to I18n.t("generic.link.change"),
edit_support_case_contract_recipient_path(contract_recipient, case_id: @current_case) %></td>
</tr>
</div>
<% end %>
</tbody>
</table>

<%= link_to I18n.t("generic.button.add"),
new_support_case_contract_recipient_path(case_id: @current_case), class: 'govuk-button' %>
4 changes: 4 additions & 0 deletions app/views/support/cases/contract_recipients/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= render partial: "support/cases/components/case_header", locals: { current_case: @current_case } %>
<h1 class="govuk-heading-l"><%= I18n.t("support.contract_recipients.header.new") %></h1>

<%= render "form" %>
9 changes: 8 additions & 1 deletion app/views/support/cases/show/_tasklist.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@
<%= I18n.t("support.case.label.tasklist.handover_contract") %>
</h3>
<%= govuk_task_list(id_prefix: "handover_contract") do |task_list|
task_list.with_item(title: I18n.t("support.case.label.tasklist.item.add_contract_recipients"), href: '#', status: govuk_tag(text: I18n.t("support.case.label.tasklist.status.to_do")))

if @current_case.contract_recipients.any?
add_contract_recipients_status = govuk_tag(text: I18n.t("support.case.label.tasklist.status.complete"), colour: "green")
else
add_contract_recipients_status = govuk_tag(text: I18n.t("support.case.label.tasklist.status.to_do"))
end

task_list.with_item(title: I18n.t("support.case.label.tasklist.item.add_contract_recipients"), href: support_case_contract_recipients_path(@current_case), status: add_contract_recipients_status)

if @current_case.upload_contract_handover_complete?
upload_contract_handover_status = govuk_tag(text: I18n.t("support.case.label.tasklist.status.complete"), colour: "green")
Expand Down
16 changes: 16 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,11 @@ en:
first_name: First name
last_name: Last name
name: Name
support_contract_recipient:
email: Email address
first_name: First name
last_name: Last name
name: Name
journey:
edit:
name:
Expand Down Expand Up @@ -1480,6 +1485,17 @@ en:
index: Evaluators
new: Add evaluator details
submit: Save changes
contract_recipients:
delete_confirmation: Are you sure you want to remove %{name}?
flash:
destroyed: "%{name} successfully removed"
success: "%{name} successfully added"
updated: "%{name} successfully updated"
header:
edit: Update contract recipient details
index: Contract Recipients
new: Add contract recipient details
submit: Save changes
case_hub_migration:
edit:
header: Check your answers before creating a new case
Expand Down
10 changes: 10 additions & 0 deletions config/locales/validation/support/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ en:
blank: Enter the first name of the evaluator
last_name:
blank: Enter the last name of the evaluator
support/contract_recipient:
attributes:
email:
blank: Enter the email address of the contract recipient
invalid: Enter an email address in the correct format, like name@example.com
taken: This email address has already been added as a contract recipient
first_name:
blank: Enter the first name of the contract recipient
last_name:
blank: Enter the last name of the contract recipient

activemodel:
errors:
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
resource :document_uploads, except: %i[show]
resource :email_evaluators, except: %i[show]
resource :review_evaluation, except: %i[show]
resources :contract_recipients, except: %i[show]
resource :upload_contract_handover, except: %i[show]
resource :email, only: %i[create] do
scope module: :emails do
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20250224123310_add_contract_recipients.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AddContractRecipients < ActiveRecord::Migration[7.2]
def change
create_table "support_contract_recipients", id: :uuid do |t|
t.references "support_case", type: :uuid
t.string "first_name", null: false
t.string "last_name", null: false
t.string "email", null: false
t.string "dsi_uid", default: "", null: false
t.timestamps
t.index %w[dsi_uid]
t.index %w[email support_case_id], unique: true
end
end
end
43 changes: 43 additions & 0 deletions spec/features/support/agent_can_add_contract_recipient_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "rails_helper"

describe "Agent can add contract recipient", :js do
include_context "with an agent"

let(:support_case) { create(:support_case) }

specify "Adding contract recipient" do
visit support_case_contract_recipients_path(case_id: support_case)

click_link "Add"

fill_in "First name", with: "Momo"
fill_in "Last name", with: "Taro"
fill_in "Email address", with: "momotaro@example.com"

click_button "Save changes"

expect(page).to have_text("Momo Taro successfully added")
expect(page).to have_text("momotaro@example.com")

click_link "Change"

fill_in "First name", with: "Oni"
fill_in "Last name", with: "Baba"
fill_in "Email address", with: "onibaba@example.com"

click_button "Save changes"

expect(page).to have_text("Oni Baba successfully updated")
expect(page).to have_text("onibaba@example.com")

click_link "Change"
click_link "Remove"

expect(page).to have_text("Are you sure you want to remove Oni Baba")

click_link "Remove"

expect(page).to have_text("Oni Baba successfully removed")
expect(page).not_to have_text("onibaba@example.com")
end
end

0 comments on commit b2af171

Please sign in to comment.