Contact model changes (#184)
* move source id from contacts * Fix contactInbox model name * rubocop fix * Fix rspec
This commit is contained in:
parent
3b6d58bbdf
commit
c21c839dca
12 changed files with 57 additions and 20 deletions
|
@ -30,11 +30,14 @@ module Messages
|
||||||
private
|
private
|
||||||
|
|
||||||
def contact
|
def contact
|
||||||
@contact ||= @inbox.contacts.find_by(source_id: @sender_id)
|
@contact ||= @inbox.contact_inboxes.find_by(source_id: @sender_id)&.contact
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_contact
|
def build_contact
|
||||||
@contact = @inbox.contacts.create!(contact_params) if contact.nil?
|
return if contact.present?
|
||||||
|
|
||||||
|
@contact = Contact.create!(contact_params)
|
||||||
|
ContactInbox.create(contact: contact, inbox: @inbox, source_id: @sender_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_message
|
def build_message
|
||||||
|
@ -120,7 +123,6 @@ module Messages
|
||||||
{
|
{
|
||||||
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
|
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
|
||||||
account_id: @inbox.account_id,
|
account_id: @inbox.account_id,
|
||||||
source_id: @sender_id,
|
|
||||||
remote_avatar_url: result['profile_pic'] || nil
|
remote_avatar_url: result['profile_pic'] || nil
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
@ -86,7 +86,7 @@ export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
message: '',
|
message: '',
|
||||||
private: false,
|
isPrivate: false,
|
||||||
showEmojiPicker: false,
|
showEmojiPicker: false,
|
||||||
showCannedModal: false,
|
showCannedModal: false,
|
||||||
};
|
};
|
||||||
|
@ -100,7 +100,7 @@ export default {
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
message(val) {
|
message(val) {
|
||||||
if (this.private) {
|
if (this.isPrivate) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const isSlashCommand = val[0] === '/';
|
const isSlashCommand = val[0] === '/';
|
||||||
|
|
|
@ -1,20 +1,22 @@
|
||||||
class Contact < ApplicationRecord
|
class Contact < ApplicationRecord
|
||||||
include Pubsubable
|
include Pubsubable
|
||||||
|
|
||||||
validates :account_id, presence: true
|
validates :account_id, presence: true
|
||||||
validates :inbox_id, presence: true
|
|
||||||
|
|
||||||
belongs_to :account
|
belongs_to :account
|
||||||
belongs_to :inbox
|
|
||||||
has_many :conversations, dependent: :destroy
|
has_many :conversations, dependent: :destroy
|
||||||
|
has_many :contact_inboxes, dependent: :destroy
|
||||||
|
has_many :inboxes, through: :contact_inboxes
|
||||||
mount_uploader :avatar, AvatarUploader
|
mount_uploader :avatar, AvatarUploader
|
||||||
|
|
||||||
|
def get_source_id(inbox_id)
|
||||||
|
contact_inboxes.find_by!(inbox_id: inbox_id).source_id
|
||||||
|
end
|
||||||
|
|
||||||
def push_event_data
|
def push_event_data
|
||||||
{
|
{
|
||||||
id: id,
|
id: id,
|
||||||
name: name,
|
name: name,
|
||||||
thumbnail: avatar.thumb.url,
|
thumbnail: avatar.thumb.url,
|
||||||
channel: inbox.try(:channel).try(:name),
|
|
||||||
pubsub_token: pubsub_token
|
pubsub_token: pubsub_token
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
8
app/models/contact_inbox.rb
Normal file
8
app/models/contact_inbox.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
class ContactInbox < ApplicationRecord
|
||||||
|
validates :inbox_id, presence: true
|
||||||
|
validates :contact_id, presence: true
|
||||||
|
validates :source_id, presence: true
|
||||||
|
|
||||||
|
belongs_to :contact
|
||||||
|
belongs_to :inbox
|
||||||
|
end
|
|
@ -6,11 +6,13 @@ class Inbox < ApplicationRecord
|
||||||
belongs_to :account
|
belongs_to :account
|
||||||
belongs_to :channel, polymorphic: true, dependent: :destroy
|
belongs_to :channel, polymorphic: true, dependent: :destroy
|
||||||
|
|
||||||
|
has_many :contact_inboxes, dependent: :destroy
|
||||||
|
has_many :contacts, through: :contact_inboxes
|
||||||
|
|
||||||
has_many :inbox_members, dependent: :destroy
|
has_many :inbox_members, dependent: :destroy
|
||||||
has_many :members, through: :inbox_members, source: :user
|
has_many :members, through: :inbox_members, source: :user
|
||||||
has_many :conversations, dependent: :destroy
|
has_many :conversations, dependent: :destroy
|
||||||
has_many :messages, through: :conversations
|
has_many :messages, through: :conversations
|
||||||
has_many :contacts, dependent: :destroy
|
|
||||||
after_create :subscribe_webhook, if: :facebook?
|
after_create :subscribe_webhook, if: :facebook?
|
||||||
after_destroy :delete_round_robin_agents
|
after_destroy :delete_round_robin_agents
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ module Facebook
|
||||||
|
|
||||||
def fb_message_params
|
def fb_message_params
|
||||||
{
|
{
|
||||||
recipient: { id: contact.source_id },
|
recipient: { id: contact.get_source_id(inbox.id) },
|
||||||
message: { text: message.content }
|
message: { text: message.content }
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,7 +9,7 @@ json.data do
|
||||||
json.array! @conversations do |conversation|
|
json.array! @conversations do |conversation|
|
||||||
json.meta do
|
json.meta do
|
||||||
json.sender do
|
json.sender do
|
||||||
json.id conversation.contact.source_id
|
json.id conversation.contact.id
|
||||||
json.name conversation.contact.name
|
json.name conversation.contact.name
|
||||||
json.thumbnail conversation.contact.avatar.thumb.url
|
json.thumbnail conversation.contact.avatar.thumb.url
|
||||||
json.channel conversation.inbox.try(:channel).try(:name)
|
json.channel conversation.inbox.try(:channel).try(:name)
|
||||||
|
|
15
db/migrate/20191027054756_create_contact_inboxes.rb
Normal file
15
db/migrate/20191027054756_create_contact_inboxes.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
class CreateContactInboxes < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
create_table :contact_inboxes do |t|
|
||||||
|
t.references :contact, foreign_key: true, index: true
|
||||||
|
t.references :inbox, foreign_key: true, index: true
|
||||||
|
t.string :source_id, null: false
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
add_index :contact_inboxes, [:inbox_id, :source_id], :unique => true
|
||||||
|
add_index :contact_inboxes, [:source_id]
|
||||||
|
remove_column :contacts, :inbox_id, :integer
|
||||||
|
remove_column :contacts, :source_id, :integer
|
||||||
|
end
|
||||||
|
end
|
18
db/schema.rb
18
db/schema.rb
|
@ -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: 2019_10_20_173522) do
|
ActiveRecord::Schema.define(version: 2019_10_27_054756) 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 "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -63,6 +63,18 @@ ActiveRecord::Schema.define(version: 2019_10_20_173522) do
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "contact_inboxes", force: :cascade do |t|
|
||||||
|
t.bigint "contact_id"
|
||||||
|
t.bigint "inbox_id"
|
||||||
|
t.string "source_id", null: false
|
||||||
|
t.datetime "created_at", precision: 6, null: false
|
||||||
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
|
t.index ["contact_id"], name: "index_contact_inboxes_on_contact_id"
|
||||||
|
t.index ["inbox_id", "source_id"], name: "index_contact_inboxes_on_inbox_id_and_source_id", unique: true
|
||||||
|
t.index ["inbox_id"], name: "index_contact_inboxes_on_inbox_id"
|
||||||
|
t.index ["source_id"], name: "index_contact_inboxes_on_source_id"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "contacts", id: :serial, force: :cascade do |t|
|
create_table "contacts", id: :serial, force: :cascade do |t|
|
||||||
t.string "name"
|
t.string "name"
|
||||||
t.string "email"
|
t.string "email"
|
||||||
|
@ -70,8 +82,6 @@ ActiveRecord::Schema.define(version: 2019_10_20_173522) do
|
||||||
t.integer "account_id", null: false
|
t.integer "account_id", null: false
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
t.integer "inbox_id", null: false
|
|
||||||
t.bigserial "source_id", null: false
|
|
||||||
t.string "avatar"
|
t.string "avatar"
|
||||||
t.string "pubsub_token"
|
t.string "pubsub_token"
|
||||||
t.index ["account_id"], name: "index_contacts_on_account_id"
|
t.index ["account_id"], name: "index_contacts_on_account_id"
|
||||||
|
@ -208,5 +218,7 @@ ActiveRecord::Schema.define(version: 2019_10_20_173522) do
|
||||||
t.index ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true
|
t.index ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
add_foreign_key "contact_inboxes", "contacts"
|
||||||
|
add_foreign_key "contact_inboxes", "inboxes"
|
||||||
add_foreign_key "users", "users", column: "inviter_id"
|
add_foreign_key "users", "users", column: "inviter_id"
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,7 +18,7 @@ module Integrations
|
||||||
end
|
end
|
||||||
|
|
||||||
def contact
|
def contact
|
||||||
Contact.find_by(source_id: sender_id)
|
::ContactInbox.find_by(source_id: sender_id).contact
|
||||||
end
|
end
|
||||||
|
|
||||||
def conversation
|
def conversation
|
||||||
|
|
|
@ -5,8 +5,6 @@ FactoryBot.define do
|
||||||
sequence(:name) { |n| "Widget #{n}" }
|
sequence(:name) { |n| "Widget #{n}" }
|
||||||
sequence(:email) { |n| "widget-#{n}@example.com" }
|
sequence(:email) { |n| "widget-#{n}@example.com" }
|
||||||
phone_number { '+123456789011' }
|
phone_number { '+123456789011' }
|
||||||
source_id { rand(100) }
|
|
||||||
account
|
account
|
||||||
inbox
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,12 +5,10 @@ require 'rails_helper'
|
||||||
RSpec.describe Contact do
|
RSpec.describe Contact do
|
||||||
context 'validations' do
|
context 'validations' do
|
||||||
it { is_expected.to validate_presence_of(:account_id) }
|
it { is_expected.to validate_presence_of(:account_id) }
|
||||||
it { is_expected.to validate_presence_of(:inbox_id) }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'associations' do
|
context 'associations' do
|
||||||
it { is_expected.to belong_to(:account) }
|
it { is_expected.to belong_to(:account) }
|
||||||
it { is_expected.to belong_to(:inbox) }
|
|
||||||
it { is_expected.to have_many(:conversations).dependent(:destroy) }
|
it { is_expected.to have_many(:conversations).dependent(:destroy) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue