Skip to content

Commit

Permalink
Merge branch 'main' into 4547-item-dropdown-problem-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
coalest authored Dec 2, 2024
2 parents 94e9931 + a93fd40 commit ecfa898
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 80 deletions.
4 changes: 2 additions & 2 deletions app/controllers/transfers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def index
.during(helpers.selected_range)
@selected_from = filter_params[:from_location]
@selected_to = filter_params[:to_location]
@from_storage_locations = Transfer.storage_locations_transferred_from_in(current_organization)
@to_storage_locations = Transfer.storage_locations_transferred_to_in(current_organization)
@from_storage_locations = StorageLocation.with_transfers_from(current_organization)
@to_storage_locations = StorageLocation.with_transfers_to(current_organization)
respond_to do |format|
format.html
format.csv { send_data Transfer.generate_csv(@transfers), filename: "Transfers-#{Time.zone.today}.csv" }
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/date_range_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def date_range_label
"this month"
when "last month"
"last month"
when "last 12 months"
"last 12 months"
when "prior year"
"prior year"
else
selected_range_described
end
Expand Down
4 changes: 3 additions & 1 deletion app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ $(document).ready(function(){
'This Month': [today.startOf('month').toJSDate(), today.endOf('month').toJSDate()],
'Last Month': [today.minus({'months': 1}).startOf('month').toJSDate(),
today.minus({'month': 1}).endOf('month').toJSDate()],
'This Year': [today.startOf('year').toJSDate(), today.endOf('year').toJSDate()]
'Last 12 Months': [today.minus({'months': 12}).plus({'days': 1}).toJSDate(), today.toJSDate()],
'Prior Year': [today.startOf('year').minus({'years': 1}).toJSDate(), today.minus({'year': 1}).endOf('year').toJSDate()],
'This Year': [today.startOf('year').toJSDate(), today.endOf('year').toJSDate()],
}
}
});
Expand Down
10 changes: 8 additions & 2 deletions app/models/storage_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class StorageLocation < ApplicationRecord
has_many :distributions, dependent: :destroy
has_many :transfers_from, class_name: "Transfer",
inverse_of: :from,
foreign_key: :id,
foreign_key: :from_id,
dependent: :destroy
has_many :transfers_to, class_name: "Transfer",
inverse_of: :to,
foreign_key: :id,
foreign_key: :to_id,
dependent: :destroy
has_many :kit_allocations, dependent: :destroy

Expand All @@ -55,6 +55,12 @@ class StorageLocation < ApplicationRecord
scope :alphabetized, -> { order(:name) }
scope :for_csv_export, ->(organization, *) { where(organization: organization) }
scope :active_locations, -> { where(discarded_at: nil) }
scope :with_transfers_to, ->(organization) {
joins(:transfers_to).where(organization_id: organization.id).distinct.order(:name)
}
scope :with_transfers_from, ->(organization) {
joins(:transfers_from).where(organization_id: organization.id).distinct.order(:name)
}

# @param organization [Organization]
# @param inventory [View::Inventory]
Expand Down
8 changes: 0 additions & 8 deletions app/models/transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ class Transfer < ApplicationRecord
}
scope :during, ->(range) { where(created_at: range) }

def self.storage_locations_transferred_to_in(organization)
includes(:to).where(organization_id: organization.id).distinct(:to_id).collect(&:to).uniq.sort_by(&:name)
end

def self.storage_locations_transferred_from_in(organization)
includes(:from).where(organization_id: organization.id).distinct(:from_id).collect(&:from).uniq.sort_by(&:name)
end

validates :from, :to, :organization, presence: true
validate :storage_locations_belong_to_organization
validate :storage_locations_must_be_different
Expand Down
31 changes: 31 additions & 0 deletions app/views/partners/profiles/_actions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<%# locals: (partner:) %>

<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="card card-primary">
<div class="card-footer">
<% if partner.approved? %>
<%= link_to '', class: 'btn btn-info disabled' do %>
<i class="fa fa-check"></i> Approved
<% end %>
<% elsif partner.awaiting_review? %>
<%= link_to '', class: "btn btn-warning disabled" do %>
<i class="fa fa-clock"></i> Pending Approval
<% end %>
<% else %>
<%= link_to partners_approval_request_path, method: :post, class: "btn btn-success" do %>
<i class="fa fa-paper-plane"></i> Submit for Approval
<% end %>
<% end %>

<%= link_to edit_partners_profile_path, class: 'btn btn-primary' do %>
<i class="fa fa-pencil"></i> Update Information
<% end %>
</div>
</div>
</div>
</div>
</div>
</section>
37 changes: 3 additions & 34 deletions app/views/partners/profiles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
</div><!-- /.container-fluid -->
</section>

<%= render 'actions', partner: current_partner %>

<div class="row">
<div class="col-md-12">
<div class="container-fluid">
Expand All @@ -42,37 +44,4 @@
</div>
</div>

<section class="content">
<div class="container-fluid">
<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- jquery validation -->
<div class="card card-primary">
<div class="card-footer">
<% if current_partner.approved? %>
<%= link_to '', class: 'btn btn-info disabled' do %>
<i class="fa fa-check"></i> Approved
<% end %>
<% elsif current_partner.awaiting_review? %>
<%= link_to '', class: "btn btn-warning disabled" do %>
<i class="fa fa-clock"></i> Pending Approval
<% end %>
<% else %>
<%= link_to partners_approval_request_path, method: :post, class: "btn btn-success" do %>
<i class="fa fa-paper-plane"></i> Submit for Approval
<% end %>
<% end %>

<%= link_to edit_partners_profile_path, class: 'btn btn-primary' do %>
<i class="fa fa-pencil"></i> Update Information
<% end %>
</div>
</div>
<!-- /.card -->
</div>
<!--/.col (left) -->
</div>
<!-- /.row -->
</div><!-- /.container-fluid -->
</section>
<%= render 'actions', partner: current_partner %>
1 change: 0 additions & 1 deletion app/views/users/mailer/invitation_instructions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@
<% if @resource.invitation_due_at %>
<p><%= t("devise.mailer.invitation_instructions.accept_until", due_date: l(@resource.invitation_due_at, format: :'devise.mailer.invitation_instructions.accept_until_format')) %></p>
<% end %>
<p>For security reasons these invitations expire. This invitation will expire in 8 hours or if a new password reset is triggered.</p>
<p>If your invitation has an expired message, go <%= link_to "here", new_user_password_url %> and enter your email address to reset your password.</p>
<p>Feel free to ignore this email if you are not interested or if you feel it was sent by mistake.</p>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<p><%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p>

<p>For security reasons these invitations expire. This invitation will expire in 8 hours or if a new password reset is triggered.</p>
<p>For security reasons these invitations expire. This invitation will expire in 6 hours or if a new password reset is triggered.</p>
<p>If your invitation has an expired message, go <%= link_to "here", new_user_password_url %> and enter your email address to reset your password.</p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
2 changes: 1 addition & 1 deletion clock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ module Clockwork
end

every(1.day, "Send reminder emails", at: "12:00", if: lambda { |_| Rails.env.production? }) do
ReminderDeadlineJob.perform_now
ReminderDeadlineJob.perform_later
end
end
2 changes: 1 addition & 1 deletion config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
# The period the generated invitation token is valid, after
# this period, the invited resource won't be able to accept the invitation.
# When invite_for is 0 (the default), the invitation won't expire.
# config.invite_for = 2.weeks
config.invite_for = 2.weeks

# Number of invitations users can send.
# - If invitation_limit is nil, there is no limit for invitations, users can
Expand Down
2 changes: 1 addition & 1 deletion config/locales/devise_invitable.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ en:
hello: "Hello %{email}"
someone_invited_you: "Someone has invited you to %{url}, you can accept it through the link below."
accept: "Accept invitation"
accept_until: "This invitation will be due in %{due_date}."
accept_until: "This invitation will expire at %{due_date} GMT or if a new password reset is triggered."
ignore: "If you don't want to accept the invitation, please ignore this email.<br />\nYour account won't be created until you access the link above and set your password."
time:
formats:
Expand Down
12 changes: 9 additions & 3 deletions spec/mailers/custom_devise_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@
end

context "when user is invited" do
let(:user) { create(:user) }
let(:invitation_sent_at) { Time.zone.now }
let(:user) { create(:user, invitation_sent_at: invitation_sent_at) }

it "invites to user" do
expect(mail.subject).to eq("Your Human Essentials App Account Approval")
expect(mail.html_part.body).to include("Your request has been approved and you're invited to become an user of the Human Essentials inventory management system!")
end

it "has invite expiration message" do
expect(mail.html_part.body).to include("For security reasons these invitations expire. This invitation will expire in 8 hours or if a new password reset is triggered.")
it "has invite expiration message and reset instructions" do
expect(mail.html_part.body).to include("This invitation will expire at #{user.invitation_due_at.strftime("%B %d, %Y %I:%M %p")} GMT or if a new password reset is triggered.")
end

it "has reset instructions" do
expect(mail.html_part.body).to match(%r{<p>If your invitation has an expired message, go <a href="http://.+?/users/password/new">here</a> and enter your email address to reset your password.</p>})
expect(mail.html_part.body).to include("Feel free to ignore this email if you are not interested or if you feel it was sent by mistake.")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/mailers/user_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
let(:mail) { ActionMailer::Base.deliveries.last }

it "sends an email with instructions" do
expect(mail.body.encoded).to include("For security reasons these invitations expire. This invitation will expire in 8 hours or if a new password reset is triggered.")
expect(mail.body.encoded).to include("For security reasons these invitations expire. This invitation will expire in 6 hours or if a new password reset is triggered.")
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/models/storage_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@
expect(results.length).to eq(1)
expect(results.first.discarded_at).to be_nil
end

it "->with_transfers_to yields storage locations with transfers to an organization" do
storage_location1 = create(:storage_location, name: "loc1", organization: organization)
storage_location2 = create(:storage_location, name: "loc2", organization: organization)
storage_location3 = create(:storage_location, name: "loc3", organization: organization)
storage_location4 = create(:storage_location, name: "loc4", organization: create(:organization))
storage_location5 = create(:storage_location, name: "loc5", organization: storage_location4.organization)
create(:transfer, from: storage_location3, to: storage_location1, organization: organization)
create(:transfer, from: storage_location3, to: storage_location2, organization: organization)
create(:transfer, from: storage_location5, to: storage_location4, organization: storage_location4.organization)

expect(StorageLocation.with_transfers_to(organization).to_a).to match_array([storage_location1, storage_location2])
end

it "->with_transfers_from yields storage locations with transfers from an organization" do
storage_location1 = create(:storage_location, name: "loc1", organization: organization)
storage_location2 = create(:storage_location, name: "loc2", organization: organization)
storage_location3 = create(:storage_location, name: "loc3", organization: organization)
storage_location4 = create(:storage_location, name: "loc4", organization: create(:organization))
storage_location5 = create(:storage_location, name: "loc5", organization: storage_location4.organization)
create(:transfer, from: storage_location3, to: storage_location1, organization: organization)
create(:transfer, from: storage_location3, to: storage_location2, organization: organization)
create(:transfer, from: storage_location5, to: storage_location4, organization: storage_location4.organization)

expect(StorageLocation.with_transfers_from(organization).to_a).to match_array([storage_location3])
end
end

context "Methods >" do
Expand Down
15 changes: 0 additions & 15 deletions spec/models/transfer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,6 @@
end
end

context "Methods >" do
it "`self.storage_locations_transferred_to` and `..._from` constrains appropriately" do
storage_location1 = create(:storage_location, name: "loc1", organization: organization)
storage_location2 = create(:storage_location, name: "loc2", organization: organization)
storage_location3 = create(:storage_location, name: "loc3", organization: organization)
storage_location4 = create(:storage_location, name: "loc4", organization: create(:organization))
storage_location5 = create(:storage_location, name: "loc5", organization: storage_location4.organization)
create(:transfer, from: storage_location3, to: storage_location1, organization: organization)
create(:transfer, from: storage_location3, to: storage_location2, organization: organization)
create(:transfer, from: storage_location5, to: storage_location4, organization: storage_location4.organization)
expect(Transfer.storage_locations_transferred_to_in(organization).to_a).to match_array([storage_location1, storage_location2])
expect(Transfer.storage_locations_transferred_from_in(organization).to_a).to match_array([storage_location3])
end
end

describe "versioning" do
it { is_expected.to be_versioned }
end
Expand Down
58 changes: 52 additions & 6 deletions spec/system/distributions_by_county_system_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
RSpec.feature "Distributions by County", type: :system do
include_examples "distribution_by_county"

let(:year) { Time.current.year }
let(:issued_at_last_year) { Time.current.utc.change(year: year - 1).to_datetime }
let(:current_year) { Time.current.year }
let(:issued_at_last_year) { Time.current.utc.change(year: current_year - 1).to_datetime }

before do
sign_in(user)
Expand All @@ -18,8 +18,9 @@
partner_1.profile.served_areas.each do |served_area|
expect(page).to have_text(served_area.county.name)
end
expect(page).to have_text("50", count: 4)
expect(page).to have_text("$525.00", count: 4)

expect(page).to have_css("table tbody tr td", text: "50", exact_text: true, count: 4)
expect(page).to have_css("table tbody tr td", text: "$525.00", exact_text: true, count: 4)
end

it("works for this year") do
Expand All @@ -31,8 +32,53 @@
partner_1.profile.served_areas.each do |served_area|
expect(page).to have_text(served_area.county.name)
end
expect(page).to have_text("25", count: 4)
expect(page).to have_text("$262.50", count: 4)

expect(page).to have_css("table tbody tr td", text: "25", exact_text: true, count: 4)
expect(page).to have_css("table tbody tr td", text: "$262.50", exact_text: true, count: 4)
end

it("works for prior year") do
# Should NOT return distribution issued before previous calendar year
last_day_of_two_years_ago = Time.current.utc.change(year: current_year - 2, month: 12, day: 31).to_datetime
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: last_day_of_two_years_ago)

# Should return distribution issued during previous calendar year
one_year_ago = issued_at_last_year
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: one_year_ago)

# Should NOT return distribution issued after previous calendar year
first_day_of_current_year = Time.current.utc.change(year: current_year, month: 1, day: 1).to_datetime
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: first_day_of_current_year)

visit_distribution_by_county_with_specified_date_range("Prior Year")

partner_1.profile.served_areas.each do |served_area|
expect(page).to have_text(served_area.county.name)
end
expect(page).to have_css("table tbody tr td", text: "25", exact_text: true, count: 4)
expect(page).to have_css("table tbody tr td", text: "$262.50", exact_text: true, count: 4)
end

it("works for last 12 months") do
# Should NOT return disitribution issued before 12 months ago
one_year_and_one_day_ago = 1.year.ago.prev_day.to_datetime
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: one_year_and_one_day_ago)

# Should return distribution issued during previous 12 months
today = issued_at_present
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: today)

# Should NOT return distribution issued in the future
tomorrow = 1.day.from_now.to_datetime
create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: tomorrow)

visit_distribution_by_county_with_specified_date_range("Last 12 Months")

partner_1.profile.served_areas.each do |served_area|
expect(page).to have_text(served_area.county.name)
end
expect(page).to have_css("table tbody tr td", text: "25", exact_text: true, count: 4)
expect(page).to have_css("table tbody tr td", text: "$262.50", exact_text: true, count: 4)
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/system/partners/approval_process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
before do
click_on 'My Profile'
assert page.has_content? 'Uninvited'
click_on 'Update Information'
all('a', text: 'Update Information').last.click

fill_in 'Other Agency Type', with: 'Lorem'

Expand All @@ -41,7 +41,7 @@
click_on 'Update Information'
assert page.has_content? 'Details were successfully updated.'

find_link(text: 'Submit for Approval').click
all('a', text: 'Submit for Approval').last.click
assert page.has_content? 'You have submitted your details for approval.'
assert page.has_content? 'Awaiting Review'
end
Expand Down Expand Up @@ -77,7 +77,7 @@
login_as(partner_user)
visit partner_user_root_path
click_on 'My Profile'
click_on 'Submit for Approval'
all('a', text: 'Submit for Approval').last.click
end

it "should render an error message", :aggregate_failures do
Expand Down

0 comments on commit ecfa898

Please sign in to comment.