Skip to content

Commit

Permalink
Merge branch 'main' into 4406-add-print-picklists
Browse files Browse the repository at this point in the history
  • Loading branch information
norrismei authored Sep 24, 2024
2 parents a537810 + 5fbeea1 commit 8243bad
Show file tree
Hide file tree
Showing 104 changed files with 1,317 additions and 127 deletions.
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- I have added tests that prove my fix is effective or that my feature works,
- New and existing unit tests pass locally with my changes ("bundle exec rake"),
- Title include "WIP" if work is in progress.
- I acknowledge that I will *not* force push my branch once reviews have started.
-->

Expand Down
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ Make sure to install **Ubuntu** as your Linux distribution. (This should be defa
Recertification Required Partner
Email: recertification_required@example.com
Password: password!
Waiting Approval Partner
Email: waiting@example.com
Password: password!
```
</details>

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -801,4 +801,4 @@ DEPENDENCIES
webmock (~> 3.23)

BUNDLED WITH
2.5.16
2.5.19
18 changes: 16 additions & 2 deletions app/controllers/admin/account_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Admin::AccountRequestsController < AdminController
before_action :set_account_request, only: [:reject, :close]

def index
@open_account_requests = AccountRequest.requested.order('created_at DESC')
.page(params[:open_page]).per(15)
Expand All @@ -11,12 +13,24 @@ def for_rejection
end

def reject
account_request = AccountRequest.find(account_request_params[:id])
account_request.reject!(account_request_params[:rejection_reason])
@account_request.reject!(account_request_params[:rejection_reason])
redirect_to admin_account_requests_path, notice: "Account request rejected!"
end

def close
@account_request.close!(account_request_params[:rejection_reason])
redirect_to admin_account_requests_path, notice: "Account request closed!"
rescue => e
redirect_to admin_account_requests_path, alert: e.message
end

def account_request_params
params.require(:account_request).permit(:id, :rejection_reason)
end

private

def set_account_request
@account_request = AccountRequest.find(account_request_params[:id])
end
end
5 changes: 5 additions & 0 deletions app/controllers/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def create
@distribution.initialize_request_items
end
@items = current_organization.items.alphabetized
@partner_list = current_organization.partners.where.not(status: 'deactivated').alphabetized

if Event.read_events?(current_organization)
inventory = View::Inventory.new(@distribution.organization_id)
@storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc|
Expand Down Expand Up @@ -147,6 +149,8 @@ def new
@distribution.copy_from_donation(params[:donation_id], params[:storage_location_id])
end
@items = current_organization.items.alphabetized
@partner_list = current_organization.partners.where.not(status: 'deactivated').alphabetized

if Event.read_events?(current_organization)
inventory = View::Inventory.new(current_organization.id)
@storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc|
Expand Down Expand Up @@ -175,6 +179,7 @@ def edit
current_user.has_role?(Role::ORG_ADMIN, current_organization)
@distribution.line_items.build if @distribution.line_items.size.zero?
@items = current_organization.items.alphabetized
@partner_list = current_organization.partners.alphabetized
@audit_warning = current_organization.audits
.where(storage_location_id: @distribution.storage_location_id)
.where("updated_at > ?", @distribution.created_at).any?
Expand Down
18 changes: 17 additions & 1 deletion app/controllers/partner_groups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class PartnerGroupsController < ApplicationController
before_action :set_partner_group, only: %i[edit destroy]

def new
@partner_group = current_organization.partner_groups.new
@item_categories = current_organization.item_categories
Expand All @@ -16,7 +18,6 @@ def create
end

def edit
@partner_group = current_organization.partner_groups.find(params[:id])
@item_categories = current_organization.item_categories
end

Expand All @@ -30,8 +31,23 @@ def update
end
end

def destroy
if @partner_group.partners.any?
redirect_to partners_path + "#nav-partner-groups", alert: "Partner Group cannot be deleted."
else
@partner_group.destroy
respond_to do |format|
format.html { redirect_to partners_path + "#nav-partner-groups", notice: "Partner Group was successfully deleted." }
end
end
end

private

def set_partner_group
@partner_group = current_organization.partner_groups.find(params[:id])
end

def partner_group_params
params.require(:partner_group).permit(:name, :send_reminders, :deadline_day, :reminder_day, item_category_ids: [])
end
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ def donations_summary

def manufacturer_donations_summary
@recent_donations_from_manufacturers = current_organization.donations.during(helpers.selected_range).by_source(:manufacturer)
@top_manufacturers = current_organization.manufacturers.by_donation_count
@donations = current_organization.donations.during(helpers.selected_range)
@recent_donations = @donations.recent
@top_manufacturers = current_organization.manufacturers.by_donation_count(10, helpers.selected_range)
end

def purchases_summary
Expand Down
6 changes: 5 additions & 1 deletion app/mailers/distribution_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ def partner_mailer(current_organization, distribution, subject, distribution_cha
pdf = DistributionPdf.new(current_organization, @distribution).compute_and_render
attachments[format("%s %s.pdf", @partner.name, @distribution.created_at.strftime("%Y-%m-%d"))] = pdf
cc = [@partner.email]
cc.push(@partner.profile&.pick_up_email) if distribution.pick_up?
if distribution.pick_up? && @partner.profile&.pick_up_email
pick_up_emails = @partner.profile.split_pick_up_emails
cc.push(pick_up_emails)
end
cc.flatten!
cc.compact!
cc.uniq!

Expand Down
15 changes: 13 additions & 2 deletions app/models/account_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class AccountRequest < ApplicationRecord

has_one :organization, dependent: :nullify

enum status: %w[started user_confirmed admin_approved rejected].map { |v| [v, v] }.to_h
enum status: %w[started user_confirmed admin_approved rejected admin_closed].map { |v| [v, v] }.to_h

scope :requested, -> { where(status: %w[started user_confirmed]) }
scope :closed, -> { where(status: %w[admin_approved rejected]) }
scope :closed, -> { where(status: %w[admin_approved rejected admin_closed]) }

def self.get_by_identity_token(identity_token)
decrypted_token = JWT.decode(identity_token, Rails.application.secret_key_base, true, { algorithm: 'HS256' })
Expand Down Expand Up @@ -62,6 +62,11 @@ def processed?
organization.present?
end

# @return [Boolean]
def can_be_closed?
started? || user_confirmed?
end

def confirm!
update!(confirmed_at: Time.current, status: 'user_confirmed')
AccountRequestMailer.approval_request(account_request_id: id).deliver_later
Expand All @@ -73,6 +78,12 @@ def reject!(reason)
AccountRequestMailer.rejection(account_request_id: id).deliver_later
end

# @param reason [String]
def close!(reason)
raise 'Cannot be closed from this state' unless can_be_closed?
update!(status: 'admin_closed', rejection_reason: reason)
end

private

def email_not_already_used_by_organization
Expand Down
4 changes: 4 additions & 0 deletions app/models/donation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def storage_view
storage_location.nil? ? "N/A" : storage_location.name
end

def in_kind_value_money
Money.new(value_per_itemizable)
end

private

def combine_duplicates
Expand Down
11 changes: 8 additions & 3 deletions app/models/manufacturer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ def volume
donations.joins(:line_items).sum(:quantity)
end

def self.by_donation_count(count = 10)
# selects manufacturers that have donation qty > 0
def self.by_donation_count(count = 10, date_range = nil)
# selects manufacturers that have donation qty > 0 in the provided date range
# and sorts them by highest volume of donation
select { |m| m.volume.positive? }.sort.reverse.first(count)
joins(donations: :line_items).where(donations: { issued_at: date_range })
.select('manufacturers.*, sum(line_items.quantity) as donation_count')
.group('manufacturers.id')
.having('sum(line_items.quantity) > 0')
.order('donation_count DESC')
.limit(count)
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/partner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Partner < ApplicationRecord
validates :email, presence: true, uniqueness: { case_sensitive: false },
format: { with: URI::MailTo::EMAIL_REGEXP, on: :create }

validates :quota, numericality: true, allow_blank: true
validates :quota, numericality: { greater_than_or_equal_to: 0 }, allow_blank: true

validate :correct_document_mime_type

Expand Down
24 changes: 24 additions & 0 deletions app/models/partners/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Profile < Base

validate :client_share_is_0_or_100
validate :has_at_least_one_request_setting
validate :pick_up_email_addresses

self.ignored_columns = %w[
evidence_based_description
Expand All @@ -118,6 +119,12 @@ def client_share_total
served_areas.map(&:client_share).compact.sum
end

def split_pick_up_emails
return nil if pick_up_email.nil?

pick_up_email.split(/,|\s+/).compact_blank
end

private

def check_social_media
Expand All @@ -144,5 +151,22 @@ def has_at_least_one_request_setting
errors.add(:base, "At least one request type must be set")
end
end

def pick_up_email_addresses
# pick_up_email is a string of comma-separated emails, check specs for details
return if pick_up_email.nil?

emails = split_pick_up_emails
if emails.size > 3
errors.add(:pick_up_email, "There can't be more than three pick up email addresses.")
nil
end
if emails.uniq.size != emails.size
errors.add(:pick_up_email, "There should not be repeated email addresses.")
end
emails.each do |e|
errors.add(:pick_up_email, "Invalid email address for '#{e}'.") unless e.match? URI::MailTo::EMAIL_REGEXP
end
end
end
end
3 changes: 3 additions & 0 deletions app/services/exports/export_donations_csv_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ def base_table
"Variety of Items" => ->(donation) {
donation.line_items.map(&:name).uniq.size
},
"In-Kind Value" => ->(donation) {
donation.in_kind_value_money
},
"Comments" => ->(donation) {
donation.comment
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@
<td><%= js_button(text: 'Reject',
icon: 'ban',
class: 'reject-button',
data: { request_id: open_account_request.id }) %></td>
data: { request_id: open_account_request.id, modal: 'reject' }) %></td>
<td><%= js_button(text: 'Close (Admin)',
icon: 'times',
class: 'reject-button',
data: { request_id: open_account_request.id, modal: 'close' }) %></td>

</tr>
25 changes: 17 additions & 8 deletions app/views/admin/account_requests/_rejection_modal.html.erb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<div class="modal" id="rejection-modal">
<div class="modal" id="reject-modal">
<div class="modal-dialog">

<!-- Modal content-->
<!-- Dynamic modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">
Reject Account Request
<h4 class="modal-title" style="text-transform:capitalize">
</h4>
<button type="button" class="close btn-close" data-bs-dismiss="modal">&times;</button>
</div>
<div class="modal-body">
<%= simple_form_for AccountRequest.new, url: reject_admin_account_requests_path, method: :post do |f| %>
<%= simple_form_for AccountRequest.new, url: '', method: :post do |f| %>
<%= f.hidden_field :id, id: :reject_account_request_id %>
<div class="form-inputs">
<%= f.input :rejection_reason, required: true, autofocus: true, wrapper: :input_group %>
<a class="text-danger" style="display:none">Reason must be provided</a>
</div>
<%= submit_button %>
<% end %>
Expand All @@ -23,14 +22,24 @@
</div>

</div>

<!-- set url and title dynamically -->
<script type="module">
$(() => {
$('.reject-button').click((event) => {
event.preventDefault();
let url = `/admin/account_requests/${$(event.target).data('modal')}`

$('#account_request_rejection_reason').val('');
$('#reject_account_request_id').val($(event.target).data('requestId'));
$('#rejection-modal').modal('show');
$('#new_account_request').attr('action', url)
$('.modal-title').text(`${$(event.target).data('modal')} Account Request`)
$('#reject-modal').modal('show');
})
$('button[type="submit"]').click((event) => {
if($('#reject-modal #account_request_rejection_reason').val().trim() === ''){
event.preventDefault();
$('.text-danger').show()
}
})
})
</script>
11 changes: 0 additions & 11 deletions app/views/admin/account_requests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,3 @@
</div>
</div>
</section>

<script type="module">
$(() => {
$('.reject-button').click((event) => {
event.preventDefault();
$('#account_request_rejection_reason').val('');
$('#reject_account_request_id').val($(event.target).data('requestId'));
$('#rejection-modal').modal('show');
})
})
</script>
3 changes: 2 additions & 1 deletion app/views/distributions/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<% end %>

<%= f.association :partner,
collection: current_organization.partners.alphabetized,
collection: @partner_list,
label: "Partner",
error: "Which partner is this distribution going to?" %>

Expand All @@ -36,6 +36,7 @@
<div class="distribution-title">
<legend class="<%= 'with-request' if distribution.request %>">Items in this distribution</legend>
<% if distribution.request %>
<div class="distribution-request-unit">Quantity - Total Units</div>
<div class="distribution-request-unit">Requested</div>
<% end %>
</div>
Expand Down
Loading

0 comments on commit 8243bad

Please sign in to comment.