Contact model changes (#184)

* move source id from contacts

* Fix contactInbox model name

* rubocop fix

* Fix rspec
This commit is contained in:
Sojan Jose 2019-10-27 13:14:36 +05:30 committed by GitHub
parent 3b6d58bbdf
commit c21c839dca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 57 additions and 20 deletions

View file

@ -30,11 +30,14 @@ module Messages
private
def contact
@contact ||= @inbox.contacts.find_by(source_id: @sender_id)
@contact ||= @inbox.contact_inboxes.find_by(source_id: @sender_id)&.contact
end
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
def build_message
@ -120,7 +123,6 @@ module Messages
{
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
account_id: @inbox.account_id,
source_id: @sender_id,
remote_avatar_url: result['profile_pic'] || nil
}
end

View file

@ -86,7 +86,7 @@ export default {
data() {
return {
message: '',
private: false,
isPrivate: false,
showEmojiPicker: false,
showCannedModal: false,
};
@ -100,7 +100,7 @@ export default {
},
watch: {
message(val) {
if (this.private) {
if (this.isPrivate) {
return;
}
const isSlashCommand = val[0] === '/';

View file

@ -1,20 +1,22 @@
class Contact < ApplicationRecord
include Pubsubable
validates :account_id, presence: true
validates :inbox_id, presence: true
belongs_to :account
belongs_to :inbox
has_many :conversations, dependent: :destroy
has_many :contact_inboxes, dependent: :destroy
has_many :inboxes, through: :contact_inboxes
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
{
id: id,
name: name,
thumbnail: avatar.thumb.url,
channel: inbox.try(:channel).try(:name),
pubsub_token: pubsub_token
}
end

View 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

View file

@ -6,11 +6,13 @@ class Inbox < ApplicationRecord
belongs_to :account
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 :members, through: :inbox_members, source: :user
has_many :conversations, dependent: :destroy
has_many :messages, through: :conversations
has_many :contacts, dependent: :destroy
after_create :subscribe_webhook, if: :facebook?
after_destroy :delete_round_robin_agents

View file

@ -35,7 +35,7 @@ module Facebook
def fb_message_params
{
recipient: { id: contact.source_id },
recipient: { id: contact.get_source_id(inbox.id) },
message: { text: message.content }
}
end

View file

@ -9,7 +9,7 @@ json.data do
json.array! @conversations do |conversation|
json.meta do
json.sender do
json.id conversation.contact.source_id
json.id conversation.contact.id
json.name conversation.contact.name
json.thumbnail conversation.contact.avatar.thumb.url
json.channel conversation.inbox.try(:channel).try(:name)

View 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

View file

@ -10,7 +10,7 @@
#
# 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
enable_extension "plpgsql"
@ -63,6 +63,18 @@ ActiveRecord::Schema.define(version: 2019_10_20_173522) do
t.datetime "updated_at", null: false
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|
t.string "name"
t.string "email"
@ -70,8 +82,6 @@ ActiveRecord::Schema.define(version: 2019_10_20_173522) do
t.integer "account_id", null: false
t.datetime "created_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 "pubsub_token"
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
end
add_foreign_key "contact_inboxes", "contacts"
add_foreign_key "contact_inboxes", "inboxes"
add_foreign_key "users", "users", column: "inviter_id"
end

View file

@ -18,7 +18,7 @@ module Integrations
end
def contact
Contact.find_by(source_id: sender_id)
::ContactInbox.find_by(source_id: sender_id).contact
end
def conversation

View file

@ -5,8 +5,6 @@ FactoryBot.define do
sequence(:name) { |n| "Widget #{n}" }
sequence(:email) { |n| "widget-#{n}@example.com" }
phone_number { '+123456789011' }
source_id { rand(100) }
account
inbox
end
end

View file

@ -5,12 +5,10 @@ require 'rails_helper'
RSpec.describe Contact do
context 'validations' do
it { is_expected.to validate_presence_of(:account_id) }
it { is_expected.to validate_presence_of(:inbox_id) }
end
context 'associations' do
it { is_expected.to belong_to(:account) }
it { is_expected.to belong_to(:inbox) }
it { is_expected.to have_many(:conversations).dependent(:destroy) }
end