chore: conversation participants api

This commit is contained in:
Sojan 2022-03-09 02:13:14 +05:30
parent 1aa7d6f7d8
commit cef897fae2
9 changed files with 108 additions and 1 deletions

View file

@ -0,0 +1,32 @@
class Api::V1::Accounts::Conversations::ParticipantsController < Api::V1::Accounts::Conversations::BaseController
def show
@participants = @conversation.conversation_participants
end
def create
ActiveRecord::Base.transaction do
@participants = participants_to_be_added_ids.map { |user_id| @conversation.conversation_participants.find_or_create_by(user_id: user_id) }
end
end
def destroy
ActiveRecord::Base.transaction do
params[:user_ids].map { |user_id| @conversation.conversation_participants.find_by(user_id: user_id)&.destroy }
end
head :ok
end
private
def participants_to_be_added_ids
params[:user_ids] - current_participant_ids
end
def participants_to_be_removed_ids
current_participant_ids - params[:user_ids]
end
def current_participant_ids
@current_participant_ids ||= @conversation.conversation_participants.pluck(:user_id)
end
end

View file

@ -74,8 +74,10 @@ class Conversation < ApplicationRecord
has_many :mentions, dependent: :destroy_async has_many :mentions, dependent: :destroy_async
has_many :messages, dependent: :destroy_async, autosave: true has_many :messages, dependent: :destroy_async, autosave: true
has_one :csat_survey_response, dependent: :destroy_async has_one :csat_survey_response, dependent: :destroy_async
has_many :conversation_participants, dependent: :destroy_async
has_many :notifications, as: :primary_actor, dependent: :destroy has_many :notifications, as: :primary_actor, dependent: :destroy
before_save :ensure_snooze_until_reset before_save :ensure_snooze_until_reset
before_create :mark_conversation_pending_if_bot before_create :mark_conversation_pending_if_bot

View file

@ -0,0 +1,40 @@
# == Schema Information
#
# Table name: conversation_participants
#
# id :bigint not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# conversation_id :bigint not null
# user_id :bigint not null
#
# Indexes
#
# index_conversation_participants_on_account_id (account_id)
# index_conversation_participants_on_conversation_id (conversation_id)
# index_conversation_participants_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id) ON DELETE => cascade
# fk_rails_... (conversation_id => conversations.id) ON DELETE => cascade
# fk_rails_... (user_id => users.id) ON DELETE => cascade
#
class ConversationParticipant < ApplicationRecord
validates :account_id, presence: true
validates :conversation_id, presence: true
validates :user_id, presence: true
belongs_to :account
belongs_to :conversation
belongs_to :user
before_validation :ensure_account_id
private
def ensure_account_id
self.account_id = conversation&.account_id
end
end

View file

@ -76,6 +76,7 @@ class User < ApplicationRecord
has_many :assigned_conversations, foreign_key: 'assignee_id', class_name: 'Conversation', dependent: :nullify has_many :assigned_conversations, foreign_key: 'assignee_id', class_name: 'Conversation', dependent: :nullify
alias_attribute :conversations, :assigned_conversations alias_attribute :conversations, :assigned_conversations
has_many :csat_survey_responses, foreign_key: 'assigned_agent_id', dependent: :nullify has_many :csat_survey_responses, foreign_key: 'assigned_agent_id', dependent: :nullify
has_many :conversation_participants, dependent: :destroy_async
has_many :inbox_members, dependent: :destroy_async has_many :inbox_members, dependent: :destroy_async
has_many :inboxes, through: :inbox_members, source: :inbox has_many :inboxes, through: :inbox_members, source: :inbox

View file

@ -0,0 +1,3 @@
json.array! @participants do |participant|
json.partial! 'api/v1/models/agent.json.jbuilder', resource: participant.user
end

View file

@ -0,0 +1,3 @@
json.array! @participants do |participant|
json.partial! 'api/v1/models/agent.json.jbuilder', resource: participant.user
end

View file

@ -71,6 +71,7 @@ Rails.application.routes.draw do
resources :messages, only: [:index, :create, :destroy] resources :messages, only: [:index, :create, :destroy]
resources :assignments, only: [:create] resources :assignments, only: [:create]
resources :labels, only: [:create, :index] resources :labels, only: [:create, :index]
resource :participants, only: [:show, :create, :destroy]
end end
member do member do
post :mute post :mute

View file

@ -0,0 +1,11 @@
class AddConversationParticipants < ActiveRecord::Migration[6.1]
def change
create_table 'conversation_participants', force: :cascade do |t|
t.references :account, null: false, foreign_key: { on_delete: :cascade }
t.references :user, null: false, foreign_key: { on_delete: :cascade }
t.references :conversation, null: false, foreign_key: { on_delete: :cascade }
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_02_18_120357) do ActiveRecord::Schema.define(version: 2022_03_08_193420) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements" enable_extension "pg_stat_statements"
@ -337,6 +337,17 @@ ActiveRecord::Schema.define(version: 2022_02_18_120357) do
t.index ["pubsub_token"], name: "index_contacts_on_pubsub_token", unique: true t.index ["pubsub_token"], name: "index_contacts_on_pubsub_token", unique: true
end end
create_table "conversation_participants", force: :cascade do |t|
t.bigint "account_id", null: false
t.bigint "user_id", null: false
t.bigint "conversation_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["account_id"], name: "index_conversation_participants_on_account_id"
t.index ["conversation_id"], name: "index_conversation_participants_on_conversation_id"
t.index ["user_id"], name: "index_conversation_participants_on_user_id"
end
create_table "conversations", id: :serial, force: :cascade do |t| create_table "conversations", id: :serial, force: :cascade do |t|
t.integer "account_id", null: false t.integer "account_id", null: false
t.integer "inbox_id", null: false t.integer "inbox_id", null: false
@ -787,6 +798,9 @@ ActiveRecord::Schema.define(version: 2022_02_18_120357) do
add_foreign_key "campaigns", "inboxes", on_delete: :cascade add_foreign_key "campaigns", "inboxes", on_delete: :cascade
add_foreign_key "contact_inboxes", "contacts", on_delete: :cascade add_foreign_key "contact_inboxes", "contacts", on_delete: :cascade
add_foreign_key "contact_inboxes", "inboxes", on_delete: :cascade add_foreign_key "contact_inboxes", "inboxes", on_delete: :cascade
add_foreign_key "conversation_participants", "accounts", on_delete: :cascade
add_foreign_key "conversation_participants", "conversations", on_delete: :cascade
add_foreign_key "conversation_participants", "users", on_delete: :cascade
add_foreign_key "conversations", "campaigns", on_delete: :cascade add_foreign_key "conversations", "campaigns", on_delete: :cascade
add_foreign_key "conversations", "contact_inboxes", on_delete: :cascade add_foreign_key "conversations", "contact_inboxes", on_delete: :cascade
add_foreign_key "conversations", "teams", on_delete: :cascade add_foreign_key "conversations", "teams", on_delete: :cascade