Skip to content

Commit

Permalink
Scope additional resources at the organization level (#1945)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 authored Jul 31, 2024
1 parent 52a30c8 commit e4abba6
Show file tree
Hide file tree
Showing 54 changed files with 447 additions and 140 deletions.
5 changes: 3 additions & 2 deletions app/activity_notifications/chat_message_sent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
class ChatMessageSent < Noticed::Base
deliver_by :database, format: :to_database, association: :notifications_as_recipient

param :contributor_id, :request_id, :user_id, :message_id
param :contributor_id, :request_id, :user_id, :message_id, :organization_id

def to_database
{
type: self.class.name,
contributor_id: params[:contributor_id],
request_id: params[:request_id],
user_id: params[:user_id],
message_id: params[:message_id]
message_id: params[:message_id],
organization_id: params[:organization_id]
}
end

Expand Down
5 changes: 3 additions & 2 deletions app/activity_notifications/contributor_marked_inactive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
class ContributorMarkedInactive < Noticed::Base
deliver_by :database, format: :to_database, association: :notifications_as_recipient

param :contributor_id
param :contributor_id, :organization_id

def to_database
{
type: self.class.name,
contributor_id: params[:contributor_id]
contributor_id: params[:contributor_id],
organization_id: params[:organization_id]
}
end

Expand Down
5 changes: 3 additions & 2 deletions app/activity_notifications/contributor_subscribed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
class ContributorSubscribed < Noticed::Base
deliver_by :database, format: :to_database, association: :notifications_as_recipient

param :contributor_id
param :contributor_id, :organization_id

def to_database
{
type: self.class.name,
contributor_id: params[:contributor_id]
contributor_id: params[:contributor_id],
organization_id: params[:organization_id]
}
end

Expand Down
5 changes: 3 additions & 2 deletions app/activity_notifications/message_received.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
class MessageReceived < Noticed::Base
deliver_by :database, format: :to_database, association: :notifications_as_recipient

param :contributor_id, :request_id, :message_id
param :contributor_id, :request_id, :message_id, :organization_id

def to_database
{
type: self.class.name,
contributor_id: params[:contributor_id],
request_id: params[:request_id],
message_id: params[:message_id]
message_id: params[:message_id],
organization_id: params[:organization_id]
}
end

Expand Down
5 changes: 3 additions & 2 deletions app/activity_notifications/onboarding_completed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
class OnboardingCompleted < Noticed::Base
deliver_by :database, format: :to_database, association: :notifications_as_recipient

param :contributor_id
param :contributor_id, :organization_id

def to_database
{
type: self.class.name,
contributor_id: params[:contributor_id]
contributor_id: params[:contributor_id],
organization_id: params[:organization_id]
}
end

Expand Down
5 changes: 3 additions & 2 deletions app/activity_notifications/request_scheduled.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
class RequestScheduled < Noticed::Base
deliver_by :database, format: :to_database, association: :notifications_as_recipient

param :request_id
param :request_id, :organization_id

def to_database
{
type: self.class.name,
request_id: params[:request_id]
request_id: params[:request_id],
organization_id: params[:organization_id]
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

module ContributorQuickEditForm
class ContributorQuickEditForm < ApplicationComponent
def initialize(contributor:)
def initialize(organization:, contributor:)
super

@organization = organization
@contributor = contributor
end

private

attr_reader :contributor
attr_reader :organization, :contributor

def available_tags
Contributor.all_tags_with_count.to_json
organization.contributors_tags_with_count.to_json
end
end
end
9 changes: 3 additions & 6 deletions app/components/contributors_index/contributors_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module ContributorsIndex
class ContributorsIndex < ApplicationComponent
def initialize(organization:, contributors:, state:, active_count:, inactive_count:, unsubscribed_count:, filter_count:, tag_list: nil)
def initialize(organization:, contributors:, state:, active_count:, inactive_count:, unsubscribed_count:, filter_count:, available_tags:, tag_list: nil)
super

@organization = organization
Expand All @@ -13,15 +13,12 @@ def initialize(organization:, contributors:, state:, active_count:, inactive_cou
@inactive_count = inactive_count
@unsubscribed_count = unsubscribed_count
@filter_count = filter_count
@available_tags = available_tags
end

private

attr_reader :organization, :contributors, :tag_list, :state, :active_count, :inactive_count, :unsubscribed_count, :filter_count

def available_tags
Contributor.all_tags_with_count.to_json
end
attr_reader :organization, :contributors, :tag_list, :state, :active_count, :inactive_count, :unsubscribed_count, :filter_count, :available_tags

def active_contributors_count
tag_list.present? && state == :active ? filter_count : active_count
Expand Down
2 changes: 1 addition & 1 deletion app/components/request_form/request_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@

<div class="RequestForm-column">
<%= c 'device_frame', class: 'RequestForm-preview' do %>
<%= c 'chat_preview', organization: organization do %>
<%= c 'chat_preview', organization: request.organization do %>
<span data-request-form-target="preview"></span>
<% end %>
<% end %>
Expand Down
10 changes: 3 additions & 7 deletions app/components/request_form/request_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

module RequestForm
class RequestForm < ApplicationComponent
def initialize(organization:, request:)
def initialize(request:, available_tags:)
super

@organization = organization
@request = request
@available_tags = available_tags
end

private

attr_reader :organization, :request

def available_tags
Contributor.all_tags_with_count.to_json
end
attr_reader :request, :available_tags

def schedule_send_for_or_default
datetime = request.planned? ? request.schedule_send_for : Time.current
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/contributors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def index
@active_count = Contributor.active.count
@inactive_count = Contributor.inactive.count
@unsubscribed_count = Contributor.unsubscribed.count
@available_tags = Contributor.all_tags_with_count.to_json
@available_tags = @organization.contributors_tags_with_count.to_json

@contributors = filtered_contributors
@contributors = @contributors.with_tags(tag_list_params)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def index

def activity_notifications
grouped = current_user.notifications_as_recipient
.where(organization_id: @organization.id)
.newest_first
.includes({ contributor: { avatar_attachment: :blob } }, :request, :message, :user)
.last_four_weeks
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class RequestsController < ApplicationController
before_action :notifications_params, only: :notifications
before_action :disallow_edit, only: %i[edit update]
before_action :disallow_destroy, only: :destroy
before_action :available_tags, only: %i[new edit]

def index
@filter = filter_param
Expand Down Expand Up @@ -34,7 +35,7 @@ def create
end

def new
@request = Request.new
@request = Request.new(organization: @organization)
end

def edit; end
Expand All @@ -58,7 +59,7 @@ def update

def destroy
if @request.destroy
redirect_to requests_url(filter: :planned), notice: t('request.destroy.successful', request_title: @request.title)
redirect_to requests_url(filter: :planned), flash: { notice: t('request.destroy.successful', request_title: @request.title) }
else
render :edit, status: :unprocessable_entity
end
Expand Down Expand Up @@ -111,6 +112,10 @@ def set_request
@request = Request.find(params[:id])
end

def available_tags
@available_tags ||= @organization.contributors_tags_with_count.to_json
end

def request_params
params.require(:request).permit(:title, :text, :tag_list, :schedule_send_for, files: [])
end
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class SearchController < ApplicationController
def index
@results = []
query = params[:q]
@results = PgSearch.multisearch(query).map(&:searchable) if query
return unless query

@results = PgSearch.multisearch(query).where(organization_id: @organization.id).map(&:searchable)
end
end
5 changes: 4 additions & 1 deletion app/jobs/mark_inactive_contributor_inactive_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ def perform(organization_id:, contributor_id:)

contributor.deactivated_at = Time.current
contributor.save(validate: false)
ContributorMarkedInactive.with(contributor_id: contributor.id).deliver_later(User.all)
ContributorMarkedInactive.with(
contributor_id: contributor.id,
organization_id: organization.id
).deliver_later(organization.users + User.admin.all)
User.admin.find_each do |admin|
PostmarkAdapter::Outbound.contributor_marked_as_inactive!(admin, contributor, organization)
end
Expand Down
5 changes: 4 additions & 1 deletion app/jobs/resubscribe_contributor_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def perform(organization_id, contributor_id, adapter)

contributor.update!(unsubscribed_at: nil)
adapter.send_welcome_message!(contributor, organization)
ContributorSubscribed.with(contributor_id: contributor.id).deliver_later(User.all)
ContributorSubscribed.with(
contributor_id: contributor.id,
organization_id: organization.id
).deliver_later(organization.users + User.admin.all)
User.admin.find_each do |admin|
PostmarkAdapter::Outbound.contributor_resubscribed!(admin, contributor, organization)
end
Expand Down
5 changes: 4 additions & 1 deletion app/jobs/unsubscribe_contributor_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def perform(organization_id, contributor_id, adapter)

contributor.update!(unsubscribed_at: Time.current)
adapter.send_unsubsribed_successfully_message!(contributor, organization)
ContributorMarkedInactive.with(contributor_id: contributor.id).deliver_later(User.all)
ContributorMarkedInactive.with(
contributor_id: contributor.id,
organization_id: organization.id
).deliver_later(organization.users + User.admin.all)
User.admin.find_each do |admin|
PostmarkAdapter::Outbound.contributor_unsubscribed!(admin, contributor, organization)
end
Expand Down
1 change: 1 addition & 0 deletions app/models/activity_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ActivityNotification < ApplicationRecord
belongs_to :request, optional: true
belongs_to :message, optional: true
belongs_to :user, optional: true
belongs_to :organization

scope :last_four_weeks, -> { where(created_at: 4.weeks.ago..Time.current) }
end
28 changes: 4 additions & 24 deletions app/models/contributor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class Contributor < ApplicationRecord

after_create_commit :notify_recipient

multisearchable against: %i[first_name last_name username note]
multisearchable against: %i[first_name last_name username note],
additional_attributes: ->(contributor) { { organization_id: contributor.organization_id } }

has_many :replies, class_name: 'Message', as: :sender, dependent: :destroy
has_many :received_messages, class_name: 'Message', inverse_of: :recipient, foreign_key: 'recipient_id', dependent: :destroy
Expand All @@ -24,6 +25,7 @@ class Contributor < ApplicationRecord
accepts_nested_attributes_for :json_web_token

acts_as_taggable_on :tags
acts_as_taggable_tenant :organization_id

default_scope { order(:first_name, :last_name) }
scope :active, -> { where(deactivated_at: nil, unsubscribed_at: nil) }
Expand Down Expand Up @@ -66,28 +68,6 @@ def self.with_lowercased_email(email)
find_by('lower(email) in (?)', Array.wrap(email).map(&:downcase))
end

def self.all_tags_with_count
ActsAsTaggableOn::Tag
.joins(:taggings)
.select('tags.id, tags.name, count(taggings.id) as taggings_count')
.group('tags.id')
.where(taggings: { taggable_type: name })
.all
.map do |tag|
{
id: tag.id,
name: tag.name,
value: tag.name,
count: tag.taggings_count,
color: Contributor.tag_color_from_id(tag.id)
}
end
end

def self.tag_color_from_id(tag_id)
ApplicationController.helpers.color_from_id(tag_id)
end

def reply(message_decorator)
request = active_request or return nil
ActiveRecord::Base.transaction do
Expand Down Expand Up @@ -241,7 +221,7 @@ def stats
private

def notify_recipient
OnboardingCompleted.with(contributor_id: id).deliver_later(User.all)
OnboardingCompleted.with(contributor_id: id, organization_id: organization.id).deliver_later(organization.users + User.admin.all)
end
end
# rubocop:enable Metrics/ClassLength
22 changes: 18 additions & 4 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class Message < ApplicationRecord

default_scope { order(created_at: :desc) }

multisearchable against: :text, if: :reply?
multisearchable against: :text, if: :reply?,
additional_attributes: ->(message) { { organization_id: message.organization.id } }

belongs_to :sender, polymorphic: true, optional: true
belongs_to :recipient, class_name: 'Contributor', optional: true
Expand Down Expand Up @@ -63,14 +64,27 @@ def sent_from_contributor?

private

# rubocop:disable Metrics/AbcSize
def notify_recipient
# binding.pry
if reply?
MessageReceived.with(contributor_id: sender_id, request_id: request.id, message_id: id).deliver_later(User.all)
MessageReceived.with(
contributor_id: sender_id,
request_id: request.id,
message_id: id,
organization_id: organization.id
).deliver_later(organization.users + User.admin.all)
elsif !broadcasted?
ChatMessageSent.with(contributor_id: recipient.id, request_id: request.id, user_id: sender_id,
message_id: id).deliver_later(User.all)
ChatMessageSent.with(
contributor_id: recipient.id,
request_id: request.id,
user_id: sender_id,
message_id: id,
organization_id: organization.id
).deliver_later(organization.users + User.admin.all)
end
end
# rubocop:enable Metrics/AbcSize

def send_if_outbound
return if manually_created? || reply?
Expand Down
Loading

0 comments on commit e4abba6

Please sign in to comment.