Skip to content

Commit

Permalink
sending emails to additional contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
DevMagnataur committed Oct 10, 2024
1 parent fec2b5a commit fe1c98e
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 45 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/base/_frontend.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ $govuk-new-organisation-colours: true;

td.govuk-table__cell :first-child {
margin-top: 0;
}

.recipient-spacing {
margin-right: 8px; /* Adjust as needed */
}
10 changes: 8 additions & 2 deletions app/controllers/support/cases/message_threads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ def show
end

def new
@to_recipients = Array(current_case.email).to_json
@to_recipients = Array(current_case.email + @current_case.additional_contacts.pluck(:email)).to_json
end

def edit
@reply_form = Email::Draft.find(params[:id])
end

def create
contact_email = [[current_case.email, current_case.is_evaluator ? "Lead, Evaluator" : "Lead"]] if current_case.email.present?
emails = Array(contact_email + current_case.additional_contacts_emails)
draft = Email::Draft.new(
default_content: default_template,
default_subject:,
template_id: params[:template_id],
ticket: current_case.to_model,
to_recipients: Array(current_case.email).to_json,
to_recipients: emails.to_json,
).save_draft!

redirect_to edit_support_case_message_thread_path(id: draft.id)
Expand All @@ -48,6 +50,10 @@ def create
def submit
@reply_form = Email::Draft.find(params[:id])
@reply_form.attributes = new_thread_params
emails = @reply_form.to_recipients.map(&:first)
@reply_form.to_recipients = emails.to_json
@reply_form.cc_recipients = @reply_form.cc_recipients.map(&:first).to_json if @reply_form.cc_recipients.present?
@reply_form.bcc_recipients = @reply_form.bcc_recipients.map(&:first).to_json if @reply_form.bcc_recipients.present?
if @reply_form.valid?(:new_message)
@reply_form.save_draft!
@reply_form.deliver_as_new_message
Expand Down
54 changes: 53 additions & 1 deletion app/controllers/support/cases/messages/replies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ def create
default_content: default_template,
template_id: params[:template_id],
ticket: current_case.to_model,
reply_to_email: current_email,
reply_to_email: [current_email],
role: %w[Lead] + [current_case.is_evaluator == true ? "Evaulator" : ""],
).save_draft!

redirect_to redirect_url
end

def submit
@reply_form = Email::Draft.find(params[:id])
emails = @reply_form.to_recipients.map(&:first)
@reply_form.reply_to_email = current_email
@reply_form.to_recipients = emails.uniq.to_json
@reply_form.attributes = form_params
@reply_form.email.to_recipients = emails
@reply_form.reply_to_email.to_recipients = emails
@reply_form.cc_recipients = @reply_form.cc_recipients.map(&:first).to_json if @reply_form.cc_recipients.present?
@reply_form.bcc_recipients = @reply_form.bcc_recipients.map(&:first).to_json if @reply_form.bcc_recipients.present?
if @reply_form.valid?
@reply_form.save_draft!
@reply_form.delivery_as_reply
Expand All @@ -47,6 +54,51 @@ def back_to_url_b64
current_url_b64
end

def add_recipient
@reply_form = Email::Draft.find(params[:id])
sender_email = [[current_email.sender["address"], [""]]]
current_case_role = current_case.is_evaluator ? ["Lead, Evaluator"] : %w[Lead]
emails = [[current_case.email, current_case_role]] + current_case.additional_contacts_emails + sender_email
emails.each do |email|
# Extract the email part from @reply_form.to_recipients for comparison
existing_emails = @reply_form.to_recipients.map { |recipient| recipient[0] }
# Add the email only if it's not already in the list (ignoring roles)
@reply_form.to_recipients << email unless existing_emails.include?(email[0])
end
debugger
if current_email.cc_recipients.present? && !@reply_form.cc_recipients.map { |recipient| recipient[0] }.include?(current_email.cc_recipients[0]["address"])
sender_cc_recipients = [[current_email.cc_recipients[0]["address"], [""]]] if current_email.cc_recipients.present?
@reply_form.cc_recipients = Array(@reply_form.cc_recipients + sender_cc_recipients).to_json if sender_cc_recipients.present?
end
if current_email.bcc_recipients.present? && !@reply_form.bcc_recipients.map { |recipient| recipient[0] }.include?(current_email.bcc_recipients[0]["address"])
sender_bcc_recipients = [[current_email.bcc_recipients[0]["address"], [""]]] if current_email.bcc_recipients.present?
@reply_form.bcc_recipients = Array(@reply_form.bcc_recipients + sender_bcc_recipients).to_json if sender_bcc_recipients.present?
end
@reply_form.save_draft!

respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.replace("recipient-frame",
partial: "support/cases/message_threads/recipient_table",
locals: { email: @reply_form })
end
end
end

def remove_recipient
@reply_form = Email::Draft.find(params[:id])
recipient_to_remove = [params[:email], params[:role]]
@reply_form.to_recipients.reject! { |recipient| recipient == recipient_to_remove }
@reply_form.save_draft!
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.replace("recipient-frame",
partial: "support/cases/message_threads/recipient_table",
locals: { email: @reply_form })
end
end
end

private

def default_template = render_to_string(partial: "support/cases/messages/reply_form_template")
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/tickets/message_threads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def create
def submit
@draft = Email::Draft.find(params[:id])
@draft.attributes = new_thread_params
emails = @draft.to_recipients.map(&:first)
@draft.to_recipients = emails.uniq.to_json
@draft.cc_recipients = @draft.cc_recipients.map(&:first).to_json if @draft.cc_recipients.present?
@draft.bcc_recipients = @draft.bcc_recipients.map(&:first).to_json if @draft.bcc_recipients.present?
@draft.email.to_recipients = emails.uniq.to_json
if @draft.valid?(:new_message)
@draft.save_draft!
@draft.deliver_as_new_message
Expand Down
39 changes: 31 additions & 8 deletions app/javascript/components/add-recipients.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ function getCollectionValues(collectionName) {
function removeRecipient(value, collectionName) {
const collection = document.getElementsByName(collectionName)[0];
const collectionValues = getCollectionValues(collectionName);
collection.value = JSON.stringify(collectionValues.filter(v => v !== value));
collection.value = JSON.stringify(
collectionValues.filter(v => JSON.stringify(v) !== JSON.stringify(value))
);
}

function createRemoveLink(tableId, inputValue, collectionName) {
Expand All @@ -33,22 +35,38 @@ function createRecipientRow(tableId, label, value, collectionName) {
labelTh.scope = "row";
labelTh.appendChild(document.createTextNode(label));

const recipientTd = document.createElement("td");
recipientTd.classList.add("govuk-table__cell");
const recipientEmail = document.createTextNode(value);
recipientTd.appendChild(recipientEmail);
const recipientEmailTd = document.createElement("td");
recipientEmailTd.classList.add("govuk-table__cell");
const recipientEmail = document.createTextNode(value[0]);
recipientEmailTd.appendChild(recipientEmail);

const recipientRoleTd = document.createElement("td");
recipientRoleTd.classList.add("govuk-table__cell", "recipient-spacing");
const rolevalue = value[1] && value[1].length > 0
? (value[1][1] && value[1][1].length > 1)
? [capitalizeFirstLetter(value[1][0]), capitalizeFirstLetter(value[1][1])]
: value[1][0].length > 1
? [capitalizeFirstLetter(value[1][0])] // Handle the single element correctly here
: [capitalizeFirstLetter(value[1])]
: ""; const recipientRole = document.createTextNode(rolevalue);
recipientRoleTd.appendChild(recipientRole);

const recipientRemoveTd = document.createElement("td");
recipientRemoveTd.classList.add("govuk-table__cell", "govuk-table__cell--numeric");
recipientRemoveTd.appendChild(createRemoveLink(tableId, value, collectionName));

recipientRow.appendChild(labelTh);
recipientRow.appendChild(recipientTd);
recipientRow.appendChild(recipientEmailTd);
recipientRow.appendChild(recipientRoleTd);
recipientRow.appendChild(recipientRemoveTd);

return recipientRow;
}

function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}

function showTables(inputFieldName) {
const inputField = document.getElementsByName(inputFieldName)[0];
buildTable(inputField.dataset.table, inputField.dataset.collection);
Expand All @@ -69,14 +87,19 @@ function addRecipient(inputFieldName) {
const inputField = document.getElementsByName(inputFieldName)[0];
const collection = document.getElementsByName(inputField.dataset.collection)[0];
var collectionValues = collection.value ? JSON.parse(collection.value) : [];

const inputValue = inputField.value;
collectionValues.push(inputValue);
collectionValues = [...new Set(collectionValues)].filter(Boolean) // ensure values are unique and non-null
const newRecipient = [inputValue, []];
collectionValues.push(newRecipient);
collectionValues = collectionValues.filter((item, index, self) =>
item[0] && self.findIndex(r => r[0] === item[0]) === index
);
collection.value = JSON.stringify(collectionValues);
buildTable(inputField.dataset.table, inputField.dataset.collection);
inputField.value = "";
}


function getRecipientsButtons() {
return document.querySelectorAll('[data-component="add-recipients"]');
}
Expand Down
1 change: 1 addition & 0 deletions app/models/email/draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Email::Draft
attribute :mailbox, default: -> { Email.default_mailbox }
attribute :microsoft_graph, default: -> { MicrosoftGraph.client }
attribute :reply_to_email
attribute :role
attribute :ticket
attribute :template_id
attribute :template_parser, default: -> { Email::TemplateParser.new }
Expand Down
3 changes: 2 additions & 1 deletion app/models/frameworks/evaluation/email_ticketable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def email_prefix
end

def default_recipients
Array(contact.try(:email)).to_json
emails = contact.try(:email) || ""
Array([[emails, ""]]).to_json if emails.present?
end

def unique_attachments(folder: "all")
Expand Down
4 changes: 4 additions & 0 deletions app/models/support/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,9 @@ def assign_to_agent(agent, assigned_by: Current.agent)
update!(agent:)
agent.notify_assigned_to_case(support_case: self, assigned_by:)
end

def additional_contacts_emails
case_additional_contacts.pluck(:email, :role)
end
end
end
20 changes: 9 additions & 11 deletions app/views/support/cases/message_threads/_recipient_table.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-three-quarters">
<table id="recipient-table" class="govuk-table added_recipients_table">
<tbody class="govuk-table__body">
<%= render "support/cases/message_threads/recipients", recipients: email.to_recipients, recipient_type: "TO" %>
<%= render "support/cases/message_threads/recipients", recipients: email.cc_recipients, recipient_type: "CC" %>
<%= render "support/cases/message_threads/recipients", recipients: email.bcc_recipients, recipient_type: "BCC" %>
</tbody>
</table>
</div>
</div>
<%= turbo_frame_tag "recipient-frame" do %>
<table id="recipient-table" class="govuk-table added_recipients_table">
<tbody id="recipient-table-body" class="govuk-table__body">
<%= render "support/cases/message_threads/recipients", recipients: email.to_recipients, recipient_type: "TO" %>
<%= render "support/cases/message_threads/recipients", recipients: email.cc_recipients, recipient_type: "CC" %>
<%= render "support/cases/message_threads/recipients", recipients: email.bcc_recipients, recipient_type: "BCC" %>
</tbody>
</table>
<% end %>
28 changes: 22 additions & 6 deletions app/views/support/cases/message_threads/_recipients.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
<% if recipients.present? %>
<tr class="govuk-table__row">
<th scope="row" class="govuk-table__header"><%= recipient_type %></th>
<td class="govuk-table__cell"><%= recipients %></td>
</tr>
<% end %>
<% if recipients.present?%>
<%if params[:action] == "add_recipient" || params[:action] == "remove_recipient"%>
<%recipients.each do |recipient|%>
<tr class="govuk-table__row">
<th scope="row" class="govuk-table__header"><%= recipient_type %></th>
<td class="govuk-table__cell"><%= recipient[0] %></td>
<td class="govuk-table__cell"><%= recipient[1].join(", ").titleize %></td>
<td class="govuk-table__cell">
<%= link_to "remove",
remove_recipient_support_case_message_replies_path(id: params[:id], email: recipient[0], role: recipient[1]),
data: { turbo_frame: "recipient-frame", turbo_method: "post" },
class: "govuk-link" %>
</td>
</tr>
<%end%>
<%else%>
<tr class="govuk-table__row">
<th scope="row" class="govuk-table__header"><%= recipient_type %></th>
<td class="govuk-table__cell"><%= recipients %></td>
</tr>
<%end%>
<%end%>
7 changes: 6 additions & 1 deletion app/views/support/cases/messages/replies/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@
<%= I18n.t("support.case.label.message_threads.using_template", template: @reply_form.template.title) %>
</div>
<% end %>
<% if reply %>
<div class="govuk-button-group">
<%= link_to "Add Additional Contacts",
add_recipient_support_case_message_replies_path(id: params[:id]),
data: { turbo_frame: "recipient-frame", turbo_method: "post" },
class: "govuk-button" %>
</div>
<%= render "support/cases/message_threads/recipient_table", email: %>
<% else %>
<%= render "support/cases/messages/replies/new_message_components", form:, unique_id: %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render "support/cases/message_threads/recipient_table", email: @reply_form%>
1 change: 0 additions & 1 deletion app/views/support/cases/messages/replies/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
</strong>
</div>
<% end %>
<%= render "support/cases/messages/replies/form", email: @last_received_reply, reply: true %>
<% end %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@
scope module: :messages do
resources :replies, only: %i[create edit] do
post "submit", on: :member
post "add_recipient", on: :collection
post "remove_recipient", on: :collection
end
end
end
Expand Down
11 changes: 0 additions & 11 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,6 @@
t.string "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "archived"
t.datetime "archived_at"
t.index ["la_code"], name: "index_local_authorities_on_la_code", unique: true
t.index ["name"], name: "index_local_authorities_on_name", unique: true
end
Expand Down Expand Up @@ -775,10 +773,6 @@
t.uuid "establishment_group_type_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "archived"
t.datetime "archived_at"
t.date "opened_date"
t.date "closed_date"
t.index ["establishment_group_type_id"], name: "index_establishment_groups_on_establishment_group_type_id"
t.index ["name"], name: "index_support_establishment_groups_on_name"
t.index ["uid"], name: "index_support_establishment_groups_on_uid", unique: true
Expand Down Expand Up @@ -884,11 +878,6 @@
t.string "federation_name"
t.string "federation_code"
t.uuid "local_authority_id"
t.boolean "archived"
t.datetime "archived_at"
t.date "closed_date"
t.string "reason_establishment_opened"
t.string "reason_establishment_closed"
t.index ["establishment_type_id"], name: "index_support_organisations_on_establishment_type_id"
t.index ["local_authority_id"], name: "index_support_organisations_on_local_authority_id"
t.index ["urn"], name: "index_support_organisations_on_urn", unique: true
Expand Down
Binary file added dump.rdb
Binary file not shown.
Loading

0 comments on commit fe1c98e

Please sign in to comment.