From d799030370c88e758889b1f9d09e4deca17b350f Mon Sep 17 00:00:00 2001 From: Daniel Orner Date: Wed, 14 Dec 2022 07:11:43 -0500 Subject: [PATCH] 3197: Merge partner records (#3214) * 3197: Merge partner records (WIP) * 3197: Request merging * Lint fixes * Spec and lint fixes * More spec fixes * More spec and lint fixes * last fix??? * Fix migrations to handle bad data * Fix merge error * Fix DB migrations * Fix but that prevented partners from updating their profile info * QA fixes * Spec fixes * Last fixes (finger cross) Co-authored-by: Edwin --- app/assets/stylesheets/application.scss | 1 + .../partners/approval_requests_controller.rb | 2 +- app/controllers/partners/base_controller.rb | 2 +- .../partners/children_controller.rb | 4 +- .../partners/dashboards_controller.rb | 16 ++- .../partners/distributions_controller.rb | 5 +- .../individuals_requests_controller.rb | 4 +- .../partners/profiles_controller.rb | 12 +- .../partners/requests_controller.rb | 12 +- app/controllers/partners_controller.rb | 4 +- app/controllers/profiles_controller.rb | 18 +-- app/helpers/partners_helper.rb | 10 +- app/models/partner.rb | 112 ++++++++++++++---- app/models/partners/family.rb | 3 +- app/models/partners/item_request.rb | 19 +-- app/models/partners/partner_form.rb | 16 --- .../partners/{partner.rb => profile.rb} | 105 +--------------- app/models/partners/request.rb | 30 ----- app/models/request.rb | 4 + app/models/role.rb | 13 +- app/models/user.rb | 7 +- app/services/organization_update_service.rb | 9 -- app/services/partner_approval_service.rb | 5 - app/services/partner_create_service.rb | 3 +- app/services/partner_deactivate_service.rb | 3 - app/services/partner_invite_service.rb | 2 +- app/services/partner_reactivate_service.rb | 3 - ...partner_request_recertification_service.rb | 1 - .../partners/family_request_create_service.rb | 3 +- .../partners/request_approval_service.rb | 3 +- .../partners/request_create_service.rb | 35 ++---- .../reports/partner_info_report_service.rb | 2 +- .../partners/navigation/_navbar.html.erb | 2 +- .../partners/navigation/_sidebar.html.erb | 2 +- app/views/partners/dashboards/show.html.erb | 4 +- .../partners/distributions/index.html.erb | 4 +- app/views/partners/profiles/edit.html.erb | 30 ++--- .../edit/_agency_information.html.erb | 24 ++-- .../profiles/edit/_agency_stability.html.erb | 4 +- .../edit/_attached_documents.html.erb | 4 +- app/views/partners/profiles/show.html.erb | 12 +- .../_agency_distribution_information.html.erb | 6 +- .../show/_agency_information.html.erb | 26 ++-- .../profiles/show/_agency_stability.html.erb | 34 +++--- .../show/_attached_documents.html.erb | 4 +- .../show/_diaper_pick_up_person.html.erb | 6 +- .../show/_executive_director.html.erb | 14 +-- .../profiles/show/_media_information.html.erb | 10 +- .../show/_organizational_capacity.html.erb | 6 +- .../profiles/show/_partner_settings.html.erb | 12 +- .../profiles/show/_population_served.html.erb | 30 ++--- .../show/_sources_of_funding.html.erb | 8 +- .../requests/_request_options_card.html.erb | 6 +- app/views/partners/requests/index.html.erb | 2 +- app/views/partners/requests/new.html.erb | 3 +- app/views/profiles/edit.html.erb | 24 ++-- app/views/requests/show.html.erb | 3 +- db/migrate/20220819194804_migrate_roles.rb | 2 +- .../20221028192638_move_partner_for_roles.rb | 30 +++++ ...0221028194319_move_partner_for_families.rb | 60 ++++++++++ .../20221104191611_move_item_request_index.rb | 44 +++++++ db/schema.rb | 10 +- db/seeds.rb | 67 ++--------- spec/factories/partner_user.rb | 2 +- spec/factories/partners.rb | 6 +- spec/factories/partners/family.rb | 2 +- .../partners/{partner.rb => profile.rb} | 3 +- spec/factories/partners/user.rb | 2 +- spec/factories/roles.rb | 13 +- spec/mailers/custom_devise_mailer_spec.rb | 8 +- spec/models/partner_spec.rb | 61 +++++++--- spec/models/partners/family_spec.rb | 1 + spec/models/partners/item_request_spec.rb | 19 +-- spec/models/partners/partner_form_spec.rb | 25 ---- .../{partner_spec.rb => profile_spec.rb} | 102 +--------------- spec/models/partners/request_spec.rb | 31 ----- spec/models/role_spec.rb | 13 +- spec/requests/dashboard_requests_spec.rb | 2 +- .../partners/children_requests_spec.rb | 5 +- .../partners/dashboard_requests_spec.rb | 2 +- spec/requests/partners/distributions_spec.rb | 2 +- .../family_requests_controller_spec.rb | 15 ++- .../requests/partners/family_requests_spec.rb | 11 +- .../partners/profiles_requests_spec.rb | 37 ++++++ spec/requests/partners/requests_spec.rb | 16 +-- spec/requests/partners/user_requests_spec.rb | 2 +- spec/requests/partners_requests_spec.rb | 20 ++-- spec/requests/profiles_requests_spec.rb | 14 +-- spec/requests/users_requests_spec.rb | 2 +- .../organization_update_service_spec.rb | 48 -------- .../services/partner_approval_service_spec.rb | 8 -- spec/services/partner_create_service_spec.rb | 6 +- spec/services/partner_invite_service_spec.rb | 8 +- ...er_request_recertification_service_spec.rb | 8 -- .../family_request_create_service_spec.rb | 23 +--- .../partners/request_approval_service_spec.rb | 8 +- .../partners/request_create_service_spec.rb | 48 +------- .../partner_info_report_service_spec.rb | 6 +- spec/system/partner_system_spec.rb | 8 +- spec/system/partners/approval_process_spec.rb | 8 +- .../coworker_invitations_system_spec.rb | 2 +- .../partners/family_requests_system_spec.rb | 9 +- spec/system/partners/helps_system_spec.rb | 4 +- .../partners/managing_requests_system_spec.rb | 24 ++-- .../partner_distribution_system_spec.rb | 2 +- spec/system/sign_in_system_spec.rb | 2 +- 106 files changed, 697 insertions(+), 902 deletions(-) delete mode 100644 app/models/partners/partner_form.rb rename app/models/partners/{partner.rb => profile.rb} (59%) delete mode 100644 app/models/partners/request.rb create mode 100644 db/migrate/20221028192638_move_partner_for_roles.rb create mode 100644 db/migrate/20221028194319_move_partner_for_families.rb create mode 100644 db/migrate/20221104191611_move_item_request_index.rb rename spec/factories/partners/{partner.rb => profile.rb} (51%) delete mode 100644 spec/models/partners/partner_form_spec.rb rename spec/models/partners/{partner_spec.rb => profile_spec.rb} (58%) delete mode 100644 spec/models/partners/request_spec.rb create mode 100644 spec/requests/partners/profiles_requests_spec.rb diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index c9e13b77c7..534002610c 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -159,3 +159,4 @@ body a { body:not(.sidebar-mini-md) .content-wrapper, body:not(.sidebar-mini-md) .main-footer, body:not(.sidebar-mini-md) .main-header { margin-left: 280px; } + diff --git a/app/controllers/partners/approval_requests_controller.rb b/app/controllers/partners/approval_requests_controller.rb index 31b5e08bf3..e4bc36b15e 100644 --- a/app/controllers/partners/approval_requests_controller.rb +++ b/app/controllers/partners/approval_requests_controller.rb @@ -1,7 +1,7 @@ module Partners class ApprovalRequestsController < BaseController def create - svc = Partners::RequestApprovalService.new(partner: current_partner.partner) + svc = Partners::RequestApprovalService.new(partner: current_partner) svc.call if svc.errors.none? diff --git a/app/controllers/partners/base_controller.rb b/app/controllers/partners/base_controller.rb index ce4084eec0..090256c1c0 100644 --- a/app/controllers/partners/base_controller.rb +++ b/app/controllers/partners/base_controller.rb @@ -23,7 +23,7 @@ def verify_partner_is_active end def authorize_verified_partners - return if current_partner.verified? + return if current_partner.approved? redirect_to partners_requests_path, notice: "Please review your application details and submit for approval in order to make a new request." end diff --git a/app/controllers/partners/children_controller.rb b/app/controllers/partners/children_controller.rb index 53a7ae1ecd..64b8033934 100644 --- a/app/controllers/partners/children_controller.rb +++ b/app/controllers/partners/children_controller.rb @@ -36,7 +36,7 @@ def new family = current_partner.families.find_by!(id: params[:family_id]) @child = family.children.new - requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.partner.id).call + requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call @formatted_requestable_items = requestable_items.map do |rt| [rt.name, rt.id] end @@ -50,7 +50,7 @@ def active def edit @child = current_partner.children.find_by(id: params[:id]) - requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.partner.id).call + requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call @formatted_requestable_items = requestable_items.map do |rt| [rt.name, rt.id] end diff --git a/app/controllers/partners/dashboards_controller.rb b/app/controllers/partners/dashboards_controller.rb index 70a12d709b..e5c5864da7 100644 --- a/app/controllers/partners/dashboards_controller.rb +++ b/app/controllers/partners/dashboards_controller.rb @@ -9,19 +9,17 @@ def index; end def show @partner = current_partner @partner_requests = @partner.requests.order(created_at: :desc).limit(10) - @upcoming_distributions = ::Partner.find(@partner.partner_id) - .distributions.order(issued_at: :desc) - .where('issued_at >= ?', Time.zone.today) - @distributions = ::Partner.find(@partner.partner_id) - .distributions.order(issued_at: :desc) - .where('issued_at < ?', Time.zone.today) - .limit(5) + @upcoming_distributions = @partner.distributions.order(issued_at: :desc) + .where('issued_at >= ?', Time.zone.today) + @distributions = @partner.distributions.order(issued_at: :desc) + .where('issued_at < ?', Time.zone.today) + .limit(5) - @parent_org = Organization.find(@partner.essentials_bank_id) + @parent_org = Organization.find(@partner.organization_id) @requests_in_progress = @parent_org .ordered_requests - .where(partner: @partner.partner_id) + .where(partner: @partner.id) .where(status: 0) @families = @partner.families diff --git a/app/controllers/partners/distributions_controller.rb b/app/controllers/partners/distributions_controller.rb index 749545aa83..4c30987f77 100644 --- a/app/controllers/partners/distributions_controller.rb +++ b/app/controllers/partners/distributions_controller.rb @@ -7,10 +7,9 @@ class DistributionsController < BaseController def index @partner = current_partner - @distributions = ::Partner.find(@partner.partner_id) - .distributions.order(issued_at: :desc) + @distributions = @partner.distributions.order(issued_at: :desc) - @parent_org = Organization.find(@partner.essentials_bank_id) + @parent_org = Organization.find(@partner.organization_id) end def print diff --git a/app/controllers/partners/individuals_requests_controller.rb b/app/controllers/partners/individuals_requests_controller.rb index d9256a83a3..45c109ef82 100644 --- a/app/controllers/partners/individuals_requests_controller.rb +++ b/app/controllers/partners/individuals_requests_controller.rb @@ -3,7 +3,7 @@ class IndividualsRequestsController < BaseController def new @request = FamilyRequest.new({}, initial_items: 1) - requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.partner.id).call + requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call @formatted_requestable_items = requestable_items.map do |rt| [rt.name, rt.id] end.sort @@ -24,7 +24,7 @@ def create else @request = FamilyRequest.new({}, initial_items: 1) @errors = create_service.errors - @requestable_items = Organization.find(current_partner.essentials_bank_id).valid_items.map do |item| + @requestable_items = Organization.find(current_partner.organization_id).valid_items.map do |item| [item[:name], item[:id]] end.sort diff --git a/app/controllers/partners/profiles_controller.rb b/app/controllers/partners/profiles_controller.rb index 4be0cdffcd..c2ddfbda37 100644 --- a/app/controllers/partners/profiles_controller.rb +++ b/app/controllers/partners/profiles_controller.rb @@ -5,7 +5,7 @@ def show; end def edit; end def update - if current_partner.update(partner_params) + if current_partner.update(partner_params) && current_partner.profile.update(profile_params) flash[:success] = "Details were successfully updated." redirect_to partners_profile_path else @@ -16,11 +16,13 @@ def update private def partner_params - params.require(:partners_partner).permit( - :name, + params.require(:partner).permit(:name) + end + + def profile_params + params.require(:profile).permit( :agency_type, :other_agency_type, - :partner_status, :proof_of_partner_status, :agency_mission, :address1, @@ -88,7 +90,7 @@ def partner_params :enable_individual_requests, :enable_quantity_based_requests, documents: [] - ) + ).select { |_, v| v.present? } end end end diff --git a/app/controllers/partners/requests_controller.rb b/app/controllers/partners/requests_controller.rb index 0c8d00a278..2e6964dc6f 100644 --- a/app/controllers/partners/requests_controller.rb +++ b/app/controllers/partners/requests_controller.rb @@ -8,10 +8,10 @@ def index end def new - @partner_request = Partners::Request.new + @partner_request = ::Request.new @partner_request.item_requests.build - requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.partner.id).call + requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call @formatted_requestable_items = requestable_items.map do |rt| [rt.name, rt.id] end.sort @@ -36,7 +36,7 @@ def create @partner_request = create_service.partner_request @errors = create_service.errors - @formatted_requestable_items = Organization.find(current_partner.essentials_bank_id).valid_items.map do |item| + @formatted_requestable_items = Organization.find(current_partner.organization_id).valid_items.map do |item| [item[:name], item[:id]] end.sort @@ -49,11 +49,11 @@ def create private def partner_request_params - params.require(:partners_request).permit(:comments, item_requests_attributes: [ - :item_id, + params.require(:request).permit(:comments, item_requests_attributes: [ + :item_id, :quantity, :_destroy - ]) + ]) end end end diff --git a/app/controllers/partners_controller.rb b/app/controllers/partners_controller.rb index e62180721c..ed3a5523f2 100644 --- a/app/controllers/partners_controller.rb +++ b/app/controllers/partners_controller.rb @@ -50,10 +50,10 @@ def approve_application def show @partner = current_organization.partners.find(params[:id]) - @impact_metrics = @partner.profile.impact_metrics unless @partner.uninvited? + @impact_metrics = @partner.impact_metrics unless @partner.uninvited? @partner_distributions = @partner.distributions.includes(:partner, :storage_location, line_items: [:item]).order("issued_at DESC") @partner_profile_fields = current_organization.partner_form_fields - @partner_users = @partner.profile.users.order(name: :asc) + @partner_users = @partner.users.order(name: :asc) respond_to do |format| format.html diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index f751b491dd..d146e5cc87 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -1,12 +1,12 @@ class ProfilesController < ApplicationController def edit - @partner = current_organization.partners.find(params[:id]).profile + @partner = current_organization.partners.find(params[:id]) end def update - @partner = current_organization.partners.find(params[:id]).profile - if @partner.update(edit_profile_params) - redirect_to partner_path(@partner.partner) + "#partner-information", notice: "#{@partner.name} updated!" + @partner = current_organization.partners.find(params[:id]) + if @partner.update(edit_partner_params) && @partner.profile.update(edit_profile_params) + redirect_to partner_path(@partner) + "#partner-information", notice: "#{@partner.name} updated!" else flash[:error] = "Something didn't work quite right -- try again?" render action: :edit @@ -15,12 +15,14 @@ def update private + def edit_partner_params + params.require(:partner).permit(:name) + end + def edit_profile_params - params.require(:partners_partner).permit( - :name, + params.require(:profile).permit( :agency_type, :other_agency_type, - :partner_status, :proof_of_partner_status, :agency_mission, :address1, @@ -99,6 +101,6 @@ def edit_profile_params :enable_individual_requests, :enable_quantity_based_requests, documents: [] - ) + ).select { |_, v| v.present? } end end diff --git a/app/helpers/partners_helper.rb b/app/helpers/partners_helper.rb index bec35c6770..8166cd4457 100644 --- a/app/helpers/partners_helper.rb +++ b/app/helpers/partners_helper.rb @@ -13,12 +13,12 @@ def humanize_boolean(boolean) end def partner_status_badge(partner) - if partner.partner_status == "verified" - tag.span partner.partner_status, class: %w(badge badge-pill badge-primary float-right) - elsif partner.partner_status == "recertification_required" - tag.span partner.partner_status, class: %w(badge badge-pill badge-danger float-right) + if partner.status == "approved" + tag.span partner.display_status, class: %w(badge badge-pill badge-primary float-right) + elsif partner.status == "recertification_required" + tag.span partner.display_status, class: %w(badge badge-pill badge-danger float-right) else - tag.span partner.partner_status, class: %w(badge badge-pill badge-info float-right) + tag.span partner.display_status, class: %w(badge badge-pill badge-info float-right) end end end diff --git a/app/models/partner.rb b/app/models/partner.rb index 15897ee10c..62c43154b2 100644 --- a/app/models/partner.rb +++ b/app/models/partner.rb @@ -17,6 +17,7 @@ # class Partner < ApplicationRecord + resourcify require "csv" ALLOWED_MIME_TYPES = [ @@ -31,9 +32,14 @@ class Partner < ApplicationRecord belongs_to :partner_group, optional: true has_many :item_categories, through: :partner_group has_many :requestable_items, through: :item_categories, source: :items + has_one :profile, class_name: 'Partners::Profile', dependent: :destroy has_many :distributions, dependent: :destroy - has_many :requests, dependent: :destroy + has_many :requests, dependent: :destroy, class_name: '::Request' + has_many :users, through: :roles, class_name: '::User', dependent: :destroy + has_many :families, dependent: :destroy, class_name: 'Partners::Family' + has_many :children, through: :families, class_name: 'Partners::Child' + has_many :authorized_family_members, through: :families, class_name: 'Partners::AuthorizedFamilyMember' has_many_attached :documents @@ -64,8 +70,60 @@ class Partner < ApplicationRecord where(status: status.to_sym) } - def deactivated? - status == 'deactivated' + AGENCY_TYPES = { + "CAREER" => "Career technical training", + "ABUSE" => "Child abuse resource center", + "CHURCH" => "Church outreach ministry", + "CDC" => "Community development corporation", + "HEALTH" => "Community health program", + "OUTREACH" => "Community outreach services", + "CRISIS" => "Crisis/Disaster services", + "DISAB" => "Developmental disabilities program", + "DOMV" => "Domestic violence shelter", + "CHILD" => "Early childhood services", + "EDU" => "Education program", + "FAMILY" => "Family resource center", + "FOOD" => "Food bank/pantry", + "GOVT" => "Government Agency/Affiliate", + "HEADSTART" => "Head Start/Early Head Start", + "HOMEVISIT" => "Home visits", + "HOMELESS" => "Homeless resource center", + "INFPAN" => "Infant/Child Pantry/Closet", + "PREG" => "Pregnancy resource center", + "REF" => "Refugee resource center", + "TREAT" => "Treatment clinic", + "WIC" => "Women, Infants and Children", + "OTHER" => "Other" + }.freeze + + ALL_PARTIALS = %w[ + media_information + agency_stability + organizational_capacity + sources_of_funding + population_served + executive_director + diaper_pick_up_person + agency_distribution_information + attached_documents + ].freeze + + # @return [String] + def display_status + case status + when :awaiting_review + 'Submitted' + when :uninvited + 'Pending' + when :approved + 'Verified' + else + status.titleize + end + end + + def primary_user + users.order('created_at ASC').first end # @return [Boolean] @@ -73,22 +131,7 @@ def deletable? uninvited? && distributions.none? && requests.none? && - (profile.nil? || profile&.users&.none?) - end - - # - # Returns the Partners::Partner record which is stored in - # the partnerbase DB and contains mostly profile data of - # the partner user. - def profile - @profile ||= ::Partners::Partner.find_by(partner_id: id) - end - - # - # Returns the primary User record which is the - # first & main user associated to a partner agency. - def primary_partner_user - profile&.primary_user + users&.none? end # better to extract this outside of the model @@ -154,6 +197,10 @@ def contact_person } end + def partials_to_show + organization.partner_form_fields.presence || ALL_PARTIALS + end + def quantity_year_to_date distributions .includes(:line_items) @@ -161,8 +208,33 @@ def quantity_year_to_date .references(:line_items).map(&:line_items).flatten.sum(&:quantity) end + def impact_metrics + { + families_served: families_served_count, + children_served: children_served_count, + family_zipcodes: family_zipcodes_count, + family_zipcodes_list: family_zipcodes_list + } + end + private + def families_served_count + families.count + end + + def children_served_count + children.count + end + + def family_zipcodes_count + families.pluck(:guardian_zip_code).uniq.count + end + + def family_zipcodes_list + families.pluck(:guardian_zip_code).uniq + end + def correct_document_mime_type if documents.attached? && documents.any? { |doc| !doc.content_type.in?(ALLOWED_MIME_TYPES) } errors.add(:documents, "Must be a PDF or DOC file") @@ -170,7 +242,7 @@ def correct_document_mime_type end def invite_new_partner - UserInviteService.invite(email: email, roles: [Role::PARTNER], resource: profile) + UserInviteService.invite(email: email, roles: [Role::PARTNER], resource: self) end def should_invite_because_email_changed? diff --git a/app/models/partners/family.rb b/app/models/partners/family.rb index 498e24f617..c7ad7dae93 100644 --- a/app/models/partners/family.rb +++ b/app/models/partners/family.rb @@ -21,11 +21,12 @@ # sources_of_income :jsonb # created_at :datetime not null # updated_at :datetime not null +# old_partner_id :bigint # partner_id :bigint # module Partners class Family < Base - belongs_to :partner, class_name: "Partners::Partner" + belongs_to :partner, class_name: '::Partner' has_many :children, dependent: :destroy has_many :authorized_family_members, dependent: :destroy serialize :sources_of_income, Array diff --git a/app/models/partners/item_request.rb b/app/models/partners/item_request.rb index bf74e3b788..c996bc8936 100644 --- a/app/models/partners/item_request.rb +++ b/app/models/partners/item_request.rb @@ -2,18 +2,19 @@ # # Table name: item_requests # -# id :bigint not null, primary key -# name :string -# partner_key :string -# quantity :string -# created_at :datetime not null -# updated_at :datetime not null -# item_id :integer -# partner_request_id :bigint +# id :bigint not null, primary key +# name :string +# partner_key :string +# quantity :string +# created_at :datetime not null +# updated_at :datetime not null +# item_id :integer +# old_partner_request_id :integer +# partner_request_id :bigint # module Partners class ItemRequest < Base - belongs_to :request, class_name: 'Partners::Request', foreign_key: :partner_request_id, inverse_of: :item_requests + belongs_to :request, class_name: '::Request', foreign_key: :partner_request_id, inverse_of: :item_requests has_many :child_item_requests, dependent: :destroy has_many :children, through: :child_item_requests diff --git a/app/models/partners/partner_form.rb b/app/models/partners/partner_form.rb deleted file mode 100644 index b1c9597e60..0000000000 --- a/app/models/partners/partner_form.rb +++ /dev/null @@ -1,16 +0,0 @@ -# == Schema Information -# -# Table name: partner_forms -# -# id :bigint not null, primary key -# sections :text default([]), is an Array -# created_at :datetime not null -# updated_at :datetime not null -# essentials_bank_id :integer -# -module Partners - class PartnerForm < Base - has_one :partner, primary_key: :essentials_bank_id, foreign_key: :essentials_bank_id, dependent: :destroy, inverse_of: :partner_form - validates :essentials_bank_id, presence: true, uniqueness: true - end -end diff --git a/app/models/partners/partner.rb b/app/models/partners/profile.rb similarity index 59% rename from app/models/partners/partner.rb rename to app/models/partners/profile.rb index c91040c68c..33a8851abd 100644 --- a/app/models/partners/partner.rb +++ b/app/models/partners/profile.rb @@ -80,16 +80,10 @@ # partner_id :integer # module Partners - class Partner < Base + class Profile < Base self.table_name = "partner_profiles" - resourcify - - has_many :users, through: :roles, class_name: '::User', dependent: :destroy - has_many :requests, dependent: :destroy - has_many :families, dependent: :destroy - has_many :children, through: :families - has_many :authorized_family_members, through: :families - has_one :partner_form, primary_key: :essentials_bank_id, foreign_key: :essentials_bank_id, dependent: :destroy, inverse_of: :partner + belongs_to :partner + has_one :organization, through: :partner, class_name: "::Organization" has_one_attached :proof_of_partner_status has_one_attached :proof_of_form_990 @@ -111,101 +105,8 @@ class Partner < Base ages_served ] - VERIFIED_STATUS = 'verified'.freeze - RECERTIFICATION_REQUESTED_STATUS = 'recertification_required'.freeze - DEACTIVATED_STATUS = "deactivated".freeze - - AGENCY_TYPES = { - "CAREER" => "Career technical training", - "ABUSE" => "Child abuse resource center", - "CHURCH" => "Church outreach ministry", - "CDC" => "Community development corporation", - "HEALTH" => "Community health program", - "OUTREACH" => "Community outreach services", - "CRISIS" => "Crisis/Disaster services", - "DISAB" => "Developmental disabilities program", - "DOMV" => "Domestic violence shelter", - "CHILD" => "Early childhood services", - "EDU" => "Education program", - "FAMILY" => "Family resource center", - "FOOD" => "Food bank/pantry", - "GOVT" => "Government Agency/Affiliate", - "HEADSTART" => "Head Start/Early Head Start", - "HOMEVISIT" => "Home visits", - "HOMELESS" => "Homeless resource center", - "INFPAN" => "Infant/Child Pantry/Closet", - "PREG" => "Pregnancy resource center", - "REF" => "Refugee resource center", - "TREAT" => "Treatment clinic", - "WIC" => "Women, Infants and Children", - "OTHER" => "Other" - }.freeze - - ALL_PARTIALS = %w[ - media_information - agency_stability - organizational_capacity - sources_of_funding - population_served - executive_director - diaper_pick_up_person - agency_distribution_information - attached_documents - ].freeze - - def primary_user - users.order('created_at ASC').first - end - - def verified? - partner_status == VERIFIED_STATUS - end - - def deactivated? - status_in_diaper_base == DEACTIVATED_STATUS - end - - def organization - Organization.find_by!(id: essentials_bank_id) - end - - def partner - ::Partner.find_by!(id: partner_id) - end - - def partials_to_show - partner_form&.sections || ALL_PARTIALS - end - - def impact_metrics - { - families_served: families_served_count, - children_served: children_served_count, - family_zipcodes: family_zipcodes_count, - family_zipcodes_list: family_zipcodes_list - } - end - - private - def has_no_social_media? website.blank? && twitter.blank? && facebook.blank? && instagram.blank? end - - def families_served_count - families.count - end - - def children_served_count - children.count - end - - def family_zipcodes_count - families.pluck(:guardian_zip_code).uniq.count - end - - def family_zipcodes_list - families.pluck(:guardian_zip_code).uniq - end end end diff --git a/app/models/partners/request.rb b/app/models/partners/request.rb deleted file mode 100644 index d27bd6db0f..0000000000 --- a/app/models/partners/request.rb +++ /dev/null @@ -1,30 +0,0 @@ -# == Schema Information -# -# Table name: partner_requests -# -# id :bigint not null, primary key -# comments :text -# for_families :boolean -# sent :boolean default(FALSE), not null -# created_at :datetime not null -# updated_at :datetime not null -# organization_id :bigint -# partner_id :bigint -# partner_user_id :integer -# -module Partners - class Request < Base - self.table_name = "partner_requests" - - belongs_to :partner, class_name: "Partners::Partner", dependent: :destroy - belongs_to :partner_user, class_name: "::User", optional: true - - has_many :item_requests, class_name: 'Partners::ItemRequest', foreign_key: :partner_request_id, dependent: :destroy, inverse_of: :request - accepts_nested_attributes_for :item_requests, allow_destroy: true, reject_if: proc { |attributes| attributes["quantity"].blank? } - has_many :child_item_requests, through: :item_requests - - validates :partner, presence: true - validates :partner_user, presence: true, on: :create - validates_associated :item_requests - end -end diff --git a/app/models/request.rb b/app/models/request.rb index 5fc89e60a6..7af47c1fc1 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -25,6 +25,10 @@ class Request < ApplicationRecord belongs_to :organization belongs_to :distribution, optional: true + has_many :item_requests, class_name: "Partners::ItemRequest", foreign_key: :partner_request_id, dependent: :destroy, inverse_of: :request + accepts_nested_attributes_for :item_requests, allow_destroy: true, reject_if: proc { |attributes| attributes["quantity"].blank? } + has_many :child_item_requests, through: :item_requests + enum status: { pending: 0, started: 1, fulfilled: 2, discarded: 3 }, _prefix: true validates :distribution_id, uniqueness: true, allow_nil: true diff --git a/app/models/role.rb b/app/models/role.rb index ae2a7e4980..95faf041ed 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -2,12 +2,13 @@ # # Table name: roles # -# id :bigint not null, primary key -# name :string -# resource_type :string -# created_at :datetime not null -# updated_at :datetime not null -# resource_id :bigint +# id :bigint not null, primary key +# name :string +# resource_type :string +# created_at :datetime not null +# updated_at :datetime not null +# old_resource_id :bigint +# resource_id :bigint # class Role < ApplicationRecord has_and_belongs_to_many :users, join_table: :users_roles diff --git a/app/models/user.rb b/app/models/user.rb index 1ffbb0256a..91af7c0168 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -45,8 +45,8 @@ class User < ApplicationRecord has_one :partner_role_join, class_name: "UsersRole", dependent: :destroy has_one :partner_role, through: :partner_role_join, class_name: "Role", source: :role - has_one :partner, through: :partner_role, source: :resource, source_type: "Partners::Partner" - has_many :partners, through: :roles, source: :resource, source_type: "Partners::Partner" + has_one :partner, through: :partner_role, source: :resource, source_type: "Partner" + has_many :partners, through: :roles, source: :resource, source_type: "Partner" attr_accessor :organization_admin # for creation / update time @@ -65,8 +65,7 @@ class User < ApplicationRecord scope :partner_users, -> { with_role(Role::PARTNER, :any) } scope :org_users, -> { with_role(Role::ORG_USER, :any) } - has_many :requests, class_name: "Partners::Request", foreign_key: :partner_id, dependent: :destroy, inverse_of: :partner_user - has_many :submitted_partner_requests, class_name: "Partners::Request", foreign_key: :partner_user_id, dependent: :destroy, inverse_of: :partner_user + has_many :requests, class_name: "::Request", foreign_key: :partner_id, dependent: :destroy, inverse_of: :partner_user has_many :submitted_requests, class_name: "Request", foreign_key: :partner_user_id, dependent: :destroy, inverse_of: :partner_user def password_complexity diff --git a/app/services/organization_update_service.rb b/app/services/organization_update_service.rb index ce9b144910..3ad4902f30 100644 --- a/app/services/organization_update_service.rb +++ b/app/services/organization_update_service.rb @@ -6,19 +6,10 @@ def self.update(organization, params) result = organization.update(params) return false unless result - sync_visible_partner_form_sections(organization) update_partner_flags(organization) true end - # @param organization [Organization] - def self.sync_visible_partner_form_sections(organization) - return unless organization.saved_change_to_attribute?(:partner_form_fields) - - partner_form = Partners::PartnerForm.where(essentials_bank_id: organization.id).first_or_create - partner_form.update!(sections: organization.partner_form_fields) - end - # @param organization [Organization] def self.update_partner_flags(organization) fields = %i[ diff --git a/app/services/partner_approval_service.rb b/app/services/partner_approval_service.rb index 2c53432f14..30dcf0e6c3 100644 --- a/app/services/partner_approval_service.rb +++ b/app/services/partner_approval_service.rb @@ -9,7 +9,6 @@ def call return self unless valid? Partners::Base.transaction do - partner.profile.update!(partner_status: approved_partner_status) partner.approved! PartnerMailer.application_approved(partner: partner).deliver_later @@ -32,8 +31,4 @@ def valid? errors.none? end - - def approved_partner_status - Partners::Partner::VERIFIED_STATUS - end end diff --git a/app/services/partner_create_service.rb b/app/services/partner_create_service.rb index 13a91a8815..0028155e32 100644 --- a/app/services/partner_create_service.rb +++ b/app/services/partner_create_service.rb @@ -20,8 +20,7 @@ def call ActiveRecord::Base.transaction do @partner.save! - Partners::Partner.create!({ - essentials_bank_id: organization.id, + Partners::Profile.create!({ partner_id: @partner.id, name: @partner.name, enable_child_based_requests: organization.enable_child_based_requests, diff --git a/app/services/partner_deactivate_service.rb b/app/services/partner_deactivate_service.rb index cb7d04ce9d..b83429fb5b 100644 --- a/app/services/partner_deactivate_service.rb +++ b/app/services/partner_deactivate_service.rb @@ -9,10 +9,7 @@ def initialize(partner:) def call ActiveRecord::Base.transaction do - partners_partner = Partners::Partner.find_by!(partner_id: partner.id) - partner.update!(status: 'deactivated') - partners_partner.update!(status_in_diaper_base: 'deactivated', partner_status: 'pending') rescue StandardError => e errors.add(:base, e.message) raise ActiveRecord::Rollback diff --git a/app/services/partner_invite_service.rb b/app/services/partner_invite_service.rb index fedef441ee..bf5cb06ef9 100644 --- a/app/services/partner_invite_service.rb +++ b/app/services/partner_invite_service.rb @@ -15,7 +15,7 @@ def call partner.update!(status: 'invited') UserInviteService.invite(email: partner.email, roles: [Role::PARTNER], - resource: partner.profile) + resource: partner) end attr_reader :partner diff --git a/app/services/partner_reactivate_service.rb b/app/services/partner_reactivate_service.rb index 9e6b05d644..688b9d42f0 100644 --- a/app/services/partner_reactivate_service.rb +++ b/app/services/partner_reactivate_service.rb @@ -9,10 +9,7 @@ def initialize(partner:) def call ActiveRecord::Base.transaction do - partners_partner = Partners::Partner.find_by!(partner_id: partner.id) - partner.update!(status: 'approved') - partners_partner.update!(status_in_diaper_base: 'approved', partner_status: 'verified') rescue StandardError => e errors.add(:base, e.message) raise ActiveRecord::Rollback diff --git a/app/services/partner_request_recertification_service.rb b/app/services/partner_request_recertification_service.rb index 9f6dd27400..ab6b2000cb 100644 --- a/app/services/partner_request_recertification_service.rb +++ b/app/services/partner_request_recertification_service.rb @@ -14,7 +14,6 @@ def call return self unless valid? Partners::Base.transaction do - partner.profile.update!(partner_status: Partners::Partner::RECERTIFICATION_REQUESTED_STATUS) partner.recertification_required! PartnerMailer.recertification_request(partner: partner).deliver_later rescue StandardError => e diff --git a/app/services/partners/family_request_create_service.rb b/app/services/partners/family_request_create_service.rb index 9af41d4a69..0099c56d8b 100644 --- a/app/services/partners/family_request_create_service.rb +++ b/app/services/partners/family_request_create_service.rb @@ -1,7 +1,6 @@ # This service object is meant to transform a family request # into the correct parameters that would be expected a regular -# Partners::ItemRequest. It will create the relevant -# organization request (i.e. ::Request), the Partners::Request, +# Partners::ItemRequest. It will create the relevant Request, # the Partners::ItemRequest(s), and Partners::ChildItemRequest(s) module Partners class FamilyRequestCreateService diff --git a/app/services/partners/request_approval_service.rb b/app/services/partners/request_approval_service.rb index d0d95a9464..d4b3857e81 100644 --- a/app/services/partners/request_approval_service.rb +++ b/app/services/partners/request_approval_service.rb @@ -14,7 +14,6 @@ def initialize(partner:) def call return self unless valid? - partner.profile.update(partner_status: 'submitted') partner.awaiting_review! OrganizationMailer.partner_approval_request(organization: partner.organization, partner: partner).deliver_later @@ -26,7 +25,7 @@ def call attr_reader :partner def valid? - if partner.profile.partner_status == 'submitted' + if partner.status == 'awaiting_review' errors.add(:base, 'partner has already requested approval') end diff --git a/app/services/partners/request_create_service.rb b/app/services/partners/request_create_service.rb index 71e356ebcd..e5c4058083 100644 --- a/app/services/partners/request_create_service.rb +++ b/app/services/partners/request_create_service.rb @@ -13,7 +13,10 @@ def initialize(partner_user_id:, comments: nil, for_families: false, item_reques end def call - @partner_request = Partners::Request.new(partner_id: partner.id, organization_id: organization_id, comments: comments, for_families: @for_families, sent: true, partner_user_id: partner_user_id) + @partner_request = ::Request.new(partner_id: partner.id, + organization_id: organization_id, + comments: comments, + partner_user_id: partner_user_id) @partner_request = populate_item_request(@partner_request) @partner_request.assign_attributes(additional_attrs) @@ -29,13 +32,10 @@ def call return self if errors.present? - Partners::Base.transaction do + Request.transaction do @partner_request.save! - @organization_request = build_organization_request(@partner_request) - @organization_request.save! - - NotifyPartnerJob.perform_now(@organization_request.id) + NotifyPartnerJob.perform_now(@partner_request.id) rescue StandardError => e errors.add(:base, e.message) raise ActiveRecord::Rollback @@ -66,6 +66,12 @@ def populate_item_request(partner_request) partner_request.item_requests << item_requests + partner_request.request_items = partner_request.item_requests.map do |ir| + { + item_id: ir.item_id, + quantity: ir.quantity + } + end partner_request end @@ -88,26 +94,11 @@ def organization_item_data end def organization_id - @organization_id ||= partner.essentials_bank_id + @organization_id ||= partner.organization_id end def partner @partner ||= ::User.find(partner_user_id).partner end - - def build_organization_request(partner_request) - ::Request.new( - organization_id: partner_request.organization_id, - partner_id: partner_request.partner.partner_id, - partner_user_id: partner_user_id, - comments: partner_request.comments, - request_items: partner_request.item_requests.map do |ir| - { - item_id: ir.item_id, - quantity: ir.quantity - } - end - ) - end end end diff --git a/app/services/reports/partner_info_report_service.rb b/app/services/reports/partner_info_report_service.rb index 4d504fcf82..ced0ebb1fb 100644 --- a/app/services/reports/partner_info_report_service.rb +++ b/app/services/reports/partner_info_report_service.rb @@ -22,7 +22,7 @@ def report @report = { name: 'Partner Agencies and Service Area', entries: entries } end - # @return [Array] + # @return [Array] def partner_agency_profiles @partner_agency_profiles ||= organization.partners.map(&:profile).compact end diff --git a/app/views/layouts/partners/navigation/_navbar.html.erb b/app/views/layouts/partners/navigation/_navbar.html.erb index 12c7cf9211..3712f3e58f 100644 --- a/app/views/layouts/partners/navigation/_navbar.html.erb +++ b/app/views/layouts/partners/navigation/_navbar.html.erb @@ -21,7 +21,7 @@ <% current_user.switchable_roles.each do |role| %> <% next if role == current_role %> - <%= link_to switch_to_role_users_path(role_id: role.id), class:"dropdown-item" do %> + <%= link_to switch_to_role_users_path(organization_id: current_partner.organization_id, role_id: role.id), class:"dropdown-item" do %> <%= "Switch to: #{role.resource.name}" %> <% end %> <% end %> diff --git a/app/views/layouts/partners/navigation/_sidebar.html.erb b/app/views/layouts/partners/navigation/_sidebar.html.erb index 22754a08ab..44d31e62ea 100644 --- a/app/views/layouts/partners/navigation/_sidebar.html.erb +++ b/app/views/layouts/partners/navigation/_sidebar.html.erb @@ -37,7 +37,7 @@ - <% if current_partner.enable_child_based_requests? %> + <% if current_partner.profile.enable_child_based_requests? %>